C++, <iostream>, using namespace std, NO POINTERS!!! 1. Using separate source fi
ID: 3710419 • Letter: C
Question
C++, <iostream>, using namespace std, NO POINTERS!!!
1. Using separate source files and header files as appropriate, create a class to represent desks. 1. A desk object has attributes of color (string) and number of drawers (integer) 2. Member functions are 1. default constructor with default parameter values of "Steel Blue" and 4, and that displays "IntConstructor called n 2. individual get methods for each attribute that are written in-line 3. a get method that uses pass-by-reference for both parameters and has return-type void 4. one set method that sets both attributes (NOT to be written in-line) 5. PrintMe method that displays "I'm a desk with drawers.", replacing and with the appropriate attribute values 6. Destructor that displays "InltDestructor called " 2. Write a complete program that uses the desk class. 1. Declare a vector of 5 objects of desk class 2. Declare a single desk object and initialize it at declaration to have color "Silver" 3. Call the PrintMe method of the single object 4. Call a void function that has a vector of desks as its parameter. That function should: 1. Ask the user for 5 colors and 5 numbers and assign them to each desk in the vector 2. For the 2nd desk, if the color's name is 4 or more characters long, change the 3rd character of the name to'# 3. Open file "output.txt" and write to it using the same text that PrintMe uses for all desks in your vector 4. Close the output fileExplanation / Answer
desk.h:
#include <string>
using namespace std;
class Desk {
string color;
int drawers;
public:
Desk(string c = "Steel Blue", int d = 4);
string getColor(){
return color;
}
int getDrawers() {
return drawers;
}
void get(string &c,int &d) {
c = color;
d = drawers;
}
void set(const string& color,const int drawers);
void PrintMe();
~Desk();
};
desk.cpp:
#include <iostream>
#include "desk.h"
#include <vector>
#include <fstream>
using namespace std;
Desk::Desk(string c, int d){
color = c;
drawers = d;
}
void Desk::set(const string& c,const int d) {
color = c;
drawers = d;
}
void Desk::PrintMe() {
cout<<"I am a "<<color<<" desk with "<<drawers<<" drawers. ";
}
Desk::~Desk() {
cout << " Destructor called ";
}
//test begins here
void test(vector<Desk> v) {
string c;int d;
ofstream out("output.txt");
for (int i = 0; i < v.size(); i++) {
cout << "Color:" << ' ';
cin>>c;
cout << "Drawers" << ' ';
cin >> d;
v[i].set(c,d);
}
if(v[1].getColor().length()>3){
string c=v[1].getColor();
c[2]='#';
v[1].set(c,v[1].getDrawers());
}
for (int i = 0; i < v.size(); i++) {
out<<"I am a "<<v[i].getColor()<<" desk with "<<v[i].getDrawers()<<" drawers. ";
}
out.close();
}
int main() {
vector<Desk> v(5,Desk());
Desk d("red");
d.PrintMe();
test(v);
return 0;
}
Console I/O:
Destructor called
I am a red desk with 4 drawers.
Color:
red
Drawers
6
Color:
blue
Drawers
1
Color:
red
Drawers
3
Color:
green
Drawers
4
Color:
yellow
Drawers
3
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
Destructor called
output.txt:
I am a red desk with 6 drawers.
I am a bl#e desk with 1 drawers.
I am a red desk with 3 drawers.
I am a green desk with 4 drawers.
I am a yellow desk with 3 drawers.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.