设计Person类和Student类,Student类继承自Person类,要求写出两个类的无参和有参的构造函数,设计两个类
2个回答
展开全部
#include<iostream>
#include <string>
using namespace std;
class person
{
public:
person();
person(string name,int age);
void display();
protected:
string name;
int age;
};
person::person(){}
person::person(string name,int age)
{
this->name=name;
this->age=age;
}
void person::display()
{
cout<<this->name<<" "<<this->age<<endl;
}
class student : public person
{
public:
student();
student(string name,int age,int num);
void display();
void setnum(int num);
private:
int num;
};
student::student(){}
student::student(string name,int age,int num):person(name,age)
{
this->num=num;
}
void student::setnum(int num)
{
this->num=num;
}
void student::display()
{
person::display();
cout<<this->num<<endl;
}
int main(int argc, char* argv[])
{
person p1("suyuanming",24);
p1.display();
student std1("suyuanming",24,13);
std1.display();
return 0;
}
#include <string>
using namespace std;
class person
{
public:
person();
person(string name,int age);
void display();
protected:
string name;
int age;
};
person::person(){}
person::person(string name,int age)
{
this->name=name;
this->age=age;
}
void person::display()
{
cout<<this->name<<" "<<this->age<<endl;
}
class student : public person
{
public:
student();
student(string name,int age,int num);
void display();
void setnum(int num);
private:
int num;
};
student::student(){}
student::student(string name,int age,int num):person(name,age)
{
this->num=num;
}
void student::setnum(int num)
{
this->num=num;
}
void student::display()
{
person::display();
cout<<this->num<<endl;
}
int main(int argc, char* argv[])
{
person p1("suyuanming",24);
p1.display();
student std1("suyuanming",24,13);
std1.display();
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询