C++ i need this question answered within 8 hours java version of this question i
ID: 3559443 • Letter: C
Question
C++ i need this question answered within 8 hours
java version of this question is answered here. but i need it in C++ and have extra question regarding operator.
http://www.chegg.com/homework-help/questions-and-answers/-create-class-called-auto-member-owner-name-auto-type-maximum-speed-gasoline-tank-capacity-q4523084
1. (Includes the following subproblems 'a' and 'b')
a. Create a class called Auto, that has member for owner name, auto type, maximum speed, gasoline tank capacity. Provide methods set() and display() for setting and displaying these values. Add two constructors to Auto class, one that takes two arguments, name of owner and type of Auto, and the other that does not take any. Create a Auto object to demo the methods.
b. Create a class called Truck whose base class is Auto. Add members to Truck class that would make this specific to Truck. Override the set() method and the display() method of the Auto class. Provide a Truck constructor to initialize values. Create a Truck object to demo its features using its methods. Write a overloaded operator function to compare two truck objects. (say == or != operator)
My code so far:
#include
#include
using namespace std;
//Provided Person class.
class Auto{
private:
string owner;
string autotype;
int maxspeed;
int tankcap;
public:
Auto(){
maxspeed = 0;
tankcap = 0;
}
Auto(string set_owner = "no info", string set_autotype = "no info")
{
owner = set_owner;
autotype = set_autotype;
}
void set(int n_maxspeed, int n_tankcap)
{
maxspeed = n_maxspeed;
tankcap = n_tankcap;
}
void display()
{
cout << "The owner of vehecle is " << owner << " and the auto type is " << autotype << "." << " This auto's max speed is " << maxspeed << " and its tank capacity is " << tankcap << ".";
}
};
class Truck: public Auto
{
public:
int m_loadcap;
Truck(int loadcap = 0, string owner ="no info", string autotype = "no info", int maxspeed = 0, int tankcap = 0) : Auto("no info", "no info"), m_loadcap(loadcap){}
};
int main(){
Auto v1("Jisoo", "SUV");
v1.set(100, 10);
v1.display();
Truck t1(100,"James", "Sedan");
t1.display();
}
Output: The owner of vehecle is Jisoo and the auto type is SUV. This auto's max speed is 100 and its tank capacity is 10.The owner of vehecle is no info and the auto type is no info. This auto's max speed is 0 and its tank capacity is 0.Program ended with exit code: 0
Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
// a. Create a class called Auto, that has member for owner name, auto type, maximum speed, gasoline tank capacity.
// Provide methods set() and display() for setting and displaying these values.
// Add two constructors to Auto class, one that takes two arguments, name of owner and type of Auto,
// and the other that does not take any. Create a Auto object to demo the methods.
class Auto{
private:
string owner;
string autotype;
int maxspeed;
int tankcap;
public:
Auto(){
maxspeed = 0;
tankcap = 0;
}
string getOwner() { return owner; }
string getAutotype() { return autotype; }
int getMaxspeed() { return maxspeed; }
int getTankcap() { return tankcap; }
Auto(string set_owner = "no info", string set_autotype = "no info")
{
owner = set_owner;
autotype = set_autotype;
}
void set(int n_maxspeed, int n_tankcap)
{
maxspeed = n_maxspeed;
tankcap = n_tankcap;
}
void display()
{
cout << "The owner of vehecle is " << owner << " and the auto type is " << autotype << "." << " This auto's max speed is " << maxspeed << "and its tank capacity is " << tankcap << ".";
}
};
// b. Create a class called Truck whose base class is Auto. Add members to Truck class that would make this specific to Truck.
// Override the set() method and the display() method of the Auto class. Provide a Truck constructor to initialize values.
// Create a Truck object to demo its features using its methods. Write a overloaded operator function to compare two truck objects. (say == or != operator)
class Truck: public Auto
{
private:
int m_loadcap;
public:
Truck(int loadcap = 0, string owner ="no info", string autotype = "no info"):
Auto(owner, autotype), m_loadcap(loadcap){}
bool operator==(Truck C1)
{
return ((m_loadcap == C1.m_loadcap) &&
(this->getOwner().compare(C1.getOwner())==0) &&
(this->getAutotype().compare(C1.getAutotype())==0) &&
(this->getMaxspeed() == C1.getMaxspeed()) &&
(this->getTankcap() == C1.getTankcap()));
}
void set(int loadcap, int n_maxspeed, int n_tankcap)
{
m_loadcap = loadcap;
Auto::set(m_loadcap, n_tankcap);
}
void display()
{
Auto::display();
cout <<"Load Capacity of truck is " << m_loadcap << endl;
}
};
int main(){
Auto v1("Jisoo", "SUV");
v1.set(100, 10);
v1.display();
Truck t1(100,"James", "Sedan");
t1.display();
Truck t2(200,"King","Nano");
t2.display();
t2.set(200,0,0);
Truck t3(300,"King","Nano");
t3.set(200,0,0);
t3.display();
cout <<"t2 and t3 are " << ((t2==t3)?" Same ":" Not same ") << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.