A class is like a structure but it includes fields that are
functions. These are called member functions. Some of the fields
are public and some are private. Private functions and data are only available
to other functions inside the class. Public functions and data are available
to outside code. Try to put the public stuff first. Makes it clearer what
the user can use. Need member functions to read and set the private data
as outside functions won't be able to.
class list { public: private: }; // listclass and struct are almost interchangeable. In a class, the default is private. In a struct, the default is public. If you don't use the keywords, all methods are public and all data is private. The above is the declaration of a class.
list joe;is the definition of an instance called joe. Each instance of a class has a new instantiation of the private variables. If a private variable is declared static, then all the instances share the value of that variable.
Return type Class_name::function_nameMember functions don't have to specify the private members of the class. For example, in the C version of a function that examined the value of an element in a structure, the structure was passed as a pointer to the function.
void foo(struct martha * one) { printf("%d",one->item); }In C++, the pointer is implicit and the elements can be simple variables in the private part of the class. so
void foo() { printf("%d",item); }item can be treated as a parameter to the function. There is a pointer to the data in the class instance available called this. So the variable above was usable as
this->item; There is a more complete example here. A version with detailed commentary is here.