首页 > 其他分享 >每日打卡-14

每日打卡-14

时间:2023-04-26 18:00:58浏览次数:39  
标签:wide perim 14 area 每日 getVertexCount 打卡 setvalues cout

一.问题描述

  请编写一个抽象类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

相关文章

  • c++11/14线程池
    c++11//#pragmaonce#include<iostream>#include<thread>#include<memory>#include<vector>#include<queue>#include<functional>//std:;function#include<future>//std::pac......
  • c++打卡第十六天
    一、问题描述。  二、设计思路。①、我们可以写出最终获得利息加本金的公式,即两千乘以利息加上其所存的年限,其中年限可以通过20除以不同的年限求得最大值。②、获取各个年范围后,使用for循环穷举出每种情况所得的最终金额,同时打印出当所获得金额最大时,每个不同的存钱方案是......
  • 打卡4(java)
    importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in);inta=sc.nextInt(),b=sc.nextInt();intc=sc.nextInt(),d=sc.nextInt();intx=a*60+b,y=c*60+d;......
  • 编程打卡:来玩玩Ruby语言吧2.1!
    编程打卡:来玩玩Ruby语言吧2.1!我们前面实现了一个有趣的树类Tree,但它不具有简洁的用户接口,来设置一棵新树,为它写一个初始化方法,接受散列表和数组嵌套的结构。写好之后,你可以这样设置新树:{'grandpa'=>{'dad'=>{'child1'=>{},'child2'=>{}},'uncle'=>{'child3�......
  • 打卡第十二天
    输入一个8位二进制数,将其转换为十进制输出一、1.将二进制数每一位乘以该位的2的次方相加- 二、三、#include<iostream>usingnamespacestd;doublepower(doublex,intn){ doubleq=1.0; while(n--) q*=x; returnq;}intmain(){ intq=0; cout<<"输入数字:"; for(inti......
  • 4.26打卡
    #include<iostream>#include<iomanip>#include<cmath>usingnamespacestd;constdoubleTINY_VALUE=1e-10;doubletsin(doublex){doubleg=0;doublet=x;intn=1;do{g+=t;n++;t=-t*x*x/(2*......
  • 2023/4/25每日随笔
        今天,上午上机数据库,了解了数据库的数据库操作的sql语句的select关于from,groupby,orderby,联合多表查询等等,添加语句,删除语句,删除表结构等等对于以后项目的有用的操作,晚上又看了看数据库,准备完成第一张内容的编写,数据是描述事务的符号表示,数据库是存储数据的仓库,只不......
  • 每日八股文之Java
    1、请你说说ConcurrentHashMap数组+链表+红黑树、锁的粒度ConcurrentHashMap的底层数据结构与HashMap一样,也是采用“数组+链表+红黑树”的形式采用锁定头节点的方式降低了锁粒度,以较低的性能代价实现了线程安全。它的线程安全的实现机制:初始化数组或头节点时,ConcurrentHa......
  • 第八天打卡
    #include<iostream>#defineN10usingnamespacestd;intmain(){inta[N],x,i,aa;for(i=0;i<N;i++)scanf("%d",&a[i]);for(i=1;i<=N-1;i++)for(x=0;x<N-i;x++){if(a[x]<a[x+1]){......
  • 打卡9
    特殊a串数列求和给定两个均不超过9的正整数a和n,要求编写程序求a+aa+aaa++⋯+aa⋯a(n个a)之和。输入格式:输入在一行中给出不超过9的正整数a和n。输出格式:在一行中按照“s=对应的和”的格式输出。输入样例:23输出样例:s=246代码:#include<stdio.h>intmain(){inta,n;......