How do you write a C++ program for the information below? You will be using clas
ID: 3730041 • Letter: H
Question
How do you write a C++ program for the information below?
You will be using classes, objects, strings, and appropriate data members and member functions, constructors, destructor, etc. to handle the tasks outlined below.
The class will be named Computer and will contain the following data members (in the private access region):
Manufacturer
Model
Price
Rebate
- Computer Manufacturer
- Computer Model
- Computer Price
- Computer Rebate, if any
You will determine the type of data to be used for each data member. (You may add additional data members).
After the declaration of the class, declare two objects of the class called computer1 and computer2. Both objects will be initialized using the default constructor. You will prompt the user to provide the necessary data for the first object and the second object. A member function display() will display the information about each computer in the format shown below. You will need to define and implement all of the needed accessor and mutator member functions. A user-defined function named displayComputers() will retrieve the information about each computer and displays them in the format shown below (This is a second display of the information). At the beginning of each display, display a label indicting the output is displayed via the member function display() and the output is displayed via the user- defined function displayComputers().
Explanation / Answer
Answer:
Computer.cpp:
#include <iostream>
#include<string>
using namespace std;
void displayComouters(char* manufacturer, char* model,float price, float rebate); // user defined display function
class Computer{
private:
char manufacturer[20];
char model[20];
float price;
float rebate;
public:
//member display function
void display(){
cout<<"Manufacturer : "<<getManufacturer()<<endl;
cout<<"Model Number : "<<getModel()<<endl;
cout<<"Pice : $"<<getPrice()<<endl;
cout<<"Rebate : $"<<getRebate()<<endl;
}
//accessors and mutator functions
string getManufacturer(){
return manufacturer;
}
void setManufacturer(char* manfctr){
for(int i = 0; i < 20; i++){
manufacturer[i] = manfctr[i];
}
}
char* getModel(){
return model;
}
void setModel(char* mdl){
for(int i = 0; i < 20; i++){
model[i] = mdl[i];
}
}
float getPrice(){
return price;
}
void setPrice(float prc){
price = prc;
}
float getRebate(){
return rebate;
}
void setRebate(float rebte){
rebate = rebte;
}
};
int main()
{
char manufacturer[20];
char model[20];
float price;
float rebate;
//read input from user
cout<<"Please enter Manufacturer : ";
cin.getline(manufacturer,20);
cout<<"Please enter Model : ";
cin.getline(model,20);
cout<<"Please enter Price : ";
cin>>price;
cout<<"Please enter rebate : ";
cin>>rebate;
//objects for Computer class
Computer computer1;
Computer computer2;
//setting the member values of class
computer1.setManufacturer(manufacturer);
computer1.setModel(model);
computer1.setPrice(price);
computer1.setRebate(rebate);
cout<<"Display from member function display()"<<endl;
computer1.display();
cout<<"Display from user defined function displayComouters()"<<endl;
displayComouters(manufacturer,model,price,rebate);
//read input from user
cout<<endl<<"Enter the details of 2nd computer "<<endl;
cout<<"Please enter Manufacturer : ";
cin>>manufacturer;
cout<<"Please enter Model : ";
cin>>model;
cout<<"Please enter Price : ";
cin>>price;
cout<<"Please enter rebate : ";
cin>>rebate;
computer2.setManufacturer(manufacturer);
computer2.setModel(model);
computer2.setPrice(price);
computer2.setRebate(rebate);
cout<<"Display from member function display()"<<endl;
computer1.display();
cout<<"Display from user defined function displayComouters()"<<endl;
displayComouters(manufacturer,model,price,rebate);
return 0;
}
void displayComouters(char* manufacturer, char* model,float price, float rebate){
cout<<"Manufacturer : "<<manufacturer<<endl;
cout<<"Model Number : "<<model<<endl;
cout<<"Pice : $"<<price<<endl;
cout<<"Rebate : $"<<rebate<<endl;
}
Output:
Please enter Manufacturer : DELL
Please enter Model : E#36
Please enter Price : 450
Please enter rebate : 10
Display from member function display()
Manufacturer : DELL
Model Number : E#36
Pice : $450
Rebate : $10
Display from user defined function displayComouters()
Manufacturer : DELL
Model Number : E#36
Pice : $450
Rebate : $10
Enter the details of 2nd computer
Please enter Manufacturer : LENOVO
Please enter Model : D30
Please enter Price : 350
Please enter rebate : 20
Display from member function display()
Manufacturer : DELL
Model Number : E#36
Pice : $450
Rebate : $10
Display from user defined function displayComouters()
Manufacturer : LENOVO
Model Number : D30
Pice : $350
Rebate : $20
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.