Introduction A C++ class with lots and lots of comment on each line for you to understand what each line does and how the program flow works. The Code Code: #include <iostream> // Included for cout using namespace std; // Using the std namespace so we don't need to type std::cout class MyClass // The name of the class with the body of the class { public: // Part of the class that is accessable anywhere. I have always // kept my functions in public which I want them to be called // with the help of the object of the class. Others as private int get_counter(); // Returns the value of 'counter' protected variable void count_more(); // Increases 'counter' by 1 void set_counter(int); // Sets the integer 'counter' to the passed integer value MyClass(); // This is the constructor which is called when you // create the object of the class MyClass(int); // This is also constructor, but with an integer value // as a parameter and is called when you create an object // passing integer as parameter. e.g. "MyClass theOtherVar(407);" ~MyClass(); // This is the destructor, This is called when the object // of the class goes out of scope. protected: // Part of the class which is *only* accessable to the class // itself and its sub classes. (everything defined in class MyClass) int counter; // Private integer 'counter' }; // Don't forget in C++ class definitions end with a ; // Now we define the body of the functions we defined in the class. MyClass::MyClass() // This is the constructor of the class MyClass { cout << "Constructor (MyClass()) is called\n"; counter = 0; // Sets the integer protected 'counter' variable value to 0 } MyClass::MyClass(int x) // This is the same constructor but with an integer being passed to it { cout << "Constructor (MyClass(int x)) is called\n"; counter = x; // Sets the integer 'counter' to the supplied integer value } int MyClass::get_counter() { return counter; // Returns the value of the integer 'counter' } void MyClass::count_more() { counter++; // Increases the integer 'counter' by 1 } void MyClass::set_counter(int _counter) { counter = _counter; // Sets the integer 'counter' to the passed integer of '_counter' } MyClass::~MyClass() // This is the destructor of the class MyClass { cout << "Destructor is called\n"; } int main() // main function where the execution starts { // Creates a variables of the class MyClass, you initalize them // Just like normal varible types such as int, char, int, etc. // Here the MyClass() is called MyClass theVar; // The reason this one has (407) at the end is because of the definition of // theOtherVar has one that supports an integer. Sends the integer of 407 to // the MyClass(int x) constructor. MyClass theOtherVar(407); // This will display the current value of 'counter' by calling the function get_counter() // in the class MyClass cout << "Current counter level of theVar: " << theVar.get_counter() << endl; // Now increase the value of the counter by calling // the count_more function of the class theVar.count_more(); // This will display the current value of 'counter' by calling get_counter, cout << "Current counter level of theVar: " << theVar.get_counter() << endl; // the function set_counter and passes the integer 5 which sets // the integer 'counter' to 5 theVar.set_counter(5); // This will display the current value of 'counter' which is currently 5 cout << "Current counter level of theVar: " << theVar.get_counter() << endl; // Display the value of 'counter' in the other definition of MyClass (theOtherVar), cout << "Current counter level of theOtherVar: " << theOtherVar.get_counter() << endl; // calls count_more which increases the integer 'counter' by 1 theOtherVar.count_more(); // Displays theOtherVar's version of 'counter', cout << "Current counter level of theOtherVar: " << theOtherVar.get_counter() << endl; // sets 'counter' back to 5 and displays the value of 'counter' theOtherVar.set_counter(5); // This will display the current value of 'counter' which is currently 5 cout << "Current counter level of theOtherVar: " << theOtherVar.get_counter() << endl; return 0; } You can download the above code as well.
The difference lies in the child classes and not within the class. Follow the below chart as to what happens to the base class members with the type of derivation Code: +---------------+---------------+---------------+---------------+ | Derivation -> | Private | Protected | Public | +---------------+---------------+---------------+---------------+ | Base Class | | | | +---------------|---------------+---------------+---------------+ | Private | Not Derived | Not Derived | Not Derived | | Protected | Private | Protected | Protected | | Public | Private | Protected | Public | +---------------+---------------+---------------+---------------+
I would also be helpfull to poeple if you put the code without words, it looks a bit of a jumble with them.
I want to tell them with lots and lots of comment on each line for you to understand what each line does and how the program