InHeritance and Pointers

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

57 Replies - 4041 Views - Last Post: 25 February 2010 - 07:07 PM Rate Topic: -----

#1 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

InHeritance and Pointers

Posted 19 February 2010 - 11:20 PM

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
#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
#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
#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
#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
#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!

Is This A Good Question/Topic? 0
  • +

Replies To: InHeritance and Pointers

#2 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 857
  • View blog
  • Posts: 2,343
  • Joined: 20-August 07

Re: InHeritance and Pointers

Posted 20 February 2010 - 03:02 AM

Please also explain what you are trying to do and where you're stuck. simply pasting what your assignment says you need to do doesn't really give us anything to go on.
What problems are you having exactly? Are you seeing any compiler errors? if so, posting them will help. If not, how does your expected output differ from your actual output?
Was This Post Helpful? 0
  • +
  • -

#3 Ferencn  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 71
  • View blog
  • Posts: 322
  • Joined: 01-February 10

Re: InHeritance and Pointers

Posted 20 February 2010 - 09:39 AM

While waiting for your answers, here some things you may want to consider:
Maybe:
        Vehicle myVehicle[9];

(in the class definition) is not a smart idea.
then, asking the user to input all data will be a big problem when you are testing this thing.
I don't think the purpose of this assignment is to become a datatypist ;-)

Consider as an alternative a few lines in your code along this track:

Vehicle *myVehicles[9];
myVehicles[0] = new Car("Porsche","911","Silver",2005,12000);
...
myVehicles[2] = new Motorcycle("Kawasaki","Ninja","Black",2004,9500,"custom");
...


This post has been edited by Ferencn: 20 February 2010 - 09:41 AM

Was This Post Helpful? 0
  • +
  • -

#4 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: InHeritance and Pointers

Posted 20 February 2010 - 10:34 AM

Sorry for the lack of details guys....and Ferencn thanks for what you said I will use that instead.

But here are the 2 questions I am struggling on.....
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.



One I am not sure how to do question 2 hand have my program know which part of the array to choose. But I do know that I need to have a menu driven program for this to work....but I just do not know how to get it to work. For instance if the user selects question a of the menu......then how will the program know which car to select from the array of cars(I am now doing what Ferencn has told me so it will be set up that way).


And I am extremly unsure of what question 3 wants and or how to do it!
Was This Post Helpful? 0
  • +
  • -

#5 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: InHeritance and Pointers

Posted 20 February 2010 - 12:44 PM

Here is my new cpp file please assist with the above problem using this.

#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";

	 }



This post has been edited by tarheelfan_08: 20 February 2010 - 12:54 PM

Was This Post Helpful? 0
  • +
  • -

#6 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: InHeritance and Pointers

Posted 20 February 2010 - 01:02 PM

Oh and I changed my Vehicle.h file

#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



This post has been edited by tarheelfan_08: 20 February 2010 - 01:11 PM

Was This Post Helpful? 0
  • +
  • -

#7 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: InHeritance and Pointers

Posted 20 February 2010 - 02:19 PM

Oh and their are a few errors...but these may be because the program is not finished

ERRORS:

1>------ Build started: Project: Week 6Assignment, Configuration: Debug Win32 ------
1> VehicleMain.cpp
1\truck.h(40): error C2065: 'year' : undeclared identifier
1\truck.h(40): error C2065: 'color' : undeclared identifier
1\truck.h(41): error C2065: 'make' : undeclared identifier
1\truck.h(41): error C2065: 'model' : undeclared identifier
1\truck.h(41): error C2065: 'mileage' : undeclared identifier
1\truck.h(41): error C2065: 'bedsize' : undeclared identifier
1\motorcycle.h(34): error C2556: 'int Motorcycle::getBikeType(void)' : overloaded function differs only by return type from 'std::string Motorcycle::getBikeType(void)'
1> \motorcycle.h(18) : see declaration of 'Motorcycle::getBikeType'
1\motorcycle.h(34): error C2371: 'Motorcycle::getBikeType' : redefinition; different basic types
1>motorcycle.h(18) : see declaration of 'Motorcycle::getBikeType'
1\motorcycle.h(38): error C2084: function 'void details(void)' already has a body
1>\truck.h(39) : see previous definition of 'details'
1\motorcycle.h(39): error C2065: 'year' : undeclared identifier
1\motorcycle.h(39): error C2065: 'color' : undeclared identifier
1\motorcycle.h(40): error C2065: 'bikeType' : undeclared identifier
1\motorcycle.h(40): error C2065: 'model' : undeclared identifier
1\motorcycle.h(40): error C2065: 'mileage' : undeclared identifier
1\vehiclemain.cpp(45): fatal error C1075: end of file found before the left brace '{' at \vehiclemain.cpp(43)' was matched
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Was This Post Helpful? 0
  • +
  • -

#8 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: InHeritance and Pointers

Posted 20 February 2010 - 02:39 PM

Vehicle::make =  make;
    Vehicle::model = model;
    Vehicle::color = color;
    Vehicle::year = year;


I do not understand why are you using the scope resolution operator to initialize the variables. Since constructor arguments names are the same as your member variables names, use the 'this' pointer to initialize data:
this->make = make;
this->model = model;
// etc, initialize the rest


Here too:
void Vehicle::setMake(string make) {
    Vehicle::make = make;
}


Follow my first example, and modify everything.
Take a look here: http://www.cplusplus...torial/classes/
EDIT: It's 1 AM here, my english may be sucky, but I hope you understood ...

This post has been edited by sarmanu: 20 February 2010 - 02:42 PM

Was This Post Helpful? 0
  • +
  • -

#9 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: InHeritance and Pointers

Posted 20 February 2010 - 10:01 PM

I changed those and I am still getting the same errors and I got no clue how to do the final 2 questions...
Was This Post Helpful? 0
  • +
  • -

#10 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: InHeritance and Pointers

Posted 20 February 2010 - 10:46 PM

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
#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
#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
#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
#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
#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 ==========

Here is what I was supposed to do with the pointers and I am not sure if I did it right!

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.

This post has been edited by tarheelfan_08: 20 February 2010 - 10:44 PM

Was This Post Helpful? 0
  • +
  • -

#11 PlasticineGuy  Icon User is offline

  • mov dword[esp+eax],0
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,436
  • Joined: 03-January 10

Re: InHeritance and Pointers

Posted 20 February 2010 - 11:44 PM

When you have a pointer to an object, you cannot simply use '.'. Here are the two methods you CAN use:
pointer_to_object -> function();
(*pointer_to_object).function();
The . operator takes precedence over the * operator.
Was This Post Helpful? 0
  • +
  • -

#12 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: InHeritance and Pointers

Posted 20 February 2010 - 11:52 PM

All the undeclared identifier errors are caused by a variable not being created before you are trying to use it.

Look at your code again and check if you have all of those variables declared within their respective scopes.
Was This Post Helpful? 0
  • +
  • -

#13 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: InHeritance and Pointers

Posted 20 February 2010 - 11:59 PM

Those variables are defined inside the vehicle class...but they do not appear to work outside of that class!
Was This Post Helpful? 0
  • +
  • -

#14 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: InHeritance and Pointers

Posted 21 February 2010 - 01:00 AM

And plastic...do you have any examples as to where the pointer is used!
Was This Post Helpful? 0
  • +
  • -

#15 tarheelfan_08  Icon User is offline

  • D.I.C Regular

Reputation: -2
  • View blog
  • Posts: 256
  • Joined: 24-January 10

Re: InHeritance and Pointers

Posted 21 February 2010 - 08:10 AM

First step that I need help with is getting the variables to be declared....they are all in my vehicle class so why can I not use them??
Was This Post Helpful? -1
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »