السلام عليكم
لدي برنامج اقوم بكتابته حاليا وهو عباره عن كلاسين الاول المركبه والثاني السائق .. قمت بعمل الكلاسين ومازلت في البدايه .. ساضع لكم الكود وبعدها ساضع اسئلتي
#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 "<< abc.getID() <<endl;
cout <<"id "<< abc.getVehicleType() <<endl;
}
return 0;
}سؤالي هو لماذا عندما اترجم الكود يظهر مشكله في الداله getVehicleType عند طباعتها في الداله الرئيسيه؟ لان المفروض ان الكلاس VehicleType جزء من الكلاس Vehicle والمفروض حسب فهمي ان يعمل جيدا
وكيف يمكن تفادي هذه المشكله؟
السؤال الاخر اريد جعل ID للمركبه يبدا من 0 .. ثم يزيد عند اضافه مركبه جديده وينقص عند الحذف ، حاولت فيها لكن تظهر اخطاء فهل من الممكن المساعده
انتظركم