Question

write a C++ program using classes, friend functions and overloaded operators. Computer class: Design a computer...

write a C++ program using 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.

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; }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________


#include <iostream>
using namespace std;

class Computer
{
private :
   int RAM;
   int hardDrive;
   double speed;
   string type;
  
  
public :  
Computer(int ram,int hardDrive,double s,string type)
{
   this->RAM=ram;
   this->hardDrive=hardDrive;
   this->speed=s;
   this->type=type;
   }
Computer()
{
   this->RAM=8;
   this->hardDrive=500;
   this->speed=1.6;
   this->type="desktop";
   }  
  
   void setRam(int ram)
   {
       this->RAM=ram;
   }
   int getRam()
   {
       return RAM;
   }  
   void setHd(int hd)
   {
       this->hardDrive=hd;
   }
   int getHd()
   {
       return hardDrive;
   }
   void setSpeed(double s)
   {
       this->speed=s;
   }
   double getSpeed()
   {
       return speed;
   }
   void setType(string type)
   {
       this->type=type;
   }
   string getType() const
   {
       return type;
   }
  
   double calculatePrice()
   {
       double price=0;
       if(type.compare("Desktop")==0)
       {
           price=600+RAM*5.00+hardDrive*0.15+(speed-1.6)*200;
       }
       else if(type.compare("Laptop"))
       {
           price=400+RAM*4.00+hardDrive*0.10+(speed-1.6)*200;
       }
       return price;
   }
   friend std::ostream& operator<<(std::ostream& dout, const Computer&);

friend std::istream& operator>>(std::istream& din, Computer&);
bool operator==(const Computer& c); // operator==()
bool operator<(Computer& c);
};

bool Computer::operator<(Computer& c)
{
   if(this->calculatePrice()<c.calculatePrice())
   return true;
   else
   return false;
}
   ostream& operator<<(std::ostream& dout, const Computer& c)
   {
           cout <<"Amount of Ram(in GB) :"<<c.RAM <<endl;
       cout <<"Hard Drive Size(in GB):"<<c.hardDrive<<endl;
       cout <<"Speed(in GHz) :"<<c.speed<<endl;
       cout <<"Type :"<<c.type<<endl;
return dout;

   }

istream& operator>>(std::istream& din, Computer& c)
{
   cout <<"Enter Amount of Ram(in GB) :";
       din>>c.RAM ;
       cout <<"Enter Hard Drive Size(in GB):";
       din>>c.hardDrive;
       cout <<"Enter Speed(in GHz) :";
       din>>c.speed;
       cout <<"Enter Type :";
       din>>c.type;
       return din;
   }
  
bool Computer::operator==(const Computer& c)
{
   if(this->RAM==c.RAM && this->hardDrive==c.hardDrive &&this->speed==c.speed && this->type.compare(c.getType())==0)
   return true;
   else
   return false;
}


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;
  
}

____________________________

Output:

CAProgram Files (x86)\Dev-Cpp\MinGW64\bin\ComputerClassOperatorOverload Read WriteCompa.. Amount of Ram<in GB:8 Hard Drive Si

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
write a C++ program using classes, friend functions and overloaded operators. Computer class: Design a computer...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ program to implement inheritance with following requirements, Classes :- Animal.cpp, bird.cpp, canine.cpp, main.cpp (to test...

    C++ program to implement inheritance with following requirements, Classes :- Animal.cpp, bird.cpp, canine.cpp, main.cpp (to test it) :- -You may need getters and setters also. 1. Declare and define an animal base class: animal stores an age (int), a unique long ID (a boolean that is true if it is alive, and a location (a pair of double). animal requires a default constructor and a 3 parameter constructor. Both constructors should set the unique ID automatically. They should also set...

  • C++ Write a MyString class that stores a (null-terminated) char* and a length and implements all...

    C++ Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Submit your completed source (.cpp) file. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator:...

  • Help C++ Write a string class. To avoid conflicts with other similarly named classes, we will...

    Help C++ Write a string class. To avoid conflicts with other similarly named classes, we will call our version MyString. This object is designed to make working with sequences of characters a little more convenient and less error-prone than handling raw c-strings, (although it will be implemented as a c-string behind the scenes). The MyString class will handle constructing strings, reading/printing, and accessing characters. In addition, the MyString object will have the ability to make a full deep-copy of itself...

  • C++ problem with dynamic arrays is that once the array is created using the new operator...

    C++ problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This project asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have: A private member variable called dynamicArray that references a...

  • Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo t...

    Use C++! This program uses the class myStack to determine the highest GPA from a list of students with their GPA.The program also outputs the names of the students who received the highest GPA. Redo this program so that it uses the STL list and STL queue! Thank you! HighestGPAData.txt* 3.4 Randy 3.2 Kathy 2.5 Colt 3.4 Tom 3.8 Ron 3.8 Mickey 3.6 Peter 3.5 Donald 3.8 Cindy 3.7 Dome 3.9 Andy 3.8 Fox 3.9 Minnie 2.7 Gilda 3.9 Vinay...

  • C++ CODE /* This is program project 2 on page 695. * Before you begin the...

    C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...

  • C++ CODE /* This is program project 2 on page 695. * Before you begin the...

    C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...

  • For this computer assignment, you are to write a C++ program to implement a class for...

    For this computer assignment, you are to write a C++ program to implement a class for binary trees. To deal with variety of data types, implement this class as a template. The definition of the class for a binary tree (as a template) is given as follows: template < class T > class binTree { public: binTree ( ); // default constructor unsigned height ( ) const; // returns height of tree virtual void insert ( const T& ); //...

  • *Java* Given the attached Question class and quiz text, write a driver program to input the...

    *Java* Given the attached Question class and quiz text, write a driver program to input the questions from the text file, print each quiz question, input the character for the answer, and, after all questions, report the results. Write a Quiz class for the driver, with a main method. There should be a Scanner field for the input file, a field for the array of Questions (initialized to 100 possible questions), and an int field for the actual number of...

  • Write a C++ program to validate computer user-ids and passwords. A list of valid ids and...

    Write a C++ program to validate computer user-ids and passwords. A list of valid ids and passwords (unsorted) is read from a file and stored in a Binary Search Tree (BST) of UserInfo objects. When user-ids and passwords are entered during execution, this BST is searched to determine whether they are legal. Input (file): UserInfo records for valid users Input (keyboard): Ids and passwords of users logging in Output (screen): Messages indicating whether user-ids and passwords are valid, as well...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT