Gabriel M.

asked • 09/17/21

Create three separate files for this code: 1. Computer.h holds the class' declaration 2. Computer.cpp holds the class' functions' definitions 3. Main.cpp holds the test code (i.e. the main function)

#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.