hello, i'm trying to write two classes (vehicle and driver)

first i want to create the class vehicle and it's attrbutes, but there is a problem when i compile the code.

please see all my code

#include<iostream>
#include<vector>
using namespace std;
			// 
// Vehicle: Vehicle number, type, capacity, driver assigned
class Vehicle{
int id;
public:
	Vehicle(int id) : id(id) {
	cout <<"Vehicle created successfully" << endl;
	id++;
	}
	int getID() const { return id; }
	
};

class VehicleType : public Vehicle{
string name;
int capacity;
string driver_assigned;
public:
	VehicleType(int id,string name,int capacity, string driver_assigned) 
	: Vehicle(id), name(name), capacity(capacity), driver_assigned(driver_assigned){
	
	}
	void getVehicleType(){
	cout <<"id: " << getID()
		 <<"name: " << name
		 <<"capacity: " << capacity << endl;
	}
};

int main(){
vector<Vehicle*> vehicles;
int id,capacity;
string name,driver_assigned;
cout <<"Please enter the name of vehicle type: ";
	cin >> name;
	cout <<"Please enter the capacity of vehicle type: ";
	cin >> capacity;
Vehicle *abc = new VehicleType(id,name,capacity,driver_assigned);

vehicles.push_back(abc);

for (int i = 0; i < vehicles.size(); i++)
  {
    cout <<"id "<< vehicles[i]->getID() <<endl;
         cout <<"id "<< vehicles[i]->getVehicleType() <<endl;
  }
 
	return 0;
}

the error is in line 48, i'm trying to output all varibles by using getVehicleType function

what is my mstake?

also i want the vehicle id increase when create a new vehicle and decrease when i delete vehivle, i try to do so but i get error

thank you

Well, first, your error about printing the object's content is easily solved. Your function "getVehicleType" prints the values already, so you just need to call it, don't insert it into a << operator statement, just call it:

//cout <<"id "<< vehicles[i]->getVehicleType() <<endl; //ERROR!
  vehicles[i]->getVehicleType(); //OK!

Now, about the ID, that's a bigger problem. You have many mistakes related to it:

First, you don't initialize your "id" variable in the main function. So, the id for the first Vehicle will be garbage anyways. You need to know that unless you give initial values to variables, they are created with any value (usually whatever garbage bits where set to before).

Second, in your Vehicle constructor, you have a data member with the same name as a parameter to the constructor. This is weird and ambiguous, and your compiler should at least warn you not to do this, because statements like id++; are not very clear (are you incrementing that data member or the parameter?). Make your life easier and pick a different name for your parameter.

Third, I don't know if you expected the value of the "id" variable in your main function to actually get incremented as Vehicles are created, but that is not going to be the case. When you send a variable to a function, it gets copied (pass-by-value) and any modification to the value of the parameter which is done within the function will not affect the variable that was passed to the function. If you want this to happen, you need to pass-by-reference, which is done by marking the parameter type with an ampersand.

With these things in mind, you can get this working version:

#include <iostream>
#include <vector>
#include <string> //if you need class string, include its header! Very important!

using namespace std;

// Vehicle: Vehicle number, type, capacity, driver assigned
class Vehicle{
  int id;
  public:
    //notice the addition of the ampersand (&) to mark as pass-by-reference
    // notice the change of name too.
    Vehicle(int& aId) : id(aId) {
      cout <<"Vehicle created successfully" << endl;
      ++aId;
    }
    int getID() const { return id; }
	
};

class VehicleType : public Vehicle{
  string name;
  int capacity;
  string driver_assigned;
  public:
    VehicleType(int& aId,  //pass-by-reference here as well.
                const string& aName, //prefer passing strings by const-references.
                int aCapacity, 
                const string& aDriverAssigned) : 
                Vehicle(aId), 
                name(aName), 
                capacity(aCapacity), 
                driver_assigned(aDriverAssigned) { };

    void getVehicleType() {
      cout << "id: " << getID()
           << "name: " << name
           << "capacity: " << capacity << endl;
    };
};

int main() {
  vector<Vehicle*> vehicles;
  int id = 0; //initialize the value!!
  int capacity;
  string name, driver_assigned;
  cout <<"Please enter the name of vehicle type: ";
  cin >> name;
  cout <<"Please enter the capacity of vehicle type: ";
  cin >> capacity;

  vehicles.push_back(new VehicleType(id,name,capacity,driver_assigned));
  cout << "Now the id is " << id << endl;

  for (int i = 0; i < vehicles.size(); i++)
  {
    cout <<"id "<< vehicles[i]->getID() <<endl;
    vehicles[i]->getVehicleType();
  }
 
  return 0;
}

really thanks for your reply but the output still shows me that error messege

"52:18: error: 'class Vehicle' has no member named 'getVehicleType'"

is it this problem because class VehicleType is a subclass of the Vehicle?

also how to print aId by suing the getid function in vehicle class?

Ho, yeah, you need to have the getVehicleType function also declared in the base-class such that it is accessible. You need to make it virtual such that it can be overriden in the derived class. Change your Vehicle class to:

// Vehicle: Vehicle number, type, capacity, driver assigned
class Vehicle{
  int id;
  public:
    //notice the addition of the ampersand (&) to mark as pass-by-reference
    // notice the change of name too.
    Vehicle(int& aId) : id(aId) {
      cout <<"Vehicle created successfully" << endl;
      ++aId;
    }
    int getID() const { return id; }
    virtual void getVehicleType() { }; //notice the 'virtual' keyword here.	
};

works fine, thanks!

how to print aId varible by using the getid function in vehicle class?

also, why we don't put refrence to the parameter capacity

VehicleType(int& aId, const string& aName, int aCapacity, const string& aDriverAssigned)

the output for vehcile id is incorect

let say i have 5 vehicles, then all the vehicles have the same id which is 5

another question, how to use function with vector?

i need to create some function then i want to use them in the main function but when i compile, all the varibles in my functions is not declearet

how to avoid this problem?

still waiting for the explanation

>>how to print aId varible by using the getid function in vehicle class?

Vehicle v;
  std::cout << v.getId() << std::endl;

>>why we don't put refrence to the parameter capacity

Because it is not necessary.

>>the output for vehcile id is incorect
>>let say i have 5 vehicles, then all the vehicles have the same id which is 5

I'm no psychic, a vague explanation of your code is not going to tell me why it doesn't work.

>>how to use function with vector?

You use the function on the elements of the vector, not on the vector. You access the elements with any of the many function in vector that allows for that. For example, the iterators (with begin() and end()) or the index and operator [] or the function at(). Once you have the element of the vector, you just call the function on it. You do this already in your code, in the loop at the end.

>>i need to create some function then i want to use them in the main function but when i compile, all the varibles in my functions is not declearet
>>how to avoid this problem?

Again, there are no psychics reading your posts.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.