首页 > 最近刚学面向对象,还不是很熟,不知道代码那个位置错了
头像
Kid⚡
发布于 2018-10-09 23:39
+ 关注

最近刚学面向对象,还不是很熟,不知道代码那个位置错了

 #include<iostream>
#include<string>
using namespace std;
class Time
{
public:
 Time(int con_year,int con_month,int con_day);
 void showtime();
private:
 int year;//年份
 int month;//月份
 int day;//日期
};
class Product
{
public:
 Product();
 Product(string con_name);
 Product(string con_name,double con_price);
 Product(string con_name,double con_price,int year,int month,int day);//通过产品名,产品价格和出厂日期创建对象
 void setcompany(string con_company);//设置出产厂家
 void setintroduce(string con_introduce);//设置产品介绍
 void setbool(bool con_flag1,bool con_flag2);//设置可食用标志和易碎标志
 void setprotime(int year,int month,int day);//设置有效日期
 void proshow();//显示产品属性
private:
 string name;//产品名
 double price;//产品价格
 string company;//生产厂家
 string introduce;//产品介绍
 bool flag1;//可食用标志
 bool flag2;//易碎标志
 Time protime;//有效日期
 Time pretime;//出厂日期
};
Time::Time(int con_year=0,int con_month=0,int con_day=0)
{
 year=con_year;
 month=con_month;
 day=con_day;
}
void Time::showtime()
{
 cout<<year<<"-"<<month<<"-"<<day<<endl;
}
Product::Product(string con_name)
{
 name=con_name;
 price=0;
 Time time(0,0,0) ;
 pretime=time;
}
Product::Product(string con_name,double con_price)
{
 name=con_name;
 price=con_price;
 Time time(0,0,0) ;
 pretime=time;
}
Product::Product(string con_name,double con_price=0.0,int year=0,int month=0,int day=0)
{
 name=con_name;
 price=con_price;
 Time time(year,month,day);
 pretime=time;
}
void Product::setcompany(string con_company)
{
 company=con_company;
}
void Product::setintroduce(string con_introduce)
{
 introduce=con_introduce;
}
void Product::setbool(bool con_flag1,bool con_flag2)
{
 flag1=con_flag1;
 flag2=con_flag2;
}
void Product::setprotime(int year,int month,int day)
{
 Time time(year,month,day);
 protime=time;
}
void Product::proshow()
{
 cout<<"产品名:"<<name<<endl;
 cout<<"产品介绍:"<<introduce<<endl;
 if(flag1) cout<<"可食用"<<endl;
 else cout<<"不可食用"<<endl;
 if(flag2) cout<<"易碎"<<endl;
 else cout<<"非易碎"<<endl;
 cout<<"该产品出厂日期:";
 pretime.showtime();
 cout<<"该产品有效日期:";
 protime.showtime();
}
int main()
{
 Product product[3]={("燃烧我的卡路里"),
                    ("燃烧我的卡路里",100.0),
      ("燃烧我的卡路里",100.0,2018,9,25)};
 product[1].setcompany("没有公司");
 product[1].setintroduce("尚无介绍");
 product[1].setbool(false,false);
 product[1].setprotime(0,0,0);
 product[2].setcompany("没有公司");
 product[2].setintroduce("尚无介绍");
 product[2].setbool(false,false);
 product[2].setprotime(0,0,0);
 product[3].setcompany("西红人瘦");
 product[3].setintroduce("每一斤脂肪可领取1000元,一起燃烧我的卡路里");
 product[3].setbool(false,false);
 product[3].setprotime(2018,10,9);
 for(int i=0;i<3;i++)
  product[i].proshow();
 return 0;
}

全部评论

(2) 回帖
加载中...
话题 回帖