本博客内容为中国大学生MOOC国家精品课程《计算机程序设计C++》作业记录,仅供参考,观者忌照搬照抄,欢迎交流批评指正!
##第十一周编程作业
本周作业内容为继承的应用
- 公有继承中派生类Student对基类Person成员的访问
#include <iostream>
#include <string.h>
using namespace std;
class Person
{
char Name[20];
char Sex;
int Age;
public:
void Register(char *name, int age, char sex);
void ShowMe();
};
void Person::Register(char *name, int age, char sex)
{
strcpy(Name, name);
Age = age;
Sex = sex;
}
void Person::ShowMe()
{
cout << Name <<' '<< Age <<' '<< Sex;
}
class Student :public Person
{
int Number;
char ClassName[10];
public:
void RegisterStu(char *classname, int number, char *name, int age, char sex);
void ShowStu();
};
void Student::RegisterStu(char *classname, int number, char *name, int age, char sex)
{
Register(name, age, sex);
strcpy(ClassName, classname);
Number = number;
}
void Student::ShowStu()
{
cout << Number << ' ' << ClassName << ' ';
ShowMe();
}
int main()
{
char *classname;
classname = new char[10];
int number;
char *name;
name = new char[10];
int age;
char sex;
cin >> classname >> number >> name >> age >> sex;
Student a;
a.RegisterStu(classname, number, name, age, sex);
a.ShowStu(); cout << endl;
a.ShowMe();
return 0;
}
- 一个基类Person的多个派生类
#include <iostream>
#include <string.h>
using namespace std;
class Person
{
protected:
char Name[10];
char Sex;
int Age;
public:
void Register(char *name, int age, char sex);
void ShowMe();
};
void Person::Register(char *name, int age, char sex)
{
strcpy(Name, name);
Age = age;
Sex = sex;
}
void Person::ShowMe()
{
char sex[3];
if (Sex == 'm')
strcpy(sex,"男");
else
strcpy(sex,"女");
cout << "姓名 " << Name << endl;
cout << "性别 " << sex << endl;
cout <<"年龄 " << Age << endl;
}
class Teacher:public Person
{
private:
char Dept[20];
int Salary;
public:
Teacher(){ Register(" ", 0, ' '); };
Teacher(char *dept, int salary,char *name, int age, char sex);
void ShowMe();
};
Teacher::Teacher(char *dept, int salary, char *name, int age, char sex)
{
Register(name, age, sex);
strcpy(Dept, dept);
Salary = salary;
}
void Teacher::ShowMe()
{
Person::ShowMe();
cout << "工作单位 " << Dept << endl;
cout << "月薪 " << Salary << endl;
}
class Student :public Person
{
char ID[12];
char Class[12];
public:
Student(char *id, char *classroom, char *name, int age, char sex);
void ShowMe();
};
Student::Student(char *id, char *classroom, char *name, int age, char sex)
{
Register(name, age, sex);
strcpy(ID, id);
strcpy(Class, classroom);
}
void Student::ShowMe()
{
cout << "学号 " << ID << endl;
Person::ShowMe();
cout << "班级 " << Class << endl;
}
int main()
{
char id[10];
char classroom[12];
char name[10];
int age;
char sex;
char dept[20];
int salary;
cin >> name >> age >> sex >> dept>>salary;
Teacher a(dept, salary, name, age, sex);
cin >> name >> age >> sex >> id >> classroom;
Student b(id,classroom,name,age,sex);
a.ShowMe();
b.ShowMe();
return 0;
}
- 派生类Student的构造函数和析构函数
#include <iostream>
#include <string.h>
using namespace std;
class Person
{
public:
char Name[10];
int Age;
Person(char *name, int age)
{
strcpy(Name, name);
Age = age;
cout << "constructor of person " << Name << endl;
};
~Person()
{
cout << "deconstructor of person " << Name << endl;
};
void Show()
{
cout << "Person " << Name << " " << Age ;
}
};
class Student :public Person
{
char ClassName[10];
Person Monitor;
public:
Student(char *name, int age,char *classname,char *name1, int age1);
~Student(){ cout << "deconstructor of Student" << endl; };
void Show()
{
Person::Show();
cout << " in " << ClassName << endl;
cout << "Monitor: ";
Monitor.Show();
}
};
Student::Student(char *name, int age, char *classname, char *name1, int age1):Person(name, age), Monitor(name1,age1)
{
strcpy(ClassName, classname);
cout << "constructor of Student" << endl;
}
int main()
{
char name[10], name1[10], classname[10];
int age, age1;
cin >> name >> age >> classname >> name1 >> age1;
Student a(name, age, classname, name1, age1);
return 0;
}
- 从Point类继承的Circle类
#include <iostream>
using namespace std;
class Point
{
private:
int x, y; //点的x和y坐标
public:
Point(int a= 0, int b= 0); // 构造函数
void SetPoint(int, int); // 设置坐标
int GetX() { return x; } // 取x坐标
int GetY() { return y; } // 取y坐标
void Print(); //输出点的坐标 };
};
Point::Point(int a, int b)
{
SetPoint(a, b);
}
void Point::SetPoint(int a, int b)
{
x = a; y = b;
}
void Point::Print()
{
cout << "[" << x << "," << y << "]";
};
class Circle :public Point
{
private:
double radius;
public:
Circle(int x = 0, int y = 0, double r = 0.0) :Point(x, y){ SetRadius(r); }
void SetRadius(double r){ radius = r; }
double GetRadius(){ return radius; }
double Area(){ return 3.14*radius*radius; }
void Print();
};
void Circle::Print()
{
cout << "Circle c Center=";
Point::Print();
cout << endl << "Radius=" << radius;
}
int main()
{
int x1, y1, x2, y2;
double r;
cin >> x1 >> y1 >> x2 >> y2 >> r;
Point p(x1, y1);
Circle c(x2,y2,r);
cout << "Point p ";
p.Print(); cout << endl;
c.Print(); cout << endl;
cout << "The centre of circle c ";
c.Point::Print(); cout << endl;
cout << "The area of circle c " << c.Area();
return 0;
}
- 从Student类和Teacher类多重派生Graduate类
#include <iostream>
#include <string.h>
using namespace std;
class Person
{
private:
char Name[10];
char Sex;
int Age;
public:
Person(){};
Person(char *name, char sex, int age){ Register(name, age, sex);
//cout << "建立了一个Person" << endl;
}
void Register(char *name, int age, char sex);
void ShowMe();
};
void Person::Register(char *name, int age, char sex)
{
strcpy(Name, name);
Sex=sex;
Age = age;
}
void Person::ShowMe()
{
char sex[3];
if (Sex == 'm')
strcpy(sex, "男");
else
strcpy(sex, "女");
cout << "姓名 " << Name << endl;
cout << "性别 " << sex << endl;
cout << "年龄 " << Age << endl;
}
class Teacher :virtual public Person
{
private:
char Dept[20];
int Salary;
public:
Teacher(char *name, int age, char sex, char *dept, int salary) :Person(name, sex, age)
{
strcpy(Dept, dept);
Salary = salary;
//cout << "建立了一个Teacher" << endl;
}
void Show()
{
cout << "工作单位 " << Dept << endl;
cout << "月薪 " << Salary << endl;
}
};
class Student :virtual public Person
{
private:
char ID[12];
char Class[12];
public:
Student(){};
Student(char *name, int age, char sex, char *id, char *classroom) :Person(name, sex, age)
{
strcpy(ID, id);
strcpy(Class, classroom);
//cout << "建立了一个Student" << endl;
}
void Show()
{
cout << "班级 " << Class << endl;
cout << "学号 " << ID << endl;
}
};
class Graduate :public Teacher,public Student
{
public:
Graduate(char *name, int age, char sex, char *dept, int salary, char *id, char *classroom)
:Person(name,sex,age),Teacher(name, age, sex, dept, salary),Student(name,age,sex,id,classroom){}
void ShowMe()
{
Student::Show();
Person::ShowMe();
Teacher::Show();
}
};
int main()
{
char name[10], sex, dept[20], id[12], classroom[12];
int age, salary;
cin >> name >> age >> sex >> dept >> salary >> id >> classroom;
Graduate a(name, age, sex, dept, salary, id, classroom);
a.ShowMe();
return 0;
}
注:第五题使用了虚基类的继承
关于虚基类的示意图如下,在第五题中,Person类派生出Student,Teacher类,而Graduate类是继承自Student,Teacher类,因此如果不建立虚基类,构造Graduate时会建立两个Person类,造成基类的重复构造从而导致Graduate类无法调用Person类的函数,因此建立Student,Teacher类时,选择虚基类的继承方式,这样在之后的构造中只会产生一个Person类,就可以对Person类的函数进行调用了
下面是虚基类的继承:
class Teacher :virtual public Person{...}//虚基类继承的写法,需加上virtual
class Student :virtual public Person{...}
class Graduate :public Teacher,public Student
{
public:
Graduate(char *name, int age, char sex, char *dept, int salary, char *id, char *classroom)
:Person(name,sex,age),Teacher(name, age, sex, dept, salary),Student(name,age,sex,id,classroom){}
//注意:派生类的构造函数的成员初始化列表中必须列出对虚基类构造函数的调用;如果未列出,则表示使用该虚基类的缺省构造函数。
void ShowMe()
{
Student::Show();
Person::ShowMe();//由于Teacher,Student是虚继承,在这个Graduate类中可以调用Person类的函数
Teacher::Show();
}
};
如果不使用虚基类继承
class Teacher : public Person{...}
class Student : public Person{...}
class Graduate :public Teacher,public Student
{
public:
Graduate(char *name, int age, char sex, char *dept, int salary, char *id, char *classroom)
:Teacher(name, age, sex, dept, salary),Student(name,age,sex,id,classroom){}
void ShowMe()
{
Student::Show();
Person::ShowMe();//这一行会报错,提示基类Person不明确
Teacher::Show();
}
};
提示入下:
以上为第11周编程作业。