understand Class definition

This is a discussion on understand Class definition within the C++ Programming forums, part of the General Programming Boards category; Hi, I want to understand the difference between class and object in C ++. I have read some material but ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    39

    understand Class definition

    Hi,
    I want to understand the difference between class and object in C ++. I have read some material but it is not still clear.
    Please can someone explain me the # between these terms?
    Thank you

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    a class is a blueprint for an object, whereas an object is a concrete (in memory) instance of a class.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    As Sebastiani said.

    Think of a real life object. For instance a person. That person is an object. The class (think classification) of the person would have all the characteristics and abilities of that person.

    Hair Color, Eye Color, Height, Age, Weight. Walk(), Talk(), Eat(), Sleep(), etc...

    When you create a person, you create an object will all of those attributes.
    Sent from my iPadŽ

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    I was taught this way, I have never forgoten it...

    Code:
    class myClass
    {
    private:
    int m_eyeColor;   // class members
    int m_hairColor;
    
    public:
    myClass ( int eyeC, int hairC );  // constructor
    
    void walk(); // methods
    void talk();
    };
    
    myClass::myClass  ( int eyeC, int hairC )
    {
    m_eyeColor = eyeC;
    m_hairColor = hairC;
    }
    
    void myClass::walk()
    {
    // method code
    }
    
    void myClass::talk()
    {
    // method code
    }
    I know this seems like a silly example, but I was always taught to think of the
    variables in private as members, and put a m_ before the name to make it a member.
    And put the methods ( if they are really called that ) under the constructor in the class.
    After that. I had to create the constructor then complete the method functions. It was the way I was taught and has always worked for me this way. But I guess different people have different styles of the way they lay out the classes...

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Quote Originally Posted by swgh
    and put a m_ before the name to make it a member.
    Lets not make statements like this about identifiers in such way that they can be mistaken as facts.

    Variables inside of a class are members no matter what their identifier looks like or whether they're private or public or whatever. A member is simply a member of the class.
    Sent from my iPadŽ

  6. #6
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    lol ok Sly, good point it is just a point of habit i guess, I have always used this way of doing classes, as it helps me to know they are members of a class. But as you said, the m_ is not essential. I do apoligise for leading him on the wrong track, like I said, I was taught this way and have always considered it to be good practice

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    This is how we describe the inner workings of a typical forum thread:
    Code:
    class thread
    {
        string title;
        string *posts;
        int postcount;
        int startdate;
    }
    and this is how we make one:

    Code:
    thread mythread;
    mythread.title = "understand Class definition";
    mythread.posts = new string[7];
    mythread.postcount = 7;
    mythread.startdate = 2052006;
    
    mythread.posts[6] = "This is how we describe the inner workings of......" //etc etc
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Actually, this is C++, so string *posts; should be std::vector<string> posts. That way, I can just use:
    Code:
    posts.push_back("Actually, this is C++, so string *posts; should be ...");
    And int postcount; can be removed in favor of:
    Code:
    int GetPostcount() const { return posts.size(); }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 06:16 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21