//Myarray类的声明文件MyArray.h
class Myarray
{
public:
int *alist;
int length;
Myarray( int leng);
~Myarray();
void input();
void display();
};
//Myarray类的实现文件MyArray.cpp
#include<iostream.h>
#include "MyArray.h"
Myarray::Myarray(int leng)
{
alist=new int[leng];
length=leng;
}
Myarray::~Myarray()
{
delete [] alist;
}
void Myarray::input()
{
for(int i=0;i<length;i++)
cin>>alist[i];
}
void Myarray::display()
{
for(int i = 0;i < length;i++)
cout<<alist[i]<<" ";
}
//AverArray类的声明文件AverArray.h
#ifndef MYARRAY
#define MYARRAR 0
#include "MyArray.h"
#endif
class AverArray:virtual public Myarray{
public:
AverArray(int leng);
float aver();
};
//AverArray类的实现文件AverArray.cpp
#include "AverArray.h"
#include<iostream.h>
AverArray::AverArray(int leng):Myarray(leng)
{
}
float AverArray::aver()
{
int s=0;
for(int i=0;i<length;i++)
s=s+alist[i];
return (float (s)/length);
}
//RevArray类的声明文件RevArray.h
#ifndef MYARRAY
#define MYARRAY 0
#include "MyArray.h"
#endif
class RevArray:virtual public Myarray{
public:
RevArray(int leng);
void ob_array();
};
//RevArray类的实现文件RevArray.cpp
#include <iostream.h>
#include "RevArray.h"
RevArray::RevArray(int leng):Myarray(leng){}
void RevArray::ob_array()
{
for(int i=length-1;i>=0;i--)
cout<<alist[i]<<" ";
}
//NawArray类的声明及实现NawArray.h
#ifndef AVERARRAY
#define AVERARRAY 0
#include "AverArray.h"
#endif
#ifndef REVARRAY
#define REVARRAY 0
#include "RevArray.h"
#endif
class NawArray:public AverArray,public RevArray
{
public:
NawArray(int leng):Myarray(leng),AverArray(leng),RevArray(leng){}
};
//主文件Array.cpp
#include<iostream.h>
#ifndef MYARRAY
#define MYARRAY 0
#include "MyArray.h"
#endif
#ifndef AVERARRAY
#define AVERARRAY 0
#include "AverArray.h"
#endif
#ifndef REVARRAY
#define REVARRAY 0
#include "RevArray.h"
#endif
#include "NawArray.h"
const int n=5;
int main()
{ //operate class Myarray
Myarray array1(n);
cout<<"input array1:";
array1.input();
cout<<"array1:";
array1.display();
cout<<endl;
//operate class AverArray
AverArray array2(n);
cout<<"input array2:";
array2.input();
cout<<"array2:";
array2.display();
cout<<endl<<"array2 aver=";
cout<<array2.aver();
cout<<endl;
//operate class RevArray
RevArray array3(n);
cout<<"input array3:";
array3.input();
cout<<"array3:";
array3.display();
cout<<endl<<"opsition array3:";
array3.ob_array();
cout<<endl;
//operate class NawArray
NawArray array4(n);
cout<<"input array4:";
array4.input();
cout<<"array4:";
array4.display();
cout<<endl<<"opsition array4:";
array4.ob_array();
cout<<endl<<"array4 aver=";
cout<<array4.aver();
cout<<endl;
return 0;
}
[此贴子已经被作者于2006-3-16 22:23:54编辑过]