Thursday, February 28, 2013

OOCP (CPP) : Example # 13 : CPP Single Inheritance

OOCP (CPP)  :  Example #  13 : CPP Single Inheritance 

* Lets start learning about cpp inheritance.
* Following example is simple inheritance : Single Inheritance consist of one base class and one derived class.


//Written by Latest Technology Guide    
//Title : OOCP (CPP)  :  Example #  
13 : CPP Single Inheritance 

// Inheritance 01 - Simple Derived class as private from Base class
// Single Inheritance

#include <iostream.h>
#include <conio.h>

class B
{
private:
int a;
public:
int b;
void get_ab();
int get_a();
void show_a();
void line()
{
cout << "Base class Line function called..." << endl;
}
};

class D : private B
{
private:
int c;
public:
void mul();
void display();
void line()
{
cout << "Derived class line function called..." << endl;
}

};

void B :: get_ab()
{
cout << "Enter the value of A and B " << endl;
cin >> a >> b;
}

int B :: get_a()
{
return a;
}

void B :: show_a()
{
cout << "Base class called..." << endl;
cout << " A ==> " << a << endl;
}
void D :: mul()
{
get_ab();
c = b * get_a();
}
void D :: display()
{
cout << "Derived class called " << endl;
show_a();
cout << " B ==> " << b << endl;
cout << " C ==> " << c << endl;
}

//=================================================
// Void Main
//=================================================
void main()
{
clrscr();
D D1; //derived class object

D1.mul();
D1.display();

cout << endl;

D1.mul();
D1.display();
D1.line();

getch();
}


Output:
* You will find output as you have entered value.

--------------------------------------------------------------------------------
OOCP (CPP)  :  Example #  13 : CPP Single Inheritance 

* Example demonstrate how to create your first inheritance program using cpp.
* in example B stands for Base Class while D stands for Derived class.


Note: All programs are developed and tested using Turbo C++ 3.0 under Windows XP. We just want to provide guidelines to the users. If you are using any other Compiler or other operating system they you need to modify this program as per your requirements. 

No comments:

Post a Comment