#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();
}