本科实验报告
课程名称:C++面向对象程序设计
实验项目:
实验地点:明向校区
专业班级:软件1419 学号:2014006061 学生姓名:刘国鑫
指导教师:王丽娟
2015年5月10日
2.#include
using namespace std;
class Rectangle
{ public:
Rectangle(double l,double w):len(l),wid(w){} double area()
{
double area;area=len*wid;return area;
}
private:
double len,wid;
};
class Square
{ public:
Square(double l):len(l){}
double area()
{
double area;area=len*len;return area;
}
private:
double len;
};
class Circle
{ public:
Circle(double r):radius(r){}
double area()
{
3.#include"iostream"
using namespace std;
class Complex
{
private:
float Real,Image;
public:
Complex(float r,float i)
{ Real=r;Image=i; }
Complex(Complex &c)
{ Real=c.Real;Image=c.Image; }
Complex()
{ Real=0;Image=0; }
void Display()
{
cout< } }; void main() { Complex c1(20,40),c2,c3(c1); c1.Display(); c2.Display(); c3.Display(); system("pause"); } 运行结果: 4.#include #include using namespace std; class Rectangle { public: Rectangle(float l, float t, float r, float b) :left(l), top(t), right(r), bottom(b) {} float Diagonal() { return sqrt((top - bottom) * (top - bottom) + (left - right) * (left- right)); } void Show(Rectangle rec) { cout << rec.Diagonal()< ~Rectangle(){} private: float left; float top; float right; float bottom; }; int main() { Rectangle *re = new Rectangle(10, 10, 20, 20); re->Show(*re); delete re; system("pause"); } 运行结果: