下列shape类是一个表示形状的抽象类,area( )为求图形面积的函数,total( )则是一个通用的用以求不同形状 10
的图形面积总和的函数。请从shape类派生三角形类(triangle)、矩形类(rectangle)、并给出具体的求面积函数。classshape{public:virt...
的图形面积总和的函数。请从shape类派生三角形类(triangle)、矩形类(rectangle)、并给出具体的求面积函数。
class shape
{
public:
virtual float area( )=0
};
float total(shape *s[ ],int n)
{
float sum=0.0;
for(int i=0;i<n;i++)
sum += s[i]->area( );
return sum;
} 展开
class shape
{
public:
virtual float area( )=0
};
float total(shape *s[ ],int n)
{
float sum=0.0;
for(int i=0;i<n;i++)
sum += s[i]->area( );
return sum;
} 展开
3个回答
展开全部
#include<iostream>
#include<cmath>
using namespace std;
class Rectangle:public Shape
{
float x1,x2,y1,y2;
public:
Rectangle(float argx1=0,float argy1=0,float argx2=0,float argy2=0);
float getArea();
};
Rectangle::Rectangle(float argx1,float argy1,float argx2,float argy2)
{
x1=argx1;y1=argy1;x2=argx2;y2=argy2;
}
float Rectangle::getArea()
{
float length,width;
length=x1>x2 ? x1-x2 : x2-x1;
width=y1>y2 ? y1-y2 : y2-y1;
return length*width;
}
class Triangle:public Shape
{
float x1,x2,y1,y2,x3,y3;
public:
Triangle(float argx1=0,float argy1=0,float argx2=0,float argy2=0,float argx3=0,float argy3=0);
float getArea();
};
Triangle::Triangle(float argx1,float argy1,float argx2,float argy2,float argx3,float argy3)
{
x1=argx1;y1=argy1;x2=argx2;y2=argy2;x3=argx3;y3=argy3;
}
float Triangle::getArea()
{
float a,b,c,s;
//分别求三边
a=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
b=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
c=sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
s=(a+b+c)/2;
return sqrt(s*(s-a)*(s-b)*(s-c)); //海伦公式求三角形面积
}
#include<cmath>
using namespace std;
class Rectangle:public Shape
{
float x1,x2,y1,y2;
public:
Rectangle(float argx1=0,float argy1=0,float argx2=0,float argy2=0);
float getArea();
};
Rectangle::Rectangle(float argx1,float argy1,float argx2,float argy2)
{
x1=argx1;y1=argy1;x2=argx2;y2=argy2;
}
float Rectangle::getArea()
{
float length,width;
length=x1>x2 ? x1-x2 : x2-x1;
width=y1>y2 ? y1-y2 : y2-y1;
return length*width;
}
class Triangle:public Shape
{
float x1,x2,y1,y2,x3,y3;
public:
Triangle(float argx1=0,float argy1=0,float argx2=0,float argy2=0,float argx3=0,float argy3=0);
float getArea();
};
Triangle::Triangle(float argx1,float argy1,float argx2,float argy2,float argx3,float argy3)
{
x1=argx1;y1=argy1;x2=argx2;y2=argy2;x3=argx3;y3=argy3;
}
float Triangle::getArea()
{
float a,b,c,s;
//分别求三边
a=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
b=sqrt((x1-x3)*(x1-x3)+(y1-y3)*(y1-y3));
c=sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
s=(a+b+c)/2;
return sqrt(s*(s-a)*(s-b)*(s-c)); //海伦公式求三角形面积
}
展开全部
//Test.java
public class Test {
public static void main(String[] args) {
Rectangle r = new Rectangle(12,10);
System.out.println("面积:"+r.area());
System.out.println("周长:"+r.girth());
}
}
abstract class Shape{
public abstract int area();
public abstract int girth();}
class Rectangle extends Shape{
private int w,h;
public Rectangle(int w,int h)
public int area()
public int girth() }
public class Test {
public static void main(String[] args) {
Rectangle r = new Rectangle(12,10);
System.out.println("面积:"+r.area());
System.out.println("周长:"+r.girth());
}
}
abstract class Shape{
public abstract int area();
public abstract int girth();}
class Rectangle extends Shape{
private int w,h;
public Rectangle(int w,int h)
public int area()
public int girth() }
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
题干不太清楚?
下面只做为一个模板,仅供参考:
#include <iostream>
#include <cmath>
using namespace std;
class shape
{
public:
virtual float area()=0;
};
class Triangle:public shape
{
private:
float L1;
float L2;
float L3;
public:
Triangle(float l1=0.0,float l2=0.0,float l3=0.0)
{
L1=l1;
L2=l2;
L3=l3;
}
float area()
{
double p=(L1+L2+L3)/2.0;
return pow(p*(p-L1)*(p-L2)*(p-L3),0.5);
}
};
class Rectangle :public shape
{
private:
float H;
float W;
public:
Rectangle(float h=0.0,float w=0.0)
{
H=h;
W=w;
}
float area()
{
return W*H;
}
};
float total(shape *s[],int n)
{
float sum=0.0;
for(int i=0;i<n;i++)
sum+=s[i]->area();
return sum;
}
int main()
{
shape *s[2];
int n=2;
Rectangle Rec(2.0,2.5);
Triangle Tri(3,4,5);
s[0]=&Rec;s[1]=&Tri;
cout<<total(s,n)<<endl;
return 0;
}
下面只做为一个模板,仅供参考:
#include <iostream>
#include <cmath>
using namespace std;
class shape
{
public:
virtual float area()=0;
};
class Triangle:public shape
{
private:
float L1;
float L2;
float L3;
public:
Triangle(float l1=0.0,float l2=0.0,float l3=0.0)
{
L1=l1;
L2=l2;
L3=l3;
}
float area()
{
double p=(L1+L2+L3)/2.0;
return pow(p*(p-L1)*(p-L2)*(p-L3),0.5);
}
};
class Rectangle :public shape
{
private:
float H;
float W;
public:
Rectangle(float h=0.0,float w=0.0)
{
H=h;
W=w;
}
float area()
{
return W*H;
}
};
float total(shape *s[],int n)
{
float sum=0.0;
for(int i=0;i<n;i++)
sum+=s[i]->area();
return sum;
}
int main()
{
shape *s[2];
int n=2;
Rectangle Rec(2.0,2.5);
Triangle Tri(3,4,5);
s[0]=&Rec;s[1]=&Tri;
cout<<total(s,n)<<endl;
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询