c++中多继承的问题
#include<iostream>#include<string>usingnamespacestd;classtime{public:time(inth,intm,i...
#include<iostream>
#include<string>
using namespace std;
class time{
public:
time(int h,int m,int s)
{ hours=h; minutes=m; seconds=s; }
void display()
{ cout<<"出生时间:"<<hours
<<"时:"<<minutes
<<"分:"<<seconds<<"秒:"
<<endl;
}
protected:
int hours,minutes,seconds;
};
class date{
public:
date(int m,int d,int y)
{ month=m; day=d; year=y;
}
void display()
{ cout<<"出生年月:"<<year<<"年:"<<month<<"月:"<<day<<"日"<<endl;
}
protected:
int month,day,year;
};
class birthtime:public time,
public date{
public:
birthtime(int h,int m,int s,int m,int d,int y, string name1):time(int h,int m,int s),date(int m,int d,int y) { name=name1; }
void display()
{ time::display(); date::display(); cout<<"名字为:"<<name<<endl;
}
protected:
string name;
};
int main()
{
time t1(11,11,11);
date d1(2018,11,11);
birthtime b1(11,11,11,2018,11,11,"Mary");
b1.display();
return 0;
}
请问一下,这个代码错在哪里?😣 展开
#include<string>
using namespace std;
class time{
public:
time(int h,int m,int s)
{ hours=h; minutes=m; seconds=s; }
void display()
{ cout<<"出生时间:"<<hours
<<"时:"<<minutes
<<"分:"<<seconds<<"秒:"
<<endl;
}
protected:
int hours,minutes,seconds;
};
class date{
public:
date(int m,int d,int y)
{ month=m; day=d; year=y;
}
void display()
{ cout<<"出生年月:"<<year<<"年:"<<month<<"月:"<<day<<"日"<<endl;
}
protected:
int month,day,year;
};
class birthtime:public time,
public date{
public:
birthtime(int h,int m,int s,int m,int d,int y, string name1):time(int h,int m,int s),date(int m,int d,int y) { name=name1; }
void display()
{ time::display(); date::display(); cout<<"名字为:"<<name<<endl;
}
protected:
string name;
};
int main()
{
time t1(11,11,11);
date d1(2018,11,11);
birthtime b1(11,11,11,2018,11,11,"Mary");
b1.display();
return 0;
}
请问一下,这个代码错在哪里?😣 展开
展开全部
程序有一些问题,具体如下:
类名需大写开头驼峰命名,不然会出现与系统的内置函数名冲突,比如time。
类的构造函数初始化列表,调用父类构造函数初始化时,不能加数据类型。因为是调用,不是声明。birthtime类的构造函数存在这个问题。
birthtime类的参数m命名重复,而且单字母命名没有意义。可以使用完整的单词命名,比如用minute、month,防止出现重名,增强程序的可读性。
子类重载父类方法时,父类被重载方法应该有virtual声明,子类重载父类方法需有override声明。birthday类重载父类的display方法存在这个问题。
类的属性定义时最好一行一个,比如“int hours, minutes, seconds”,最好分三行写。
另外还有一些写法上的优化,给出优化后的程序:
#include <iostream>
#include <string>
using namespace std;
class Time {
public:
Time(int h, int m, int s) {
hours = h;
minutes = m;
seconds = s;
}
virtual void display() {
cout << "出生时间:" << hours
<< "时:" << minutes
<< "分:" << seconds << "秒"
<< endl;
}
protected:
int hours;
int minutes;
int seconds;
};
class Date {
public:
Date(int m, int d, int y) {
month = m;
day = d;
year = y;
}
virtual void display() {
cout << "出生年月:" << year << "年:" << month << "月:" << day << "日" << endl;
}
protected:
int month;
int day;
int year;
};
class BirthTime : public Time, public Date {
public:
BirthTime(int hour, int minute, int second, int month, int day, int year, string name) : Time(hour, minute, second),
Date(month, day, year) {
this->name = move(name);
}
void display() override {
Time::display();
Date::display();
cout << "名字为:" << name << endl;
}
protected:
string name;
};
int main() {
Time t1(11, 11, 11);
Date d1(2018, 11, 11);
BirthTime b1(11, 11, 11, 2018, 11, 11, "Mary");
b1.display();
return 0;
}
展开全部
语法错误,birthtime构造函数中,time()和date()内不加类型
更多追问追答
追问
那个time()和date()构造函数里面有两个形参名相同影响吗
追答
不带类型进去,只需要传值,所以不存在形参名相同的问题
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
入路由器设置界面
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询