Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   A Brief intro to classes in C++ (http://www.go4expert.com/articles/brief-intro-classes-cpp-t1971/)

shabbir 23Nov2006 11:37

A Brief intro to classes in C++
 
1 Attachment(s)

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: CPP

#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.

pradeep 23Nov2006 17:43

Re: A Brief intro to classes in C++
 
Very well written indeed. Really helpful introduction to classes. :)

bothie 24Nov2006 12:09

Re: A Brief intro to classes in C++
 
what is the difference between a private member and a protected member

shabbir 24Nov2006 12:36

Re: A Brief intro to classes in C++
 
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        |
+---------------+---------------+---------------+---------------+


Vromoth 30Nov2006 05:19

Re: A Brief intro to classes in C++
 
I would also be helpfull to poeple if you put the code without words, it looks a bit of a jumble with them.

shabbir 30Nov2006 09:11

Re: A Brief intro to classes in C++
 
Quote:

Originally Posted by Vromoth
I would also be helpfull to poeple if you put the code without words, it looks a bit of a jumble with them.

Why not? We dont mind having a code do all the wording

rahul.mca2001 6Mar2008 13:56

Re: A Brief intro to classes in C++
 
when we need a member in application level should it be private or protected

sanjujoshhi 8Apr2008 17:06

Re: A Brief intro to classes in C++
 
it was good

programming girl 25Apr2008 23:41

Re: A Brief intro to classes in C++
 
yahhhh that's right

DeepSeas 11Jun2008 19:40

Re: A Brief intro to classes in C++
 
That's a great description. I'll definitely have to use it when explaining C++. Thank you!


All times are GMT +5.5. The time now is 01:25.