Object Oriented C++

Could anybody check my coding? Why it didn't give the actual amount (logic err)? I need to include instructor/destructor. Tqvm!

#include<iostream>
#include<string.h>
using namespace std;

class CAR {
private:
char name[30];
char plate_num[10];
double price,total_price,average;

public:
CAR(char[],char[],double) ;
double count_total(double,double);
void display(double);
};

CAR::CAR(char n[],char np[],double h) {
strcpy(name,n);
strcpy(plate_num,pn);
price=p;
}

double CAR::count_total(double price2,double total_price)
{total_price=total_price+price2;
cout<<"\n Total car price : RM "<<total_price;
return total_price;
}

void CAR::display(double price)
{
if (price>=60000)
{
cout<<"Car name : "<<name;
cout<<"\n Car plate number :"<<plate_num;
cout<<"\n Car price : RM "<<price;
cout<<"\n";
}
else
cout<<"Car price is less than 60000";
}

//CAR::~CAR()
//{cout<<"deconstructed"<<endl;
//}

int main() {
char name[30],plate_num[10];
int i;
double price,total=0,average2,average;
CAR data1(char,char,double); // a=0,b=0,c=0 call default constructor
CAR data2(double,int);

for(i=1;i<=100;i++){

cout<<"Please type car name:\n";
cin>>name;
cout<<"Please type car plate number :\n";
cin>>plate_num;
cout<<"Please type car price :\n";
cin>>price;
CAR data1(name,plate_num,price);

data1.display(price);
total=data1.count_total(price,total);}
average2=total/(i-1);
cout<<"\n Total car average : RM "<<average2;
cout<<endl;
return 0;
}



Last edited on
1
2
3
4
5
6
CAR::CAR(char n[],char np[],double h)
{
    strcpy(name,n);
    strcpy(plate_num,pn);
    price=p;
}

|25|error: 'pn' was not declared in this scope|
|26|error: 'p' was not declared in this scope|
||=== Build finished: 2 errors, 8 warnings ===|

This won't compile.
After correcting this function, program works fine.
HOWEVER, you doesn.t using member variables price,total_price,average (aside from assigning to it in constructor)
Tqvm! I already corrected but still hv err.:
Compilation finished with errors:

#include<iostream>
#include<string>
using namespace std;

class CAR {
private:
char name[30];
char plate_num[10];
double price,total_price,average;

public:
CAR(char[],char[],double) ;
double count_total(double,double);
void display(double);
};

CAR::CAR(char name[30],char plate_num[10],double price) {
strcpy(name);
strcpy(plate_num);
price=0+price;
}

double CAR::count_total(double price,double total_price)
{total_price=total_price+price;
cout<<"\n Total car price : RM "<<total_price;
return total_price;
}

void CAR::display(double price)
{
if (price>=60000)
{
cout<<"Car name : "<<name;
cout<<"\n Car plate number :"<<plate_num;
cout<<"\n Car price : RM "<<price;
cout<<"\n";
}
else
cout<<"Car price is less than 60000";
}

//CAR::~CAR()
//{cout<<"deconstructed"<<endl;
//}

int main() {
char name[30],plate_num[10];
int i;
double price,total=0,average;
CAR data1(char,char,double); // a=0,b=0,c=0 call default constructor
CAR data2(double,int);

for(i=1;i<=100;i++){

cout<<"Please type car name:\n";
cin>>name;
cout<<"Please type car plate number :\n";
cin>>plate_num;
cout<<"Please type car price :\n";
cin>>price;
CAR data1(name,plate_num,price);

data1.display(price);
total=data1.count_total(price,total);}
average=total/(i-1);
cout<<"\n Total car average : RM "<<average;
cout<<endl;
return 0;
}

-Compilation finished with errors:
source.cpp: In constructor 'CAR::CAR(char*, char*, double)':
source.cpp:21:13: error: 'strcpy' was not declared in this scope
(How to DECLARE?)
Last edited on
strcpy() is declared in <cstring> header.
Oh, my mistake (cos i delete ".h" and forgot to put "c" instead. But why I got this?:

Compilation finished with errors:
source.cpp: In constructor 'CAR::CAR(char*, char*, double)':
source.cpp:19:13: error: too few arguments to function 'char* strcpy(char*, const char*)'
In file included from string.h:642:0,
from c++/4.7/cstring:44,
from source.cpp:2:
x86_64-linux-gnu/bits/string3.h:103:1: note: declared here
source.cpp:20:18: error: too few arguments to function 'char* strcpy(char*, const char*)'
In file included from string.h:642:0,
from c++/4.7/cstring:44,
from source.cpp:2:
x86_64-linux-gnu/bits/string3.h:103:1: note: declared here
1
2
strcpy(name);
strcpy(plate_num);

Exactly what it says: strcpy takes 2 arguments. You provided it with one. Where (or from) will you copy your string.
I can run this but the output is haywire:

#include<iostream>
#include<cstring>
using namespace std;

class CAR {
private:
char name[30];
char plate_num[10];
double price,total_price,average;

public:
CAR(char[],char[],double) ;
double count_total(double,double);
void display(double);
};

CAR::CAR(char name[30],char plate_num[10],double price) {
price=0+price;
strcpy(name,plate_num);
}

double CAR::count_total(double price,double total_price)
{total_price=total_price+price;
cout<<"\n Total car price : RM "<<total_price;
return total_price;
}

void CAR::display(double price)
{
if (price>=60000)
{
cout<<"Car name : "<<name;
cout<<"\n Car plate number :"<<plate_num;
cout<<"\n Car price : RM "<<price;
cout<<"\n";
}
else
cout<<"Car price is less than 60000";
}

//CAR::~CAR()
//{cout<<"deconstructed"<<endl;
//}

int main() {
char name[30],plate_num[10];
int i;
double price,total=0,average;
CAR data1(char,char,double); // a=0,b=0,c=0 call default constructor
CAR data2(double,int);

for(i=1;i<=100;i++){

cout<<"Please type car name:\n";
cin>>name;
cout<<"Please type car plate number :\n";
cin>>plate_num;
cout<<"Please type car price :\n";
cin>>price;
CAR data1(name,plate_num,price);

data1.display(price);
total=data1.count_total(price,total);}
average=total/(i-1);
cout<<"\n Total car average : RM "<<average;
cout<<endl;
return 0;
}
Last edited on
You didn't assigned name and plate_num anything. Local variables with the same name shadows those from class. To get access to class variables rename function arguments line name_, plate_num_ or you can access class members as this->name, this->plate_num
Topic archived. No new replies allowed.