| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 969 人关注过本帖, 1 人收藏
标题:怎样用C++编出这道题,大家帮帮忙
只看楼主 加入收藏
惜缘
Rank: 1
等 级:新手上路
帖 子:32
专家分:4
注 册:2010-7-2
结帖率:100%
收藏(1)
已结贴  问题点数:20 回复次数:10 
怎样用C++编出这道题,大家帮帮忙
自己定义三个类即点、线和矩形,通过带参数的构造函数完成对对象的初始化赋值,三个类中都要实现一个显示的操作,要求该操作能够完成显示点和线(两个端点)的坐标数值。线的类中除了显示的操作外还要有一个显示线段长度的操作。矩形的类中除了显示的操作外还要有一个显示矩形面积的操作。

类是这样的:
point :  Private:x,y    public:void draw()

line:Private:point p1,p2  public:void draw()  Distance()

rect:Private:point u1,u2  public:void draw()  Area()

主函数是这样的:
#include <iostream>
using namespace std;

void main()
{
    point p(2,3);
    point p11(3,8),
        p21(7,5);
    line l(p11,p21);
    rect r(p11,p21);
    p.draw();
    l.draw();
    r.draw();
    cout<<"线的长度为:"<<l.Distance()<<endl;
    cout<<"矩形的面积为:"<<r.Area()<<endl;
}
输出之后是这样的:
point的x坐标值是:2  point的y坐标值是:3
line的起点是point的x坐标值是:3  point的y坐标只是:8
line的终点为point的x坐标值是:7  point的y坐标值是:5
rect的起点为point的x坐标值是:3  point的y坐标值是:8
rect的重点为point的x坐标值是:7  point的y坐标值是:5
线的长度为:5
矩形的面积为:12


请大家帮帮忙,把这个程序补充完整,谢谢,今晚就要
搜索更多相关主题的帖子: void 能够 include public 
2010-09-26 19:59
m21wo
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:4
帖 子:440
专家分:1905
注 册:2010-9-23
收藏
得分:4 
这是我写的!希望对你有用!
程序代码:
#include <iostream>
#include <cmath>
using namespace std;
class point
{
public:
    double x;
    double y;
    point(double x1=0,double y1=0):x(x1),y(y1)
    {}
    void draw()
    {
        cout<<"point的x坐标值是:"<<x<<"   point的y坐标值是:"<<y<<endl;
    }
};
class line:public point
{
private:
    point p1;
    point p2;
public:
    line(point xp1,point xp2):p1(xp1.x,xp1.y),p2(xp2.x,xp2.y)
    {}
    void draw()
    {
        cout<<"line的起点是point的x坐标值是:"<<p1.x<<"   point的y坐标值是:"<<p1.y<<endl;
        cout<<"line的终点为point的x坐标值是:"<<p2.x<<"   point的y坐标值是:"<<p2.y<<endl;
    }
    double Distance()
    {
        double m=(p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y);
        return sqrt(m);
    }
};
class rect :public point
{
private:
    point u1;
    point u2;
public:
    rect(point ru1,point ru2):u1(ru1.x,ru1.y),u2(ru2.x,ru2.y)
    {}
    void draw()
    {
        cout<<"rect的起点为point的x坐标值是:"<<u1.x<<"  point的y坐标值是:"<<u1.y<<endl;
        cout<<"rect的终点为point的x坐标值是:"<<u2.x<<" point的y坐标值是:"<<u2.y<<endl;
    }
    double Area()
    {
        return fabs((u2.x-u1.x)*(u2.y-u1.y));
    }
};

int  main()
{
    point p(2,3);
    point p11(3,8);
    point p21(7,5);
    line l(p11,p21);
    rect r(p11,p21);
    p.draw();
    l.draw();
    r.draw();
    cout<<"线的长度为:"<<l.Distance()<<endl;
    cout<<"矩形的面积为:"<<r.Area()<<endl;
}

If You Want Something, Go Get It, Period.
2010-09-26 22:37
惜缘
Rank: 1
等 级:新手上路
帖 子:32
专家分:4
注 册:2010-7-2
收藏
得分:0 
谢谢,但是类图中的x与y是私有的,你变成是公有的了,这是不允许更改的。
2010-09-27 17:30
m21wo
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:4
帖 子:440
专家分:1905
注 册:2010-9-23
收藏
得分:4 
那你就声明友元类啊!如下:
程序代码:
#include <iostream>
#include <cmath>
using namespace std;
class point
{
private:
    double x;
    double y;
public:
    point(double x1=0,double y1=0):x(x1),y(y1)
    {}
    void draw()
    {
        cout<<"point的x坐标值是:"<<x<<"   point的y坐标值是:"<<y<<endl;
    }
    friend class line;
    friend class rect;
};
class line
{
private:
    point p1;
    point p2;
public:
    line(point xp1,point xp2):p1(xp1.x,xp1.y),p2(xp2.x,xp2.y)
    {}
    void draw()
    {
        cout<<"line的起点是point的x坐标值是:"<<p1.x<<"   point的y坐标值是:"<<p1.y<<endl;
        cout<<"line的终点为point的x坐标值是:"<<p2.x<<"   point的y坐标值是:"<<p2.y<<endl;
    }
    double Distance()
    {
        double m=(p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y);
        return sqrt(m);
    }
};
class rect
{
private:
    point u1;
    point u2;
public:
    rect(point ru1,point ru2):u1(ru1.x,ru1.y),u2(ru2.x,ru2.y)
    {}
    void draw()
    {
        cout<<"rect的起点为point的x坐标值是:"<<u1.x<<"  point的y坐标值是:"<<u1.y<<endl;
        cout<<"rect的终点为point的x坐标值是:"<<u2.x<<" point的y坐标值是:"<<u2.y<<endl;
    }
    double Area()
    {
        return fabs((u2.x-u1.x)*(u2.y-u1.y));
    }
};

int  main()
{
    point p(2,3);
    point p11(3,8);
    point p21(7,5);
    line l(p11,p21);
    rect r(p11,p21);
    p.draw();
    l.draw();
    r.draw();
    cout<<"线的长度为:"<<l.Distance()<<endl;
    cout<<"矩形的面积为:"<<r.Area()<<endl;
}

If You Want Something, Go Get It, Period.
2010-09-27 17:36
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:1 
class point
{
    double x;
    double y;
public:
    point(double x1=0,double y1=0):x(x1),y(y1)
    {}
    double get_x(){ return x;}
    double get_y(){ return y;}
    void draw()
    {
        cout<<"point的x坐标值是:"<<x<<"   point的y坐标值是:"<<y<<endl;
    }
};
2010-09-27 17:57
惜缘
Rank: 1
等 级:新手上路
帖 子:32
专家分:4
注 册:2010-7-2
收藏
得分:0 
d:\msdev98\myprojects\c++\kao\point.h(3) : error C2011: 'point' : 'class' type redefinition

这个错误说明了什么啊,我的程序中有头文件,和源文件
2010-09-27 19:03
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:4 
// 重定义错误
#include <iostream>
#include <cmath>
using namespace std;
class point
{
    double x;
    double y;
public:
    point(double x1=0,double y1=0):x(x1),y(y1)
    {}
    double get_x(){ return x;}
    double get_y(){ return y;}
    void draw()
    {
        cout<<"point的x坐标值是:"<<x<<"   point的y坐标值是:"<<y<<endl;
    }
};

class line:public point
{
private:
    point p1;
    point p2;
public:
    line(point xp1,point xp2):p1(xp1.get_x(),xp1.get_y()),p2(xp2.get_x(),xp2.get_y())
    {}
    void draw()
    {
        cout<<"line的起点是point的x坐标值是:"<<p1.get_x()<<"   point的y坐标值是:"<<p1.get_y()<<endl;
        cout<<"line的终点为point的x坐标值是:"<<p2.get_x()<<"   point的y坐标值是:"<<p2.get_y()<<endl;
    }
    double Distance()
    {
        double m=(p2.get_x()-p1.get_x())*(p2.get_x()-p1.get_x())+(p2.get_y()-p1.get_y())*
            (p2.get_y()-p1.get_y());
        return sqrt(m);
    }
};
class rect :public point
{
private:
    point u1;
    point u2;
public:
    rect(point ru1,point ru2):u1(ru1.get_x(),ru1.get_y()),u2(ru2.get_x(),ru2.get_y())
    {}
    void draw()
    {
        cout<<"rect的起点为point的x坐标值是:"<<u1.get_x()<<"  point的y坐标值是:"<<u1.get_y()<<endl;
        cout<<"rect的终点为point的x坐标值是:"<<u2.get_x()<<" point的y坐标值是:"<<u2.get_y()<<endl;
    }
    double Area()
    {
        return fabs((u2.get_x()-u1.get_x())*(u2.get_y()-u1.get_y()));
    }
};

int  main()
{
    point p(2,3);
    point p11(3,8);
    point p21(7,5);
    line l(p11,p21);
    rect r(p11,p21);
    p.draw();
    l.draw();
    r.draw();
    cout<<"线的长度为:"<<l.Distance()<<endl;
    cout<<"矩形的面积为:"<<r.Area()<<endl;
    return 0;
}
2010-09-27 22:05
惜缘
Rank: 1
等 级:新手上路
帖 子:32
专家分:4
注 册:2010-7-2
收藏
得分:0 
'point' : no appropriate default constructor available
这个问题应该怎么解决呢,我建立了头文件和源文件,不是在一个文件下编写的
2010-09-28 21:22
惜缘
Rank: 1
等 级:新手上路
帖 子:32
专家分:4
注 册:2010-7-2
收藏
得分:0 
在一个文件下运行的程序我做完了,我想寻求一个在多个头文件与源文件下运行这个程序的代码,希望能帮我解决一下,谢谢
2010-09-28 21:33
寒风中的细雨
Rank: 17Rank: 17Rank: 17Rank: 17Rank: 17
等 级:贵宾
威 望:66
帖 子:1710
专家分:8645
注 册:2009-9-15
收藏
得分:5 
给个例子你参考下
//c.h文件
#ifndef c_h
#define c_h
class point
{
    double x;
    double y;
public:
    point(double x1=0,double y1=0);
    double get_x();
    double get_y();
    void draw();
};

class line:public point
{
private:
    point p1;
    point p2;
public:
    line(point xp1,point xp2);
    void draw();
    double Distance();
};

class rect :public point
{
private:
    point u1;
    point u2;
public:
    rect(point ru1,point ru2);
    void draw();
    double Area();
};
#endif
////////////////////////////////
//work.cpp文件
#include <iostream>
#include <cmath>
#include "c.h"
using namespace std;

point::point(double x1,double y1):x(x1),y(y1)
{}
   
double point::get_x()
{ return x;}

double point::get_y()
{ return y;}

void point::draw()
{
    cout<<"point的x坐标值是:"<<x<<"   point的y坐标值是:"<<y<<endl;
}

line::line(point xp1,point xp2):p1(xp1.get_x(),xp1.get_y()),p2(xp2.get_x(),xp2.get_y())
{}
void line::draw()
{
    cout<<"line的起点是point的x坐标值是:"<<p1.get_x()<<"   point的y坐标值是:"<<p1.get_y()<<endl;
    cout<<"line的终点为point的x坐标值是:"<<p2.get_x()<<"   point的y坐标值是:"<<p2.get_y()<<endl;
}

double line::Distance()
{
    double m=(p2.get_x()-p1.get_x())*(p2.get_x()-p1.get_x())+(p2.get_y()-p1.get_y())*
        (p2.get_y()-p1.get_y());
    return sqrt(m);   
}

rect::rect(point ru1,point ru2):u1(ru1.get_x(),ru1.get_y()),u2(ru2.get_x(),ru2.get_y())
{}

void rect::draw()
{
    cout<<"rect的起点为point的x坐标值是:"<<u1.get_x()<<"  point的y坐标值是:"<<u1.get_y()<<endl;
    cout<<"rect的终点为point的x坐标值是:"<<u2.get_x()<<" point的y坐标值是:"<<u2.get_y()<<endl;
}

double rect::Area()
{
        return fabs((u2.get_x()-u1.get_x())*(u2.get_y()-u1.get_y()));
}
//////////////////////////
//main.cpp文件
#include "c.h"
#include <iostream>
using namespace std;

int  main()
{
    point p(2,3);
    point p11(3,8);
    point p21(7,5);
    line l(p11,p21);
    rect r(p11,p21);
    p.draw();
    l.draw();
    r.draw();
    cout<<"线的长度为:"<<l.Distance()<<endl;
    cout<<"矩形的面积为:"<<r.Area()<<endl;
    return 0;
}
2010-09-28 22:05
快速回复:怎样用C++编出这道题,大家帮帮忙
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022015 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved