C++ 设计一个圆类,计算圆的面积和周长。
1.定义一个园(circle),属性为半径(radins)、圆周长和面积,操作为输入半径并计算周长、面积、输出半径和面积。要求定义构造函数(以半径为参数。默认值为0,周长...
1.定义一个园(circle),属性为半径(radins)、圆周长和面积,操作为输入半径并计算周长、面积、输出半径和面积。要求定义构造函数(以半径为参数。默认值为0,周长和面积在构造函数中生成)和复制构造函数。
2.定义一个point类,派生出Rectangle类和Circle类,计算各派生类对象的面积。
3.有一个交通工具类vehicle,将它作为基类派生小车类car,卡车类truck和轮船类boat,定义这些类并定义一个虚函数用来显示各类信息。
三道路题目解决下,谢谢各位了 ,一题10分 展开
2.定义一个point类,派生出Rectangle类和Circle类,计算各派生类对象的面积。
3.有一个交通工具类vehicle,将它作为基类派生小车类car,卡车类truck和轮船类boat,定义这些类并定义一个虚函数用来显示各类信息。
三道路题目解决下,谢谢各位了 ,一题10分 展开
2个回答
展开全部
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;
}
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;
}
展开全部
#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;
}
第二个我还不熟悉写,我尽力了.不采纳没关系
#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;
}
第二个我还不熟悉写,我尽力了.不采纳没关系
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |