4个回答
展开全部
#include<iostream>
//#include<iomanip>//此头文件与下面的setprecision(3)对应,可同时加上,控制输出精度
using namespace std;
#define PI 3.1415926
//点类如下:
class Point
{
public:
Point()
{}
Point(float x1,float y1)
{
x=x1;
y=y1;
}
virtual float area()=0;
virtual float perim()=0;//周长
private:
float x;
float y;
};
//矩形类如下:
class Rectangle:public Point
{
public:
Rectangle()
{}
Rectangle(float x1,float y1,float length1,float width1);
float area();
float perim();
private:
float length;
float width;
};
Rectangle::Rectangle(float x1,float y1,float length1,float width1):Point(x1,y1)
{
length=length1;
width=width1;
}
float Rectangle::area()
{
float Area=0;
Area=length*width;
return Area;
}
float Rectangle::perim()
{
float Perim=0;
Perim=2*(length+width);
return Perim;
}
class Circle:public Point
{
public:
Circle(float x1,float y1,float r1);
float area();
float perim();
private:
float r;
};
Circle::Circle(float x1,float y1,float r1):Point(x1,y1)
{
r=r1;
}
float Circle::area()
{
return PI*r*r;
}
float Circle::perim()
{
return 2*PI*r;
}
int main()
{
Point *p;
Rectangle jx(0,0,2,3);//利用构造函数初始化,可以任意给定其值,前两个为point(0,0),后两个数为矩形的长和宽。
Circle circle(0,0,3);//前两个为point(0,0),第三个常量"3"为圆的半径。
p=&jx;
// cout<<"Rectangle的周长是:"<<setprecision(3)<<p->perim()<<endl;//利用setprecision(3)可控制输出精度,若不用则如下即可。
cout<<"Rectangle的周长是:"<<p->perim()<<endl;
cout<<"Rectangle的面积是:"<<p->area()<<endl;
p=&circle;
cout<<"Circle的周长是:"<<p->perim()<<endl;
cout<<"Circle的面积是:"<<p->area()<<endl;
}
希望对你有帮助,我也是初学者,加油!呵呵
//#include<iomanip>//此头文件与下面的setprecision(3)对应,可同时加上,控制输出精度
using namespace std;
#define PI 3.1415926
//点类如下:
class Point
{
public:
Point()
{}
Point(float x1,float y1)
{
x=x1;
y=y1;
}
virtual float area()=0;
virtual float perim()=0;//周长
private:
float x;
float y;
};
//矩形类如下:
class Rectangle:public Point
{
public:
Rectangle()
{}
Rectangle(float x1,float y1,float length1,float width1);
float area();
float perim();
private:
float length;
float width;
};
Rectangle::Rectangle(float x1,float y1,float length1,float width1):Point(x1,y1)
{
length=length1;
width=width1;
}
float Rectangle::area()
{
float Area=0;
Area=length*width;
return Area;
}
float Rectangle::perim()
{
float Perim=0;
Perim=2*(length+width);
return Perim;
}
class Circle:public Point
{
public:
Circle(float x1,float y1,float r1);
float area();
float perim();
private:
float r;
};
Circle::Circle(float x1,float y1,float r1):Point(x1,y1)
{
r=r1;
}
float Circle::area()
{
return PI*r*r;
}
float Circle::perim()
{
return 2*PI*r;
}
int main()
{
Point *p;
Rectangle jx(0,0,2,3);//利用构造函数初始化,可以任意给定其值,前两个为point(0,0),后两个数为矩形的长和宽。
Circle circle(0,0,3);//前两个为point(0,0),第三个常量"3"为圆的半径。
p=&jx;
// cout<<"Rectangle的周长是:"<<setprecision(3)<<p->perim()<<endl;//利用setprecision(3)可控制输出精度,若不用则如下即可。
cout<<"Rectangle的周长是:"<<p->perim()<<endl;
cout<<"Rectangle的面积是:"<<p->area()<<endl;
p=&circle;
cout<<"Circle的周长是:"<<p->perim()<<endl;
cout<<"Circle的面积是:"<<p->area()<<endl;
}
希望对你有帮助,我也是初学者,加油!呵呵
展开全部
你给的条件太少,我只能写个简单的!还有什么条件自己往里填就行了!
static void Main(string[] args)
{
basketball b = new basketball();
b.play1();
soccer s = new soccer();
s.play2();
Console.ReadKey();
}
}
class sport
{
public void play()
{
Console.WriteLine("都属于团体运动");
}
}
class basketball : sport
{
public void play1()
{
base.play();
Console.WriteLine("篮球场上人员为5人");
Console.WriteLine();
}
}
class soccer : sport
{
public void play2()
{
base.play();
Console.WriteLine("足球场上人员为11人");
}
static void Main(string[] args)
{
basketball b = new basketball();
b.play1();
soccer s = new soccer();
s.play2();
Console.ReadKey();
}
}
class sport
{
public void play()
{
Console.WriteLine("都属于团体运动");
}
}
class basketball : sport
{
public void play1()
{
base.play();
Console.WriteLine("篮球场上人员为5人");
Console.WriteLine();
}
}
class soccer : sport
{
public void play2()
{
base.play();
Console.WriteLine("足球场上人员为11人");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
没明白
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询