cpp single inheritance

Single inheritance

let us consider a simple example to understand single inheritance in the below program show a base class B and a derived class D. The class B consist 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

example of single inheritance

single inheritance pic

#include < iostream >
using namespace std;
class B
{
int a; // private its not inherit
public:
int b; // public its ready for inherit
void get_data();
int get_A();
int show_A();
};
class D: public B //public derivation
{
int c;
public:
void mul();
void display();
};
void B :: get_data()
{
a=8; b=90;
}
int B:: get_A()
{
return a;
}
void B:: show_A()
{
cout << “a=” << a << “\n”;
}
void D:: mul()
{
c=b*get_A();
}
void D:: display()
{
cout << “a=” << get_A() << “\n”;
cout << “b=”< cout << “c=”< }
d.display();
void main()
{
D d;
d.get_data();
d.mul();
d.show_A();
d.b=20;
d.mul();
d.display();
}

output:
a=8
a=8
b=90
c=720


a=8
b=20
c=160

The class D is a public derivation of the base class B. Therefore D inherits all the public members of B and retains their visibility. Thus a public member of the base class B is also a public member of the derived class D. The private member of B cannot be inherit by D. The class D, in effect, will have more members than what it contains at the time of declaration

in the above example that the object of class D have access to all the public members of B. Let us have a look at the functions show_A() and mul():

void show_A()
{
cout<<“a=”< }
void mul()
{
c=b*get_A(); // means c=b*a
}

so here the data member a is private in B and cannot be inherited, objects of D are able to access it through an inherited member function of B

let us now consider the case of private derivation

class B
{
int a;
public :
int b:
void get_data();
void get_A();
void show_A();
};
class D : private B // private derivation
{ int C;
public :
void mul();
void display();
};

The membership of the derived class D is shown in fig 8.4 in private derivation, the public members of the base class become private members of the derived class. Therefore the objects of D can not have direct access to the public member function of B.
the statement such as

d.get_data(); // get_data() is private
d.get_A(); // so also get_A()
d.show_A(); // and show_A()

will not work. however , These functions can be used inside mul() and display like the normal functions as shown below

void mul()
{
get_data();
c=b*get_A();
}
void display()
{
show_A(); // outputs value of ‘a’
cout << “b=” << b << “\n” << “c=” << c << “\n\n”;
}

program 8.2 incorporates these modification for private derivation .compare this with program 8.1

single Inheritance : PRIVATE
#include < iostream >
using namespace std;
class B
{
int a; // private so not inherit
public :
int b;
// public ready to inherit
void get_data();
int get_A();
int show_A();
}; class D: private B // this is private derivation
{ int c;
public :
void mul();
void display();
};
void B :: get_data()
{
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_data();
c=b*get_A(); // ‘a’ cannot used directly
}
void D ::display()
{ show_A(); // outputs value of ‘a’
cout << “b=” << b << “\n” << “c=” << c << “\n\n”;
}
void main()
{
D d;
// d.get_data(); //will not work
d.mul();
// d.show_A(); //will not work
d.display();
// d.b=20; // will not work because b has become private
d.mul();
d.display();
return 0;
}

output
Enter values for a and b 5: 10
a=5
b=10
c=50
Enter values for a and b: 12 20
a=12
b=20
c=240

Suppose A base class and a derived class define a function of the same name. what will happen when a derived class object invokes the function?. in such cases the derived class function supersedes the base class definition. The base class function. Will be called only if the derived class does not redefine the function.

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise