بسم الله الرحمن الرحيم
يا اخوانى اريد المساعدة فى ايجاد خطأ ما.
الموضوع عمل class سيارة
المستخدم يجب عليه وضع الأسم و سعه المحرك,, وأيضا أجزاء جديده وتحديد السعر,,,
لقد قمت بعون الله بعمل هذا,,, المشاكل التى قابلتنى
1- مشكله فى مسح الأسم من ال destructor
2- يمكن للمستخدم ان يضع او يحذف اجزاء اخرى, المشكله هى الdestructor يقوم بالعمل تلقائيا فيقوم بمسح كل الأجزاء
القديمه
ارجو من الأدارة بعدم غلق الموضوع فأنا لا اطلب حل التمرين ولكن المساعدة , وها هو الكود الذى كتبته
أرجو من الأخوة اذا رايتم اى خطأ حتى لو صغير بالأفاده
ولكم جزيل الشكر مقدما
// 1-HEADER FILE
#ifndef __dclass_h
#define __dclass_h
const int size =50;
void clearBuf();
void menu();
class Car
{
private:
int _nr_parts;
int _hp;
float *_parts;
char *_brandname;
public:
Car (void);
Car (int hp, float *parts, int nr_parts, const char *name);
~Car (void);
void set_hp (int hp);
int get_hp (void)const;
void set_brandname (const char *name);
char get_brandname ()const;
void set_Car (int hp,float *parts,int nr_parts, const char *brandname);
void print_car (void)const;
void add_part (int part_nr);
void remove_part (int part_nr);
void add_parts (float *part_nrs, int nr_parts);
void remove_parts (float *part_nrs, int nr_parts);
};
#endif//2-cpp
#include "D_class.h"
#include <iostream>
#include <cstring>
using namespace std;
void clearBuf() // this function to clear the buffer
{
char k;
do
{
cin.get(k);
}while(k!='\n');
}
Car::Car () //defualt constructor
{
_nr_parts = 0;
_hp = 0;
_parts = NULL;
_brandname = NULL;
}
Car::Car (int hp, float *parts, int nr_parts, const char *name)
{
_hp = 0;
_parts = NULL;
_brandname = NULL;
_nr_parts = 0;
set_Car(hp, parts, nr_parts, name);
print_car();
}
Car::~Car (void) //destructor
{
cout<<"Destructor****"<<endl;
if(_parts)
{
delete[]_parts;
_parts=NULL;
cout << "Dynamic parts deleted.."<<endl;
}
if(_brandname)
{
delete[]_brandname;
_brandname=NULL;
cout << "Dynamic name deleted.."<<endl;
}
else
{
cout<<"something wrong"<<endl;
}
}
//Set functions
void Car::set_hp (int hp)
{
_hp = hp;
}
void Car::set_brandname (const char *name)
{
int length = strlen(name);
_brandname = new char [length];
strcpy(_brandname,name);
}
void Car::add_part (int part_nr)
{
_nr_parts = _nr_parts + part_nr;
}
void Car::remove_part (int part_nr)
{
if(_nr_parts>part_nr)
{
_nr_parts = _nr_parts - part_nr;
}
else
{
cout<< "you will remove all parts"<<endl;
_nr_parts=0;
}
}
void Car::add_parts (float *part_nrs, int nr_parts)
{
if (part_nrs)
{
cout << "What is the cost of the new parts ? "<<endl;
for(int m=0;m<nr_parts; m++) // add values into the array
{
cout << "Part nr ("<<m+1<<") costs : ";
cin >> *(part_nrs+m);
}
//Step 1 call add part function to set the length of the all array
add_part (nr_parts);
//Step 2 creat temp array to store the 2 arrays
float *temp = new float [_nr_parts];
if (temp)
{
if (_parts)
{
cout<<"\nThis nooooooooooooooooooooo work";
for (int k=0; k< (_nr_parts); k++)
{
*(temp + k) = *(_parts + k);
}
for (int j = (_nr_parts - nr_parts); j<(_nr_parts); j++)
{
*(temp + j) = *(part_nrs+j);
}
}
else if (_parts == NULL)
{
cout<<"\ntest that _part point to NULL"<<endl;
for (int m=0; m< _nr_parts; m++)
{
*(temp + m) = *(part_nrs+m);
}
}
delete []_parts;
delete []part_nrs;
_parts=temp;
}
else
cout<<"no temp created"<<endl;
}
else
cout<<"no Dynamic array created... "<<endl;
}
void Car::remove_parts (float *part_nrs, int nr_parts)
{
cout<<"Remove Function"<<endl;
if (part_nrs)
{
cout<<"\ntest if i pass D array"<<endl;
// the following part is to store what will be removed
if (_nr_parts>0)
{
for (int k=(_nr_parts-nr_parts-1); k<nr_parts; k++)
{
*(part_nrs+k)=*(_parts+k);
cout<<"Part nr("<<k+1<<") with price of <"<<*(part_nrs+k)<<">will be remove"<<endl;
}
}
else
{
cout<<"there is no items to be remove.....!!!!!"<<endl;
delete[]_parts;
}
// now start remove the parts
remove_part (nr_parts);
if (_nr_parts>0) // to test if the user doesn't wants to remove all parts
{
float *temp = new float [_nr_parts];
for (int i=0; i<_nr_parts; i++)
{
*(temp+i)=*(_parts+i);
}
delete []_parts;
_parts=temp;
}
else
{
cout<<"all parts have been removed"<<endl;
delete []_parts;
_parts=NULL;
}
}
else
cout<<"no Darray have pased"<<endl;
}
void Car::set_Car (int hp, float *parts,int nr_parts, const char *brandname)
{
set_hp(hp);
cout<<"confirm your choice"<<endl;
int choose=0;
menu();
cin>>choose;
switch (choose)
{
case 1 : add_parts (parts, nr_parts);
break;
case 2 : remove_parts(parts, nr_parts);
break;
default: cout << " invalid Number..."<<endl;
}
set_brandname(brandname);
}
void menu()
{
cout << "choose .."<<endl;
cout << "1 - Add part "<<endl;
cout << "2 - remove parts "<<endl;
}
// get function
char Car::get_brandname ()const
{
for (int i=0; i<strlen(_brandname); i++)
{
cout<<*(_brandname+i);
}
return 0;
}
int Car::get_hp (void)const
{
return _hp;
}
//print function
void Car::print_car()const
{
cout <<"the name is :";
cout<<get_brandname ();
cout<<"\nwith ";
cout<<get_hp ();
cout<<" Hp."<<endl;
cout<<"Parts Details :"<<endl;
for (int n =0; n<(_nr_parts); n++)
{
cout << " part Nr <"<<n+1<<"> cost :";
cout << *(_parts+n)<<" Euro"<<endl;
}
}//3-Main
#include <iostream>
#include <stdlib.h>
#include "D_class.h"
using namespace std;
int main()
{
char *names = NULL;
int Hp=0;
float *Part=NULL;
int Nr_parts=0;
int choose=0;
char c='n';
cout << "Enter a Car Details "<<endl;
cout << "1- Enter the name : ";
names=new char [size];
cin.get (names , size);
clearBuf();
cout << "2- Enter the Hp: ";
cin >> Hp;
do
{
if (Part)
{
delete[]Part;
Part=NULL;
}
menu();
cin >> choose;
switch (choose)
{
case 1 : cout <<"How Many Parts? ";
cin>>Nr_parts;
Part = new float [Nr_parts];
break;
case 2 : cout <<"How Many Parts? ";
cin>>Nr_parts;
Part = new float [Nr_parts];
break;
default: cout << " invalid Number..."<<endl;
}
Car maro (Hp, Part, Nr_parts,names);
cout<<"do you want to add or remove other parts..(Y/N)?";
cin>>c;
cout<<endl;
}while (c!='n');
return 0;
}شكرا والسلام عليكم