Gabriel M.

asked • 09/18/21

Change the Computer class to a struct. 1. Keep the default and non-default constructors 2. Your struct does not need the accessors and mutators since all variables are public by default.

3.Also Perforom the following:
Write a main():
- Create an instance of type Computer using the default constructor.
- Set the values for year, model, and purpose using any values you'd like.
- Print the values of year, model, and purpose to the standard output.
- Prompt the user to enter a value for year, model, and purpose
- Create a new instance of type Computer using the non-default constructor.
- Print the values of year, model, and purpose.

#include <iostream>
#include <string>

using namespace std;

class Computer {

private:
int year;
string model;
string purpose;
public:
Computer(int y, string m, string m2) {
year = y;
model = m;
purpose = m2;
}
~Computer() {
cout << "Removed from memory" << endl;
}

void setYear(int y) {
year = y;
}
int getYear() {
return year;
}

const string& getModel() {
return model;
}

void setModel(const string m) {
model = m;
}

const string getPurpose() {
return purpose;
}

void setPurpose(string p) {
this->purpose = p;
}
};

int main() {

// test code to make sure the class works as expected
Computer c1;
Computer c2(1901, "non-default1");
Computer c3(1902, "non-default2", "purpose2");

c1.setYear(1900);
c1.setModel("model1");
c1.setPurpose("purpose1");

cout << c1.getYear() << endl;
cout << c1.getModel() << endl;
cout << c1.getPurpose() << endl;

cout << c2.getYear() << endl;
cout << c2.getModel() << endl;
cout << c2.getPurpose() << endl;

cout << c3.getYear() << endl;
cout << c3.getModel() << endl;
cout << c3.getPurpose() << endl;

return 0;
}


1 Expert Answer

By:

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.