| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 457 人关注过本帖
标题:[求助]关于友元的一个问题
只看楼主 加入收藏
sami3166
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2006-6-29
收藏
 问题点数:0 回复次数:4 
[求助]关于友元的一个问题

下面这个源程序编译运行后,最后的totalweight 是0;不知道哪里错了,怎么改,请各位帮忙看看


#include <iostream>
using namespace std;

class Car;
class Boat
{
public:
Boat(Boat &b) {weight1=b.weight1;}
Boat(float m=0) {weight1=m;}
void Set();
void Print();
friend float totalweight(Boat &b,Car &c);
private:
float weight1;
};
void Boat::Set()
{
cin>>weight1;
}
void Boat::Print()
{
cout<<"the weight of boat is "<<weight1<<endl;
}

class Car
{
public:
Car(Car &c) {weight2=c.weight2;}
Car(float n=0) {weight2=n;}
void Set();
void Print();
friend float totalweight(Boat &b,Car &c);
private:
float weight2;
};
void Car::Set()
{
cin>>weight2;
}
void Car::Print()
{
cout<<"the weight of car is "<<weight2<<endl;
}

float totalweight(Boat &b,Car &c)
{
float w;
w=b.weight1+c.weight2;
return (w);
}

void main()
{ float weight1,weight2;
Boat boat(weight1);
Car car(weight2);
cout<<"input boat's weight:"<<endl;
boat.Set();
cout<<"input car's weight:"<<endl;
car.Set();
boat.Print();
car.Print();
Boat b;
Car c;
cout<<"total weight is "<<totalweight(b,c)<<endl;
}

搜索更多相关主题的帖子: public include private 源程序 friend 
2006-06-30 16:57
sami3166
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2006-6-29
收藏
得分:0 

我刚学C++五天,好多地方都很模糊,请大家多帮帮忙

2006-06-30 17:23
心动音符
Rank: 1
等 级:禁止访问
威 望:1
帖 子:832
专家分:0
注 册:2005-9-15
收藏
得分:0 
你5天就学到CLASS了啊  你真厉害啊 我学了好几个月啊

2006-06-30 17:26
sami3166
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2006-6-29
收藏
得分:0 
这可不能怪我啊!老师讲得快啊,也可能是以前学过C的缘故。
你能帮忙看看错在哪吗?
2006-06-30 17:37
sami3166
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2006-6-29
收藏
得分:0 

我知道了,呵呵。
#include <iostream>
using namespace std;

class Car;
class Boat
{
public:
Boat(Boat &b) {weight1=b.weight1;}
Boat(float m=0) {weight1=m;}
void Set();
void Print();
friend float totalweight(Boat &b,Car &c);
private:
float weight1;
};
void Boat::Set()
{
cin>>weight1;
}
void Boat::Print()
{
cout<<"the weight of boat is "<<weight1<<endl;
}

class Car
{
public:
Car(Car &c) {weight2=c.weight2;}
Car(float n=0) {weight2=n;}
void Set();
void Print();
friend float totalweight(Boat &b,Car &c);
private:
float weight2;
};
void Car::Set()
{
cin>>weight2;
}
void Car::Print()
{
cout<<"the weight of car is "<<weight2<<endl;
}

float totalweight(Boat &b,Car &c)
{
float w;
w=b.weight1+c.weight2;
return (w);
}

void main()
{ float weight1,weight2;
Boat boat(weight1);
Car car(weight2);
cout<<"input boat's weight:"<<endl;
boat.Set();
cout<<"input car's weight:"<<endl;
car.Set();
boat.Print();
car.Print();
Boat b;
Car c;
cout<<"total weight is "<<totalweight(b,c)<<endl; //totalweight(b,c)应改为totalweight(boat,car)
}

2006-06-30 18:36
快速回复:[求助]关于友元的一个问题
数据加载中...
 
   



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

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