资源描述
实验内容:
(1) 定义一个人员类Person,包括姓名、编号、性别等数据成员和用于输入、输出的成员函数,在此基础上派生出学生类Student类(增加成绩)和教师类Teacher(增加教龄),并实现对学生和教师信息的输入和输出。
实验源代码:
#include<iostream> //编译预处理命令
using namespace std; //使用命名空间std
#ifndef PERSON_H
#define PERSON_H
class Person //声明类Person
{
protected:
char name[18]; //姓名
int num; //编号
char sex[3]; //性别
public:
char na[18],se[3];
int nu;
Person(){} //构造函数
void Imput() //输入信息
{
cout<<"请输入姓名:";
cin>>na;
cout<<"请输入编号:";
cin>>nu;
cout<<"请输入性别:";
cin>>se;
}
void Set() //设置信息
{
strcpy(name,na);
num=nu;
strcpy(sex,se);
}
void Show() const; //显示信息函数定义声明
};
#endif
//Student.h:
#include<iostream> //编译预处理命令
using namespace std; //使用命名空间std
#ifndef STUDENT_H
#define STUDENT_H
class Student:public Person //声明类Student
{
protected:
double record; //成绩
public:
double re;
Student(){} //构造函数
void Imput()
{
cout<<"请输入学生的信息:"<<endl;
Person::Imput(); //调用基类成员函数Imput()
cout<<"请输入成绩:";
cin>>re;
}
void Set()
{
Person::Set(); //调用基类成员函数Set()
record=re;
}
void Show() const;
};
#endif
//Teacher.h:
#include<iostream> //编译预处理命令
using namespace std; //使用命名空间std
#ifndef TEACHER_H
#define TEACHER_H
class Teacher:public Person
{
protected:
int age; //教龄
public:
int ag;
Teacher(){} //构造函数
void Imput()
{
cout<<endl<<"请输入教师的信息:"<<endl;
Person::Imput(); //调用基类成员函数Imput()
cout<<"请输入教龄:";
cin>>ag;
}
void Set()
{
Person::Set(); //调用基类成员函数Set()
age=ag;
}
void Show() const;
};
#endif
//Person.cpp:
#include"Person.h"
void Person::Show() const //显示信息函数定义
{
cout<<"姓名:"<<name<<endl;
cout<<"编号:"<<num<<endl;
cout<<"性别:"<<sex<<endl;
}
//Student.cpp:
#include"Person.h"
#include"Student.h"
void Student::Show() const
{
cout<<endl<<"学生的信息为:"<<endl;
Person::Show(); //调用基类成员函数Show()
cout<<"成绩:"<<record<<endl<<endl;
}
//Teacher.cpp:
#include"Person.h"
#include"Teacher.h"
void Teacher::Show() const
{
cout<<"教师的信息为:"<<endl;
Person::Show(); //调用基类成员函数Show()
cout<<"教龄:"<<age<<endl<<endl;
}
//main.cpp:
#include"Person.h"
#include"Student.h"
#include"Teacher.h"
int main()
{
Student s; //定义对象s
s.Imput(); //输入学生信息
s.Set(); //设置学生信息
Teacher t; //定义对象t
t.Imput(); //输入教师信息
t.Set(); //设置教师信息
s.Show(); //显示学生信息
t.Show(); //显示教师信息
system("PAUSE");
return 0;
}
实验结果:
(2) 定义一个抽象类Shape,包含纯虚函数Area()(用来计算面积)和SetShape()(用来重设形状大小)。然后派生出三角形类Triangle类、矩形Rect类、圆Circle类,分别求其面积。最后定义一个Total_Area类,计算这几个形状的面积之和,各形状的数据通过Total_Area类的构造函数或成员函数来设置。编写一个完整的程序。
实验源代码:
//Shape.h:
#ifndef SHAPE_H
#define SHAPE_H
class Shape //声明抽像类Shape
{
public:
virtual double Area()=0; //纯虚函数Area()
virtual void SetShape()=0; //纯虚函数SetShape()
};
#endif
//Triangle.h:
#ifndef TRIANGLE_H
#define TRIANGLE_H
#include"Shape.h"
class Triangle:public Shape //声明三角形类Triangle
{
private:
double E; //底
double H; //高
public:
Triangle(){} //参数为空的构造函数
Triangle(double e,double h):E(e),H(h){}
double Area(){return 0.5*E*H;} //返回三角形面积
void SetShape(); //重设三角形的大小
};
#endif
//Rect.h:
#ifndef RECT_H
#define RECT_H
#include"Shape.h"
class Rect:public Shape //声明矩形类Rect
{
private:
double L; //长
double W; //宽
public:
Rect(){} //参数为空的构造函数
Rect(double l,double w):L(l),W(w){}
double Area(){return L*W;} //返回矩形面积
void SetShape(); //重设矩形的大小
};
#endif
//Circle.h:
#ifndef CIRCLE_H
#define CIRCLE_H
#include"Shape.h"
class Circle:public Shape //声明圆类Circle
{
private:
double R; //半径
public:
Circle(){} //参数为空的构造函数
Circle(double r):R(r){}
double Area(){return 3.1415926*R*R;} //返回圆的面积
void SetShape(); //重设圆的大小
};
#endif
//Total_Area.h:
#include<iostream> //编译预处理命令
#include"Triangle.h"
#include"Rect.h"
#include"Circle.h"
using namespace std; //使用命名空间
#ifndef TOTAL_AREA_H
#define TOTAL_AREA_H
class Total_Area //声明类Total_Area
{
private:
Shape *a[3]; //定义抽象类Shape类型的数组指针
public:
Total_Area(double Te=0,double Th=0,double Rl=0,double Rw=0,double Cr=0)
//分配空间
{
a[0]=new Triangle(Te,Th);
a[1]=new Rect(Rl,Rw);
a[2]=new Circle(Cr);
}
~Total_Area() //析构函数
{
delete a[0];
delete a[1];
delete a[2];
}
void SetShape() //设置形状的大小
{
a[0]->SetShape();
a[1]->SetShape();
a[2]->SetShape();
}
double GetTriangle(){return a[0]->Area();}
double GetRect(){return a[1]->Area();}
double GetCircle(){return a[2]->Area();}
double GetArea(); //返回三个形状的面积和
};
#endif
//Triangle.cpp:
#include"Triangle.h"
#include<iostream> //编译预处理命令
using namespace std; //使用命名空间
void Triangle::SetShape()
{
cout<<"请输入重设的三角形的底和高:"<<endl;
cin>>E>>H;
}
//Rect.cpp:
#include"Rect.h"
#include<iostream> //编译预处理命令
using namespace std; //使用命名空间
void Rect::SetShape()
{
cout<<"请输入重设的矩形的长和宽:"<<endl;
cin>>L>>W;
}
//Circle.cpp:
#include"Circle.h"
#include<iostream> //编译预处理命令
using namespace std; //使用命名空间
void Circle::SetShape()
{
cout<<"请输入重设的圆的半径:"<<endl;
cin>>R;
}
//Total_Area.cpp:
#include"Total_Area.h"
double Total_Area::GetArea()
{
return a[0]->Area()+a[1]->Area()+a[2]->Area();
}
//main.cpp:
#include<iostream> //编译预处理命令
#include"Shape.h"
#include"Triangle.h"
#include"Rect.h"
#include"Circle.h"
#include"Total_Area.h"
using namespace std; //使用命名空间
int main() //主函数main()
{
cout<<"输入的三角形的底和高,矩形的长和宽,圆的半径为:2和4,3和5,6"<<endl;
Total_Area s(2,4,3,5,6); //定义对象s
cout<<"这个三角形的面积为:"<<s.GetTriangle()<<endl;
cout<<"这个矩形的面积为:"<<s.GetRect()<<endl;
cout<<"这个圆的面积为:"<<s.GetCircle()<<endl;
cout<<"这三个形状面积之和为:"<<s.GetArea()<<endl<<endl;
s.SetShape(); //调用重设形状大小函数
cout<<"重设的三角形的面积为:"<<s.GetTriangle()<<endl;
cout<<"重设的矩形的面积为:"<<s.GetRect()<<endl;
cout<<"重设的圆的面积为:"<<s.GetCircle()<<endl;
cout<<"重设的三个形状面积之和为:"<<s.GetArea()<<endl;
system("PAUSE");
return 0;
}
实验结果:
展开阅读全文