C++类和对象题目

gyk1100 2009-04-25 09:38:22
设计并定义一个Triange类来描述三角形,使用Point类的对象来表示端点,Point类的定义如下:class Point
{ public:
Point(double nx=0.0,double ny=0.0):x(nx),y(ny){}
Point(Point &np):x(np.x),y(np.y){}
double GetX(){return x;}
double GetY(){return y;}
void SetX(double nx){x=nx;}
void SetY(double ny){y=ny;}
void SetPoint(double nx,double ny){x=nx;y=ny;}
void SetPoint(Point &np){x=np.x;y=np.y}
privite:
double x,y
} ;
在Triangle类中包括Point类的3个对象p1,p2和p3作为其数据成员。Triangle类具有计算三角形周长和面积的功能,计算过程在成 员函数GetPerimeter中实现
如何实现……
...全文
501 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
smxblysq 2010-10-17
  • 打赏
  • 举报
回复
有点复杂了
yunjin9856 2009-04-26
  • 打赏
  • 举报
回复
#include <iostream.h>
const double PI=3.14159265;
class Circle
{ public:
Circle(double r) { radius = r; } //基类构造函数赋值半径
virtual double area() { return 0.0; }// 定义虚函数求表面积
virtual double volume() { return 0.0; }//定义虚函数求体积
protected:
double radius;
};
class Sphere:public Circle
{ public:
Sphere( double r ):Circle( r ){}
double area() { return 4.0 * PI * radius * radius; }
double volume()
{ return 4.0 * PI * radius * radius * radius / 3.0; }
};
class Column:public Circle
{ public:
Column( double r,double h ):Circle( r ) { height = h; }
double area()
{ return 2.0 * PI * radius * ( height + radius ); }
double volume()
{ return PI * radius * radius * height; }
private:
double height;
};
void main()
{ Circle *p; //基类指针访问派生类的成员
Sphere sobj(2);
p = &sobj; // 基类指针指向派生类对象
cout << "球体:" << endl;
cout << "体积 = " << p->volume() << endl; // 求球的体积
cout << "表面积 = " << p->area() << endl; // 求球的表面积
Column cobj( 3,5 );
p = &cobj; //指向圆柱体的对象
cout << "圆柱体:" << endl;
cout << "体积 = " << p->volume() << endl; //求圆柱的体积
cout << "表面积 = " << p->area() << endl; //求圆柱的表面积
}
不是现成的,自己找灵感吧!
gyk1100 2009-04-25
  • 打赏
  • 举报
回复
这个倒简单,几行代码就能完事
using namespace std;
bool sf(int a,int b,int c)
{
if((a+b)>c)return true;
else cout<<"不能组成三角形"<<endl;
return false;
关键是那个东西要用到类和对象很恶心,完全没学过这方面的东西……
baiwei156 2009-04-25
  • 打赏
  • 举报
回复
不过我没有增加对3点是否可以构成三角形的检测。。。。。
baiwei156 2009-04-25
  • 打赏
  • 举报
回复

#include<iostream>
#include<string>
#include "math.h"

using namespace std;
//设计并定义一个Triange类来描述三角形,使用Point类的对象来表示端点,Point类的定义如下:
class Point
{
public:
Point(long double nx=0.0,long double ny=0.0):x(nx),y(ny){}
Point(Point &np):x(np.x),y(np.y){}
long double GetX(){return x;}
long double GetY(){return y;}
void SetX(long double nx){x=nx;}
void SetY(long double ny){y=ny;}
void SetPoint(long double nx,long double ny){x=nx;y=ny;}
void SetPoint(Point &np){x=np.x;y=np.y;}
private:
long double x,y ;
} ;
class Triangle
{
Point p1,p2,p3;
long double a,b,c;

public:
Triangle(long double x1,long double y1,long double x2,long double y2,long double x3,long double y3):p1(x1,y1),p2(x2,y2),p3(x3,y3)
{
a=GetLength(p1,p2);
b=GetLength(p1,p3);
c=GetLength(p2,p3);



}
long double GetLength(Point p,Point q)
{

return sqrt(pow(p.GetX()-q.GetX(),2)+pow(p.GetY()-q.GetY(),2));

}


long double GetPerimeter()
{
return a+b+c;
}
long double GetArea()
{


long double p=GetPerimeter()/2;
return sqrt( p*(p-a)*(p-b)*(p-c));


}


};
int main()
{
Triangle t1(0,0,0,3,4,0);
cout<<t1.GetPerimeter()<<endl;
cout<<t1.GetArea()<<endl;

system("pause");
return 0;
}
wollww 2009-04-25
  • 打赏
  • 举报
回复
有点鄙视
gyk1100 2009-04-25
  • 打赏
  • 举报
回复
汗,那是我高中同学问我的题目,他今年上大一学的是C++,我只学了C,这是他的题目,我确实一点不会……类和对象基本上没接触过……
自己瞎鼓捣了一个,不知道是不是正确的,现在手头也没有编译器,很麻烦……那些说什么不道德的牛人们,我也是没办法,C++特性至今不熟,错了还不知道怎么错的……
class Triangle
{
proteced:
P1:Point;
P2:Point;
P3:Point;
private:
FLongRound:float;
FWithHalf:float;
void zhouchang(int LPoint;int RPoint);
void mianji(int MPoint;int Height);
public:
FX:float;
FY:float;
FZ:float;
property bianchang:float read Fbianchang write Fbianchang
property mianji:float read Fmianji write Fmianji
}
deltamaster 2009-04-25
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 hairetz 的回复:]
又是这种求作业答案的。
你先自己写,看了下需求,又不复杂,遇到错误再贴出来,大家帮你解决。
[/Quote]
同感。
  • 打赏
  • 举报
回复
又是这种求作业答案的。
你先自己写,看了下需求,又不复杂,遇到错误再贴出来,大家帮你解决。
nickyjay0501 2009-04-25
  • 打赏
  • 举报
回复
9494
notsea 2009-04-25
  • 打赏
  • 举报
回复
楼主直接就是要答案嘛,不道德
buyan2009 2009-04-25
  • 打赏
  • 举报
回复
ding
deltamaster 2009-04-25
  • 打赏
  • 举报
回复
……很难吗?
具体是遇到哪些问题?不要整个都让别人写吧。
liliangbao 2009-04-25
  • 打赏
  • 举报
回复
帮顶~

64,683

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧