Question

In: Computer Science

I. Create the class Item with the following members:   1. id, a protected variable of type...

I. Create the class Item with the following members:
  1. id, a protected variable of type int
  2. name, a protected variable of type string
  3. price, a protected variable of type double
  4. a public non-default constructor which accepts three parameters to initialize the variables above

II. Create the class Chair. The class publicly inherits from Item and has the following members
  1. numLegs, a private variable of type int
  2. a non-default constructor to initialize numLegs, id, name, and price
  3. accessors and mutators for numLegs
  4. an int conversion constructor to convert from an int to a type of class Chair.
  5. a string conversion operator which returns a string consisting of the values of id, name, price, and int with spaces between the values
  6. overloaded output stream operator
  7. overloaded input stream operator

III. Write test code to create an instance of type Chair and test all members

Solutions

Expert Solution

//C++ program

#include<iostream>
#include <sstream>   
#include <string>

using namespace std;

class Item{
   protected:
int id;
   string name;
   double price;
   public:
       Item(int i ,string n , double p){
           id = i;
           name = n;
           price=p;
       }
};

class chair:public Item{
   private:
       int numLegs;
   public:
       chair(int i , string n , double p,int leg):Item(i,n,p){
           numLegs = leg;
       }
       void setNumLegs(int leg){
           numLegs = leg;
       }
       int getNumLegs(){
           return numLegs;
       }
       string tostring(){
           ostringstream str1,str2,str3;
  
       str1 << id;
       str2 << price;
       str3 << numLegs;
           return str1.str()+" "+name+" "+str2.str()+" "+str3.str()+"\n";
       }
       friend ostream & operator << (ostream & out , const chair &c){
           out<<c.id<<" "<<c.name<<" "<<c.price<<" "<<c.numLegs<<"\n";
           return out;
       }
       friend istream & operator >> (istream & in , chair c){
           cout<<"Enter id : ";
           in>>c.id;
           cout<<"Enter name : ";
           in>>c.name;
           cout<<"Enter price : ";
           in>>c.price;
           cout<<"Enter numLegs : ";
           in>>c.numLegs;
           return in;
       }
      
};

int main(){
   chair c (10,"Hello world",100.67,4);
   cout<<c.tostring();
  
   cout<<c;
}

//sample output


Related Solutions

I. Create the class Item with the following members: 1. id, a protected variable of type...
I. Create the class Item with the following members: 1. id, a protected variable of type int 2. name, a protected variable of type string 3. price, a protected variable of type double 4. a public non-default constructor which accepts three parameters to initialize the variables above 5. a copy constructor 6. overload the = operator such that both operands are of type Item 7. overload the = operator such that the right operand is of type double. Assign the...
create a class in C++ having the following 4 private members: one double variable representing the...
create a class in C++ having the following 4 private members: one double variable representing the balance of a bank account on string variable representing the bank account number a function to deposit money from the bank account a function to withdraw money from the bank account the following 2 public members: -two wrapper functions. - one parameterless constructor - one constructor initializing the account balance and account number - sample: #include <iostream> using namespace std; class account { public:...
object oriented programming java Create a Student class that have two data members id (assign to...
object oriented programming java Create a Student class that have two data members id (assign to your ID) and name (assign to your name). Create the object of the Student class by new keyword and printing the objects value. You may name your object as Student1. The output should be like this: 20170500 Asma Zubaida
Design a Ship class that has the following members: • A member variable for the name...
Design a Ship class that has the following members: • A member variable for the name of the ship (a string) • A member variable for the year that the ship was built (a string) • A constructor and appropriate accessors and mutators • A virtual print function that displays the ship’s name and the year it was built. Design a CruiseShip class that is derived from the Ship class. The CruiseShip class should have the following members: • A...
create a public class and a main class to deletebyStudentID number with get ID and getStudentByName...
create a public class and a main class to deletebyStudentID number with get ID and getStudentByName in public class. when the user is prompted to search for the student and delete them by their. ID has seven digits and 6 students each. Ex. Delete student by their ID         Enter students name and their ID         Vanessa,         1111-111         Vanessa is now deleted
Question 1 - Create a class named Student that has fields for an ID number, number...
Question 1 - Create a class named Student that has fields for an ID number, number of credit hours earned, and number of points earned. (For example, many schools compute grade point averages based on a scale of 4, so a three-credit-hour class in which a student earns an A is worth 12 points.) Include methods to assign values to all fields. A Student also has a field for grade point average. Include a method to compute the grade point...
****in java please*** Create a class CheckingAccount with a static variable of type double called interestRate,...
****in java please*** Create a class CheckingAccount with a static variable of type double called interestRate, two instance variables of type String called firstName and LastName, an instance variable of type int called ID, and an instance variable of type double called balance. The class should have an appropriate constructor to set all instance variables and get and set methods for both the static and the instance variables. The set methods should verify that no invalid data is set. Also...
Create a "Date" class that contains: three private data members: month day year (I leave it...
Create a "Date" class that contains: three private data members: month day year (I leave it to you to decide the type) "setters" and "getters" for each of the data (6 functions in total) One advantage of a "setter" is that it can provide error checking. Add assert statements to the setter to enforce reasonable conditions. For example, day might be restricted to between 1 and 31 inclusive. one default constructor (no arguments) one constructor with three arguments: month, day,...
JAVA PROBLEM Part 1: Create a Car class in accordance with the following specifications. I will...
JAVA PROBLEM Part 1: Create a Car class in accordance with the following specifications. I will provide the CarClassTester class as a test driver to test your Car class for its basic structure. Do not change the CarClassTester class source code. After you get your Car class working correctly using this test driver, proceed to part 2 below. Car Class Specifications: The Car class must be in a separate package from any driver/tester program. The Car class will contain, at...
Create a class called Car (Car.java). It should have the following private data members: • String...
Create a class called Car (Car.java). It should have the following private data members: • String make • String model • int year Provide the following methods: • default constructor (set make and model to an empty string, and set year to 0) • non-default constructor Car(String make, String model, int year) • getters and setters for the three data members • method print() prints the Car object’s information, formatted as follows: Make: Toyota Model: 4Runner Year: 2010 public class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT