How can i replace this rooms = (struct Room*) malloc(SIZE * sizeof(struct Room)); in this code? #include #include using namespace std; struct Room { double len; double width; double squareFootage; }; void show(struct Room room){ cout<<"squareFootage: "<>SIZE; rooms = (struct Room*) malloc(SIZE * sizeof(struct Room)); cout<<("\nEnter the dimensions for each room:\n"); for (int i = 0; i < SIZE; i++) { cout<<"Room dimensions: "<< i+1 <>rooms[i].len; cout<<"Width: "; cin>>rooms[i].width; rooms[i].squareFootage = area(rooms[i].len, rooms[i].width); } double apartmentSquareFootage = 0.0; cout<<"\nDisplaying apartment details: "<
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
How can i replace this rooms = (struct Room*) malloc(SIZE * sizeof(struct Room)); in this code?
#include <iostream>
#include <string>
using namespace std;
struct Room {
double len;
double width;
double squareFootage;
};
void show(struct Room room){
cout<<"squareFootage: "<<room.squareFootage<<endl;
cout<<"Length: "<<room.len<<endl;
cout<<"Width: "<<room.width<<endl;
}
double area (double len, double width) {
return len * width ;
}
int main(){
int SIZE=0;
static struct Room* rooms;
cout<<("\nEnter the number of rooms in the apartment: ");
cin>>SIZE;
rooms = (struct Room*) malloc(SIZE * sizeof(struct Room));
cout<<("\nEnter the dimensions for each room:\n");
for (int i = 0; i < SIZE; i++) {
cout<<"Room dimensions: "<< i+1 <<endl;
cout<<"Length: ";
cin>>rooms[i].len;
cout<<"Width: ";
cin>>rooms[i].width;
rooms[i].squareFootage = area(rooms[i].len, rooms[i].width);
}
double apartmentSquareFootage = 0.0;
cout<<"\nDisplaying apartment details: "<<endl;
for (int i = 0; i < SIZE; i++){
cout<<"Room dimensions"<<i + 1<<endl;
show(rooms[i]);
apartmentSquareFootage += rooms[i].squareFootage;
}
cout<<"Apartment square footage: "<<apartmentSquareFootage<<endl;
}
Step by step
Solved in 2 steps with 1 images