c++第三题,求教大神!
3个回答
展开全部
#include <stdio.h>
#include <string.h>
#include <time.h>
class Car
{
private:
char * mPP; // 品牌
char * mXH; // 型号
int mNF; // 年份
int mJG; // 价格
protected:
char * newStr(const char * str)
{
if(str == NULL) return NULL;
int len = strlen(str);
if(len == 0) return NULL;
char * r = new char[len + 1];
strcpy(r, str);
return r;
}
int getYear()
{
time_t now = time(NULL);
tm * t = localtime(&now);
return t->tm_year + 1900;
}
public:
Car() : mPP(NULL), mXH(NULL), mNF(0), mJG(0) {}
Car(const char * pp, const char * xh, int jg)
{
mPP = newStr(pp);
mXH = newStr(xh);
mNF = getYear();
mJG = jg;
}
Car(const char * pp, const char * xh, int jg, int year)
{
mPP = newStr(pp);
mXH = newStr(xh);
mNF = year;
mJG = jg;
}
Car(const Car & l)
{
mPP = newStr(l.mPP);
mXH = newStr(l.mXH);
mNF = l.mNF;
mJG = l.mJG;
}
~Car()
{
delete [] mPP;
delete [] mXH;
}
const char * getPP() const {return (const char *)mPP;}
const char * getXH() const {return (const char *)mXH;}
int getNF() const {return mNF;}
int getJG() const {return mJG;}
void setPP(const char * pp) {delete [] mPP; mPP = newStr(pp);}
void setXH(const char * xh) {delete [] mXH; mXH = newStr(xh);}
void setNF(int year) {mNF = year;}
void setJG(int jg) {mJG = jg;}
};
int main()
{
char xhBuf[256], ppBuf[256];
int year, jg;
printf("请输入品牌:");
scanf("%s", ppBuf);
printf("请输入型号:");
scanf("%s", xhBuf);
printf("请输入价格:");
scanf("%d", &jg);
printf("请输入年份:");
scanf("%d", &year);
Car a(ppBuf, xhBuf, jg, year);
printf("%s\t%s\t%d\t%d\n", a.getPP(), a.getXH(), a.getJG(), a.getNF());
Car b(a);
printf("%s\t%s\t%d\t%d\n", b.getPP(), b.getXH(), b.getJG(), b.getNF());
Car c(ppBuf, xhBuf, jg);
printf("%s\t%s\t%d\t%d\n", c.getPP(), c.getXH(), c.getJG(), c.getNF());
Car d;
d.setJG (jg);
d.setNF (year);
d.setPP (ppBuf);
d.setXH (xhBuf);
printf("%s\t%s\t%d\t%d\n", d.getPP(), d.getXH(), d.getJG(), d.getNF());
return 0;
}
展开全部
//////////////////////////////////////////////////
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <iostream>
#include <string.h>
class Car{
public:
Car(){
strcpy(name,"Ford");
strcpy(type,"福克斯");
year=2014;
price=16.99;
}
float getprice(){return price;}
char* getname(){return name;}
char* gettype(){return type;}
int getyear(){return year;}
private:
char name[20];
char type[10];
int year;
float price;
};
main()
{
Car a;
cout<<"year:"<<a.getyear()<<endl;
cout<<"price:"<<a.getprice()<<endl;
cout<<"Name:"<<a.getname()<<endl;
cout<<"Type:"<<a.gettype()<<endl;
system("PAUSE");
return 0;
}
#include <string.h>
class Car{
public:
Car(){
strcpy(name,"Ford");
strcpy(type,"福克斯");
year=2014;
price=16.99;
}
float getprice(){return price;}
char* getname(){return name;}
char* gettype(){return type;}
int getyear(){return year;}
private:
char name[20];
char type[10];
int year;
float price;
};
main()
{
Car a;
cout<<"year:"<<a.getyear()<<endl;
cout<<"price:"<<a.getprice()<<endl;
cout<<"Name:"<<a.getname()<<endl;
cout<<"Type:"<<a.gettype()<<endl;
system("PAUSE");
return 0;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询