Tuesday, July 28, 2009

C++ beginner help?

Ok, so i was looking at this code, and I already know the function cin, but I saw it with .ignore() or something else. What does it mean when you put a . and then something else? The ones I saw were cin.clear(); cin.ignore(); and cin.fail(), I want to know these but also in general I didn't know you could put cin with something esle...

C++ beginner help?
Everyone worded this kind of weird.





cin is a Class inside of C++ (at least in the one you are working on).





A class is basically like a structure, with functions and variables in it.





say you had a class CMyClass





class CMyClass


{


private:


int privateInt; // a variable only this class can access








public:


int publicInt; // a variable anything can access





CMyClass ( void ) // constructor


{ // initialize your variables


}





~CMyClass() {} //destructor, if you needed to free mem





int load ( int c )


{


// load something


privateInt = c;


return c;


}


}














Now you can create a copy of this class:





CMyClass cin;





cin.load( 7 );





If this was a pointer you could have done this:


CMyClass *cin;





cin = new CMyClass();


cin-%26gt;load( 7 );





(you access class functions through pointers differently)
Reply:cin is an object and has several methods. You mention three of them...





cin.ignore(); // throw away the next char on the stream


cin.fail(); // returns 1 if you asked cin to do something that it can't, such as convert a char to int


cin.clear(); // clears the fail state if failure occurs
Reply:both the above answers are what did i mean to say:-)
Reply:The basic principle in an object oriented language like C++ is 'object(dot)property', or object.property.


So, when you see something like


cin.clear(), it basically means that cin is an actual object, and that 'clear' is one of its methods - as are ignore, fail, etc. This will help you alot in the future, because whenever you see something like





rs.FindFirst





in code, you will know that 'rs' is an object of some kind, and 'FindFirst' is a method of that object. Sometimes the objects are built in objects, other times they are user-defined. But just knowing that they are objects can help alot. Good luck to you.


No comments:

Post a Comment