C++高手赐教,请帮忙编写下面这道题。
3.设计一个基类Person,包含name数据成员:由它派生出学生类Student和教师类Teacher,其中学生类添加学号no数据,教师类添加职称title数据;每个类...
3.设计一个基类Person,包含name数据成员:由它派生出学
生类Student和教师类Teacher,其中学生类添加学号no数据,
教师类添加职称title数据;每个类要定义构造函数和输出成
员的函数;然后由学生类Student和教师类Teacher派生
S_Teacher类(这类人是在学校读书的在职教师)。请给出测
试用例。 展开
生类Student和教师类Teacher,其中学生类添加学号no数据,
教师类添加职称title数据;每个类要定义构造函数和输出成
员的函数;然后由学生类Student和教师类Teacher派生
S_Teacher类(这类人是在学校读书的在职教师)。请给出测
试用例。 展开
展开全部
#include <iostream>
#include <string>
using namespace std;
class Person{
public:
Person(){name="";}
Person(string n){
name=n;
}
~Person(){}
virtual void print(){}
protected:
string name;
};
class Student:virtual public Person{
public:
Student(){name=no="";}
Student(string name,string no){
this->name=name;
this->no=no;
}
~Student(){}
void print(){
cout<<"Student "<<no<<":"<<name<<endl;
}
protected:
string no;
};
class Teacher:virtual public Person{
public:
Teacher(){name=title="";}
Teacher(string name,string title){
this->name=name;
this->title=title;
}
~Teacher(){}
void print(){
cout<<"Teacher "<<name<<":"<<title<<endl;
}
protected:
string title;
};
class S_Teacher:public Student,public Teacher{
public:
S_Teacher(){name=no=title="";}
S_Teacher(string name,string no,string title){
this->name=name;
this->no=no;
this->title=title;
}
~S_Teacher(){}
void print(){
cout<<"S_Teacher "<<name<<":"<<no<<","<<title<<endl;
}
};
void main()
{
Student a("Tom","12345");
Teacher b("Lucy","Pro");
S_Teacher c("Li Lei","9999","ass");
a.print();
b.print();
c.print();
}
#include <string>
using namespace std;
class Person{
public:
Person(){name="";}
Person(string n){
name=n;
}
~Person(){}
virtual void print(){}
protected:
string name;
};
class Student:virtual public Person{
public:
Student(){name=no="";}
Student(string name,string no){
this->name=name;
this->no=no;
}
~Student(){}
void print(){
cout<<"Student "<<no<<":"<<name<<endl;
}
protected:
string no;
};
class Teacher:virtual public Person{
public:
Teacher(){name=title="";}
Teacher(string name,string title){
this->name=name;
this->title=title;
}
~Teacher(){}
void print(){
cout<<"Teacher "<<name<<":"<<title<<endl;
}
protected:
string title;
};
class S_Teacher:public Student,public Teacher{
public:
S_Teacher(){name=no=title="";}
S_Teacher(string name,string no,string title){
this->name=name;
this->no=no;
this->title=title;
}
~S_Teacher(){}
void print(){
cout<<"S_Teacher "<<name<<":"<<no<<","<<title<<endl;
}
};
void main()
{
Student a("Tom","12345");
Teacher b("Lucy","Pro");
S_Teacher c("Li Lei","9999","ass");
a.print();
b.print();
c.print();
}
展开全部
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;
class person
{
public:
person(string i):name(i){};
void personout(){cout<<name;};
private:
string name;
};
class student: public person{
public:
student(string i,int j):person(i),no(j){};
void stout(){cout<<no;};
private:
int no;
};
class teacher: public person{
public:
teacher(string i,string j):person(i),title(j){};
void tout(){cout<<title;};
private:
string title;
};
class S_Teacher: public teacher, public student{
};
int main()
{
system("pause");
}
通过编译
#include <stdlib.h>
#include <string>
using namespace std;
class person
{
public:
person(string i):name(i){};
void personout(){cout<<name;};
private:
string name;
};
class student: public person{
public:
student(string i,int j):person(i),no(j){};
void stout(){cout<<no;};
private:
int no;
};
class teacher: public person{
public:
teacher(string i,string j):person(i),title(j){};
void tout(){cout<<title;};
private:
string title;
};
class S_Teacher: public teacher, public student{
};
int main()
{
system("pause");
}
通过编译
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询