C++实验(2) 继承与多态

C++实验(2)    继承与多态

一、实验目的和要求:

1.学习定义和使用类的继承关系,定义派生类;

2.熟悉不同继承方式下对基类成员的访问控制;

3.掌握在派生类中初始化基类成员的方法;

4.掌握使用虚函数实现动态多样性。

要求:认真调试程序,认真书写实验报告,并对结果作分析。

二、实验内容

1.定义一个人员类Person,包括姓名、编号、性别等数据成员和用于输入、输出的成员函数,在此基础上派生出学生类Student类(增加成绩)和教师类Teacher(增加教龄),并实现对学生和教师信息的输入和输出。

2. 定义一个抽象类Shape,包含纯虚函数Area()(用来计算面积)和SetShape()(用来重设形状大小)。然后派生出三角形类Triangle类、矩形Rect类、圆Circle类,分别求其面积。最后定义一个Total_Area类,计算这几个形状的面积之和,各形状的数据通过Total_Area类的构造函数或成员函数来设置。编写一个完整的程序。

3.开发一个简单的大学人员管理程序,该程序可以管理大学的一些基本人员:学生(student)、教师(teacher)、教授(professor)。首先设计一个虚基类person。通过该类保存人员的最基本信息:姓名(name)、年龄(age)和性别(sex)。然后使用该类派生出学生类student和教师类teacher,在其中添加各自的特性,如在student类中添加如下信息:专业(speciality),在teacher类中添加院系(department)等。还有部分教师在工作的同时在职修读学位,因此同时具有教师和学生双重身份,所以由student类和teacher类再次派生出stuTeacher类。为每个类定义一个输出函数print(),输出该类相关信息。
嘿嘿,萌新一个,如有不对,请指出,后会改进。
#include "iostream"
#include "string"

using  namespace std;

class Person
{
private: string  m_Name; 
		 int m_Id;
		 string m_Sex;
public:
		 Person() {}
		 Person(string name,string sex,int id)
		 {
			 m_Name = name;
			 m_Sex = sex;
			 m_Id = id;
		 }
		 void Person_display()
		 {
			 cout  << m_Name << " " << m_Sex << " " << m_Id << " " ;
		 }

};
class  Student :Person
{
private: int m_Score;
public: 
	Student() {}
	Student( string name, string sex, int id,int score) :Person(name, sex, id)
	{
		m_Score = score;
	}
	void Student_display()
	{
		cout << "Student Informain:  ";
		Person_display();
		cout << " " << m_Score << endl;
	}
	~Student() 
	{
		cout << "Object has been detele" << endl;
	}
};
class Teacher :Person
{
private: int m_Age;
public:
	Teacher() {}
	Teacher(string name, string sex, int id,int age):Person(name, sex, id)
	{
		m_Age = age;
	}
	void Teacher_display()
	{
		cout << "Teacher Informain:  ";
		Person_display();
		cout << " " << m_Age << endl;
	}
	~Teacher()
	{
		cout << "Object has been detele" << endl;
	}
};
int main()
{
	Student student("刘 ", " 男",5067, 120);
	student.Student_display();
	Teacher teacher("刘老师", "男 ", 674, 45); 
	teacher.Teacher_display();
	return 0;
}

(2)

#include "iostream"
#include  "string"

using namespace std;

class Shape
{
public:

	virtual double Get_Area()=0;
	virtual  void Set_Area()=0;
};
class Triangle :public Shape
{
private:double m_Len1,m_Len2, m_Len3;
public:	

	Triangle() {}
	Triangle(double len1,double len2,double len3)
	{
		m_Len1 = len1;
		m_Len2 = len2;
		m_Len3 = len3;	
	}
	~Triangle() {}
	virtual double Get_Area()
	{	
		double P;
		double S;
		P = (m_Len1 + m_Len2 + m_Len3) / 2;
		S = sqrt(P*(P - m_Len1)*(P - m_Len2)*(P - m_Len3));
		return S;
	}
	virtual void  Set_Area()
	{
		cout << "输入三角形三边: ";
			cin >> m_Len1;
			cin >> m_Len2;
			cin>> m_Len3;
	}
};

class Rect :public Shape
{
private: double m_Len;
		 double m_Wid;
public: 
	
	Rect() {}
	Rect(double len,double wid)
	{
		m_Len = len;
		m_Wid = wid;
	
	}
	virtual double Get_Area()
	{	
		double S; 
		S = m_Len * m_Wid;
		return S;
	}
	virtual void  Set_Area()
	{
		cout << "输入矩形的长,宽: ";
		cin >> m_Len;
		cin >> m_Wid;
	}
};

class Circle :public Shape
{
private: double m_R;
public:
	double S;
	 Circle(){}
	  Circle(double r)
		  {
			  m_R = r;
		  }
	  ~Circle()
	  {}
	  virtual void  Set_Area()
	  {
		  cout << "输入圆的半径: ";
		  cin >> m_R;
	  }
	  virtual double Get_Area()
	  { 
		  S =m_R * m_R*3.14;
		  return S;
	  }
};
class Total_Area : public Triangle,public Rect,public Circle
{
public:
	double S_ALL;
	Total_Area() {}
	~Total_Area() 
	{
	
	}
	double ALL_Area(Shape *p_Triangle,Shape *p_Rect,Shape *p_Circle)
	{	
		
		S_ALL = p_Triangle->Get_Area() + p_Rect->Get_Area() + p_Circle->Get_Area();
		return S_ALL;
	}
};

int main()
{
	Triangle triangle;
	Rect rect;
	Circle circle;
	Total_Area total_area;
	triangle.Set_Area();
	rect.Set_Area();
	circle.Set_Area();
	cout << "Triangle_S= " << triangle.Get_Area() << endl;
	cout << "Rect_S=     " << rect.Get_Area() << endl;
	cout << "Circle_S=   " << circle.Get_Area() << endl;
	cout << "Total_Area= " << total_area.ALL_Area(&triangle, &rect, &circle) << endl;
	return 0;
}

(3)

#include "iostream"
#include "string"
using namespace std;

class Person
{
private: string m_Name;
		 string m_Sex;
		 int m_Age;
public:
	Person() {}
	Person(string name, string sex, int age)
	{
		m_Name = name;
		m_Sex = sex;
		m_Age = age;
	}
	string Person_Get_Name()
	{
		return m_Name;
	}
	string Person_Get_Sex()
	{
		return m_Sex;
	}
	int Person_Get_Age()
	{
		return m_Age;
	}
};

class Student :virtual public Person
{
private: string m_Speciality;
public:
	Student() {}
	Student(string name, string sex, int age,string speciality):Person(name,sex,age)
	{
		m_Speciality = speciality;
	}
	~Student() 
	{

	}
	string Student_Get_Speciality()
	{
		return m_Speciality;
	}
	void Stu_print()
	{
		cout << "Student Information: " << Person_Get_Name() << " " << Person_Get_Sex() << " " << Person_Get_Age() << " " << m_Speciality << endl;
	}
};
class Teacher :virtual public Person
{
private: string m_Department;
public:
	Teacher() {}
	Teacher(string name, string sex, int age, string department) :Person(name, sex, age)
	{
		m_Department = department;
	}
	~Teacher()
	{
		
	}
	string Teacher_Get_Department()
	{
		return  m_Department;
	}
	void Tea_print()
	{
		cout << "Teacher Information: " << Person_Get_Name() << " " << Person_Get_Sex ()<< " " << Person_Get_Age() << " " << m_Department << endl;
	}
};
class Professor :virtual public Person
{
private: string m_Position;
public:
	Professor() {}
	Professor(string name, string sex, int age, string position) :Person(name, sex, age)
	{
		m_Position = position;
	}
	~Professor()
	{
		
	}
	void Pro_print()
	{ 
		cout << "Position Information: " << Person_Get_Name() << " " << Person_Get_Sex() << " " << Person_Get_Age() << " " << m_Position << endl;
	}
};

class StuTeacher :public Student,public Teacher
{
	public:
	StuTeacher() {}
	StuTeacher(string name, string sex,int  age, string speciality,string  department):Person(name,sex,age),Student(name,sex,age, speciality),Teacher(name, sex, age, department)
	{
	}
	~StuTeacher()
	{

	}
	void Stu_Tea_print()
	{
		cout << "StuTeacher Information: " << Person_Get_Name() << " " << Person_Get_Sex() << " " << Person_Get_Age() << " " <<Student_Get_Speciality() <<" "<< Teacher_Get_Department()<< endl;
	}
};
int main()
{
	Student student(" 李", "男", 21, "物联网工程专业");
	student.Stu_print();
	Teacher teacher("王老师", "女", 38, "计算机机与信息与信息工程学院");
	teacher.Tea_print();
	Professor professor("李教授", "男", 68, "高级教授");
	professor.Pro_print();
	StuTeacher  stutracher("在读老师", "男", 46," 物联网工程专业", "计算机与信息工程学院");
	stutracher.Stu_Tea_print();
	return 0;
}
不足之处,请大佬们指导。

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页