C++实验报告(1—8周)(3)

2019-03-03 10:46

实验四 派生类与继承(一)

实验目的和要求:

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 using namespace std; #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 using namespace std; #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 using namespace std; #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 using namespace std;

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 #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_


C++实验报告(1—8周)(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2013-2014学年新人教版八年级上物理期末试卷(附答案)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: