| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 503 人关注过本帖
标题:有一道关于运算符重载友元函数的问题,望各位大哥赐教!
只看楼主 加入收藏
system3288
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:79
专家分:140
注 册:2010-10-9
结帖率:88.89%
收藏
已结贴  问题点数:20 回复次数:2 
有一道关于运算符重载友元函数的问题,望各位大哥赐教!
//运算符重载友元函数
各位大哥,帮我看看这个程序有啥子问题,搞了一上午硬是没搞得懂错在哪里,我觉得没错呀,如果看出错误,望指出错误,谢谢啦。
#include "stdafx.h"
#include <iostream>
#include <windows.h>
using namespace std;
class Point
{
public:
    Point(double i,double j)
    {
        x=i,y=j;
    }
    double Area()const
    {
        return 0.0;
    }
private:
    double x;
    double y;
};
class Rectangle:public Point
{
public:
    Rectangle(double i,double j,double k,double l);
    double Area()const
    {
        return w*h;
    }
private:
    double w,h;
};
Rectangle::Rectangle(double i,double j,double k,double l):Point(i,j)
{
    w=k;
    h=l;
}
void fun(Point &s)
{
    printf("s.Area=%5.1f\n",s.Area());
}
int _tmain(int argc, _TCHAR* argv[])
{
    Rectangle abc(3.0,5.2,8.0,9.0);
    fun(abc);
    system("pause");
    return 0;
}

搜索更多相关主题的帖子: 运算符 函数 重载 
2010-10-29 13:50
djxh77710
Rank: 2
来 自:中国
等 级:论坛游民
帖 子:71
专家分:88
注 册:2008-10-20
收藏
得分:15 
Rectangle类是系统中已经定义了的类,在WINDGID.H中
所以你再定义,就会出现错误..如果你想使用系统中Rectangle类的函数,可以从它里面继承一下就可以了,但是不能再定义一个类名

就好像中国的身份证号码一样,是不能出现俩个一样的身份证号的..

No Pains ,No Gains....
2010-10-30 12:23
m21wo
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:4
帖 子:440
专家分:1905
注 册:2010-9-23
收藏
得分:5 
程序代码:
#include <iostream>
//#include <windows.h>            // windows 头文件中有Rectangle类
using namespace std;
class Point
{
public:
    Point(double i=0,double j=0)
    {
        x=i,y=j;
    }
    double Area()const
    {
        return 0.0;
    }
private:
    double x;
    double y;
};
class Rectangle:public Point
{
public:
    Rectangle(double i,double j,double k,double l);
    double Area()const
    {
        return w*h;
    }
private:
    double w,h;                    
};
Rectangle::Rectangle(double i=0,double j=0,double k=0,double l=0):Point(i,j)
{
    w=k;
    h=l;
}
void fun(Rectangle &s)     // 改为Rectangle类型!!
{
    printf("s.Area=%5.1f\n",s.Area());
}
int main()  // 括号里面是 命令行参数!根本没必要写!你又没用
{
    Rectangle abc(3,5,8,9);
    fun(abc);
   // system("pause");
    return 0;
}

小改了一下!

If You Want Something, Go Get It, Period.
2010-10-30 13:05
快速回复:有一道关于运算符重载友元函数的问题,望各位大哥赐教!
数据加载中...
 
   



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

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