Question

In: Computer Science

QUESTION 2 write a c++ program Assume that you own a construction company that builds different...

QUESTION 2

write a c++ program

  1. 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.

    1. [5 points] Define a base class called building. Include at least two member variables and three member functions
    2. [10 points] Define two derived classes that inherit properties from the base class
    3. [10 points] Demonstrate at least one example each of overriding and overloading by defining the functions and calling them
    4. [10 points] Indicate constructor, overriding and overloading occurs with proper comments
    5. [5 points] Write a brief description of what your program does.

Solutions

Expert Solution

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.


Related Solutions

(IN C) Program Question 2: Write a program that solves for c in the Pythagorean Theorem:...
(IN C) Program Question 2: Write a program that solves for c in the Pythagorean Theorem: a2 + b2 = c2 The user will enter values for a and b, and you will calculate c. All of this code will go in just one source file.
WRITE A C++ PROGRAM: QUESTION 1 Assume you have a hobby to collect a particular type...
WRITE A C++ PROGRAM: QUESTION 1 Assume you have a hobby to collect a particular type of items. Specify the category to which the items you collect belong to ____________________________ [2 points] Define a struct with the name the same as the category that you filled in the blank above [4 points] The struct must have at least two members corresponding to properties relevant to the items in the category you specified. The struct members must have names meaningful to...
I need specific codes for this C program assignment. Thank you! C program question: Write a...
I need specific codes for this C program assignment. Thank you! C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and...
Please if you are able to answer the question below: Write C++ program using native C++...
Please if you are able to answer the question below: Write C++ program using native C++ (you can use STL)  that produces Huffman code for a string of text entered by the user.  Must accept all ASCII characters.  Pleas explain how you got frequencies of characters, how you sorted them, how you got codes.
this has to be coded in c ++ • Write a program which generates a DIFFERENT...
this has to be coded in c ++ • Write a program which generates a DIFFERENT random number between 1 and 100 everytime you run the program. Please go online and see how poeple are trying to solve this. You can submit their code. Make sure you include a comment which shows from where the code was taken. You do not need to understand the code but if you can, thats great. This program will be used in the upcoming...
C# language Question: You need to write a program for students who receive bursaries in the...
C# language Question: You need to write a program for students who receive bursaries in the department, managing the free hours they have to do. For each recipient you store the recipient’s name and the number of hours outstanding. All recipients start with 90 hours. Implement class Recipients which has the private attributes Name and Hours. In addition to the constructor, the class has the following methods: public String getName() // Returns the name of the recipient public int getHours()...
C++ Question: write a program that prompts the user for the length and width of a...
C++ Question: write a program that prompts the user for the length and width of a rectangle in inches.  The program then uses functions to compute the perimeter and area of the rectangle and to convert those to meters and square meters respectively. Sample output from one instance of the program is shown below: ```html Welcome to the Foot-To-Meter Rectangle Calculator ================================================= Enter the rectangle length in feet: 2 Enter the rectangle width in feet: 3 The rectangle dimensions are: 0.61...
Write the program in C++ The Rebel food truck has asked you to write a program...
Write the program in C++ The Rebel food truck has asked you to write a program for them to do both inventory and sales for their food wagon. Ben and Dave run the food truck and they already have full time day jobs so they could really use your help. The food truck sells: hamburger, hotdogs, chilli, fries and soda. The truck has spots for 200 hamburger patties, 200 hot dogs, 75 hamburger buns, 75 hot dog buns, 500 ounces...
Write the LEGv8 assembly program for following c program. Assume array1, array2 and array3 are stored...
Write the LEGv8 assembly program for following c program. Assume array1, array2 and array3 are stored in memory with base addresses at 1000, 2000 and 3000 respectively. int absoluteDifference(int x, int y) { int r; if (x > y) r = x - y; else r = y - x; return r; } int main() { for (int i=0; i < 10; i++) { int a = array1[i]; int b = array2[i]; int c = absoluteDifference(a, b); array3[i] = c;...
Write the LEGv8 assembly program for following c program. Assume array1, array2 and array3 are stored...
Write the LEGv8 assembly program for following c program. Assume array1, array2 and array3 are stored in memory with base addresses at 1000, 2000 and 3000 respectively. int absoluteDifference(int x, int y) { int r; if (x > y) r = x - y; else r = y - x; return r; } int main() { for (int i=0; i < 10; i++) { int a = array1[i]; int b = array2[i]; int c = absoluteDifference(a, b); array3[i] = c;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT