打印

构造函数问题

[复制链接]
767|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
xsgy123|  楼主 | 2013-10-31 16:48 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
不带参数的构造函数有什么问题?
#include <iostream>
#include <cstring>
using namespace std;
class book
{
char name[20];
char chubanshe[20];
char zhuozhe[20];
char time[20];
double price;
public:
book()//不带参数的构造函数有问题
{
strcpy(name,"name");
strcpy(chubanshe,"chuban");
strcpy(zhuozhe,"zhuo");
strcpy(time,"time");
price=0;

}
book(char name1[20],char chuban[20],char zhuo[20],char time1[20],double price1)
{
strcpy(name,name1);
strcpy(chubanshe,chuban);
strcpy(zhuozhe,zhuo);
strcpy(time,time1);
price=price1;
}
void show()
{
cout<<name<<"        "<<chubanshe<<"         "<<zhuozhe<<"        "<<time<<"        "<<"$"<<price<<endl;       
}
bool operator==(book b)
{
if (strcmp(name,b.name)==0&&strcmp(chubanshe,b.chubanshe)==0&&strcmp(zhuozhe,b.zhuozhe)==0&&strcmp(time,b.time)==0&&price==b.price)
{
return 1;
}
else
{
return 0;
}
}
book& operator=(book b)
{
strcpy(name,b.name);
strcpy(chubanshe,b.chubanshe);
strcpy(zhuozhe,b.zhuozhe);
strcpy(time,b.time);
price=b.price;
return (*this);
}
bool operator<(book b)
{
if (price<b.price)
{
return 1;
}
else
{
return 0;
}
}
book& operator++()
{
price=price+1;
return (*this);
}
book operator++(int)
{
book old;
old=(*this);
++(*this);
return old;
}
};
void main()
{       
book c();
book a("qwe","asd","zxc","qaz",99);

a.show();
c.show();
c=a;
a.show();
c.show();
if (a==c)
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}


(a++).show();
c.show();

(++a).show();
c.show();

if (c<a)
{
cout<<"yes"<<endl;
}
else
{
cout<<"no"<<endl;
}


}

相关帖子

沙发
秋天落叶| | 2013-10-31 17:03 | 只看该作者
在main()函数中,修改:
book c/*()*/;

使用特权

评论回复
板凳
yybj| | 2013-10-31 17:24 | 只看该作者
没有任何问题!带不带,看你自己的设计

使用特权

评论回复
地板
火箭球迷| | 2013-10-31 18:30 | 只看该作者
bool operator==(book b)
{
if (strcmp(name,b.name)==0&&strcmp(chubanshe,b.chubanshe)==0&&strcmp(zhuozhe,b.zhuozhe)==0&&strcmp(time,b.time)==0&&price==b.price)
{
return 1;
}
请问一下,你用的编译器能使用book形参???应该不可以的吧??我记得我的编译器提示是不能使用非完整类型对象的,应该使用的是const引用类型

使用特权

评论回复
5
vivilzb1985| | 2013-11-4 23:27 | 只看该作者
例程很详尽的啊,谢谢分享了

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

229

主题

2603

帖子

1

粉丝