C++ 设计一个圆类,计算圆的面积和周长。
发布网友
发布时间:2022-11-24 09:35
我来回答
共2个回答
热心网友
时间:2024-11-23 20:06
1.
class Circle
{
public:
Circle():r(0), leng(0), area(0){
}
Circle(const int rm):r(rm){
leng = 2 * pi * rm;
area = pi * rm * rm;
}
Circle& Circle(Circle& other){
r = other.r;
leng = other.leng;
area = other.area;
return *this;
}
void setR(const double rm)
{
r = rm;
leng = 2 * pi * r;
area = pi * r * r;
}
void output()
{
cout<<"r is "<<r<<", "<<"area is "<<area<<endl;
}
private:
double r;
double leng;
double area;
};
2.
#include <iostream>
using namespace std;
const double pi = 3.14;
//Point类,派生出Rectangle类和Circle类,计算各派生类对象的面积Area()。
template <class T>
class Point
{
public:
Point(){}
Point(const T x, const T y);
Point& operator= (const Point<T>&p);
public:
T x;
T y;
};
template <class T>
Point<T>::Point(const T x, const T y)
{
this->x = x;
this->y = y;
}
template <class T>
Point<T>& Point<T>::operator= (const Point<T>&p)
{
this->x = p.x;
this->y = p.y;
return *this;
}
template <class T>
class Shape
{
public:
virtual double Area() = 0;
};
template <class T>
class Rectangle:public Shape<T>
{
public:
Rectangle(const Point<T>& p1, const Point<T>& p2);
double Area();
private:
Point<T> pLT;
Point<T> pRD;
};
template <class T>
Rectangle<T>::Rectangle(const Point<T>& p1, const Point<T>& p2)
{
pLT = p1;
pRD = p2;
}
template <class T>
double Rectangle<T>::Area()
{
T w = pLT.x - pRD.x;
T h = pLT.y - pRD.y;
if(w<0) w = -w;
if(h<0) h = -h;
return double(w * h);
}
//Circle
template <class T>
class Circle:public Shape<T>
{
public:
Circle(const Point<T>& p1, const T& r);
double Area();
private:
Point<T> origin;
T r;
};
template <class T>
Circle<T>::Circle(const Point<T>& p1, const T& r)
{
origin = p1;
this->r = r;
}
template <class T>
double Circle<T>::Area()
{
return pi * r * r;
}
int main()
{
Point<int> p1(9, 0);
Point<int> p2(1, 2);
Rectangle<int> R(p1, p2);
Circle<int> C(p1, 10);
printf("Rectangle ' Area is %f\n", R.Area());
printf("Circle ' Area is %f\n", C.Area());
return 0;
}
3.
#include <iostream>
using namespace std;
class vehicle
{
public:
virtual void run(){cout<<"vehicle is run."<<endl;}
};
class Car:public vehicle
{
public:
void run(){cout<<"Car is run."<<endl;}
};
inclass truck:public vehicle
{
public:
void run(){cout<<"truck is run."<<endl;}
};
class boat:public vehicle
{
public:
void run(){cout<<"boat is run."<<endl;}
};
int main()
{
vehicle *p1 = new Car;
p1->run();
vehicle *p2 = new truck;
p2->run();
vehicle *p3 = new boat;
p3->run();
vehicle *p4 = new vehicle;
p4->run();
delete p1;
delete p2;
delete p3;
delete p4;
return 0;
}
热心网友
时间:2024-11-23 20:06
#include<iostream>
#include<cmath>
using namespace std;
class Circle{
double r,Area,Circumference;
public:
Circle(double a=0);
Circle(Circle &);
void SetR(double R);
double GetR(){return r;}
double GetAreaCircle(){return Area;}
double GetCircumference(){return Circumference;}
};
Circle::Circle(double a){
r=a;
Area=r*r*3.14159265;
Circumference=2*r*3.14159265;
}
Circle::Circle(Circle & cl){
r=cl.r;
Area=cl.Area;
Circumference=cl.Circumference;
}
void Circle::SetR(double R){
r=R;
Area=r*r*3.14159265;
Circumference=2*r*3.14159265;
}
第二个我还不熟悉写,我尽力了.不采纳没关系