C++ program – illustrate a Single Inheritance

by Nideesh C on April 14, 2011 · 0 comments

in C++




A program to illustrate a single inheritance. We have a base class B and a derived class D. The class B contains one private data member, one public data member and three public member functions. The class D contains one private data member and two public member functions.

#include<iostream.h>
#include<conio.h>
class B
{
int a;
public:
int b;
void get_ab();
int get_a();
void show_a();
};
class D: private B
{
int c;
public:
void mul();
void display();
};
void B::get_ab()
{
cout<<"Enter Values for a and b";
cin>>a>>b;
}
int B::get_a()
{
return a;
}
void B::show_a(){
			cout<<"a= "<<a<<"\n";
		}
void D::mul()
{
			get_ab();
			c=b*get_a();
       	}
void D:: display()
{
show_a();
cout<<"b= "<<b<<"\n";
cout<<"c= "<<c<<"\n\n";
}
void main()
{
			clrscr();
			D d;
			d.mul();
			d.display();
			d.mul();
			d.display();
			getch();
		}

OUTPUT

 A=5
 A=5
 B=10
 C=50

 A=5
 B=20
 C=100

 

Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C++ program – illustrate Hybrid Inheritance.
  2. C++ program – illustrate Multilevel Inheritance
  3. C++ program – implement three classes using multiple inheritance
  4. C++ program – illestrate virtual functions
  5. C++ program – Using class to generate mark sheet using multiple inheritance

Leave a Comment

Previous post:

Next post: