⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ex7_16.cpp

📁 Visual C++ 2005的源代码
💻 CPP
字号:
// Ex7_16.cpp : main project file.
// Using scalar properties
#include "stdafx.h"

using namespace System;

// Class defining a person's height
value class Height
{
private:
  // Records the height in feet and inches
  int feet;
  int inches;

  literal int inchesPerFoot = 12;
  literal double inchesToMeters = 2.54/100;

public:
  // Create a height from inches value
  Height(int ins)
  {
    feet = ins / inchesPerFoot;
    inches = ins % inchesPerFoot;
  }

  // Create a height from feet and inches
  Height(int ft, int ins) : feet(ft), inches(ins){}

  // The height in meters
  property double meters                         // Scalar property
  {
    // Returns the property value
    double get() 
    { 
      return inchesToMeters*(feet*inchesPerFoot+inches);
    }

    // You would define the set() function for the property here...
  }

  // Create a string representation of the object
  virtual  String^ ToString() override
  {
    return feet + L" feet "+ inches + L" inches";
  }
};

// Class defining a person's weight
value class Weight
{
private:
  int lbs;
  int oz;

  literal int ouncesPerPound = 16;
  literal double lbsToKg = 1.0/2.2;

public:
  Weight(int pounds, int ounces)
  {
    lbs = pounds;
    oz = ounces;
  }
  
  property int pounds                            // Scalar property
  {
    int get() { return lbs;  }
    void set(int value) {  lbs = value;  }
  }
  
  property int ounces                            // Scalar property
  {
    int get() { return oz;  }
    void set(int value) {  oz = value;  }
  }

  property double kilograms                      // Scalar property
  {
    double get() { return lbsToKg*(lbs + oz/ouncesPerPound);  }
  }

  virtual String^ ToString() override
  { return lbs + L" pounds " + oz + L" ounces"; }
};

// Class defining a person
ref class Person
{
private:
  Height ht;
  Weight wt;

public:
  property String^ Name;                         // Trivial scalar property

  Person(String^ name, Height h, Weight w) : ht(h), wt(w)
  {
    Name = name;
  }

  Height getHeight(){ return ht;  }
  Weight getWeight(){ return wt;  }
};

int main(array<System::String ^> ^args)
{
  Weight hisWeight = Weight(185, 7);
  Height hisHeight = Height(6, 3);
  Person^ him = gcnew Person(L"Fred", hisHeight, hisWeight);

  Weight herWeight = Weight(105, 3);
  Height herHeight = Height(5, 2);
  Person^ her = gcnew Person(L"Freda", herHeight, herWeight);

  Console::WriteLine(L"She is {0}", her->Name);
  Console::WriteLine(L"Her weight is {0:F2} kilograms.", 
                                                    her->getWeight().kilograms);
  Console::WriteLine(L"Her height is {0} which is {1:F2} meters.",
                                      her->getHeight(),her->getHeight().meters);

  Console::WriteLine(L"He is {0}", him->Name);
  Console::WriteLine(L"His weight is {0}.", him->getWeight());
  Console::WriteLine(L"His height is {0} which is {1:F2} meters.",
                                       him->getHeight(),him->getHeight().meters);
  return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -