Description
Objectives:
Classes, friend functions and overloaded operators.
Computer class:
Design a computer class (Computer) that describes a computer object. A computer has
the following properties:
• Amount of Ram in GB (examples: 8, 16), defaults to 8.
• Hard drive size in GB (examples: 500, 1000), defaults to 500.
• Speed in GHz (examples: 1.6, 2.4), defaults to 1.6.
• Type (laptop, desktop), defaults to “desktop”
Provide the following functions for the class:
• A default constructor (constructor with no parameters) that initializes all the
variables to their default values.
• A constructor with four parameters to initialize all four member variables.
• Getters(accessors) and setters(mutators) for all four member variables.
• Calculate the price using the following formulas:
o Laptop: 600.00 + amount of ram in GB * 5.00 + Hard drive size * .15 +
(speed in GHz – 1.6) * 200
o Desktop: 400.00 + amount of ram in GB * 4.00 + Hard drive size in GB * .10 +
(speed in GHz – 1.6) * 200
• Overload the << operator to output all properties of a computer. • Overload the >> operator that reads all four properties of a computer.
• Overload the == operator test if two computers are the same (all properties must
match)
• Overload the < operator that compares the prices of two computers.
You don’t have to check for invalid values.
Test the Computer class with the following main program.
CS2400 Lab 7 June 25, 2019
Computer Class (100 points)
int main()
{
Computer comp; //use default constructor
Computer comp1(16, 1000, 1.6, “Laptop”);
cout << comp << endl; //output defaults
cout << endl;
cout << comp1 << endl;
cout << endl;
comp1.setRam(32);
comp1.setHd(2000);
int compRam = comp1.getRam();
cout << “The computer ram was changed to ”
<< comp1.getRam() << endl;
cout << “The computer hd was changed to ”
<< comp1.getHd() << endl;
cout << “Updated info” << endl << endl;
cout << comp1 << endl;
cout << endl;
comp1.setType(“Desktop”);
cout << “Computer type was changed to Desktop” << endl;
cout << comp1 << endl;
Computer comp2;
cout << “Enter specs of a computer (Ram, HD, Speed, Type)” << endl; cin >> comp2;
cout << comp2 << endl;
if (comp1 < comp2)
{
cout << “Computer 2 is more expensive” << endl;
}
if (comp1 == comp2)
{
cout << “comp1 and comp2 have the same specifications” << endl;
}
return 0;
}