Thread: Can someone explain classes to me as though I were a child?

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    2

    Can someone explain classes to me as though I were a child?

    I cannot seem to grasp the concept of classes. I know how to define them, but how do I define functions within classes, write the functions,access said functions, and input user data into a function within a class to get an answer? I've checked quite a few tutorials, but I cannot understand. I'm quite confused ATM, so any help at all is appreciated.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Here's a simple example:
    Code:
    #include <iostream>
    #include <string>
    
    class Person
    {
    public:
       void SetName( const std::string&  name )
       {
          m_Name = name;
       }
    
       std::string GetName() const
       {
          return m_Name;
       }
    
    private:
       std::string  m_Name;
    };
    
    int main()
    {
       Person p;
       p.SetName( "Homer Simpson" );
       std::cout << "Hello, my name is " << p.GetName() << std::endl;
       return 0;
    }
    What part of the above are you having trouble with?
    Last edited by cpjust; 08-14-2008 at 02:35 PM. Reason: Fixed "fishiness".

  3. #3
    village skeptic
    Join Date
    Aug 2008
    Posts
    31
    What does the ampersand (&) represent when used next to the string declaration and the setName() function parameter?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What does the ampersand (&) represent when used next to the string declaration and the setName() function parameter?
    It means that the m_Name member variable is a reference, i.e., an alias of a std::string. It looks a little fishy to me though, since it should just be an object, not a reference, as far as I can tell. A constructor should be explicitly defined to initialise the reference member variable.
    Last edited by laserlight; 08-14-2008 at 01:44 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    2
    Quote Originally Posted by cpjust View Post
    Here's a simple example:
    Code:
    #include <iostream>
    #include <string>
    
    class Person
    {
    public:
       void SetName( const std::string&  name )
       {
          m_Name = name;
       }
    
       std::string GetName() const
       {
          return m_Name;
       }
    
    private:
       std::string&  m_Name;
    };
    
    int main()
    {
       Person p;
       p.SetName( "Homer Simpson" );
       std::cout << "Hello, my name is " << p.GetName() << std::endl;
       return 0;
    }
    What part of the above are you having trouble with?
    would you mind perhaps explaining what you did there. That'd help me alot. like why have private and public, and what should be put under each.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Do you know how to define functions that are not members of classes? Do you know how to call such free functions?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    I've checked quite a few tutorials, but I cannot understand. I'm quite confused ATM, so any help at all is appreciated.
    I suggest you get a good beginning C++ book. C++ is a complicated language, and most tutorials only have one or two "pages" per topic, but most books will devote a full chapter (or more) to each topic... You get much more detail & explanation.

    If you don't want to a buy a book, Thinking In C++ by Bruce Ecklel is available FREE online (or you can buy the hard copies). But, you might want to start-out with a simpler book. Thinking In C++ goes into lots of depth, and (to me) it seems a little advanced for someone learning their first programming language.
    Last edited by DougDbug; 08-14-2008 at 02:32 PM.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by laserlight View Post
    It means that the m_Name member variable is a reference, i.e., an alias of a std::string. It looks a little fishy to me though, since it should just be an object, not a reference, as far as I can tell.
    Doh! How did that get there?
    I fixed it now.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    You need to dive into a book. Here's a good, free one that you can download:

    http://www.mindview.net/Books/TICPP/...ngInCPP2e.html

  10. #10
    Registered User
    Join Date
    Aug 2008
    Posts
    8
    ++1 for the recommendations you have gotten for Thinking in C++! I'm reading it now, and it's good. C++ for Dummies is also good, but the treatment of classes isn't the best. A loooong time ago, back about when dirt was still a new thing, I had a C++ college course, and the text was Deitel's book C++ How to Program. It, also, is a good book and I wish I could locate my copy--if I still have it. The newer editions, however, are over $100, and that is a lot of bucks. See if you can find an old one. http://product.half.ebay.com/C-How-t...26681QQtgZinfo

    Classes are a little like structs, but C++ goes a bit further. Declare the class if the definition isn't coming first... and it should not come first. Then define the class. Put the constructor definition in the declaration of the class--eases headaches.

    Declarations look just like function declarations in headers, with a few goodies added--public and private declarations of the class variables (known in the object-oriented world as "properties") and class member functions (known in the same object-oriented world as "methods"). The "constructor" is a member function with the same name as the class and is a function that runs when any class object is instantiated... like in main:

    Code:
    int i;
    SomeObjectClassNameLikeFoo j;
    This gets you an integer named i and a class member named j. Whatever code you put in the constructor gets run at this point.

    Say you have a class called XmasCardList. If has some variables like name, address, cardReceived, etc. For testing, convuleted but organized, have main call a function called fTestXmasCardList. thus

    Code:
    void fTestXmasCardList(){ 
    XmasCardList cTestClass;
    // some code testing the class object 
    cout << cTestClass.name << endl;
    cout << cTestClass.address << endl;
    //more code here
    } // fTextXmasCardList
    Public variables can be accessed from outside the class, as in cTestClass.name, cTestClass.address, etc. Private variables can ONLY be accessed by a class member function. Often you want to have a member function return the member variable for you to avoid having the object's variables assigned in ways that might break things.

    Hope this helps a little. Classes are part and parcel of what separates C from C++ and the advantage is that it moves us from the old top down structured programming to object-oriented programming. Get the book and start reading it.
    Last edited by Bladeforger; 08-14-2008 at 04:37 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. College Classes
    By nubby in forum C Programming
    Replies: 2
    Last Post: 10-07-2007, 12:32 AM
  3. Posting a message to a child window
    By axr0284 in forum Windows Programming
    Replies: 6
    Last Post: 01-27-2005, 09:35 AM
  4. Child window inside child window
    By Magos in forum Windows Programming
    Replies: 13
    Last Post: 04-20-2004, 06:51 AM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM