return 0;}
int MAXI(int a,int b){ return a>b?a:b;
}
double MAXI(double a,double b){
return a>b?a:b;}
int MAXI(int a,int b,int c){ int max;
max=a>b?a:b; max=max>c?max:c;
return max;}
double MAXI(double a,double b,double c){ double max; max=a>b?a:b;
max=max>c?max:c; return max; }
二、实验内容和原理
(1)声明一个基类BaseClass,有整数类型成员变量Number,构造基派生类DerivedClass,实现其构造函数和析构函数,完善类的功能与结构。 (2)声明一个基类SHAPE,在此基础上派生出Rectangle和Circle,二者都有GetArea()函数计算对象的面积。使用Rectangle类创建一个派生类Square;完善类的功能与结构。 三、主要仪器设备 计算机
Visual Studio 2008 四、操作方法与实验步骤
// 实验一.cpp : #include \"stdafx.h\" #include \"iostream\" using namespace std; class base{ private: int Number; public:
base(int i):Number(i){ cout<<\"base construction.....\"<~base(){cout<<\"base destruction...\"<} };class derive: public base{ private: int no; public:
derive(int n):no(n),base(0){cout<<\"derive construction...\"<~derive(){cout<<\"derive destruction ....\"<int _tmain(int argc, _TCHAR* argv[]) { base b(90); b.print(); derive d1(23);d1.print(); derive d2(12);
d2.print(); return 0; }
void base::print(){
cout<<\"base 的值为\"<cout<<\"derive 的值为\"<// 实验二.cpp : #include \"stdafx.h\" #include\"iostream\" using namespace std; class SHAPE{public: double GetArea(){return 0;} };
class Rectangle:virtual public SHAPE { private :
double Length , width; public: Rectangle(double l,double w):Length(l),width(w){ };
void print(){cout<<\"长方形的长和宽为\"<};double GetArea(){return Length*width;}
class Circle:virtual public SHAPE { private :
double Radius; public: };
class Square:virtual public Rectangle{ private:
double len; public: Square(double l):Rectangle(0,0),len(l){} void print(){cout<<\"正方形的边长为\"<int _tmain(int argc, _TCHAR* argv[]) { Rectangle r(3,4); r.print();cout<<\"长方形的面积\"<cout<<\"圆的面积\"<s.print();cout<<\"正方形的面积\"<}double GetArea(){return len*len;}
Circle(double r):Radius(r){ };
void print(){cout<<\"圆的半径为\"<return 0; }二、实验内容和原理
(1)编写一个抽象类SHAPE,在此基础上派生出Rectangle和Circle,二者都有GetArea()函数计算对象的面积。计算周长的函数GetPerim();完善类的功能与结构。
(2)声明一个车(Vehicle)基类,有Run、Stop等成员函数,由此派生出自行车(Bicycle)类、汽车(Motorcar)类,从(Bicycle)和(Motorcar)派生出摩托车(Motorcycle)类,它们都有Run、Stop等成员函数。利用虚函数解决问题。 三、主要仪器设备 计算机
Visual Studio 2008 四、实验步骤
// 实验一.cpp : #include \"stdafx.h\" #include\"iostream\" using namespace std;
class SHAPE{ public: virtual double GetArea() const=0{return 0;} };
class Rectangle:virtual public SHAPE { private :
double Length , width;
public: Rectangle(double l,double w):Length(l),width(w){ };
void print(){cout<<\"长方形的长和宽为\"<class Circle:virtual public SHAPE { private : double Radius;public: Circle(double r):Radius(r){ };
void print(){cout<<\"圆的半径为\"<double GetArea() const{return Radius*Radius*3.14;} double GetPerim(){return 2*3.14*Radius;}};
int _tmain(int argc, _TCHAR* argv[]) {Rectangle r(3,4); r.print();
cout<<\"长方形的面积\"<cout<<\"长方形的周长为\"<c.print();cout<<\"圆的面积\"<return 0; }// 实验二.cpp : //
#include \"stdafx.h\" #include \"iostream\" using namespace std; class Vehicle{ public: };
class Bicycle:virtual public Vehicle{ public:
void Run(){cout<<\"Bicycle run\"<void Stop(){cout<<\"Bicycle stop\"<class Motorcar: virtual public Vehicle{ public:void Run(){cout<<\"Motorcar run\"<public: void Run(){cout<<\"Motorcycle run\"<void Stop(){cout<<\"Motorcycle stop\"<int _tmain(int argc, _TCHAR* argv[]) { Bicycle b;b.Run(); b.Stop(); Motorcar l;
l.Run(); l.Stop();
Motorcycle m; m.Run(); }
m.Stop(); return 0;