实验四 派生类与继承(一)
实验目的和要求:
1.理解类的继承的概念,能够定义和使用类的继承关系。2.掌握派生类的声明与定义方法。 3.理解不同的继承类型
预习内容:
派生类的声明与定义;不同的继承类型的差异。
程序代码
/*myArray.h */
#ifndef MYARRAY_H_ #define MYARRAY_H_ class MyArray {
protected:
int *alist; //指向动态申请的一组空间 int length; //整数的个数 public:
MyArray(int leng); virtual ~MyArray(); void input(); void display(); }; #endif
/* reArray.h*/
#ifndef REARRAY_H_ #define REARRAY_H_ #include \
class ReArray:public MyArray {
public:
void reviseArray();
ReArray(int length):MyArray(length){} }; #endif
/* sortArray.h */
#ifndef SORTARRAY_H_ #define SORTARRAY_H_ #include \
class SortArray:public MyArray {
public:
void sortit();
SortArray(int length):MyArray(length){} }; #endif
/*MyArray.cpp*/ #include
MyArray::MyArray(int leng) {
length=leng;
alist=new int[length]; }
MyArray::~MyArray() {
delete []alist; }
void MyArray::input() {
cout<<\请输入数列:\ for(int i=0;i cin>>alist[i]; } } void MyArray::display() { cout<<\输出数列:\ for(int i=0;i cout< /*reArray.cpp*/ #include void ReArray::reviseArray() { int i,j, temp; for ( i=0; i if (alist[i] temp = alist[j]; alist[j] = alist[i]; alist[i] = temp; } } /* sortArray.cpp*/ #include void SortArray::sortit() { int i,j, temp; for ( i=0; i if (alist[j] temp = alist[j]; alist[j] = alist[i]; alist[i] = temp;3 } } 运行结果 实验总结 实验五 派生类与继承(二) 实验目的和要求: 1.理解类的继承的概念,能够定义和使用类的继承关系。 2.掌握派生类的声明与定义方法。 3.理解不同的继承类型 预习内容: 派生类的声明与定义;不同的继承类型的差异。 程序代码 /* Building.h */ #ifndef BUILDING_H_ #define BUILDING_H_ #include class Building { protected: string name; int floors; double areas; public: Building(string name1,int fl,double areas1); void print(); }; #endif /* House.h */ #ifndef HOUSE_H_ #define HOUSE_H_ #include class House:public Building { private: int rooms; int balcony; public: House(string name,int floors,double areas,int rooms,int balcony); void print(); }; #endif /* Office.h */ #ifndef OFFICE_H_ #define OFFICE_H_