{ String t; t.length=a1.length+a2.length; t.sp=new char[t.length+1]; strcpy(t.sp,a1.sp); strcat(t.sp,a2.sp); return t; } }; void main() { String s1(\ s1.show(); s2.show(); s2+=s1; s2.show(); } 实验结果:
实验分析: 在类中定义一个构造函数与析构函数,及一个重载函数; 重载函数中定义一个string类,先用字符串拷贝函数将s1拷贝到新定义的字符串中,再用字符串连接函数将s2连接到新字符串后。返回新定义的字符串。 五、编写程序,定义抽象基类Shape(形状),由它派生出3个派生类: Circle(圆形)、Rectangle(矩形)和Square (正方形),用函数函数ShowArea()分别显示各种图形的面积,最后还要显示所有图形的总面积。要求用基类指针数组,使它的每一个元素指向一一个派生类对象。 #include
double S; S=3.14*radium*radium; return S; } private: double radium; }; void Circle::showArea() { cout<<\图形为圆形\ cout<<\图形面积为:\} class Rectangle:public Shape { public: Rectangle(double m,double n){length=m;wide=n;} void showArea(); double Area() { double S; S=length*wide; return S;
} private: double length,wide; }; void Rectangle::showArea() { cout<<\图形为矩形\ cout<<\图形面积为:\} class Square:public Shape { public: Square(double i){length=i;} void showArea(); double Area() { double S; S=length*length; return S; } private: double length;
}; void Square::showArea() { cout<<\图形为正方形\ cout<<\图形面积为:\} struct node { Shape *data; node *next; }; int main() { Circle circle(4); Rectangle rect(3,4); Square square(5); Shape *p1,*p2,*p3,*p4; p2=&circle; p3=▭ p4=□ node *first,*q,*t; first=new node;
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库c++多态性实验报告(6)在线全文阅读。
相关推荐: