C++语言实验报告
班级:计科1501
学号:1508030130
姓名:刘琦
一、实验目的
1.掌握面向对象编程的基本思想
2.培养解决实际问题的能力
3.熟练掌握VC++6.0调试环境
二、实验题目
分别定义Teacher
(教师)类和Cadre
(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre
(教师兼干部)类。要求:
(1)在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。
(2)在Teacher
类中还包含数据成员title(职称),在Cadre
类中还包含数据成员post(职务)。在Teacher_Cadre
类中还包含数据成员wages(工资)。
(3)对两个基类中的姓名、年龄、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。
(4)在类体中声明成员函数,在类外定义成员函数。
(5)在派生类Teacher_Cadre
的成员函数show
中调用Teacher
类中的display
函数,输出姓名、年龄、性别、职称、地址、电话,然后再用cout
语句输出职务与工资。
(6)利用前面所学的知识,扩充新功能。
三、实验步骤
1.题目分析
要求一、二、四很容易实现;
第三个要求是对两个基类中相同的数据成员使用相同的名字,引用时指定作用域。因为要引用数据成员,而继承得来的基类的私有成员只能通过基类的成员函数来引用,不能通过任何其他方式引用。所以要想在派生类中引用基类的成员变量,一个方法是直接引用基类成员函数,但是这样的话需要额外构造基类成员函数;另一种方法是将基类的成员变量声明为保护(protected)。显然,后一种方法更加优秀。
要求五:①在成员函数show()中调用Teacher类的display()函数。因为是public继承,所以Teacher类中的public成员函数display()在其派生类Teacher_Cadre中依然是public成员函数。故在成员函数show()中可直接调用。又因为Teacher_Cadre的父类Cadre中也有public成员函数display(),所以在调用Teacher类的成员函数display()时需要加上作用域限定符Teacher::。
②使用cout输出职务与工资。子类Teacher_Cadre通过public继承了父类Teacher和Cadre的成员函数和成员变量,又因为两个父类的成员变量均为protected,故在类Teacher_Cadre中他们依旧是protected,可被Teacher_Cadre类的成员函数直接引用。所以直接使用cout输出即可。
要求六:可以尝试以此为基础构造一个教职工信息管理系统,分别用以上三个类声明数组来存储信息,再通过fstream头文件的应用来进行文件的读取与输出。
2.程序源代码
//
9.cpp -- homework
#include
<iostream>
#include
<string>
using
namespace std;
class
Teacher
{
public:
Teacher(){}
Teacher(string
na, int a, string se, string adr, long pn, string titl) :
name(na),
age(a),
sex(se),
address(adr),
phone_number(pn),
title(titl){}
void
set();
void
display();
protected:
string
name;
int
age;
string
sex;
string
address;
long
phone_number;
string
title;
};
class
Cadre
{
public:
Cadre(){}
Cadre(string
na, int a, string se, string adr, long pn, string pst) :
name(na),
age(a),
sex(se),
address(adr),
phone_number(pn),
post(pst){}
void
set();
void
display();
protected:
string
name;
int
age;
string
sex;
string
address;
long
phone_number;
string
post;
};
class
Teacher_Cadre : public Teacher, public Cadre
{
public:
Teacher_Cadre(){}
Teacher_Cadre(string
na, int a, string se, string adr, long pn, string titl, string pst,
float w) :
Teacher(na,
a, se, adr, pn, titl),
Cadre(na,
a, se, adr, pn, pst),
wages(w){}
void
show();
protected:
float
wages;
};
//
main function
int
main()
{
Teacher_Cadre
t("ql", 18, "female", "xiankejidaxue",
18300000000, "Student", "monitor", 180000);
t.show();
return
0;
}
//
class Teacher‘s member function
void
Teacher::set()
{
cout
<< "please input the data of the teacher: \n";
cout
<< "please input the name: ";
cin
>> name;
cout
<< "please input the age: ";
cin
>> age;
cout
<< "please input the sex: ";
cin
>> sex;
cout
<< "please input the address: ";
cin
>> address;
cout
<< "please input the phone number: ";
cin
>> phone_number;
cout
<< "please input the title: ";
cin
>> title;
}
void
Teacher::display()
{
//
cout << "The data of the teacher is: \n";
cout
<< "Name: " << name << endl;
cout
<< "Age: " << age << endl;
cout
<< "Sex: " << sex << endl;
cout
<< "Address: " << address << endl;
cout
<< "Phone Number: " << phone_number <<
endl;
cout
<< "Title: " << title << endl;
}
//
class Cadre‘s member function
void
Cadre::set()
{
cout
<< "please input the data of the cadre: \n";
cout
<< "please input the name: ";
cin
>> name;
cout
<< "please input the age: ";
cin
>> age;
cout
<< "please input the sex: ";
cin
>> sex;
cout
<< "please input the address: ";
cin
>> address;
cout
<< "please input the phone number: ";
cin
>> phone_number;
cout
<< "please input the post: ";
cin
>> post;
}
void
Cadre::display()
{
//
cout << "The data of the cadre is: \n";
cout
<< "Name: " << name << endl;
cout
<< "Age: " << age << endl;
cout
<< "Sex: " << sex << endl;
cout
<< "Address: " << address << endl;
cout
<< "Phone Number: " << phone_number <<
endl;
cout
<< "Post: " << post << endl;
}
//
// class Teacher_Cadre‘s member function
//
void Teacher_Cadre::set()
//
{
//
cout << "please input the data of the teacher_cadre: \n";
//
cout << "please input the name: ";
//
cin >> name;
//
cout << "please input the age: ";
//
cin >> age;
//
cout << "please input the sex: ";
//
cin >> sex;
//
cout << "please input the address: ";
//
cin >> address;
//
cout << "please input the phone number: ";
//
cin >> phone_number;
//
cout << "please input the title: ";
//
cin >> title;
//
cout << "please input the post: ";
//
cin >> post;
//
cout << "please input the wage: ";
//
cin >> wages;
//
}
void
Teacher_Cadre::show()
{
cout
<< "The data of the teacher_cadre is: \n";
Teacher::display();
cout
<< "Post: " << Cadre::post << endl;
cout
<< "Wages: " << wages << endl;
}
3.程序运行结果及分析
在gcc编译器下没有警告与错误,运行结果如下:
The
data of the teacher_cadre is:
Name:
ql
Age:
18
Sex:
female
Address:
xiankejidaxue
Phone
Number: 18300000000
Title:
Student
Post:
monitor
Wages:
180000