三个班级相互联系

时间:2017-11-08 11:56:36

标签: c++ class objective-c++

Kelvin.h

class CelciusDeg;
class FarenheitDeg;

class Kelvin
{
    double val;
public:
    friend void show(const Kelvin&);
    friend void CelciusDeg::count(const Kelvin&);
    friend void FarenheitDeg::count(const Kelvin&);
    void count(const CelciusDeg&);
    void count(const FarenheitDeg&);
    Kelvin();
    Kelvin(double);
    ~Kelvin();
};

CelciusDeg.h

class Kelvin;
class FarenheitDeg;

class CelciusDeg
{
    double val;
public:
    friend void show(const CelciusDeg&);
    friend void Kelvin::count(const CelciusDeg&);
    friend void FarenheitDeg::count(const CelciusDeg&);
    void count(const Kelvin&);
    void count(const FarenheitDeg&);
    CelciusDeg();
    CelciusDeg(double);
    ~CelciusDeg();
};

FarenheitDeg.h

class CelciusDeg;
class Kelvin;

class FarenheitDeg
{
    double val;
public:
    friend void show(const FarenheitDeg&);
    friend void Kelvin::count(const FarenheitDeg&);
    friend void CelciusDeg::count(const FarenheitDeg&);
    void count(const CelciusDeg&);
    void count(const Kelvin&);
    FarenheitDeg();
    FarenheitDeg(double);
    ~FarenheitDeg();
};

我正在尝试制作一个程序,它可以在3种不同类型的温度对象中存储临时值,并将它们相互计数。如何使用前向声明使计数方法工作?

2 个答案:

答案 0 :(得分:0)

我猜你可以调用非默认构造函数并将其插入两个朋友的函数

答案 1 :(得分:0)

您好我会做一个不同的设计:

class Kelvin
{
   double val;

public :
   explicit Kelvin(double val);
   double getVal() const;
};

class Farenheit
{
   double val;

public :
   explicit Farenheit(double val);
   double getVal() const;
};

class Celcius
{
   double val;

public :
   explicit Celcius(double val);
   double getVal() const;
};

Kelvin toKelvin(Farenheit & o);
Kelvin toKelvin(Celcius & o);

Celcius toCelcius(Farenheit & o);
Celcius toCelcius(Kelvin & o);

Farenheit toFarenheit(Kelvin & o);
Farenheit toFarenheit(Celcius & o);