一.问题描述
请编写一个抽象类Shape,在此基础上派生出类Rectangle和Circle,二者都有计算对象面积的函数getArea()、计算对象周长的函数getPerim()。
在此基础上,通过继承Rectangle 得到一个新的类 Square,然后在 Shape 中增加一个函数int getVertexCount() const用来获得当前图形的顶点个数,用以下几种方法分别实现,并体会各自的优劣。
(1)使用dynamic_cast 实现 Shape::getVertexCount函数。
(2)使用typeid 实现Shape::getVertexCount 函数。
(3)将Shape::getVertexCount声明为纯虚函数,在派生类中给出具体实现。
二.设计思路
按照题目要求编写程序即可。
三.流程图
四.伪代码
1
五.代码实现
(1)
#include<iostream>
using namespace std;
#define PI 3.14
class shape {
public:
virtual void setvalues() = 0;
virtual float area() = 0;
virtual float perim() = 0;
int getVertexCount() const;
};
class rectangle :public shape {
protected:
float wide, high;
public:
virtual void setvalues() {
cin >> wide >> high;
if (wide < 0 || high < 0) {
cout << "Set Value Error!" << endl;
_exit(0);
}
}
virtual float area() {
float Area = wide * high;
return Area;
}
virtual float perim() {
float Perim = (wide + high) * 2;
return Perim;
}
};
class square :public rectangle {
public:
void setvalues() {
cin >> wide;
if (wide < 0 ) {
cout << "Set Value Error!" << endl;
_exit(0);
}
}
float area() {
float Area = wide * wide;
return Area;
}
float perim() {
float Perim = wide * 4;
return Perim;
}
};
class circle :public shape {
private:
float r;
public:
void setvalues() {
cin >> r;
if (r < 0) {
cout << "Set Value Error!" << endl;
_exit(0);
}
}
float area() {
float Area = r * r * PI;
return Area;
}
float perim() {
float Perim = 2 * PI * r;
return Perim;
}
};
int shape::getVertexCount() const {
if (dynamic_cast<circle*>(const_cast<shape*>(this)) != 0)
return 0;
else if (dynamic_cast<rectangle*>(const_cast<shape*>(this)) != 0 || dynamic_cast<square*>(const_cast<shape*>(this)) != 0)
return 4;
else
return -1;
}
int main() {
rectangle a;
square b;
circle d;
shape* p;
p = &a;
p->setvalues();
cout << p->area() << endl;
cout << p->perim() << endl;
cout << p->getVertexCount() << endl << endl;
p = &b;
p->setvalues();
cout << p->area() << endl;
cout << p->perim() << endl;
cout << p->getVertexCount() << endl << endl;
p = &d;
p->setvalues();
cout << p->area() << endl;
cout << p->perim() << endl;
cout << p->getVertexCount() << endl << endl;
return 0;
}
(2)
#include<iostream>
using namespace std;
#define PI 3.14
class shape {
public:
virtual void setvalues() = 0;
virtual float area() = 0;
virtual float perim() = 0;
int getVertexCount() const;
};
class rectangle :public shape {
protected:
float wide, high;
public:
virtual void setvalues() {
cin >> wide >> high;
if (wide < 0 || high < 0) {
cout << "Set Value Error!" << endl;
_exit(0);
}
}
virtual float area() {
float Area = wide * high;
return Area;
}
virtual float perim() {
float Perim = (wide + high) * 2;
return Perim;
}
};
class square :public rectangle {
public:
void setvalues() {
cin >> wide;
if (wide < 0 ) {
cout << "Set Value Error!" << endl;
_exit(0);
}
}
float area() {
float Area = wide * wide;
return Area;
}
float perim() {
float Perim = wide * 4;
return Perim;
}
};
class circle :public shape {
private:
float r;
public:
void setvalues() {
cin >> r;
if (r < 0) {
cout << "Set Value Error!" << endl;
_exit(0);
}
}
float area() {
float Area = r * r * PI;
return Area;
}
float perim() {
float Perim = 2 * PI * r;
return Perim;
}
};
int shape::getVertexCount() const {
const type_info& info = typeid(*this);
if (info == typeid(circle))
return 0;
else if (info == typeid(rectangle) || info == typeid(square))
return 4;
else
return -1;
}
int main() {
rectangle a;
square b;
circle d;
shape* p;
p = &a;
p->setvalues();
cout << p->area() << endl;
cout << p->perim() << endl;
cout << p->getVertexCount() << endl << endl;
p = &b;
p->setvalues();
cout << p->area() << endl;
cout << p->perim() << endl;
cout << p->getVertexCount() << endl << endl;
p = &d;
p->setvalues();
cout << p->area() << endl;
cout << p->perim() << endl;
cout << p->getVertexCount() << endl << endl;
return 0;
}
(3)
#include<iostream>
using namespace std;
#define PI 3.14
class shape {
public:
virtual void setvalues() = 0;
virtual float area() = 0;
virtual float perim() = 0;
virtual int getVertexCount() const = 0;
};
class rectangle :public shape {
protected:
float wide, high;
public:
void setvalues() {
cin >> wide >> high;
if (wide < 0 || high < 0) {
cout << "Set Value Error!" << endl;
_exit(0);
}
}
float area() {
float Area = wide * high;
return Area;
}
float perim() {
float Perim = (wide + high) * 2;
return Perim;
}
int getVertexCount() const{
return 4;
}
};
class square :public rectangle {
public:
void setvalues() {
cin >> wide;
if (wide < 0 ) {
cout << "Set Value Error!" << endl;
_exit(0);
}
}
float area() {
float Area = wide * wide;
return Area;
}
float perim() {
float Perim = wide * 4;
return Perim;
}
int getVertexCount() const {
return 4;
}
};
class circle :public shape {
private:
float r;
public:
void setvalues() {
cin >> r;
if (r < 0) {
cout << "Set Value Error!" << endl;
_exit(0);
}
}
float area() {
float Area = r * r * PI;
return Area;
}
float perim() {
float Perim = 2 * PI * r;
return Perim;
}
int getVertexCount() const{
return 0;
}
};
int main() {
rectangle a;
square b;
circle d;
shape* p;
p = &a;
p->setvalues();
cout << p->area() << endl;
cout << p->perim() << endl;
cout << p->getVertexCount() << endl;
p = &b;
p->setvalues();
cout << p->area() << endl;
cout << p->perim() << endl;
cout << p->getVertexCount() << endl;
p = &d;
p->setvalues();
cout << p->area() << endl;
cout << p->perim() << endl;
cout << p->getVertexCount() << endl;
return 0;
}
标签:wide,perim,14,area,每日,getVertexCount,打卡,setvalues,cout From: https://www.cnblogs.com/leapssisbird/p/17356882.html