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
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
a class is a blueprint for an object, whereas an object is a concrete (in memory) instance of a class.
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
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Ž
I was taught this way, I have never forgoten it...
I know this seems like a silly example, but I was always taught to think of theCode: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 }
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...
Lets not make statements like this about identifiers in such way that they can be mistaken as facts.Originally Posted by swgh
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Ž
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
This is how we describe the inner workings of a typical forum thread:
and this is how we make one:Code:class thread { string title; string *posts; int postcount; int startdate; }
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;}
Actually, this is C++, so string *posts; should be std::vector<string> posts. That way, I can just use:And int postcount; can be removed in favor of:Code:posts.push_back("Actually, this is C++, so string *posts; should be ...");Code:int GetPostcount() const { return posts.size(); }