三个类相互链接

Three classes linked to each other

本文关键字:链接 三个      更新时间:2023-10-16

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();
};

摄氏

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();
};

华氏度

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 种不同类型的温度对象中并将它们相互计数。如何使用前向声明使计数方法工作?

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

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

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);