ok here is my code
1st Header file :
Code:
#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
2nd CPP file:
Code:
#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);
}
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)+1;
_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;
_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);
set_brandname(brandname);
add_parts (parts,nr_parts);
print_car();
}
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;
}
}
3rd Main
Code:
#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';
Car cars[3];
for (int i=0; i<3; i++)
{
cout << "Enter Details for car Nr "<<i+1<<endl;
cout << "1- Enter the name : ";
names = new char[size];
cin.get(names,size);
clearBuf();
cout << "2- Enter the Hp: ";
cin >> Hp;
cars[i].set_Car(Hp,Part,Nr_parts,names);
if (names)
delete[]names;
names=NULL;
/*i have stoped here ...... problem with the name;
*/
}
return 0;
}