主题:[讨论]急,改错
#include <iostream>
#include <string>
class Shape // 形状类
{
static int m_total; // 形状的总数
int m_id; // 形状的序号
double m_area; // 形状的面积
string m_type; // 形状的类型
public:
Shape()
{
m_id = m_total++;
cout << "Create Shape " << m_id << endl;
}
Shape( Shape &s )
{
m_id = m_total++;
m_type = s.m_type;
m_area = s.m_area;
cout << "Copy Shape " << m_id << " from " << s.m_id << endl;
}
~Shape() { cout << "Destroy Shape " << m_id << endl; }
virtual double Area() = 0; // 计算形状的面积
virtual void SetSize( int x = 0, int y = 0 ) = 0; // 改变形状的尺寸
bool operator == ( Shape s ) { return m_type == s.m_type; } // 判断两个形状是否是同一类型
};
int Shape::m_total = 0;
class Circle: public Shape // 圆类
{
static int m_total; // 圆的总数
int m_id; // 圆的序号
int m_r; // 圆的半径
public:
Circle( int r )
{
m_type = "Circle";
m_id = m_total++;
m_r = r;
cout << "Create Circle " << m_id << endl;
}
Circle( Circle &s ): Shape( s )
{
m_id = m_total++;
m_r = s.m_r;
cout << "Copy Circle " << m_id << " from " << s.m_id << endl;
}
~Circle() { cout << "Destroy Circle " << m_id << endl; }
double Area() // 计算圆的面积
{
m_area = 3.14 * m_r * m_r;
cout << "Circle " << m_id << " Area = " << m_area << endl;
return m_area;
}
void SetSize( int r = 0 ) { m_r = r; }
Circle & operator = ( Circle &s )
{
m_type == s.m_type;
m_area = s.m_area;
m_r = s.m_r;
return *this;
}
};
int Circle::m_total = 0;
class Rectangle: public Shape // 矩形类
{
static int m_total; // 矩形的总数
int m_id; // 矩形的序号
int m_w; // 矩形的宽度
int m_h; // 矩形的高度
public:
Rectangle( int w, int h )
{
m_type = "Rectangle";
m_id = m_total++;
m_w = w;
m_h = h;
cout << "Create Rectangle " << m_id << endl;
}
Rectangle( Rectangle &s ): Shape( s )
{
m_id = m_total++;
m_w = s.m_w;
m_h = s.m_h;
cout << "Copy Rectangle " << m_id << " from " << s.m_id << endl;
}
~Rectangle(){ cout << "Destroy Rectangle " << m_id << endl; }
double Area() // 计算矩形的面积
{
m_area = m_w * m_h;
cout << "Rectangle " << m_id << " Area = " << m_area << endl;
return m_area;
}
void SetSize( int w = 0, int h = 0 ) { m_w = w; m_h = h; }
Rectangle & operator = ( Rectangle &s )
{
m_type == s.m_type;
m_area = s.m_area;
m_w = s.m_w;
m_h = s.m_h;
return *this;
}
};
int Rectangle::m_total = 0;
class Square: public Rectangle // 正方形类
{
static int m_total; // 正方形的总数
int m_id; // 正方形的序号
public:
Square( int len )
{
m_type = "Square";
m_id = m_total++;
m_w = m_h = len;
cout << "Create Square " << m_id << endl;
}
Square( Square &s ): Rectangle( s )
{
m_id = m_total++;
cout << "Copy Square " << m_id << " from " << s.m_id << endl;
}
~Square() { cout << "Destroy Square " << m_id << endl; }
double Area() // 计算正方形的面积
{
m_area = m_w * m_h;
cout << "Square " << m_id << " Area = " << m_area << endl;
return m_area;
}
void SetSize( int len = 0 ) { m_w = m_h = len; }
Square & operator = ( Square &s )
{
m_type == s.m_type;
m_area = s.m_area;
m_w = s.m_w;
m_h = s.m_h;
return *this;
}
}
int Square::m_total = 0;
int main()
{
Circle a( 10 );
Rectangle b( 4, 5 ), c( b );
Square d[2];
Shape *p = &a;
p->Area();
p = &b;
p->Area();
p = d;
p[0].SetSize( 10 ); // 改变第一个正方形尺寸
d[0].Area();
p[1].SetSize( 5 ); // 改变第二个正方形尺寸
d[1].Area();
c = d[1];
c.Area();
if( b == c ) cout << "Same!" << endl;
else cout << "Not Same!" << endl;
return 0;
}
#include <string>
class Shape // 形状类
{
static int m_total; // 形状的总数
int m_id; // 形状的序号
double m_area; // 形状的面积
string m_type; // 形状的类型
public:
Shape()
{
m_id = m_total++;
cout << "Create Shape " << m_id << endl;
}
Shape( Shape &s )
{
m_id = m_total++;
m_type = s.m_type;
m_area = s.m_area;
cout << "Copy Shape " << m_id << " from " << s.m_id << endl;
}
~Shape() { cout << "Destroy Shape " << m_id << endl; }
virtual double Area() = 0; // 计算形状的面积
virtual void SetSize( int x = 0, int y = 0 ) = 0; // 改变形状的尺寸
bool operator == ( Shape s ) { return m_type == s.m_type; } // 判断两个形状是否是同一类型
};
int Shape::m_total = 0;
class Circle: public Shape // 圆类
{
static int m_total; // 圆的总数
int m_id; // 圆的序号
int m_r; // 圆的半径
public:
Circle( int r )
{
m_type = "Circle";
m_id = m_total++;
m_r = r;
cout << "Create Circle " << m_id << endl;
}
Circle( Circle &s ): Shape( s )
{
m_id = m_total++;
m_r = s.m_r;
cout << "Copy Circle " << m_id << " from " << s.m_id << endl;
}
~Circle() { cout << "Destroy Circle " << m_id << endl; }
double Area() // 计算圆的面积
{
m_area = 3.14 * m_r * m_r;
cout << "Circle " << m_id << " Area = " << m_area << endl;
return m_area;
}
void SetSize( int r = 0 ) { m_r = r; }
Circle & operator = ( Circle &s )
{
m_type == s.m_type;
m_area = s.m_area;
m_r = s.m_r;
return *this;
}
};
int Circle::m_total = 0;
class Rectangle: public Shape // 矩形类
{
static int m_total; // 矩形的总数
int m_id; // 矩形的序号
int m_w; // 矩形的宽度
int m_h; // 矩形的高度
public:
Rectangle( int w, int h )
{
m_type = "Rectangle";
m_id = m_total++;
m_w = w;
m_h = h;
cout << "Create Rectangle " << m_id << endl;
}
Rectangle( Rectangle &s ): Shape( s )
{
m_id = m_total++;
m_w = s.m_w;
m_h = s.m_h;
cout << "Copy Rectangle " << m_id << " from " << s.m_id << endl;
}
~Rectangle(){ cout << "Destroy Rectangle " << m_id << endl; }
double Area() // 计算矩形的面积
{
m_area = m_w * m_h;
cout << "Rectangle " << m_id << " Area = " << m_area << endl;
return m_area;
}
void SetSize( int w = 0, int h = 0 ) { m_w = w; m_h = h; }
Rectangle & operator = ( Rectangle &s )
{
m_type == s.m_type;
m_area = s.m_area;
m_w = s.m_w;
m_h = s.m_h;
return *this;
}
};
int Rectangle::m_total = 0;
class Square: public Rectangle // 正方形类
{
static int m_total; // 正方形的总数
int m_id; // 正方形的序号
public:
Square( int len )
{
m_type = "Square";
m_id = m_total++;
m_w = m_h = len;
cout << "Create Square " << m_id << endl;
}
Square( Square &s ): Rectangle( s )
{
m_id = m_total++;
cout << "Copy Square " << m_id << " from " << s.m_id << endl;
}
~Square() { cout << "Destroy Square " << m_id << endl; }
double Area() // 计算正方形的面积
{
m_area = m_w * m_h;
cout << "Square " << m_id << " Area = " << m_area << endl;
return m_area;
}
void SetSize( int len = 0 ) { m_w = m_h = len; }
Square & operator = ( Square &s )
{
m_type == s.m_type;
m_area = s.m_area;
m_w = s.m_w;
m_h = s.m_h;
return *this;
}
}
int Square::m_total = 0;
int main()
{
Circle a( 10 );
Rectangle b( 4, 5 ), c( b );
Square d[2];
Shape *p = &a;
p->Area();
p = &b;
p->Area();
p = d;
p[0].SetSize( 10 ); // 改变第一个正方形尺寸
d[0].Area();
p[1].SetSize( 5 ); // 改变第二个正方形尺寸
d[1].Area();
c = d[1];
c.Area();
if( b == c ) cout << "Same!" << endl;
else cout << "Not Same!" << endl;
return 0;
}