谭书12-5
来源:互联网 发布:门诊电子处方软件 编辑:程序博客网 时间:2024/06/06 17:36
#include<iostream>
#include<cmath>
using namespace std;
float sum=0;
float s[5];
class Shape
{public:
virtual void printArea()const{}
virtual void shapeName()const=0;
};
class Circle:public Shape
{public:
Circle(float);
virtual void shapeName()const{cout<<"Area of Circle is: ";}
virtual void printArea()const;
private:
float radius;
};
Circle::Circle(float r)
{
radius=r;
}
void Circle::printArea()const
{
s[0]=3.14*radius*radius;
sum+=s[0];
cout<<s[0]<<endl;
}
class Rectangle:public Shape
{public:
Rectangle(float ,float);
virtual void shapeName()const{cout<<"Area of Rectangle is: ";}
virtual void printArea()const;
private:
float length;
float width;
};
Rectangle::Rectangle(float l,float w)
{
length=l;
width=w;
}
void Rectangle::printArea()const
{
s[1]=length*width;
sum+=s[1];
cout<<s[1]<<endl;
}
class Triangle:public Shape
{public:
Triangle(float ,float,float);
virtual void shapeName()const{cout<<"Area of Rectangle is: ";}
virtual void printArea()const;
private:
float a;
float b;
float c;
};
Triangle::Triangle(float m,float n,float k)
{
a=m;
b=n;
c=k;
}
void Triangle::printArea()const
{
float p;
p=(a+b+c)/2;
s[2]=sqrt(p*(p-a)*(p-b)*(p-c));
sum+=s[2];
cout<<s[2]<<endl;
}
class Trapezoid:public Shape
{public:
Trapezoid(float ,float ,float);
virtual void shapeName()const{cout<<"Area of Trapezoid is: ";}
virtual void printArea()const;
private:
float up;
float down;
float height;
};
Trapezoid::Trapezoid(float u,float d,float h)
{
up=u;
down=d;
height=h;
}
void Trapezoid::printArea()const
{
s[3]=0.5*(up+down)*height;
sum+=s[3];
cout<<s[3]<<endl;
}
class Square:public Shape
{public:
Square(float);
virtual void shapeName()const{cout<<"Area of Square is: ";}
virtual void printArea()const;
private:
float side;
};
Square::Square(float s)
{
side=s;
}
void Square::printArea()const
{
s[4]=side*side;
sum+=s[4];
cout<<s[4]<<endl;
}
int main()
{
Circle c(10);
Rectangle r(5,2);
Triangle t(3,2,4);
Trapezoid z(1,2,3);
Square s(5);
Shape *p[5];
int i;
p[0]=&c;
p[0]->shapeName();
p[0]->printArea();
p[1]=&r;
p[1]->shapeName();
p[1]->printArea();
p[2]=&t;
p[2]->shapeName();
p[2]->printArea();
p[3]=&z;
p[3]->shapeName();
p[3]->printArea();
p[4]=&s;
p[4]->shapeName();
p[4]->printArea();
cout<<"The total area is: "<<sum<<endl;
return 0;
}
#include<cmath>
using namespace std;
float sum=0;
float s[5];
class Shape
{public:
virtual void printArea()const{}
virtual void shapeName()const=0;
};
class Circle:public Shape
{public:
Circle(float);
virtual void shapeName()const{cout<<"Area of Circle is: ";}
virtual void printArea()const;
private:
float radius;
};
Circle::Circle(float r)
{
radius=r;
}
void Circle::printArea()const
{
s[0]=3.14*radius*radius;
sum+=s[0];
cout<<s[0]<<endl;
}
class Rectangle:public Shape
{public:
Rectangle(float ,float);
virtual void shapeName()const{cout<<"Area of Rectangle is: ";}
virtual void printArea()const;
private:
float length;
float width;
};
Rectangle::Rectangle(float l,float w)
{
length=l;
width=w;
}
void Rectangle::printArea()const
{
s[1]=length*width;
sum+=s[1];
cout<<s[1]<<endl;
}
class Triangle:public Shape
{public:
Triangle(float ,float,float);
virtual void shapeName()const{cout<<"Area of Rectangle is: ";}
virtual void printArea()const;
private:
float a;
float b;
float c;
};
Triangle::Triangle(float m,float n,float k)
{
a=m;
b=n;
c=k;
}
void Triangle::printArea()const
{
float p;
p=(a+b+c)/2;
s[2]=sqrt(p*(p-a)*(p-b)*(p-c));
sum+=s[2];
cout<<s[2]<<endl;
}
class Trapezoid:public Shape
{public:
Trapezoid(float ,float ,float);
virtual void shapeName()const{cout<<"Area of Trapezoid is: ";}
virtual void printArea()const;
private:
float up;
float down;
float height;
};
Trapezoid::Trapezoid(float u,float d,float h)
{
up=u;
down=d;
height=h;
}
void Trapezoid::printArea()const
{
s[3]=0.5*(up+down)*height;
sum+=s[3];
cout<<s[3]<<endl;
}
class Square:public Shape
{public:
Square(float);
virtual void shapeName()const{cout<<"Area of Square is: ";}
virtual void printArea()const;
private:
float side;
};
Square::Square(float s)
{
side=s;
}
void Square::printArea()const
{
s[4]=side*side;
sum+=s[4];
cout<<s[4]<<endl;
}
int main()
{
Circle c(10);
Rectangle r(5,2);
Triangle t(3,2,4);
Trapezoid z(1,2,3);
Square s(5);
Shape *p[5];
int i;
p[0]=&c;
p[0]->shapeName();
p[0]->printArea();
p[1]=&r;
p[1]->shapeName();
p[1]->printArea();
p[2]=&t;
p[2]->shapeName();
p[2]->printArea();
p[3]=&z;
p[3]->shapeName();
p[3]->printArea();
p[4]=&s;
p[4]->shapeName();
p[4]->printArea();
cout<<"The total area is: "<<sum<<endl;
return 0;
}
阅读全文
0 0
- 谭书12-5
- 谭书12-4
- 5-12
- 5-12
- 12-5
- 2006-12-5 星期二
- 纪念 5 12
- 12月5日
- 09-12-5
- 2009-12-5
- 2010-5-12
- Effective C++(5-12)
- 2011/5/12
- 2013-5-12
- 8# 2013-12-5
- 12周项目5
- 2015/5/12
- 12 jdk1.5特性
- Jsp 简单的 注册 登录
- eclipse项目转 studio
- 指针
- LPC824 ROM-bootloader反汇编分析
- [机器学习]02.逻辑回归(Logistic Regression)
- 谭书12-5
- HTTP协议学习笔记十二
- phpcms中 改造多页文章的"上一页"和"下一页"样式
- restful架构的理解
- java中判断字符串是否为数字的方法的几种方法
- spring_由XML实现AOP面向切面编程_实现动态代理
- powerdesigner生成mysql的脚本时为什么多一个national?
- mysql(三)之常见命令、基本类型和增删改查语句
- N皇后问题