Wednesday, September 26, 2012

C++ Class Extend and Member Scope



Introduction

A simple practice of C++ class extend and member scope:

public member can be accessed by any object
protected member can be accessed by self and sub classes
private member can be accessed by self only

Test Sample

BaseClass.h

class BaseClass {
    // sub class and any other object can access public member directly
    public: 
        int _varOne; // public member field
        BaseClass()
        {
            _varOne = 0;
            _varTwo = 0;
            _varThree = 0;       
        }
        // where _varOne(varOne) denotes assign varOne to _varOne
        BaseClass (int varOne, int varTwo, int varThree) : _varOne(varOne),
                _varTwo(varTwo), _varThree(varThree)
        {
        }
        int getVarOne ()
        {
            return _varOne;
        }
        int getVarTwo ()
        {
            return _varTwo;
        }
        int getVarThree ()
        {
            return _varThree;
        }
    // sub class can access protected member directly
    // any other object cannot access protected member directly
    protected:
        int _varTwo; // protected member field
    // sub class and any other object cannot access private member directly
    private:
        int _varThree; // private member field
}; 


ExtendedClass.h


#include <iostream>
#include "BaseClass.h"

using namespace std;

/** Extends BaseClass
 * then can access all public/protected member of
 * BaseClass directly in ExtendedClass
 */
class ExtendedClass : public BaseClass {
    public:
        ExtendedClass ()
        {
            // will call super class' basic constructor
            _varFour = 0;
        }
        // call constructor in super class
        ExtendedClass (int varOne, int varTwo, int varThree, int varFour) : BaseClass (varOne, varTwo, varThree),
                _varFour(varFour) // then assign value
        {
        }
        int getVarFour ()
        {
            return _varFour;
        }
        void showAll ()
        {
            cout << "\tvarOne: " << _varOne << endl // _varOne is public
                << "\tvarTwo: " << _varTwo << endl // _varTwo is protected
                << "\tvarThree: " << getVarThree() << endl // _varThree is private member in super class

            // Error! cannot access super class' private member
            // <<"varThree: " << _varThree << endl

                << "\tvarFour: " << _varFour << endl << endl; // _varFour is in this class
        }
    private:
        int _varFour;
};


test.cpp


#include <iostream>
#include "ExtendedClass.h"

using namespace std;

int main() {
    ExtendedClass e1;
    ExtendedClass e2(1, 2, 3, 4);

    cout << "\te2._varOne: " << e2._varOne << endl << endl;
    cout << "\te2._varTwo: " << e2.getVarTwo() << endl << endl;
    cout << "\te2._varThree: " << e2.getVarThree() << endl << endl;
    cout << "\te2._varFour: " << e2.getVarFour() << endl << endl;


    // Error! _varTwo is protected member in e2's super class
    // can only access it directly in BaseClass or ExtendedClass
    // cout << "e2._varTwo: " << e2._varTwo << endl << endl;

    // Error! _varThree is private member in e2's super class
    // cout << "e2._varThree: " << e2._varThree << endl << endl;

    // Error! _varFour is private member in e2's class
    // cout << "e2._varFour: " << e2._varFour << endl << endl;

    e1.showAll();
    e2.showAll();
    system("PAUSE");
}


The Result




Reference

http://www.cplusplus.com/doc/tutorial/inheritance/


Download

Files at github
https://github.com/benbai123/C_Cplusplus_Practice/tree/master/CPP/CPP_Basic/CPP_Extend_and_scope

No comments:

Post a Comment