目录
实现一个图形类(Shape),包含受保护成员属性:周长、面积,
定义一个圆形类(Circle),继承自图形类,包含私有属性:半径
定义一个矩形类(Rect),继承自图形类,包含私有属性:长度、宽度
在主函数中,分别实例化圆形类对象以及矩形类对象,并测试相关的成员函数。
实现一个图形类(Shape),包含受保护成员属性:周长、面积,
公共成员函数:特殊成员函数书写
定义一个圆形类(Circle),继承自图形类,包含私有属性:半径
公共成员函数:特殊成员函数、以及获取周长、获取面积函数
定义一个矩形类(Rect),继承自图形类,包含私有属性:长度、宽度
公共成员函数:特殊成员函数、以及获取周长、获取面积函数
在主函数中,分别实例化圆形类对象以及矩形类对象,并测试相关的成员函数。
代码
#include <iostream>
using namespace std;
#define PI 3.14
class Shape
{
protected:
double circumference;
double area;
public:
//无参构造
Shape(){}
//有参构造
Shape(double c,double a):circumference(c),area(a){}
//拷贝构造
Shape(const Shape &s):circumference(s.circumference),area(s.area){}
//移动构造
Shape(Shape &&s):circumference(s.circumference),area(s.area){}
//拷贝赋值
Shape &operator=(const Shape &s){
circumference = s.circumference;
area = s.area;
return *this;
}
//移动赋值
Shape &operator=(Shape &&s){
circumference = s.circumference;
area = s.area;
return *this;
}
//析构函数
~Shape(){}
};
class Circle:public Shape
{
private:
int radius;
public:
//无参构造
Circle ():Shape(2*PI,PI),radius(1){}
//有参构造
Circle (int r):Shape(2*PI*r,PI*r*r),radius(r){}
//拷贝构造
Circle(const Circle &c):Shape(c.circumference,c.area),radius(c.radius){}
//移动构造
Circle(Circle &&c):Shape(c.circumference,c.area),radius(c.radius){}
//拷贝赋值
Circle &operator=(const Circle &c){
circumference = c.circumference;
area = c.area;
radius = c.radius;
return *this;
}
//移动赋值
Circle &operator=(Circle &&c){
circumference = c.circumference;
area = c.area;
radius = c.radius;
return *this;
}
//析构函数
~Circle(){}
//外部获取改变参数
Circle &argch(int r){
circumference = 2*PI*r;
area = PI*r*r;
radius = r;
return *this;
}
//获取周长函数
double circumference_get(){
//cout<<"周长为:";
return circumference;
}
//获取面积函数
double area_get(){
//cout<<"面积为:";
return area;
}
};
class Rect:public Shape
{
private:
int length;
int width;
public:
//无参构造
Rect ():Shape(2*(1+1),1*1),length(1),width(1){}
//有参构造
Rect (int l,int w):Shape(2*(l+w),l*w),length(l),width(w){}
//拷贝构造
Rect(const Rect &r):Shape(r.circumference,r.area),length(r.length),width(r.width){}
//移动构造
Rect(Rect &&r):Shape(r.circumference,r.area),length(r.length),width(r.width){}
//拷贝赋值
Rect &operator=(const Rect &r){
circumference = r.circumference;
area = r.area;
length = r.length;
width = r.width;
return *this;
}
//移动赋值
Rect &operator=(Rect &&r){
circumference = r.circumference;
area = r.area;
length = r.length;
width = r.width;
return *this;
}
//析构函数
~Rect(){}
//外部获取改变参数
Rect &argch(int l,int w){
circumference = 2*(l+w);
area = l*w;
length = l;
width = w;
return *this;
}
//获取周长函数
double circumference_get(){
//cout<<"周长为:";
return circumference;
}
//获取面积函数
double area_get(){
//cout<<"面积为:";
return area;
}
};
int main()
{
cout<<"****************以下为Circle测试**************"<<endl;
Circle c1(5);
cout<<"c1:"<<"面积为:"<<c1.area_get()<<" 周长为:"<<c1.circumference_get()<<endl;
Circle c2;
cout<<"c2:"<<"面积为:"<<c2.area_get()<<" 周长为:"<<c2.circumference_get()<<endl;
c2 = c1;
cout<<"c2:"<<"面积为:"<<c2.area_get()<<" 周长为:"<<c2.circumference_get()<<endl;
c1.argch(7);
cout<<"c1:"<<"面积为:"<<c1.area_get()<<" 周长为:"<<c1.circumference_get()<<endl;
Circle c3(c1);
cout<<"c3:"<<"面积为:"<<c3.area_get()<<" 周长为:"<<c3.circumference_get()<<endl;
cout<<"*****************以下为Rect测试***************"<<endl;
Rect r1(3,4);
cout<<"r1:"<<"面积为:"<<r1.area_get()<<" 周长为:"<<r1.circumference_get()<<endl;
Rect r2;
cout<<"r2:"<<"面积为:"<<r2.area_get()<<" 周长为:"<<r2.circumference_get()<<endl;
r2 = r1;
cout<<"r2:"<<"面积为:"<<r2.area_get()<<" 周长为:"<<r2.circumference_get()<<endl;
r1.argch(5,6);
cout<<"r1:"<<"面积为:"<<r1.area_get()<<" 周长为:"<<r1.circumference_get()<<endl;
Rect r3(r1);
cout<<"r3:"<<"面积为:"<<r3.area_get()<<" 周长为:"<<r3.circumference_get()<<endl;
return 0;
}