A Brief intro to classes in C++

Discussion in 'C++' started by shabbir, Nov 23, 2006.

  1. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83

    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.
     

    Attached Files:

  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Very well written indeed. Really helpful introduction to classes. :)
     
  3. bothie

    bothie New Member

    Joined:
    Nov 14, 2006
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    Harare,Zimbabwe
    what is the difference between a private member and a protected member
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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        |
    +---------------+---------------+---------------+---------------+
    
     
  5. Vromoth

    Vromoth New Member

    Joined:
    Nov 6, 2006
    Messages:
    43
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://vromoth.4000webs.com/
    I would also be helpfull to poeple if you put the code without words, it looks a bit of a jumble with them.
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Why not? We dont mind having a code do all the wording
     
  7. rahul.mca2001

    rahul.mca2001 New Member

    Joined:
    Feb 13, 2008
    Messages:
    103
    Likes Received:
    0
    Trophy Points:
    0
    when we need a member in application level should it be private or protected
     
  8. sanjujoshhi

    sanjujoshhi New Member

    Joined:
    Apr 4, 2008
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Design Engineer
    Location:
    India
  9. programming girl

    programming girl New Member

    Joined:
    Apr 24, 2008
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    yahhhh that's right
     
  10. DeepSeas

    DeepSeas New Member

    Joined:
    Feb 26, 2008
    Messages:
    20
    Likes Received:
    0
    Trophy Points:
    0
    That's a great description. I'll definitely have to use it when explaining C++. Thank you!
     
  11. BS Dahiya

    BS Dahiya New Member

    Joined:
    Sep 18, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Good information.
    Thanks
     
  12. seangtz

    seangtz New Member

    Joined:
    Jun 6, 2008
    Messages:
    126
    Likes Received:
    3
    Trophy Points:
    0
    Useful information...
     
  13. Unknown

    Unknown New Member

    Joined:
    Sep 15, 2008
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
  14. limon

    limon New Member

    Joined:
    Sep 29, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice