1. Welcome to Tacoma World!

    You are currently viewing as a guest! To get full-access, you need to register for a FREE account.

    As a registered member, you’ll be able to:
    • Participate in all Tacoma discussion topics
    • Communicate privately with other Tacoma owners from around the world
    • Post your own photos in our Members Gallery
    • Access all special features of the site

C++ InHeritance Help

Discussion in 'Technology' started by tarheelfan_08, Feb 19, 2010.

  1. Feb 19, 2010 at 10:21 PM
    #1
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,944
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Hey guys, I really appreciate everything that you do for me and it really helps me a lot...I have learned so much. But I have a new problem, for my assignment this week it deals with InHeritance and Pointers and I have 90% of my program set up but I need help with the last 10% of it.

    Here is my assignment and I have bold the section that I need help with!

    Create a Vehicle class to store information

    Attributes:

    make (you can also think of this as the Manufacturer)

    model

    color

    year

    mileage

    (Note: All attributes should not be public.)

    Behaviors:

    1. Create mutator (Set) and accessor (Get) functions for all attributes of this class.

    2. Create a default constructor that initializes all of the attributes to default values (blanks in the case of strings or 0 in the case of numbers.)

    3. Make sure to have validation to ensure that the mileage is never set to a number less than 0.

    4. Create a constructor that takes the make, model, year,color, and mileage values and sets them for a newly created vehicle.

    5. Create a virtual function called details that does not do anything. (This will force the cars, trucks, and motorcycles that inherit from this class to overload this function.)




    Create a Car class that inherits from the vehicle class

    Attributes:

    There are no additional attributes

    Behaviors:

    1. Override the details function to print the details for this car.

    2. (Example on an output line of a car’s details:

    “The current car is a 2008 Red Ford Mustang with 5000 miles.”)



    Create a Truck class that inherits from the vehicle class

    Attributes:

    bedsize

    Behaviors:

    1. Create mutator (Set) and accessor (Get) functions for all attributes of this class.

    2. Override the details function to print the details for this truck.

    (Example on an output line of a truck’s details:

    “The current truck is a 2006 Black Ford F150 with 10000 miles and a 10-foot bedsize.”)



    Create a Motorcycle class that inherits from the vehicle class

    Attributes:

    Biketype

    Note: (Some types of bikes would be chopper, cruiser, dirt bike, touring, and custom. )

    Behaviors:

    1. Create mutator (Set) and accessor (Get) functions for all attributes of this class.

    2. Override the details function to print the details for this motorcycle.

    (Example on an output line of a motorcycle’s details:

    “The current motorcycle is a 2007 Silver Harley Cherokee chopper with 8000 miles”)







    Create a program with a main() to be used by the inventory manager to keep track of all of the vehicles on the lot.

    1) Create the following vehicles of the correct type

    Make Model Color Year Type Bed size Bike type

    1. Porsche 911 Silver 2005 Car

    2. Ford Mustang Red 2007 Car

    3. Kawasaki Ninja Black 2004 Motorcycle custom

    4. Ford F150 White 2007 Truck 10-foot

    5. Voltzwagon Jetta Black 2006 Car

    6. Harley Cherokee Silver 2000 Motorcycle chopper

    7. Toyota Tacoma Blue 2002 Truck 12-foot

    8. Honda CBR1500CC Red 2008 Motorcycle cruiser

    9. <<Your dream vehicle here>>

    2) Prompt the user to change:

    a. The model of one car.

    b. The bed size of one truck.

    c. The bike type for one motorcycle.

    d. The year of any one vehicle (you choose the type).

    3) Create and array of pointers to these objects and use a loop to call the details() function for each one in turn and print out the details for all vehicles on the lot.



    And here is my current code!

    Vehicle.h
    Code:
    #ifndef VEHICLE_H
    #define VEHICLE_H
    
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <functional>
    #include <algorithm>
    #include <string>
    #include <cstdlib>
    #include <sstream>
    using namespace std;
    
    //Vehicle Class
    class Vehicle {
    protected:
        Vehicle myVehicle[9];
        string make;  //make
        string model; // model
        string color;  // color
        int    year;  // year
        int mileage;  // miles on car
        string type;  //Type of vehicle
    
    public:
        //Constructor that will set information for a new car
        void New_vehicle (string a, string b, string c, int d, int e) 
        {make = a; model = b; color = c; year = d; mileage = e;}
        
        Vehicle(); //Default constructor
        Vehicle(string, string, string, int, int, string);
        //mutator and accessor functions
        void setMake(string);
        void setModel(string);
        void setColor(string);
        void setYear(int);
        void setMileage(int);
        void setType(string);
    
        string getMake();
        string getModel();
        string getColor();
        int getYear();
        int getMileage();
        string getType();
    
        //Check mileage to see if valid
        void valid_mileage(int);
    
        //virtual function
        virtual void details() {
        }
    
    };
    //Sets to default values
    Vehicle::Vehicle() {
        make = " ";
        model = " ";
        color = " ";
        year = 0;
        mileage = 0;
        type = " ";
    }
    
    Vehicle::Vehicle(string make, string model, string color, int year, int mileage, string type) {
        Vehicle::make =  make;
        Vehicle::model = model;
        Vehicle::color = color;
        Vehicle::year = year;
        valid_mileage(mileage);
        Vehicle::type = type;
    }
    
    void Vehicle::setMake(string make) {
        Vehicle::make = make;
    }
    
    void Vehicle::setModel(string model) {
        Vehicle::model = model;
    }
    
    void Vehicle::setColor(string color) {
        Vehicle::color = color;
    }
    
    void Vehicle::setYear(int year) {
        Vehicle::year = year;
    }
    
    void Vehicle::setMileage(int mileage) {
        valid_mileage(mileage);
    }
    
    void Vehicle::setType(string type) {
        Vehicle::type = type;
    }
    
    
    string Vehicle::getMake() {
        return make;
    }
    string Vehicle::getModel() {
        return model;
    }
    string Vehicle::getColor() {
        return color;
    }
    int Vehicle::getYear() {
        return year;
    }
    int Vehicle::getMileage() {
        return mileage;
    }
    
    string Vehicle::getType() {
        return type;
    }
    
    
    void Vehicle::valid_mileage(int mileage) {
        if (mileage>=0)
            Vehicle::mileage=mileage;
        else {
            Vehicle::mileage=0;
            cout << "WARNING! You have entered invalid mileage!\n";
        }
    
            Vehicle& getVehicle(int n) {
            return myVehicle[n];
        }
    };
    
    #endif
    
    Car.h
    Code:
    #ifndef CAR_H
    #define CAR_H
    
    #include "Vehicle.h"
    
    //Car class that inherits from the vehicle class
    class Car:public Vehicle
        {
        private:
        //There are no new attributes
        public:
        void details() {
        cout << "The current car is a " << year << ' ' << color << ' '
        << make << ' ' << model << " with " << mileage << " miles.\n";
        }
    };
    
    #endif
    
    Truck.h
    Code:
    #ifndef TRUCK_H
    #define TRUCK_H
    
    #include "Vehicle.h"
    
    //Truck class that inherits from the vehicle class
    class Truck:public Vehicle 
        {
        private:
            int bedsize; //BedSize
    
        public:
            
            Truck(); //constructor
            Truck(int);
    
            //mutator and accessor functions
            void setBedsize(int);
            int getBedsize();
    
        };
    
        Truck::Truck() {
            bedsize = 0;
        }
    
        Truck::Truck(int bedsize) {
            Truck::bedsize = bedsize;
        }
    
        void Truck::setBedsize(int bedsize) {
            Truck::bedsize = bedsize;
        }
    
        int Truck::getBedsize() {
            return bedsize;
        }
    
        void details() {
        cout << "The current truck is a " << year << ' ' << color << ' '
        << make << ' ' << model << " with " << mileage << " miles and a " << bedsize << "-foot bedsize.\n";
        }
    };
    #endif
    
    
    Motorcycle.h
    Code:
    #ifndef MOTORCYCLE_H
    #define MOTORCYCLE_H
    
    #include "Vehicle.h"
    
    //Truck class that inherits from the vehicle class
    class Motorcycle:public Vehicle
        {
        private:
            string bikeType; //BedSize
    
        public:
            Motorcycle(); //constructor
            Motorcycle(string);
    
            //mutator and accessor functions
            void setBikeType(string);
            string getBikeType();
    
        };
    
        Motorcycle::Motorcycle() {
            bikeType = " ";
        }
    
        Motorcycle::Motorcycle(string bikeType) {
            Motorcycle::bikeType = bikeType;
        }
    
        void Motorcycle::setBikeType(string bikeType) {
            Motorcycle::bikeType = bikeType;
        }
    
        int Motorcycle::getBikeType() {
            return bikeType;
        }
    
        void details() {
        cout << "The current motorcycle is a " << year << ' ' << color << ' '
        << bikeType << ' ' << model << " with " << mileage << " miles.\n";
        }
    };
    #endif
    
    Vehicle.cpp file
    Code:
    #include "Car.h"
    #include "Truck.h"
    #include "Motorcycle.h"
    using namespace std;
    
    int main() {
        
        int tempInt;
        string tempString;
        Vehicle myVehicle;
    
        for (int i = 0; i<9; i++) {
            cout << "Enter the Make of the Vehicle: ";
            cin >> tempString;
            myVehicle.getVehicle(i).setMake(tempString);
    
            cout << "Enter the Model of the Vehicle: ";
            cin >> tempString;
            myVehicle.getVehicle(i).setModel(tempString);
    
            cout << "Enter the Color of the Vehicle: ";
            cin >> tempString;
            myVehicle.getVehicle(i).setColor(tempString);
    
            cout << "Enter the Year of the Vehicle: ";
            cin >> tempInt;
            myVehicle.getVehicle(i).setYear(tempInt);
    
            cout << "Enter the Mileage of Vehicle: ";
            cin >> tempInt;
            myVehicle.getVehicle(i).setMileage(tempInt);
    
            cout << "Enter the Type of Vehicle: ";
            cin >> tempString;
            myVehicle.getVehicle(i).setType(tempString);
    
            cout << "Enter the Bed Size of Truck(if entered as truck): ";
            cin >> tempInt;
            myVehicle.getVehicle(i).setBedSize(tempInt);
    
            cout << "Enter the Bike Type(if entered as bike): ";
            cin >> tempString;
            myVehicle.getVehicle(i).setBikeType(tempString);
    
    
            cout << "__________________________________________________________\n"<< endl;
        }
    
    
    Please assist me guys and if you see where I have done anything wrong then please help me out!
     
  2. Feb 20, 2010 at 11:45 AM
    #2
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,944
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Here is my new cpp file please assist with the above problem using this.

    Code:
    #include "Car.h"
    #include "Truck.h"
    #include "Motorcycle.h"
    using namespace std;
    
    int main() {
        
        int tempInt;
        string tempString;
        int menu=0;
    
        // My Vehicle set up(Make, model, color, year, type,bedsize, bike, mileage)
        Vehicle *myVehicles[9];
        myVehicles[0] = new Vehicle("Porsche","911","Silver",2005,"Car", 0, " ", 12000);
        myVehicles[1] = new Vehicle("Ford","Mustang","Red",2007,"Car", 0, " ", 14213);
        myVehicles[2] = new Vehicle("Kawasaki","Ninja","Black",2004,"Motorcycle", 0, " Custom ", 11989);
        myVehicles[3] = new Vehicle("Ford","F150","White",2007,"Truck", 10, " ", 23458);
        myVehicles[4] = new Vehicle("Voltzwagon","Jetta","Black",2006,"Car", 0, " ", 45000);
        myVehicles[5] = new Vehicle("Harley","Cherokee","Silver",2000,"Motorcycle", 0, "Chopper ", 53246);
        myVehicles[6] = new Vehicle("Toyota","Tacoma","Blue",2002,"Truck", 12, " ", 73000);
        myVehicles[7] = new Vehicle("Honda","CBR1500CC","Red",2008,"Motorcycle", 0, "Cruiser", 7050);
        myVehicles[8] = new Vehicle("Toyota","Tacoma","White",2009,"Truck", 6, " ", 10);
    
         while (menu!=6)//keep going untill 6 is selected
        {
            cout << "Vehicle Menu\n\n";            //Menu
            cout << "1. Change the Model of one Tar:\n";
            cout << "2. Change the Bed Size of one Truck:\n";
            cout << "3. Change the Bike Type for one Motorcycle:\n";
            cout << "4. Change the Year of a Truck:\n";
            cout << "5. Display the Vehicle Inventory List:\n";
            cout << "6. Exit the Program:\n\n\n";
    
            cout << "Please choose one of the above options: ";
            cin >> menu;
            cin.ignore();
            cout << "_______________________________________________________\n";
    
         }
    
    
     
  3. Feb 20, 2010 at 12:02 PM
    #3
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,944
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Oh and I changed my Vehicle.h file

    Code:
    #ifndef VEHICLE_H
    #define VEHICLE_H
    
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <functional>
    #include <algorithm>
    #include <string>
    #include <cstdlib>
    #include <sstream>
    using namespace std;
    
    //Vehicle Class
    class Vehicle {
    protected:
        string make;  //make
        string model; // model
        string color;  // color
        int    year;  // year
        int mileage;  // miles on car
        string type;  //Type of vehicle
    
    public:
        //Constructor that will set information for a new car
        void New_vehicle (string a, string b, string c, int d, int e) 
        {make = a; model = b; color = c; year = d; mileage = e;}
        
        Vehicle(); //Default constructor
        Vehicle(string, string, string, int, string, int, string, int);
        //mutator and accessor functions
        void setMake(string);
        void setModel(string);
        void setColor(string);
        void setYear(int);
        void setMileage(int);
        void setType(string);
    
        string getMake();
        string getModel();
        string getColor();
        int getYear();
        int getMileage();
        string getType();
    
        //Check mileage to see if valid
        void valid_mileage(int);
    
        //virtual function
        virtual void details() {
        }
    
    };
    //Sets to default values
    Vehicle::Vehicle() {
        make = " ";
        model = " ";
        color = " ";
        year = 0;
        mileage = 0;
        type = " ";
    }
        // My Vehicle set up(Make, model, color, year, type,bedsize, bike, mileage)
    Vehicle::Vehicle(string make, string model, string color, int year, string type,int bedSize, string bikeType, int mileage) {
        Vehicle::make =  make;
        Vehicle::model = model;
        Vehicle::color = color;
        Vehicle::year = year;
        valid_mileage(mileage);
        Vehicle::type = type;
    }
    
    void Vehicle::setMake(string make) {
        Vehicle::make = make;
    }
    
    void Vehicle::setModel(string model) {
        Vehicle::model = model;
    }
    
    void Vehicle::setColor(string color) {
        Vehicle::color = color;
    }
    
    void Vehicle::setYear(int year) {
        Vehicle::year = year;
    }
    
    void Vehicle::setMileage(int mileage) {
        valid_mileage(mileage);
    }
    
    void Vehicle::setType(string type) {
        Vehicle::type = type;
    }
    
    
    string Vehicle::getMake() {
        return make;
    }
    string Vehicle::getModel() {
        return model;
    }
    string Vehicle::getColor() {
        return color;
    }
    int Vehicle::getYear() {
        return year;
    }
    int Vehicle::getMileage() {
        return mileage;
    }
    
    string Vehicle::getType() {
        return type;
    }
    
    
    void Vehicle::valid_mileage(int mileage) {
        if (mileage>=0)
            Vehicle::mileage=mileage;
        else {
            Vehicle::mileage=0;
            cout << "WARNING! You have entered invalid mileage!\n";
        }
    
            Vehicle& getVehicle(int n) {
            return myVehicle[n];
        }
    };
    
    #endif
    
    
     
  4. Feb 20, 2010 at 12:06 PM
    #4
    rme

    rme Well-Known Member

    Joined:
    Aug 3, 2008
    Member:
    #8309
    Messages:
    1,916
    Gender:
    Male
    Savannah
    Vehicle:
    Green TACOMA
    XM, Panasonic Stereo, Headlights,Electric Ant,Toyota Deluxe Wheels, Rhino Lining, Cargo Rails
    Huh? I think this belongs in the WTF file....:lalala::annoyed::sorry:
     
  5. Feb 20, 2010 at 12:16 PM
    #5
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,944
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Ha I know it does!
     
  6. Feb 20, 2010 at 9:01 PM
    #6
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,944
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    anybody got any help??
     
  7. Feb 20, 2010 at 9:44 PM
    #7
    tarheelfan_08

    tarheelfan_08 [OP] Carolina Alliance

    Joined:
    Jan 4, 2008
    Member:
    #4098
    Messages:
    2,944
    Gender:
    Male
    First Name:
    Justin
    North Carolina
    Vehicle:
    2010 White 4x4 Double Cab Tacoma Short Bed
    Chrome Grill, Chrome accessories(door handles and mirror covers), LEER Super White Cover, Mirrors with Turn Signals, Window Vent Visors, Step Rails, WeatherTech Digital Floor Mats, Mini Maglight Holder, Chrome Bull Bar
    Ok guys I managed to get my code done and I think it will do what I need it to! But right now I have some errors I need help with so please assist me if you can guys! And if you see that I am completely wrong on something and its not going to do what I need then definitely tell me! The pointers may be one things that's wrong, since I am new to them!

    cpp
    Code:
    #include "Car.h"
    #include "Truck.h"
    #include "Motorcycle.h"
    using namespace std;
    
    int main() {
        
        int tempInt;
        string temp;
        int menu=0;
        int current_vehicle=0; 
        int    tmpInt=0;
    
        // My Vehicle set up(Make, model, color, year, type,bedsize, bike, mileage)
        Vehicle *myVehicles[9];
        myVehicles[0] = new Vehicle("Porsche","911","Silver",2005,"Car", 0, " ", 12000);
        myVehicles[1] = new Vehicle("Ford","Mustang","Red",2007,"Car", 0, " ", 14213);
        myVehicles[2] = new Vehicle("Kawasaki","Ninja","Black",2004,"Motorcycle", 0, " Custom ", 11989);
        myVehicles[3] = new Vehicle("Ford","F150","White",2007,"Truck", 10, " ", 23458);
        myVehicles[4] = new Vehicle("Voltzwagon","Jetta","Black",2006,"Car", 0, " ", 45000);
        myVehicles[5] = new Vehicle("Harley","Cherokee","Silver",2000,"Motorcycle", 0, "Chopper ", 53246);
        myVehicles[6] = new Vehicle("Toyota","Tacoma","Blue",2002,"Truck", 12, " ", 73000);
        myVehicles[7] = new Vehicle("Honda","CBR1500CC","Red",2008,"Motorcycle", 0, "Cruiser", 7050);
        myVehicles[8] = new Vehicle("Toyota","Tacoma","White",2009,"Truck", 6, " ", 10);
    
         while (menu!=7)//keep going until 7 is selected
        {
            cout << "Vehicle Menu\n\n";            //Menu
            cout << "1. Select the Vehicle to edit:\n";
            cout << "2. Change the Model of one Car:\n";
            cout << "3. Change the Bed Size of one Truck:\n";
            cout << "4. Change the Bike Type for one Motorcycle:\n";
            cout << "5. Change the Year of a Truck:\n";
            cout << "6. Display the Vehicle Inventory List:\n";
            cout << "7. Exit the Program:\n\n\n";
    
            cout << "Please choose one of the above options: ";
            cin >> menu;
            cin.ignore();
            cout << "_______________________________________________________\n";
    
         
    
             //switch begin
            switch ( menu )
             {
            case 1:
                cout << " Pick a vehicle to edit\n";
                cin >> current_vehicle;
                --current_vehicle;
                cin.ignore();
                cout << "_______________________________________________________\n";
                break;
    
            case 2:
                cout << " The Model of car is: "
                << *myVehicles[current_vehicle].getModel() << endl;
                cout << " Please enter the change to the model of the car: ";
                getline(cin, temp);
                myVehicles[current_vehicle].setModel(temp);
                cout << "_______________________________________________________\n";
                break;
    
            case 3:
                cout << " The Bed Size of the current truck is: "
                << myVehicles[current_vehicle].getBedSize() << endl;
                cout << " Please enter the change the change to the Bed Size: ";
                getline(cin, temp);
                myVehicles[current_vehicle].setBedSize(temp);
                cout << "_______________________________________________________\n";
                break;
    
            case 4:
                cout << " The Bike Type of the current motorcycle is: "
                << myVehicles[current_vehicle].getBikeType() << endl;
                cout << " Please enter the change to the Bike Type: ";
                getline(cin, temp);
                myVehicles[current_vehicle].setBikeType(temp);
                cout << "_______________________________________________________\n";
                break;
    
            case 5:
                cout << " The current year of the truck is: "
                << myVehicles[current_vehicle].getYear() << endl;
                cout << " Please enter the change to the year of the truck: ";
                getline(cin, temp);
                myVehicles[current_vehicle].setYear(temp);
                cout << "_______________________________________________________\n";
                break;
    
            case 6:
                //Pointers to print details for all cars
                cout << " The current Cars on the lot are: ";
                Car *details();
                //Pointer to print details for all trucks
                cout << " The current Trucks on the lot are: ";
                Truck *details();
                //Pointers to print details for all motorcycles
                cout << " The current Motorcycles on the lot are: ";
                Motorcycle *details();
                cout << "_______________________________________________________\n";
                break;
    
                default:
                cout << " Please restart the program, you have entered invalid information!\n\n\n";
                break;
            }        //switch end
        }        //while
        return 0;
    }
    
    
    Vehicle.h
    Code:
    #ifndef VEHICLE_H
    #define VEHICLE_H
    
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <functional>
    #include <algorithm>
    #include <string>
    #include <cstdlib>
    #include <sstream>
    using namespace std;
    
    //Vehicle Class
    class Vehicle {
    protected:
        string make;  //make
        string model; // model
        string color;  // color
        int    year;  // year
        int mileage;  // miles on car
        string type;  //Type of vehicle
    
    public:
        //Constructor that will set information for a new car
        void New_vehicle (string a, string b, string c, int d, int e) 
        {make = a; model = b; color = c; year = d; mileage = e;}
        
        Vehicle(); //Default constructor
        Vehicle(string, string, string, int, string, int, string, int);
        //mutator and accessor functions
        void setMake(string);
        void setModel(string);
        void setColor(string);
        void setYear(int);
        void setMileage(int);
        void setType(string);
    
        string getMake();
        string getModel();
        string getColor();
        int getYear();
        int getMileage();
        string getType();
    
        //Check mileage to see if valid
        void valid_mileage(int);
    
        //virtual function
        virtual void details() {
        }
    
    };
    //Sets to default values
    Vehicle::Vehicle() {
        make = " ";
        model = " ";
        color = " ";
        year = 0;
        mileage = 0;
        type = " ";
    }
        // My Vehicle set up(Make, model, color, year, type,bedsize, bike, mileage)
    Vehicle::Vehicle(string make, string model, string color, int year, string type,int bedSize, string bikeType, int mileage) {
        this->make =  make;
        this->model = model;
        this->color = color;
        this->year = year;
        valid_mileage(mileage);
        this->type = type;
    }
    
    void Vehicle::setMake(string make) {
        this->make=make;
    }
    
    void Vehicle::setModel(string model) {
        this->model= model;
    }
    
    void Vehicle::setColor(string color) {
        this->color= color;
    }
    
    void Vehicle::setYear(int year) {
        this->year= year;
    
    }
    
    void Vehicle::setMileage(int mileage) {
        valid_mileage(mileage);
    }
    
    void Vehicle::setType(string type) {
        this->type= type;
    }
    
    
    string Vehicle::getMake() {
        return make;
    }
    string Vehicle::getModel() {
        return model;
    }
    string Vehicle::getColor() {
        return color;
    }
    int Vehicle::getYear() {
        return year;
    }
    int Vehicle::getMileage() {
        return mileage;
    }
    
    string Vehicle::getType() {
        return type;
    }
    
    
    void Vehicle::valid_mileage(int mileage) {
        if (mileage>=0)
            Vehicle::mileage=mileage;
        else {
            Vehicle::mileage=0;
            cout << "WARNING! You have entered invalid mileage!\n";
        }
    
    };
    
    #endif
    
    car.h
    Code:
    #ifndef CAR_H
    #define CAR_H
    
    #include "Vehicle.h"
    
    //Car class that inherits from the vehicle class
    class Car:public Vehicle
        {
        private:
        //There are no new attributes
        public:
        void details() {
        cout << "The current car is a " << year << ' ' << color << ' '
        << make << ' ' << model << " with " << mileage << " miles.\n";
        }
    };
    
    #endif
    
    motorcycle.h
    Code:
    #ifndef MOTORCYCLE_H
    #define MOTORCYCLE_H
    
    #include "Vehicle.h"
    
    //Truck class that inherits from the vehicle class
    class Motorcycle:public Vehicle
        {
        private:
            string bikeType; //BedSize
    
        public:
            Motorcycle(); //constructor
            Motorcycle(string);
    
            //mutator and accessor functions
            void setBikeType(string);
            string getBikeType();
    
        };
    
        Motorcycle::Motorcycle() {
            bikeType = " ";
        }
    
        Motorcycle::Motorcycle(string bikeType) {
            this->bikeType = bikeType;
        }
    
        void Motorcycle::setBikeType(string bikeType) {
            this->bikeType = bikeType;
        }
    
        int Motorcycle::getBikeType() {
            return bikeType;
        }
    
        void details() {
        cout << "The current motorcycle is a " << year << ' ' << color << ' '
        << bikeType << ' ' << model << " with " << mileage << " miles.\n";
        };
    #endif
    
    truck.h
    Code:
    #ifndef TRUCK_H
    #define TRUCK_H
    
    #include "Vehicle.h"
    
    //Truck class that inherits from the vehicle class
    class Truck:public Vehicle 
        {
        private:
            int bedsize; //BedSize
    
        public:
            
            Truck(); //constructor
            Truck(int);
    
            //mutator and accessor functions
            void setBedsize(int);
            int getBedsize();
    
        };
    
        Truck::Truck() {
            bedsize = 0;
        }
    
        Truck::Truck(int bedsize) {
            this->bedsize = bedsize;
        }
    
        void Truck::setBedsize(int bedsize) {
            this->bedsize = bedsize;
        }
    
        int Truck::getBedsize() {
            return bedsize;
        }
    
        void details() {
        cout << "The current truck is a " << year << ' ' << color << ' '
        << make << ' ' << model << " with " << mileage << " miles and a " << bedsize << "-foot bedsize.\n";
        };
    #endif
    
    
    Errors:

    1>------ Build started: Project: Week 6Assignment, Configuration: Debug Win32 ------
    1> VehicleMain.cpp
    1>c\week 6assignment\truck.h(40): error C2065: 'year' : undeclared identifier
    1>c\week 6assignment\truck.h(40): error C2065: 'color' : undeclared identifier
    1>c\week 6assignment\truck.h(41): error C2065: 'make' : undeclared identifier
    1>c\week 6assignment\truck.h(41): error C2065: 'model' : undeclared identifier
    1>c\week 6assignment\truck.h(41): error C2065: 'mileage' : undeclared identifier
    1>c\week 6assignment\truck.h(41): error C2065: 'bedsize' : undeclared identifier
    1>c\week 6assignment\motorcycle.h(34): error C2556: 'int Motorcycle::getBikeType(void)' : overloaded function differs only by return type from 'std::string Motorcycle::getBikeType(void)'
    1> c\week 6assignment\motorcycle.h(18) : see declaration of 'Motorcycle::getBikeType'
    1>c\week 6assignment\motorcycle.h(34): error C2371: 'Motorcycle::getBikeType' : redefinition; different basic types
    1> c\week 6assignment\motorcycle.h(18) : see declaration of 'Motorcycle::getBikeType'
    1>c\week 6assignment\motorcycle.h(38): error C2084: function 'void details(void)' already has a body
    1> c\week 6assignment\truck.h(39) : see previous definition of 'details'
    1>c\week 6assignment\motorcycle.h(39): error C2065: 'year' : undeclared identifier
    1>c\week 6assignment\motorcycle.h(39): error C2065: 'color' : undeclared identifier
    1>c\week 6assignment\motorcycle.h(40): error C2065: 'bikeType' : undeclared identifier
    1>c\week 6assignment\motorcycle.h(40): error C2065: 'model' : undeclared identifier
    1>c\week 6assignment\motorcycle.h(40): error C2065: 'mileage' : undeclared identifier
    1>c\week 6assignment\vehiclemain.cpp(57): error C2228: left of '.getModel' must have class/struct/union
    1> type is 'Vehicle *'
    1> did you intend to use '->' instead?
    1>c\week 6assignment\vehiclemain.cpp(60): error C2228: left of '.setModel' must have class/struct/union
    1> type is 'Vehicle *'
    1> did you intend to use '->' instead?
    1>c\week 6assignment\vehiclemain.cpp(66): error C2228: left of '.getBedSize' must have class/struct/union
    1> type is 'Vehicle *'
    1> did you intend to use '->' instead?
    1>c\week 6assignment\vehiclemain.cpp(69): error C2228: left of '.setBedSize' must have class/struct/union
    1> type is 'Vehicle *'
    1> did you intend to use '->' instead?
    1>c\week 6assignment\vehiclemain.cpp(75): error C2228: left of '.getBikeType' must have class/struct/union
    1> type is 'Vehicle *'
    1> did you intend to use '->' instead?
    1>c\week 6assignment\vehiclemain.cpp(78): error C2228: left of '.setBikeType' must have class/struct/union
    1> type is 'Vehicle *'
    1> did you intend to use '->' instead?
    1>c\week 6assignment\vehiclemain.cpp(84): error C2228: left of '.getYear' must have class/struct/union
    1> type is 'Vehicle *'
    1> did you intend to use '->' instead?
    1>c\week 6assignment\vehiclemain.cpp(87): error C2228: left of '.setYear' must have class/struct/union
    1> type is 'Vehicle *'
    1> did you intend to use '->' instead?
    1>c\week 6assignment\vehiclemain.cpp(94): error C2556: 'Car *details(void)' : overloaded function differs only by return type from 'void details(void)'
    1> c\week 6assignment\truck.h(39) : see declaration of 'details'
    1>c\week 6assignment\vehiclemain.cpp(94): error C2040: 'details' : 'Car *(void)' differs in levels of indirection from 'void (void)'
    1>c\week 6assignment\vehiclemain.cpp(97): error C2556: 'Truck *details(void)' : overloaded function differs only by return type from 'Car *details(void)'
    1> c\week 6assignment\vehiclemain.cpp(94) : see declaration of 'details'
    1>c\week 6assignment\vehiclemain.cpp(97): error C2371: 'details' : redefinition; different basic types
    1> c\week 6assignment\vehiclemain.cpp(94) : see declaration of 'details'
    1>c\week 6assignment\vehiclemain.cpp(100): error C2556: 'Motorcycle *details(void)' : overloaded function differs only by return type from 'Car *details(void)'
    1> c\week 6assignment\vehiclemain.cpp(94) : see declaration of 'details'
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
     

Products Discussed in

To Top