include <iostream>#include <cstring>using namespace std;class Date{ int year; int month; int day;public: int Getyear() { return year; } int Getmonth() { return month; } int Getday() { return day; } void Setyear() { }};class Product{ char *name; double price; Date deptime; char *factory; bool easy_break; Date valtime; char *color; double num;public: Product(char *n); Product(char *n,double p=0); Product(char *n,double p,Date d); void SetProduct(char *n,double p,Date d,char *f,bool e,Date v,char *c,int number); void output ();};void Product::output (){ cout<<name<<" "<<price<<endl; cout<<deptime.Getyear()<<"-"<<deptime.Getmonth()<<"-"<<deptime.Getday()<<endl; cout<<factory<<" "<<easy_break<<endl; cout<<valtime.Getyear()<<"-"<<valtime.Getmonth()<<"-"<<valtime.Getday()<<endl;}Product::Product(char *n){ name=new char[strlen(n)+1]; strcpy(name,n); cout<<"Constrcting One...."<<endl;}Product::Product(char *n,double p){ name=n; price=p; cout<<"Constrcting Two...."<<endl;}Product::Product(char *n,double p,Date d){ name=n; price=p; Date deptime(d); cout<<"Constrcting Three...."<<endl;}void Product::SetProduct(char *n,double p,Date d,char *f,bool e,Date v,char *c,int number){ name=n; price=p; Date deptime(d); // factory=f; easy_break=e; Date valtime(v); color=c; num=number;}int main(){ char s[10]; cin>>s; Product p1("car"); Product p2("glass",3.00); Product p3("pen",5.00,2009,3,14); p1.SetProduct("car",100000.0,2009,3,14,"nanjing",0,2010,10,14,"red",1.5); p1.output(); p2.output(); p3.output(); return 0;}
2 回答

慕容森
TA貢獻1853條經驗 獲得超18個贊
問題1:
聲明:
Product(char *n);
Product(char *n, double p=0);
調用:
Product p1("car");
存在二義性錯誤,編譯器不知道究竟該調用上面兩個構造函數中的哪一個。
問題2:
聲明:
Product(char *n);
調用:
Product p1("car");
聲明是char*而調用傳遞的卻是字符串字面值,不符合C++標準,應將聲明改為Product(const char *n);
問題3:
聲明:
Product(char *n);
Product(char *n, double p=0);
Product(char *n, double p, Date d);
調用:
Product p3("pen", 5.00, 2009, 3, 14);
Product類沒有接受5個參數的構造函數。
- 2 回答
- 0 關注
- 248 瀏覽
添加回答
舉報
0/150
提交
取消