Free, tested & ready to use examples!
AnyExample.com
 
Web anyexample.com
 

C++ inheritance example

abstract 
With the help of inheritance it is possible to form new classes using already defined and more generalized ones. Inheritance helps to reuse existing code when defining several classes which have some common properties or methods. Following article contains brief examples of C++ public inheritance.
compatible 
  • Any modern C++ compiler
  • Let's define several classes for geometrical figures:

    • Square
    • Circle
    • Rectangle
    • Oval

    Assuming, that we do not need rotation, it is possible to define square and circle using center of object and only one property - radius for circle and side length for square. This way, it is possible to define rectangle and oval with position and two properties(width and height).

    So, in our case, each class has position property. Two classes has one property and two other objects has two properties. Let's try to define these classes using inheritance:

    • Let's define class "BasePoint" which will have position properties (x, y)
      • Let's define class "Figure1P" which will have one property geometrical figure (p1)
        • Class Square is based on this class (has x, y and p1)
        • Class Circle is also based on this class (has x, y and p1)
        • Class Figure2P extends this class with one more property (p2)
          • Class Rectangle is based on Figure2P (has x, y, p1, p2)
          • Class Oval is also based on Figure2P (has x, y, p1, p2)

    Following program contains BasePoint, Figure1P, Square, Circle, Figure2P, Rectangle, Oval classes. Then in creates array of objects and for user-entered object number prints it's information. This is also an example for C++ polymorphism and virtual functions.

    source code: C++
     
    #include <iostream> 
    #include <string> 
    using namespace std;
     
    class BasePoint { 
    public:
    	BasePoint(int px, int py):x(px),y(py) {} 
    	int x, y; //position 
    	virtual string type() = 0;
    	virtual void info() { 
    		cout << endl << "figure: " << type() << endl;
    		cout << "position: x=" << x << ", y=" << y << endl;
    	} 
    };
     
    class Figure1P : public BasePoint { 
    public:
    	Figure1P(int px, int py, int r):p1(r),BasePoint(px, py) {} 
    	int p1;
    	virtual void info() { 
    		BasePoint::info();
    		cout << "property 1: p=" << p1 << endl;
    	} 
    };
     
    class Square : public Figure1P { 
    public:
    	Square(int px, int py, int r):Figure1P(px, py, r) {};
    	virtual string type() { 
    		return "square";
    	} 
    };
     
    class Circle : public Figure1P { 
    public:
    	Circle(int px, int py, int r):Figure1P(px, py, r) {} 
    	virtual string type() { 
    		return "circle";
    	} 
    };
     
    class Figure2P : public Figure1P { 
    public:
    	Figure2P(int px, int py, int w, int h):p2(h),Figure1P(px, py, w) {} 
    	int p2;
    	virtual void info() { 
    		Figure1P::info();
    		cout << "property 2: p=" << p2 << endl;
    	} 
    };
     
    class Rectangle : public Figure2P { 
    public:
    	Rectangle(int px, int py, int w, int h):Figure2P(px, py, w, h) {} 
    	virtual string type() { 
    		return "rectangle";
    	} 
    };
     
    class Oval : public Figure2P { 
    public:
    	Oval(int px, int py, int w, int h):Figure2P(px, py, w, h) {};
    	virtual string type() { 
    		return "oval";
    	} 
    };
     
    int main(void) { 
    	BasePoint **objs = new BasePoint*[5]; 
    	// allocate space for 10 BasePoint pointers,  
    	// they may be used to store derived classes 
     
    	// creating objects 
    	objs[0] = new Circle(7, 6, 55);
    	objs[1] = new Rectangle(12, 54, 21, 14);
    	objs[2] = new Square(19, 32, 10);
    	objs[3] = new Oval(43, 10, 4, 3);
    	objs[4] = new Square(3, 41, 3);
     
    	bool flag=false; 
    	do { 
    		cout << endl << "We have 5 objects with numbers 0..4" << endl;
    		cout << "Enter object number to view information about it " << endl;
    		cout << "Enter any other number to quit " << endl;
    		char onum; // in fact, this is a character, not a number 
    		// this allows user to enter letter and quit... 
    		cin >> onum;
     
    		// flag -- user have entered number 0..4 
    		flag = ((onum >= '0')&&(onum <= '4'));
     
    		if (flag) 
    			objs[onum-'0']->info();
     
    	} while(flag);
     
    	// freeing memory 
    	for(int i=0;i<5;i++) 
    		delete objs[i];
     
    	delete [] objs;
    } 
     

    This is just an example. To find out more about inheritance read any good C++ book.

    tested 
  • Windows XP :: Microsoft Visual C++ 2003
  •  


     
    © AnyExample 2010-2013
    License | Privacy | Contact