Question

In: Computer Science

Write a C++ program where class 1 and class 2 (which is a base class) should...

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.

Solutions

Expert Solution

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


Related Solutions

Write a C++ program that creates a base class called Vehicle that includes two pieces of...
Write a C++ program that creates a base class called Vehicle that includes two pieces of information as data members, namely: wheels (type int) weight (type float) Program requirements (Vehicle class): Provide set and a get member functions for each data member. Your class should have a constructor with two parameters (one for each data member) and it must use the set member functions to initialize the two data members. Provide a pure virtual member function by the name displayData()...
In C++ write a program with a base class thet has a pure virtual function SALARY,...
In C++ write a program with a base class thet has a pure virtual function SALARY, and two derived classes. In the first derived class salary is increased by 20%, in the second derived class salary is increased by 30%
Write a C++ program test.cpp. The class should contain a protected int member variable var, which...
Write a C++ program test.cpp. The class should contain a protected int member variable var, which is initialized with an integer value between 1 and 50 in a constructor that takes an integer parameter. The class should contain a public member function called play that should print out a sequence of integers as a result of iteratively applying a math function f to the member variable var. The function f is defined as f(x) = (3x+1)/2 if x is odd...
All Code should be written in C: 1. Write a C program which prompts the user...
All Code should be written in C: 1. Write a C program which prompts the user to enter two integer values. Your program should then print out all numbers between 1 and 1000 that are divisible by both of those numbers. 2. Modify your program from question 1 such that the first 1000 numbers that are divisible by both numbers are printed out, instead of numbers up to 1000. 3. Using dynamic memory, allocate memory for an array of 100...
C++ Programming 1) What is the feature that makes a base class an abstract class? 2)...
C++ Programming 1) What is the feature that makes a base class an abstract class? 2) When designing a class hierarchy involving classes A and B, what are the two questions that must be asked to determine whether class B should be derived from class A?
write an algorithm program using python or C++ where a0=1, a1=2 an=an-1*an-2, find an ,also a5=?
write an algorithm program using python or C++ where a0=1, a1=2 an=an-1*an-2, find an ,also a5=?
Please write this program in C++ Write a program that           - a user-defined class Cuboid...
Please write this program in C++ Write a program that           - a user-defined class Cuboid which has the following elements:                    - default constructor (has no parameters)                    - constructor that has 3 parameters (height, length, width)                    - data members                              - height, length, width                    - member functions                              - to set the value of each of the data members - 3 functions                              - to get the value of each of the data members - 3...
In C++ Define a base class called Person. The class should have two data members to...
In C++ Define a base class called Person. The class should have two data members to hold the first name and last name of a person, both of type string. The Person class will have a default constructor to initialize both data members to empty strings, a constructor to accept two string parameters and use them to initialize the first and last name, and a copy constructor. Also include appropriate accessor and mutator member functions. Overload the operators == and...
Program must be in C++! Write a program which: Write a program which uses the following...
Program must be in C++! Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to...
********************C# C# C#******************** Part A: Create a project with a Program class and write the following...
********************C# C# C#******************** Part A: Create a project with a Program class and write the following two methods(headers provided) as described below: 1. A Method, public static int InputValue(int min, int max), to input an integer number that is between (inclusive) the range of a lower bound and an upper bound. The method should accept the lower bound and the upper bound as two parameters and allow users to re-enter the number if the number is not in the range...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT