الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

سؤالين في الكلاس

بدأه empror9 في 5 أغسطس 2011 · 7 رد · 701 مشاهدة · في الأسئلة المجابة
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

لدي برنامج اقوم بكتابته حاليا وهو عباره عن كلاسين الاول المركبه والثاني السائق .. قمت بعمل الكلاسين ومازلت في البدايه .. ساضع لكم الكود وبعدها ساضع اسئلتي

#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 .. ثم يزيد عند اضافه مركبه جديده وينقص عند الحذف ، حاولت فيها لكن تظهر اخطاء فهل من الممكن المساعده

انتظركم

#2

مازلت في انتظار المساعده

#3

بنظره سريعه على الكود أجد ان المشكله لديك تكمن فى هذا الجزء:

for (int i = 0; i < vehicles.size(); i++)
  {
    cout <<"id "<< abc.getID() <<endl;
         cout <<"id "<< abc.getVehicleType() <<endl;
  }

المتغير abc لم يتم تعريف المعامل subscript له - و هو [] - لذا افترض انك تقصد هنا الوصول للعناصر الموجوده داخل الـ vector، ايضا لا تستخدم member selection operator - النقطه - و لكن استخدم السهم و ذلك لأن الـ vector يحتوى على مؤشرات و بالتالي يصبح الكود لديك هو:

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

بالنسبه لجعل العداد يبدأ من صفر و يزيد و يقل تبعا لإضافه و حذف المركبات، انت تحتاج لوجود متغير static داخل الفئه و الذى ستزيد من قيمته مع كل عملية انشاء و تقل مع كل عملية حذف، المشكله هنا ستكون فى وجود overlapping، انا مشغول الأن فلا استطيع كتابة مثال لك، قم بالبحث فى الأمر و قد يساعدك احد الأعضاء به او انا إذا فرغت.

و الله ولي التوفيق

مدونتي: C++ Tips and Tricks

#4

اهلا بك واشكر لك ردك الكريم

استخدمت السهم في طباعة النتائج لكن مازالت المشكله موجوده اثناء ترجمة الكود

بالنسبه للعداد هل تقصد ان اضع static int id; مثل هكذا؟ ساقوم بالبحث عن هذه المشكله لكن المشكله الاولى اهم وياريت اقدر اجد المساعده لان الكود مازال لايعمل

تحياتي،،

#5

مساء الخير .. انجزت بعض الامور البسيطه واعاني حاليا من بعض المشاكل

هنا اولا الكود كاملا

#include<iostream>
#include<vector>
#include<string>
#include<windows.h>


using namespace std;


class Vehicle{
protected:
int id;
public:
	Vehicle(int& aId) : id(aId) {
	cout <<"Vehicle created successfully" << endl;
	++aId;
	}
	int getID() const { return id; }
	virtual void ShowVehicleType() {}; 
	~Vehicle() {
	cout <<"Vehicle deleted successfully" << endl;
	--id;
	}
};

class VehicleType : public Vehicle{
string name;
int capacity;
string driver_assigned;
public:
	VehicleType(int& aId, const string& aName, int aCapacity, const string& aDriverAssigned) 
	: Vehicle(aId), name(aName), capacity(aCapacity), driver_assigned(aDriverAssigned){ 

	}

	void ShowVehicleType(){
		cout <<"name: " << name << endl;
		cout <<"capacity: " << capacity << endl;
	}


};


void DeleteVehicle(vector<Vehicle*> vehicles){

}

int main(){
	vector<Vehicle*> vehicles;
	int id = 0,capacity,del,chose;
	string name,driver_assigned;

do{
	cout <<"Please enter the name of vehicle type: ";
	cin >> name;
	cout <<"Please enter the capacity of vehicle type: ";
	cin >> capacity;

// insert info in Vehicle
	Vehicle *abc = new VehicleType(id,name,capacity,driver_assigned);
	vehicles.push_back(abc);

// list all the vehicles
	for (int i = 0; i < vehicles.size(); i++)
	{
		cout <<"Do you want to create more vehicles? " << endl;
		cout <<"1- add more vehicles " << endl;
		cout <<"2- delete vehicles " << endl;
		cout <<"3- list all vehicles " << endl;
		cin >> chose;

				   switch(chose)
				   {
						case 1:
							// add more vehicle
							cout <<"Please enter the name of vehicle type: ";
							cin >> name;
							cout <<"Please enter the capacity of vehicle type: ";
							cin >> capacity;
							// search through the vector if it's the same name or not

					    Sleep(1500);
						break;
						case 2:
							// delete a spesfic vehicle
							cout <<"Please select vehicle to delete: ";
							cin >> del;
								for(int i = 0; vehicles.size(); i++){
								vehicles.erase(vehicles.begin()+i);
							    cout << "Vehicle deleted successfully" << endl;
								--id;
					    Sleep(1500);
						break;
								}
						case 3:
							for (int i = 0; i < vehicles.size(); i++){
									cout << "id : " << id << endl;
									vehicles->ShowVehicleType();
							}
						Sleep(1500);
					    break;
						default:	cout << "Command is not valid, Please try again" << endl;
						 exit(0);
                        Sleep(1500);
						break;
					}


    }
}while(true);
	return 0;
}

المشكله الاولى في ID تبع المركبات ، عند عرض جميع النتائج يكون ID موحد .. بمعنى ان لدي خمس مركبات ويظهر لي ID رقم خمسه وهكذا

المشكله الثانيه اود ان ابحث في الـ vector حتى لايضيف المستخدم مركبه موجوده مسبقا ، حاولت فيها كثيرا وبحثت بالانترنت لكن عند التجربه تظهر اخطاء غريبه .. فهل من طريقه واضحه وبسيطه للبحث؟

المشكله الثالثه اريد وضع بعض العبارات والشروط في دوال خاصه بها لكن عند وضعها لا يتم التعرف على متغيرات هذه الدوال لان الـ vector تم تعريفه بعد الدوال .. اتمنى فهمتم المشكله

اذا لم تفهمو مشكلتي الثالثه فهل ممكن شرح مبسط لكيف دمج الدوال مع الvector بدون اي مشاكل؟

تحياتي،،

#6

مازلت بالانتظار ايها الاحبه

#7

جرب هذا الكود بعد التعديل

#include<iostream>
#include<vector>
#include<string>

using namespace std;

static int INDEX = 0;

class Vehicle{
	protected:
	int id;
public:
	Vehicle(int aId) : id(aId) {
		//cout <<"Vehicle created successfully" << endl;
	}

	int getID() const { return id; }

	virtual void ShowVehicleType() const
	{
		cout << "id : " << getID() << endl;
	}; // abstract method

	~Vehicle() 
	{
		//cout <<"Vehicle deleted successfully" << endl;
	}
};

class VehicleType : public Vehicle{
	string name;
	int capacity;
	string driver_assigned;
public:
	VehicleType(int aId, const string& aName, int aCapacity, const string& aDriverAssigned) 
	: Vehicle(aId), name(aName), capacity(aCapacity), driver_assigned(aDriverAssigned){}

	virtual void ShowVehicleType() const
	{
		Vehicle::ShowVehicleType();
		cout <<"name : " << name << endl;
		cout <<"capacity : " << capacity << endl;
	}
};

int getChoice();
void addVehicle(vector<VehicleType>&);
void removeVehicle(vector<VehicleType>&);
void showVehicles(vector<VehicleType>&);
void pause();

int main()
{

	int choice = 0;
	vector<VehicleType> vehicles;
	do{
		choice = getChoice();
		switch(choice)
		{
			case 1 :
				addVehicle(vehicles);
				break;
			case 2 :
				removeVehicle(vehicles);
				break;
			case 3 :
				showVehicles(vehicles);
				break;
		}

	}while(choice != 4);


	pause();
	return 0;
}

int getChoice()
{
	int choice = 0;
	do{
		cout << endl;
		cout << "Do you want to create more vehicles ? " << endl;
		cout << "1- Add more vehicles " << endl;
		cout << "2- Delete vehicles " << endl;
		cout << "3- List all vehicles " << endl;
		cout << "4- Exit " << endl << endl;
		cout << "choice : ";
		cin >> choice;
	}while(choice < 1 || choice > 4);

	return choice;
}

void addVehicle(vector<VehicleType>& vehicles)
{
	int capacity;
	string name,driver_assigned;

	cout << endl << "vehicle type : ";
	cin >> name;
	cout << endl << "vehicle capacity : ";
	cin >> capacity;
	cout << endl << "vehicle driver : ";
	cin >> driver_assigned;

	// insert info in Vehicle
	VehicleType abc(++::INDEX,name,capacity,driver_assigned);
	vehicles.push_back(abc);

}

void removeVehicle(vector<VehicleType>& vehicles)
{
	int id;
	cout << endl << "vehicle id : ";
	cin >> id;

	vehicles.erase(vehicles.begin() + id - 1);
}

void showVehicles(vector<VehicleType>& vehicles)
{
	for(unsigned int i = 0;i < vehicles.size(); ++i)
	{
		cout << endl;
		vehicles.ShowVehicleType();
	}
}

void pause()
{
	char close = 'n';
	cout << "press Y to exit .....";
	do{
		cin >> close;
	}while(close != 'Y' && close != 'y');
}

تحياتى

تم تعديل هذه المشاركة بواسطة MOHAMED FATHEI في 7 أغسطس 2011 في 19:32

1


﴿وَمَا كَانَ اللَّهُ لِيُعَذِّبَهُمْ وَأَنْتَ فِيهِمْ ۚ وَمَا كَانَ اللَّهُ مُعَذِّبَهُمْ وَهُمْ يَسْتَغْفِرُونَ﴾

الأنفال ۳۳                                  
 

#8

الله يعطييك العافيه

مواضيع مشابهة