Create a class Building with data members building number, name, area (in square feet), cost/square feet, location, total cost, and year of construction. Declare the cost/square feet as the static data member. Define a parametric constructor to initialize the data members. Make use of this pointer. Create an array of objects and display details of N buildings along with the total cost
Question
Create a class Building with data members building number, name, area (in square feet), cost/square feet, location, total cost, and year of construction. Declare the cost/square feet as the static data member. Define a parametric constructor to initialize the data members. Make use of this pointer. Create an array of objects and display details of N buildings along with the total cost
Solution
Here is a simple implementation in C++:
#include<iostream>
using namespace std;
class Building {
int buildingNumber;
string name;
float area;
static float costPerSqFeet;
string location;
float totalCost;
int yearOfConstruction;
public:
Building(int buildingNumber, string name, float area, string location, int yearOfConstruction) {
this->buildingNumber = buildingNumber;
this->name = name;
this->area = area;
this->location = location;
this->yearOfConstruction = yearOfConstruction;
this->totalCost = area * costPerSqFeet;
}
void display() {
cout << "Building Number: " << buildingNumber << endl;
cout << "Name: " << name << endl;
cout << "Area: " << area << " sq feet" << endl;
cout << "Location: " << location << endl;
cout << "Year of Construction: " << yearOfConstruction << endl;
cout << "Total Cost: " << totalCost << endl;
}
static void setCostPerSqFeet(float cost) {
costPerSqFeet = cost;
}
};
float Building::costPerSqFeet = 0;
int main() {
Building::setCostPerSqFeet(2000); // Set cost per square feet
Building buildings[] = {
Building(1, "Building 1", 1000, "Location 1", 2000),
Building(2, "Building 2", 2000, "Location 2", 2001),
Building(3, "Building 3", 3000, "Location 3", 2002)
};
int N = sizeof(buildings) / sizeof(Building);
for(int i = 0; i < N; i++) {
buildings[i].display();
cout << endl;
}
return 0;
}
In this code, we first define a class Building with the required data members. The costPerSqFeet is declared as a static data member. We then define a parametric constructor to initialize the data members. The this pointer is used to refer to the current object's members. The display function is used to print the details of a building. The setCostPerSqFeet static function is used to set the cost per square feet.
In the main function, we first set the cost per square feet using the setCostPerSqFeet function. We then create an array of Building objects and initialize them using the constructor. We calculate the size of the array using sizeof(buildings) / sizeof(Building). Finally, we loop through the array and call the display function on each object to print their details.
Similar Questions
Create an array of objects and display details of N buildings along with the total cost
creata a classs vehicle withe datamenbers vehicle number name cost brand and yeat. declare year as static. create objects and display the details. create array of objects
Create a structure named warehouse with details like location, name, total_stock_rooms, occupied_rooms, and price_room. Write a C program to input details for the N warehouse. Write user-defined functions for: Function 1: Reading the details of the warehouse. This function should return the structure to the main() function. Function 2: Find the available rooms in the warehouse. Calculate the total cost for available rooms and display the details. Pass the structure returned from function 1 to function 2. Read the number of warehouses, location, name, total_stock_rooms, occupied_rooms, and price_room. Sample Input 2 Chennai Wheat warehouse 100 80 5000 Mumbai Electronic warehouse 15 10 10000 Sample Output [Wheat warehouse in Chennai: Available rooms-20: Cost 100000] [Electronic warehouse in Mumbai: Available rooms-5: Cost 50000]
Write a C++ program Create a base class Mountain with protected data members: mountain name and number of trees.Define a constructor to read the data members. Create a class Coast from Mountain with a static data member to represent the total number of dams. Define a constructor to read the dam details such as dam name, total length, and width of the dam. Define member function “Display” to print the mountain details and the surface area of a dam.surface area of a dam=length*widthCreate a class Dam_detials from Coast with a private data member: depth. Create a parametrized constructor to initialize the value of depth. Define a member function “ Display” to calculate the capacity of your dam.Member function (Display) in Dam_details overrides the member function (Display) in the Coast classCapacity=Volume/1000Volume=Surface area*depthCreate required objects, base pointers and access the member functions. Identify the type of inheritance given.Sample Input5 (Read the depth )Mountain1 (Read the mountain name)20 (Read number of trees)Dam1 (Read dam name)30 (Read length)50(Read width)Sample OutputMountain1 20 treesDam1 area 1500Volume 6000Capacity 6.0Total dam 1
A researcher intends to analyze data regarding some group of participants in a social event. To help him achieve this, you are required to develop a simple program in C++ that prompts the researcher to key in the number of participants in the event then stores the data that is collected to show expenditures by each participant on food, drinks, transport and entertainment as vote heads. The data should be organized into a 2-D array. There will be two methods for inputting and analyzing the data. The data and the associated operations/methods must be members of a structure. a) Total expenditure for all participants b) Mean expenditure for participants c) The variance for the expenditure among the participants.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.