Hey everyone! I was recently assigned this program in class and have finished it but i have some compiler errors i cant fix. I was just hoping that some of you could give it a shot and share what kind of code you came up with. Well here it is any help would be very appreciative.

Create a base class called Vehicle that has the manufacturer's name (type string), number of cylinders in the engine (type int.), and owner (type person given below instructions). Then create a class called Truck that is derived from Vehicle and has additional properties, the load capacity in tons (type double since it may contain a fractional part), and towing capacity in pounds (type int.) Be sure your classes have a reasonable complement of constructors and accessor methods, an overloaded assignment operator, and a copy constructor. Write a driver program that tests all your methods.

The definition of the class Person (as stated above) is below. The implementation of the class is part of this programming assignment.

class Person
{

public:
      Person();
      Person(string theName);
      Person(const Person& theObject);
      string getName() const;
      Person& operator=(const Person& rtSide);
      friend istream& operator >>(istream& inStream, Person& personObject);
      friend ostream& operator >>(ostream& outStream, const Person& personObject);

private:
      string name;

};

Ok so that finishes the instructions for the assignment and here's what i have come up with myself.

#include <iostream>
#include <string>

using namespace std;

//Provided Person class.
class Person{
      public:
             Person();
             Person(string theName);
             Person(const Person& theObject);
             Person(const char* theName); //allows const char[number] inputs
             string getName() const;
             Person& Person::operator =(const Person& rtSide);
             friend istream& operator >>(istream& inStream,
                                                  Person& personObject);
             friend ostream& operator <<(ostream& outStream,
                                                  const Person& personObject);
      private:
              string name;
      };

//Required Vehicle class.      
class Vehicle{
      public:
            Vehicle();
            Vehicle(string manuName, int numCyl, const Person& owner);
            Vehicle(const Vehicle& theVehicle);
            Vehicle& operator =(const Vehicle& theVehicle);
            string getManuName();
            int getNumCyl();
            Person getOwner();
      private:
              string manuName;
              int numCyl;
              Person owner;
      };

//Required derived class Truck from base class Vehicle.      
class Truck : public Vehicle{
      public:
             Truck();
             Truck(double capTons, int capPounds, string manuName, int numCyl, const Person& owner);
             Truck(const Truck& theTruck);
             Truck& Truck::operator =(const Truck& theTruck);
             double getCapTons();
             int getCapPounds();
      private:
              double capTons;
              int capPounds;
      };
      
//Person Implementation
//Constructors
Person::Person() : name(""){}
Person::Person(string theName) : name(theName){}
Person::Person(const char* theName) : name(theName){}
Person::Person(const Person& theName): name(theName.name){} //Copy Constructor
//Overloaded assignment operator
Person& Person::operator =(const Person& thePerson){
        this->name = thePerson.name;
        return *this;
        }
istream& operator >>(istream& inStream, Person& personObject){
         inStream >> personObject.name;
         return inStream;
         }
ostream& operator <<(ostream& outStream, const Person& personObject){
        outStream << personObject.name;
        return outStream;
        }
//Accessor methods
string Person::getName() const{
       return name;
       }
       
//Vehicle Implementation
//Constructors
Vehicle::Vehicle() : manuName("No Manufacturer"), numCyl(0), owner("No Owner"){}
Vehicle::Vehicle(string theManuName, int theNumCyl, const Person& theOwner)
                        : manuName(theManuName),
                          numCyl(theNumCyl),
                          owner(theOwner){}
Vehicle::Vehicle(const Vehicle& theVehicle)//Copy Constructor.
                       : manuName(theVehicle.manuName),
                         numCyl(theVehicle.numCyl),
                         owner(theVehicle.owner){}
//Overloaded Assignment Operator
Vehicle& Vehicle::operator =(const Vehicle& theVehicle){
         this->manuName = theVehicle.manuName;
         this->numCyl = theVehicle.numCyl;
         this->owner = theVehicle.owner;
         return *this;
         }
//Accessor methods
string Vehicle::getManuName(){
       return manuName;
       }
int Vehicle::getNumCyl(){
    return numCyl;
    }
Person Vehicle::getOwner(){
       return owner;
       }
       
//Truck Implementation
//Constructors
Truck::Truck() : capTons(0.0), capPounds(0){}
Truck::Truck(double theCapTons, int theCapPounds, string theManuName, int theNumCyl, const Person& theOwner)
                    : Vehicle(theManuName, theNumCyl, theOwner),
                      capTons(theCapTons),
                      capPounds(theCapPounds){}
Truck::Truck(const Truck& theTruck) //Copy Constructor.
                   : Vehicle(theTruck),
                     capTons(theTruck.capTons),
                     capPounds(theTruck.capPounds){}
//Overloaded Assigment Operator
Truck& Truck::operator =(const Truck& theTruck){
       Vehicle::operator =(theTruck);
       capTons = theTruck.capTons;
       capPounds = theTruck.capPounds;
       return *this;
       }
//Accessor methods
double Truck::getCapTons(){
       return capTons;
       }
int Truck::getCapPounds(){
    return capPounds;
}

//Driver Program
int main(){
    Vehicle v1("Honda", 4, "Brian Valle");
    
    cout << "Vehicle v1 Data: " << endl;
    cout << "Manufacturer's Name: " + v1.getManuName() << endl;
    cout << "Number of Cylinders: " + v1.getNumCyl() << endl;
    cout << "Owner: " + v1.getOwner() << endl;
    
    Truck t1(72.0, 18000, "Ford", 8, "Brian Valle");
    
    cout << "Truck t1 Data: " << endl;
    cout << "Manufacturer's Name: " + t1.getManuName();
    cout << "Number of Cylinders: " + t1.getNumCyl();
    cout << "Owner: " + t1.getOwner() << endl;
    cout << "Load Capacity (Tons): " + t1.getCapTons();
    cout << "Towing Capacity (Pounds): " + t1.getCapPounds();
    }

Again any help in fixing these compiler errors would be awesome thanks again!

What are the compiler errors?

Well there are about five errors, and I'm not even sure what they mean so i was hoping maybe some one could just copy my code and compile it themselves to see if they knew what they meant.

The problem lies in your main method. You cannot concatenate strings using + in c++.
You simply use the << operator again.

cout << "Manufacturer's Name: "<<t1.getManuName();

The problem lies in your main method. You cannot concatenate strings using + in c++.
You simply use the << operator again.

cout << "Manufacturer's Name: "<<t1.getManuName();

thank you very much i will give this a go

thank you very much i will give this a go

i made the mentioned fixes to the code, and it took care of some of the errors, but there are still two that remain. "Extra qualification 'Person::' on member 'operator='
"Extra qualification 'Truck::' on member 'operator='

not quite sure what this means...but here is the program again so far with the suggested fixes.

#include <iostream>
#include <string>

using namespace std;

//Person class.
class Person{
public:
	Person();
	Person(string theName);
	Person(const Person& theObject);
	Person(const char* theName); 
	string getName() const;
	Person& Person::operator =(const Person& rtSide);
	friend istream& operator >>(istream& inStream,
								Person& personObject);
	friend ostream& operator <<(ostream& outStream,
								const Person& personObject);
private:
	string name;
};

//Vehicle class.      
class Vehicle{
public:
	Vehicle();
	Vehicle(string manuName, int numCyl, const Person& owner);
	Vehicle(const Vehicle& theVehicle);
	Vehicle& operator =(const Vehicle& theVehicle);
	string getManuName();
	int getNumCyl();
	Person getOwner();
private:
	string manuName;
	int numCyl;
	Person owner;
};

//Derived class Truck from base class Vehicle.      
class Truck : public Vehicle{
public:
	Truck();
	Truck(double capTons, int capPounds, string manuName, int numCyl, const Person& owner);
	Truck(const Truck& theTruck);
	Truck& Truck::operator =(const Truck& theTruck);
	double getCapTons();
	int getCapPounds();
private:
	double capTons;
	int capPounds;
};

//Person Implementation
//Constructors
Person::Person() : name(""){}
Person::Person(string theName) : name(theName){}
Person::Person(const char* theName) : name(theName){}
//Copy Constructor
Person::Person(const Person& theName): name(theName.name){} 
//Overloaded assignment operator
Person& Person::operator =(const Person& thePerson){
	this->name = thePerson.name;
	return *this;
}
istream& operator >>(istream& inStream, Person& personObject){
	inStream >> personObject.name;
	return inStream;
}
ostream& operator <<(ostream& outStream, const Person& personObject){
	outStream << personObject.name;
	return outStream;
}
//Accessor methods
string Person::getName() const{
	return name;
}

//Vehicle Implementation
//Constructors
Vehicle::Vehicle() : manuName("No valid Manufacturer"), numCyl(0), owner("No valid Owner"){}
Vehicle::Vehicle(string theManuName, int theNumCyl, const Person& theOwner)
: manuName(theManuName),
numCyl(theNumCyl),
owner(theOwner){}
//Copy Constructor.
Vehicle::Vehicle(const Vehicle& theVehicle)
: manuName(theVehicle.manuName),
numCyl(theVehicle.numCyl),
owner(theVehicle.owner){}
//Overload Assignment Operator
Vehicle& Vehicle::operator =(const Vehicle& theVehicle){
	this->manuName = theVehicle.manuName;
	this->numCyl = theVehicle.numCyl;
	this->owner = theVehicle.owner;
	return *this;
}
//Accessor methods
string Vehicle::getManuName(){
	return manuName;
}
int Vehicle::getNumCyl(){
    return numCyl;
}
Person Vehicle::getOwner(){
	return owner;
}

//Truck Implementation
//Constructors
Truck::Truck() : capTons(0.0), capPounds(0){}
Truck::Truck(double theCapTons, int theCapPounds, string theManuName, int theNumCyl, const Person& theOwner)
: Vehicle(theManuName, theNumCyl, theOwner),
capTons(theCapTons),
capPounds(theCapPounds){}
//Copy Constructor.
Truck::Truck(const Truck& theTruck) 
: Vehicle(theTruck),
capTons(theTruck.capTons),
capPounds(theTruck.capPounds){}
//Overloaded Assigment Operator
Truck& Truck::operator =(const Truck& theTruck){
	Vehicle::operator =(theTruck);
	capTons = theTruck.capTons;
	capPounds = theTruck.capPounds;
	return *this;
}
//Accessor methods
double Truck::getCapTons(){
	return capTons;
}
int Truck::getCapPounds(){
    return capPounds;
}

//Driver Program to test methods
int main(){
    Vehicle v1("Mazda", 4, "Joshua Rhodes");
    
    cout << "Vehicle v1 Data: " << endl;
    cout << "Manufacturer's Name: "  <<v1.getManuName() << endl;
    cout << "Number of Cylinders: " <<v1.getNumCyl() << endl;
    cout << "Owner: " <<v1.getOwner() << endl;
    
    Truck t1(72.0, 18000, "Chevrolet", 8, "Joshua Rhodes");
    
    cout << "Truck t1 Data: " << endl;
    cout << "Manufacturer's Name: " <<t1.getManuName();
    cout << "Number of Cylinders: " <<t1.getNumCyl();
    cout << "Owner: "  <<t1.getOwner() << endl;
    cout << "Load Capacity (Tons): " <<t1.getCapTons();
    cout << "Towing Capacity (Pounds): " <<t1.getCapPounds();
}

You can concatenate the C++ string class with the + operator though.

I was actually searching to see if that was possible when I came over from java, is std::string not the c++ string class? I couldn't get the + operator to work, can you give an example or a link to an example please?


@cppjosh, What compiler are you using?

Edited 5 Years Ago by Akill10: n/a

http://www.cplusplus.com/reference/string/operator+/

#include <iostream>
#include <string>
using namespace std;

int main()
{
   string first = "www.";
   string second = "daniweb";
   string third = ".com";
   cout << first + second + third << endl;
   first += second + third;
   first += " :" + second + ":";
   cout << first << endl;
   cin.get();
}

That is so odd, I was sure I tried everything when I first started, good to know ;D

This article has been dead for over six months. Start a new discussion instead.