#include<iostream>
#include<string>
using namespace std;
class Person//基类
{
public:
Person() = default;
Person(string nam, int ag):name(nam),age(ag)//带参构造函数
{
cout << "基类已创建" << endl;
}
void PersonOut()
{
cout << "姓名:" << name << endl;
cout << "年龄:" << age << endl;
}
~Person() = default;
protected:
string name;
int age = 0;
};
class Student :public Person//派生类
{
public:
Student() = default;
Student(string nam, int ag, string cla, int ID) :Person(nam, ag), id(ID), classid(cla)//带参构造函数
{
cout << "派生已创建" << endl;
}
void StudentOut()
{
cout << "学生姓名:" << name << endl;
cout << "学生年龄:" << age << endl;
cout << "学生班级:" << classid << endl;
cout << "学生ID:" << id << endl;
}
protected:
int id = 0;
string classid;
};
int main()
{
Student stu(" 张三 ",21," 20测一 ",01);
///stu.PersonOut();
stu.StudentOut();
return 0;
}
关于#运算符#的问题,如何解决?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
关注 Student类的构造方法没赋值,然后你创建对象的时候逗号打成中文字符了,帮你改好了,希望采纳支持一下博主
#include<iostream> #include<string> using namespace std; class Person//基类 { public: Person() = default; Person(string nam, int ag) :name(nam), age(ag)//带参构造函数 { cout << "基类已创建" << endl; } void PersonOut() { cout << "姓名:" << name << endl; cout << "年龄:" << age << endl; } ~Person() = default; protected: string name; int age = 0; }; class Student :public Person//派生类 { public: Student() = default; Student(string nam, int ag, string cla, int ID) :Person(nam, ag), id(ID), classid(cla)//带参构造函数 { this->id = id; this->classid = classid; } void StudentOut() { cout << "学生姓名:" << name << endl; cout << "学生年龄:" << age << endl; cout << "学生班级:" << classid << endl; cout << "学生ID:" << id << endl; } protected: int id = 0; string classid; }; int main() { Student stu(" 张三 ",21," 20测一 ", 01); ///stu.PersonOut(); stu.StudentOut(); return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 2无用
悬赏问题
- ¥40 图书信息管理系统程序编写
- ¥15 7-1 jmu-java-m02-使用二维数组存储多元线性方程组
- ¥20 Qcustomplot缩小曲线形状问题
- ¥15 企业资源规划ERP沙盘模拟
- ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
- ¥15 前端echarts坐标轴问题
- ¥15 ad5933的I2C
- ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
- ¥15 数学建模求思路及代码
- ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题