I 'm not understand the code and my code not run
#include
#include
#include
using namespace std;
class Car{
private :
int price;
public :
int model_no;
char *name;
// Constructor
Car(){
// override the default Constructor
name = NULL;
cout << "Making a car .." << endl;
}
// Constructor with parameters -Parametrised Constructor
Car(int p,int mn,char *n){
price = p;
model_no = mn;
int l = strlen(n);
name = new char[l+1]; // dynamically declare name
strcpy(name,n);
}
}
// Deep Copy Cosntructor
Car(Car &X){
cout << “Making a copy of Car”;
price = X.price;
model_no = X.model_no;
int l = strlen(X.name);
name = new char[l+1]; // Copy contain original array
strcpy(name,X.name);
}
void setName(char *n){
if(name==NULL){
name = new char [strlen(n)+1]; // new dynamic array declare
strcpy(name,n);
}
else {
// Delete the old name (array) allocate the new name (array)
}
}
void start(){
cout << "Grrr... starting the car " << name << endl;
}
void setPrice(int p){
if(p>1000){
price =p;
}
else {
price = 1000;
}
}
int getPrice(){
return price;
}
void print(){
cout << name << endl;
cout << model_no << endl;
cout << price << endl;
cout << endl;
}
int main(){
Car C;
C.setPrice(1500);
C.setName("Nano");
C.model_no = 1001;
C.print();
Car D(100,200,"BMW");
Car E(D);
E.name[0] = 'G';
D.print();
E.print();