每日打卡-14
Posted leapssisbird
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了每日打卡-14相关的知识,希望对你有一定的参考价值。
一.问题描述
请编写一个抽象类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;
英语词汇(每日打卡)
18.7.1 Words
pseudo(syu duo) code
internationalization
literal
collaborate(ke la be rea te)
Syntax(sin tax)
enumeration (in 牛魔 ration)
以上是关于每日打卡-14的主要内容,如果未能解决你的问题,请参考以下文章
每日SQL打卡DAY 14丨报告的记录 I难度简单
乱序版 ● 剑指offer每日算法题打卡题解——数学 (题号14,57)
每日SQL打卡DAY 14丨重新格式化部门表难度中等