Saturday, March 2, 2013

OOCP (CPP) : Example # 14 : CPP Single Inheritance Example 2


OOCP (CPP)  :  Example #  14 : CPP Single Inheritance Example 2

* 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 #  
14 : CPP Single Inheritance Example 2


#include <iostream.h>


#include <conio.h>

class B

{
protected:
int a;
public:
int b;
B()
{
cout << "Base class constructor called..." << endl;
}
void get_ab();
void line()
{
cout << "Base class Line function called..." << endl;
}
~B()
{
cout << "Base class Destructor called..." << endl;
}
};

class D : public B

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

};


void B :: get_ab()

{
cout << "Enter the value of A and B " << endl;
cin >> a >> b;
}

void D :: mul()

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

//=================================================

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

D1.get_ab();

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

cout << endl;


D1.b = 20;

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

getch();

}

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

--------------------------------------------------------------------------------
OOCP (CPP)  :  Example #  14 : CPP Single Inheritance Example 2

* Example demonstrate how to create your first inheritance program using cpp example 2.
* 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. 

1 comment:

  1. thank you for sharing nice article in your blog
    visit
    web tutorial programming
    https://www.welookups.com

    ReplyDelete