Program
/*
This program demonstrates basic class inheritance in C++.
Class A serves as a base class with three member functions and a constructor.
Class B is derived from A and overrides some of its functions.
The main function creates an object of class B and calls various functions
to illustrate how inheritance and function overriding work.
*/
#include <iostream>
// Base class A
class A
{
public:
// Constructor of A
A()
{
std::cout<<"Constructor of A called\n";
}
// Member function f1 of A
public: void f1()
{
std::cout<<"F1 function of A called\n";
}
// Member function f2 of A
public: void f2()
{
std::cout<<"F2 function of A called\n";
}
// Member function f3 of A
public: void f3()
{
std::cout<<"F3 function of A called\n";
}
};
// Derived class B inheriting from A
class B: public A
{
public:
// Constructor of B, calls the constructor of A
B():A()
{
std::cout<<"Constructor of B called\n";
}
// Overriding function f1 from A
public: void f1()
{
std::cout<<"F1 function of B called\n";
A::f1(); // Explicitly calling f1 of A
}
// Overriding function f2 from A
public: void f2()
{
std::cout<<"F2 function of B called\n";
}
};
int main() {
// Creating an object of class B
B b;
// Calling functions f1, f2, and f3
b.f1(); // Calls overridden function in B, then calls A's f1
b.f2(); // Calls overridden function in B
b.f3(); // Calls inherited function from A
return 0;
}
Output
/
*
To do:
Make a similar overriding example of 3 layers.
Summary:
This program illustrates inheritance and function overriding in C++.
Class B inherits from class A and overrides some of its methods while still retaining inherited functionality.
The program execution shows the constructor calls and method overrides in action.
*/
#include <iostream>
// Base class A
/*************************************** A Class*****************************************************/
class A
{
public:A()
{
std::cout<<"Constructor of A called\n";
}
public: void f1()
{
std::cout<<"F1 function of A called\n";
}
// Member function f2 of A
public: void f2()
{
std::cout<<"F2 function of A called\n";
}
// Member function f3 of A
public: void f3()
{
std::cout<<"F3 function of A called\n";
}
};
// Derived class B inheriting from A
/***************************************** B Class ***********************************************/
class B: public A
{
public:
// Constructor of B, calls the constructor of A
B():A()
{
std::cout<<"Constructor of B called\n";
}
// Overriding function f1 from A
public: void f1()
{
std::cout<<"F1 function of B called\n";
A::f1(); // Explicitly calling f1 of A
}
// Overriding function f2 from A
public: void f2()
{
std::cout<<"F2 function of B called\n";
}
};
/**************************************** C Class***************************************************/
class C:public B
{
public:C():B()
{
std::cout<<"Constructor of C called\n\n";
}
public:void f1()
{
std::cout<<"F1 function of C called\n";
B::f1();
}
public:void f3()
{
std::cout<<"F3 function of C called\n";
}
};
/********************************************** Main Functin ***************************************/
int main()
{
// Creating an object of class B
C c;
// Calling functions f1, f2, and f3
c.f1(); // Calls overridden function in B, then calls A's f1
c.f2(); // Calls overridden function in B
c.f3(); // Calls inherited function from A
return 0;
}