In: Computer Science
Write a C++ program where class 1 and class 2 (which is a base class) should have a derived class (class 4 and class 5). Each of the derived classes should include at least 1 variable, 2 functions (one will be showing the function overriding case, the second one will be showing the function overloading case), 2 constructors (with default values, with parameters).
Then class 3 (composition) (to relate class 1 and class 2) should include 3 variables (first variable is an object belongs to class 1, second variable is an object belongs to class 2,third variable is class 3 own variable), 3 functions.
code:
#include<iostream>
using namespace std;
class a1{ //base class
public:
void display(){ //function
declarartion
cout<<"In
class 1 constructor"<<endl;
}
};
class a2{ //base class
public :
void show1(){ //function
declaration
cout<<"In
class 2"<<endl;
}
};
class a4:public a1
{
public:
//default constructor of class 4
a4(){
cout<<"This is default constructor"<<endl;
}
//constructor with parameters of
class 4
a4(int z,int y){
cout<<z;
cout<<y;
}
//function overriding of calss
4
void display(){
cout<<"This is overriding example of class
4"<<endl;
}
//overloading of class 4
void show(int a,int b){
cout<<"This os overloading example of class
4"<<endl;
}
};
class a5:public a2 // derived class of a2
{
//variable in class 5
int x=10;
public:
//default constructor of class 5
a5(){
cout<<"This is default constructor"<<endl;
}
//constructor with parameters
a5(int z,int y){
cout<<z<<endl;
cout<<y<<endl;
}
//function overriding of class
5
void display(){
cout<<"This is overriding example of class
5"<<endl;
}
//function overloading of class
5
void show(int a,int b){
cout<<"This os overloading example of class
5"<<endl;
}
void show(int a,int b,int c){
cout<<"This is overloading example of class
5"<<endl;
}
};
class a3:public a2,public a1{ // derives class of 1 and 2
//declaring three variables in class 3
a1 one;
a2 two;
static a3 three;
public:
void func1(){
cout<<"Function 1 in class 3"<<endl;
}
void func2(){
cout<<"THis is function 2 in class 3"<<endl;
}
void func3(){
cout<<"This is function 3 in class 3"<<endl;
}
};
int main(){
//creating object of class 3
a3 obj;
//calling functions of class 3
obj.func1();
obj.func2();
obj.func3();
//creatinf object of class 5
a5 obj1;
//calling show function of class 5
obj1.show(2,3,4);
}
output:
code screenshot:
Note:Please see code screenshot for understanding indentation of the code