In: Computer Science
QUESTION 2
write a c++ program
Assume that you own a construction company that builds different types of buildings. The goal is to write a program that is potentially useful for your company.
C++ Program :
#include <iostream>
using namespace std;
class Building //base class called Building
{
public :
string constrcompname="XYZ"; //declared two member variables
int totalcount=100;
public :
Building(){};//default constructor
virtual void welcome()//member function of Building class
{
cout<<"Welcome ! "<<endl;
}
void companyname()//member function of Building class
{
cout<<"The name of our construction company is :"<<constrcompname<<endl;
}
void total()//member function of Building class
{
cout<<"The total buildings our company has builded so far is : "<<totalcount<<endl;
}
};
class Apartment : public Building //derived class called Apartment for Building class
{
public :
int count=50;
int purchase,rent;
string city;
Apartment(){};
void welcome()//overriding is performed here
{
cout<<"Welcome to the Apartment site of our prestigious construction company ! "<<endl;
}
int totalcount()
{
cout<<"We have totally built "<<count<<" Apartments so far"<<endl;
}
int gotPurchased(int purchase,int rent)//operator overloadingis done here
{
cout<<"Totally "<<purchase<<" apartments have been purchased and "<<rent<<" apartments rented so far"<<endl;
}
int gotPurchased(int purchase,string city)//opertor overloading is done here
{
cout<<"Totally "<<purchase<<" apartments have purchased in "<<city<<" city so far"<<endl;
}
};
class Independenthouse : public Building //Independenthouse is a derived class of Building class
{
public :
Independenthouse(){};
void welcome()//overriding of function welcome
{
cout<<"Welcome to our Independenthouse site of our prestigious construction company and today only we have finished building one independent house"<<endl;
}
};
int main()
{
Apartment a,b;//creating objects for derived class
b.Building::welcome();//calling memeber function of base class using scope resolution operator due to member function overriding.
a.companyname();//calling the memberfunction of base class
a.total();
a.welcome();
a.totalcount();//calling the member function of derived class
a.gotPurchased(20,30);//function overloading
a.gotPurchased(20,"Sydney");//function overloading
Independenthouse i;
i.welcome();//calling member function of derived class
return 0;
}
Output :
Welcome ! The name of our construction company is :XYZ The total buildings our company has builded so far is : 100 Welcome to the Apartment site of our prestigious construction company ! We have totally built 50 Apartments so far Totally 20 apartments have been purchased and 30 apartments rented so far Totally 20 apartments have purchaseded in Sydney city so far Welcome to our Independenthouse site of our prestigious construction company and today only we have finished building one independent house
Now,let us take a look at the screenshot of the program with its output :
Now let us take a look at the screenshot of the output :
A Brief Description of what the program does :
In this program,we have created a base class called the BUILDING with two member variables and three member functions.Then we have two derived classes from the base class.Then,we have demonstrated one example each for overriding and overloading and have indicated constructor.This program simply displays the name of the construction company with the number of buildings it had built.Apartment and Independenthouse are the different types of buildings employed here.And it displays how many apartments have been purchaed and rented in a city.
Function Overriding :
Overriding of member functions is nothing but the member function of both the base class and derived class will be same.Now we can call this function using the object of the derived class.But to the call the function which is in base class,scope resolution operator is used.You may see this implemetation in the program.Member function WELCOME is the overrided function.
Function Overloading :
Function Overloading is nothing but we can have same name for functions but the parameters are different.When we look into this program,gotPurchased(int,int) and gotPurchased(int,string).Here,the member functions are same but the parameter list is different.