傻鸟算法狗都不学,re.0 C++

文章介绍了使用C++编写的两个类Circle和Rectangle,展示了如何计算它们的面积和周长。并通过Shape基类和运算符重载实现通用形状的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>

using namespace std;

class Circle{
public:
	float radius;
	Circle(float radius){
		this->radius=radius;
	}
	float getS(){
		return 3.14*radius*radius;
	}
	float getC(){
		return 6.28*radius;
	}
};

class Rectangle {
public:
	float a,b;
	Rectangle(float a,float b) {
		this->a=a;
		this->b=b;
	}
	float getS() {
		return a*b;
	}
	float getC(){
		return 2*(a+b);
	}
};

int main()
{
	Circle c1(2.5f),c2(6.f);
	Rectangle r1(10.f,20.f),r2(1.5f,2.5f);
	cout<<"c1半径:"<<c1.radius<<endl;
	cout<<"c1面积:"<<c1.getS()<<endl;
	cout<<"c1周长:"<<c1.getC()<<endl;
	cout<<"r1面积:"<<r1.getS();
}
#include<iostream>

using namespace std;

class Shape{
public:
	virtual float getS()=0;
	virtual float getC()=0;
};

class Circle:public Shape{
private:
	float r;
public:
	Circle(float r){
		this->r=r;
	}
	float getS(){
		return 3.14*r*r;
	}
	float getC(){
		return 6.28*r;
	}
};

class Rectangle:public Shape{
private:
	float a,b;
public:
	Rectangle(float a,float b){
		this->a=a;
		this->b=b;
	}
	float getS(){
		return a*b;
	}
	float getC(){
		return 2*(a+b);
	}
};

void display(Shape* str)
{
	cout<<"S:"<<str->getS()<<endl;
	cout<<"C:"<<str->getC()<<endl;
}

int main()
{
	Circle c1(3);
	Rectangle r1(6,7);
	display(&c1);
	display(&r1);
}
#include<iostream>

using namespace std;

class suan{
protected:
	double r,i;
public:
	suan(double r=0.0,double i=0.0):r(r),i(i){}
	suan operator+(const suan& other)
	{
		return suan(r+other.r,i+other.i);
	}
	suan operator++(int)
	{
		suan a=*this;
		r++;
		i++;
		return a;
	}
	suan operator++()
	{
		r++;
		i++;
		return *this;
	}
	suan operator-()
	{
		r=-r;
		i=-i;
		return *this;
	}
	void show()
	{
		if(i>0) cout<<r<<"+"<<i<<"i"<<endl;
		else if(i==0) cout<<r<<endl;
		else cout<<r<<i<<"i"<<endl;
	}
};

int main()
{
	suan a(9.0,8.8),b(9.9,8.0),r;
	suan c=a+b;
	suan d=a++;
	suan e=++a;
	suan f=-b;
	r.show();
	c.show();
	d.show();
	e.show();
	f.show();
 } 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值