新学的继承于派生章节,要做实验练习题,结果写是写出来了,就是VS报了两个错,能不能请各位大佬帮我看看是哪出的错,或者帮我敲一个能运行的,我读读代码,学习学习。谢谢各位大佬了!
原题如下,我写的代码在下方
2、分别定义Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)。要求:
① 在两个基类中都包含姓名、出生日期(日期类Date的子对象)、性别、地址、电话等数据成员。
② 在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务), 在Teacher_Cadre类中还包含数据成员wages(工资)。
③ 对两个基类中的姓名、出生日期、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。
④ 在类体中声明成员函数,在类外定义成员函数。
⑤ 在派生类Teacher_Cadre的成员函数show()中调用Teacher类中的display()函数,输出姓名、出生日期、性别、职称、地址、电话,然后再用cout语句输出职务与工资。
2、分别定义Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)。要求:
① 在两个基类中都包含姓名、出生日期(日期类Date的子对象)、性别、地址、电话等数据成员。
② 在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务), 在Teacher_Cadre类中还包含数据成员wages(工资)。
③ 对两个基类中的姓名、出生日期、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。
④ 在类体中声明成员函数,在类外定义成员函数。
⑤ 在派生类Teacher_Cadre的成员函数show()中调用Teacher类中的display()函数,输出姓名、出生日期、性别、职称、地址、电话,然后再用cout语句输出职务与工资。
#include <iostream>
#include <string>
using namespace std;
class Date
{protected:
int year, month, day;
public:
Date(int y, int m, int d)
{
year = y;
month = m;
day = d;
}
void print()
{
cout << year << "年" << month << "月" << day << "日" << endl;
}
};
class Teacher
{protected:
string name;
Date date;
string sex;
string address;
long int phone_number;
string title;
public:
Teacher(string n, int y, int m, int d, string s, string a, long int p, string t) :name(n),sex(s),address(a),phone_number(p),title(t),date(y, m, d) {};
void display1()
{
cout << "姓名:" << name << endl;
date.print();
cout << "性别:" << sex << endl << "地址:" << address << endl << "电话号码" << phone_number << endl << "职称:" << title << endl;
}
};
class Cadre
{protected:
string name;
Date date;
string sex;
string address;
long int phone_number;
string post;
public:
Cadre(string n, int y, int m, int d, string s, string a, long int p, string po) :name(n), sex(s), address(a), phone_number(p), post(po), date(y, m, d) {};
void display()
{
cout << "姓名:" << name << endl;
date.print();
cout << "性别:" << sex << endl << "地址:" << address << endl << "电话号码" << phone_number << endl << "职务:" << post << endl;
}
};
class Teacher_Cadre :public Teacher, virtual public Cadre
{
private:
long int wages;
public:
Teacher_Cadre(string n, int y, int m, int d, string s, string a, long int p, string t, string po, long int wage) :Teacher(n, y, m, d, s, a, p, t), Cadre(n, y, m, d, s, a, p, po), wages(wage)
{ wages = wage; }
void display()
{
display1();
cout << "职务:" << post << endl;
cout << "工资:" << wages << endl;
}
};
int main()
{
Teacher_Cadre a("ZhangSan",2020,9,1,"man","BeiJing",10086,"professor","president","15000");
a.display();
return 0;
}
代码74行 改为:
Teacher_Cadre a("ZhangSan", 2020, 9, 1, "man", "BeiJing", 10086, "professor", "president", 15000);
你构造函数写的 是 long类型,结果你传递const char[6] 参数
参数没对上,最后一个参数应该是整数,不是字符串。
Teacher_Cadre a("ZhangSan",2020,9,1,"man","BeiJing",10086,"professor","president",15000);
如果问题解决,请采纳!谢谢!
把提示的两个错误信息贴出来,方便进行问题定位,提高解决问题的效率
#include <iostream>
#include <string>
using namespace std;
class Date
{protected:
int year, month, day;
public:
Date(int y, int m, int d)
{
year = y;
month = m;
day = d;
}
void print()
{
cout << year << "年" << month << "月" << day << "日" << endl;
}
};
class Teacher
{protected:
string name;
Date date;
string sex;
string address;
int phone_number;
string title;
public:
Teacher(string n, int y, int m, int d, string s, string a, long int p, string t) :name(n),sex(s),address(a),phone_number(p),title(t),date(y, m, d) {};
void display1()
{
cout << "姓名:" << name << endl;
date.print();
cout << "性别:" << sex << endl << "地址:" << address << endl << "电话号码" << phone_number << endl << "职称:" << title << endl;
}
};
class Cadre
{protected:
string name;
Date date;
string sex;
string address;
int phone_number;
string post;
public:
Cadre(string n, int y, int m, int d, string s, string a, long int p, string po) :name(n), sex(s), address(a), phone_number(p), post(po), date(y, m, d) {};
void display()
{
cout << "姓名:" << name << endl;
date.print();
cout << "性别:" << sex << endl << "地址:" << address << endl << "电话号码" << phone_number << endl << "职务:" << post << endl;
}
};
class Teacher_Cadre :public Teacher, virtual public Cadre
{
private:
int wages;
public:
Teacher_Cadre(string n, int y, int m, int d, string s, string a, int p, string t, string po, int wage) :Teacher(n, y, m, d, s, a, p, t), Cadre(n, y, m, d, s, a, p, po), wages(wage)
{ wages = wage; }
void display()
{
display1();
cout << "职务:" << post << endl;
cout << "工资:" << wages << endl;
}
};
int main()
{
Teacher_Cadre a("ZhangSan",2020,9,1,"man","BeiJing",10086,"professor","president",15000);
a.display();
return 0;
}
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632