#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;
}
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;
}