5-14

//在weight.h中
#ifndef _WEIGHT_H_
#define _WEIGHT_H_
class Car;
class Boat
{
    private:
        double weight;
    public:
        Boat(double w=0.0):weight(w){}
        double getWeight(){return weight;}
        friend double getTotalWeight(Car & m,Boat & n);
};

class Car
{
    private:
        double weight;
    public:
        Car(double w=0.0):weight(w){}
        double getWeight(){return weight;}
        friend double getTotalWeight(Car & m,Boat & n);
};
#endif
 1 #include<iostream>
 2 #include"weight.h"
 3 
 4 double getTotalWeight(Car & m,Boat & n)
 5 {
 6     double sum=0;
 7     sum=m.weight+n.weight;
 8     return sum;
 9 }
10 
11 int main()
12 {
13     Boat n(50.8);
14     Car  m(40.2);
15     double s=0.0;
16     s=getTotalWeight(m,n);
17     std::cout<<"getTotalWeight"<<s<<std::endl;
18     return 0;
19 }

 

posted @ 2013-11-02 15:10  Talbot3  阅读(199)  评论(0编辑  收藏  举报