Thread: moving from c to c++

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    110

    moving from c to c++

    Hi, I've been programming in c for a while, and have started reading about c++. Having never done any OO programming before, I'm trying to get my head around objects and classes.

    I've read that a class is like a blueprint for something, and that an object is an instance of this 'something'. I understand that a class has 2 aspects, fields and methods, and I can understand the fields aspect - that a class has fields, and an object (instance of the class) has a particular set of values for these fields - but not the methods.

    If we have the class of dogs, or the class of cars, then essentially we have a c structure with fields describing characteristics of cars/dogs right? E.g. this would be something like:

    Code:
    class car{
      char registration[];
      char colour[];
      char model[];
      int age;
      int milage;  
    }
    so what are methods, and where do they come into it? How do they compare to functions and structures in c?

    Thanks
    PHI is one 'H' of alot more interesting than PI!

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    A method is a function which has implicit access to the fields of the object instance. That's really all there is to it.

    In C, you might have this:

    Code:
    struct foo
    {
        int a;
        char *b;
    };
    
    foo_do_something(struct foo *obj)
    {
        obj->a = 0;
        obj->b = "Hello, world!\n";
    }
    Whereas C++ you might have:

    Code:
    class foo
    {
        int a;
        char *b;
    
    public:
        do_something();
    };
    
    foo::do_something()
    {
        a = 0;
        b = "Hello, world!\n";
    }
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What member functions you need for a car class (or a dog class) depends entirely on what you actually need to do with the class.

    In general, you obviously need to do the same work whether it is in an object oriented programming language (such as C++) or a procedural language like C.

    The difference is that you do the work WITH a class member, rather than as a function that takes an explicit parameter of some sort.

    So, for example, if we want to draw a rectangle in C:
    Code:
    void drawRectangle(int x, int y, int w, int h)
    {
       line(x, y, x+w, y);    // Top horizontal line. 
       line(x+w, y, x+w, y+h);   // Right side vertical line. 
       line(x+w, y+h, x, y+h);   // Bottom horizontal line
       line(x, y+h, x, y);     // Left side vertical line
    }
    
    int main()
    {
        drawRectangle(100, 100, 300, 200);
    }
    In C++, the same thing could, perhaps be done by having a rectangle class:
    Code:
    class rectangle
    {
       int x, y, w, h;
    public:
       rectangle(int ax, int ay, int aw, int ah) : x(ax), y(ay), w(aw), h(aw)  {};
       void draw();
    } 
    
    void rectangle::draw()
    {
       line(x, y, x+w, y);    // Top horizontal line. 
       line(x+w, y, x+w, y+h);   // Right side vertical line. 
       line(x+w, y+h, x, y+h);   // Bottom horizontal line
       line(x, y+h, x, y);     // Left side vertical line
    }
    
    int main()
    {
        rectangle r(100, 100, 300, 200);
        r.draw();
    }
    So as you can see, in the object oriented world, we first construct a rectangle, then draw it, whereas in the C world, we call a function that knows how to draw a rectangle - both drawing functions do the exact same thing, but one uses the implicit knowledge of the rectangle size given in the constructor, the other the values given by the calling code.

    It's probably not a very good example, but...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Ok thanks, that's easy enough
    PHI is one 'H' of alot more interesting than PI!

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    matsp, that's a really good example thanks. Also, I forgot to ask my other question: What's the point of using cin and cout instead of printf etc to output to a terminal or file? There must be some reason why they changed the I/O functions from c, but why?
    PHI is one 'H' of alot more interesting than PI!

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    cin and cout works COMPLETELY different from printf/scanf. We are getting to a fairly advanced part of C++ to describe HOW you do this, but the essence of it is that you can do use cin and cout to read "objects" from stdin or write "objects" to stdout.

    For example, if we have the rectangle class from above defined with the right bits of output helper functions, we can do this:
    Code:
    int main()
    {
       rectangle r(100,100, 200, 300);
       cout << r;
    }
    we could, perhaps, get output like this:
    Code:
    rectangle: x=100, y=100, w=200, h=200
    This works by defining a operator<<(ostream &, rectangle) function [it may also be a member function of the rectangle class, in which case the rectangle parameter is implicit to the function, rather than written out] - as I said, it's quite advanced topic, so I'm not going to go into detail, it would just cause your brain to hurt! You'll get there in chapters later on in your book.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    147
    bertazoid:

    A lot of people scratch their head over cin/cout, especially when you consider the utility of printf and fprintf.

    Boost, a library you should google and think about, includes an improvement on the concept of cin/cout that incorporates formatting of the kind enjoyed by printf.

    However, the basic reason cin and cout ARE indeed important is because they are type aware and type safe. printf is not. Depending on your compiler and the warning settings, and one's habit about heading warnings vs errors, it is entirely possible to pass types as arguments that printf is not expecting and witness either garbage or crashes. Sound familiar? How many times have you had a bug associated with a mismatch in the parameters of a printf format string and the parameters supplied. Imaging an approach where printf were somehow aware of the types you've passed in and choose it's action accordingly. That's what cin/cout and the boost counterparts do.

    It may seem of minor importance at first, but those who have long careers in which to examine a history of development in practice, like myself, in comparison of C projects to C++ projects, the accumulation of such benefits provide considerable leverage and improve the reliability of the resulting projects.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Thanks both thats alot clearer now
    PHI is one 'H' of alot more interesting than PI!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-23-2007, 11:05 PM
  2. moving median function
    By supermeew in forum C Programming
    Replies: 0
    Last Post: 05-04-2006, 02:37 PM
  3. Replies: 4
    Last Post: 01-16-2006, 05:58 PM
  4. 3D moving
    By bluehead in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2005, 05:46 AM
  5. Simple program i cant get (dot moving)
    By Lupusk9 in forum C++ Programming
    Replies: 4
    Last Post: 09-14-2004, 08:04 PM