Given this program, is it possible to: 1. Printing out information from customer
ID: 3840642 • Letter: G
Question
Given this program, is it possible to:
1. Printing out information from customer & rooms USING A SEPARATE CLASS?
2. AND WITH THE SAME CLASS FROM #1, READ information FROM FILE TO CUSTOMER CLASS AND ROOM CLASS?
/* C++ Program that reads data about Customer Room Information */
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//Room class definition
class Room
{
//Private instance variables
private:
string roomName;
double length;
double width;
double height;
public:
//Default Constructor
Room()
{
roomName = "";
length = 0;
width = 0;
height = 0;
}
//Parameter Constructor
Room(string rName, double len, double wid, double hei)
{
roomName = rName;
length = len;
width = wid;
height = hei;
}
//Function that returns area
double getArea()
{
//Area = 2h(l+w)
return ( (2 * height) * (length + width) );
}
//Function that returns room information
void printRoomInfo()
{
cout << " Room Name: " << roomName << " Length: " << length << " Width: " << width<< " Height: " << height << " ";
}
//Function that prints room information to file
void printRoomInfoToFile(fstream &fout)
{
fout << " Room Name: " << roomName << " Length: " << length << " Width: " << width<< " Height: " << height << " ";
}
};
//Class definition
class Customer
{
//Private instance variables
private:
string customerName;
//Four rooms
Room *rooms;
//Number of rooms
int noOfRooms;
public:
//Default Constructor
Customer()
{
customerName = "";
}
//Parameter Constructor
Customer(string cName, Room roomsArr[], int noRooms)
{
int i;
customerName = cName;
//Assigning number of rooms
noOfRooms = noRooms;
//Allocating space
rooms = new Room[noOfRooms];
//Iterating over rooms
for(i=0; i<noOfRooms; i++)
{
//Copying objects
rooms[i] = roomsArr[i];
}
}
//Function that prints details of Customer to Console
void print()
{
int i;
double totalArea = 0;
//Printing customer name
cout << " Customer Name: " << customerName << " ";
cout << " Total number of rooms: " << noOfRooms<< " ";
//Iterating over rooms
for(i=0; i<noOfRooms; i++)
{
//Printing room info
rooms[i].printRoomInfo();
//Calculating area
totalArea += rooms[i].getArea();
}
//Printing total area
cout << " Total area of all rooms: " << totalArea<< " ";
}
//Function that prints details of Customer to file
void printToFile(fstream& fout)
{
int i;
double totalArea = 0;
//Printing customer name
fout << " Customer Name: " << customerName << " ";
fout << " Total number of rooms: " << noOfRooms<< " ";
//Iterating over rooms
for(i=0; i<noOfRooms; i++)
{
//Printing room info
rooms[i].printRoomInfoToFile(fout);
//Calculating area
totalArea += rooms[i].getArea();
}
//Printing total area
fout << " Total area of all rooms: " << totalArea<< " ";
}
};
//Main function
int main()
{
string customerName;
string roomName;
double length;
double width;
double height;
int roomCnt;
int i, j;
//Opening file for writing
fstream fout("customers.txt", ios::out);
//Array of customers
Customer customers[5];
//Iterating over each index of array
for(i=0; i<5; i++)
{
//Reading customer data
cout << " Enter Customer Name: " ;
cin >> customerName;
//Reading number of rooms
cout << " Enter number of rooms: ";
cin >> roomCnt;
//Room Array
Room *rooms;
//Allocating space
rooms = new Room[roomCnt];
//Reading room information
for(j=0; j<roomCnt; j++)
{
cout << " Room #" << (j+1) << " <Room Name> <Length> <Width> <Height>: ";
cin >> roomName >> length >> width >> height;
//Creating room object
Room roomObj(roomName, length, width, height);
//Storing room object into array
rooms[j] = roomObj;
}
//Storing in array
Customer obj(customerName, rooms, roomCnt);
//Storing customers
customers[i] = obj;
}
//Iterating over each customer
for(i=0; i<5; i++)
{
//Printing customer data to file
fout << " Customer " << (i+1) << " Info: ";
customers[i].printToFile(fout);
fout << " ";
}
cout << " Results have been written to file customers.txt... ";
//Closing file
fout.close();
return 0;
}
Using as reference for study.
Thanks!
Explanation / Answer
1. Yes it is possible to print information from Customer and room class to console or the file, options are:
a. Use printInformation function using the object of Customer or room class respectively
b. Add accessors for each data field and then write data to file, again using object of the class
2. Yes in a third class you can read from a file and create object of the Customer or Room class using constructors.
File format can be such that the attributes of the class can be identified using some delimiter and then using getline function read the data and put it in a variable, pass all the attributes to the constructor and create an object of the required class
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.