这是一个C++代码,但有错误,希望哥哥姐姐帮忙看看

作者在 2008-08-22 03:22:35 发布以下内容
问题1:警告 1 warning C4490: “override”: 重写说明符的用法不正确;“Height::Tostring”与 ref 基类方法不匹配 e:\Visual Studio 2005\Projects\Ex7_16\Ex7_16.cpp 30 

问题2:自己的理想结果:He is Fred
his Weight is 185 pounds 7 ounces.
his height is 6 feet 3 inches which is 1.91 meters.
运行的结果就最后一句不同.

 
代码如下:
#include "stdafx.h"
using namespace System;
value class Height
 {
 private:
  int feet;
  int inches;
  literal int inchesPerFoot=12;
  literal double inchesToMeters=2.54/100;
 public:
  Height(int ins)
  {
   feet=ins/inchesPerFoot;
   inches=ins%inchesPerFoot;
  }
  Height(int ft,int ins):feet(ft),inches(ins){}
  property double meters
  {
   double get()
   {
    return inchesToMeters*(feet*inchesPerFoot+inches);
   }
  }
  virtual String^ Tostring() override
  {
   return feet+L"feet"+inches+L"inches";
  }
 };
 value class Weight
 {
  private:
            int lbs;
            int oz;
            literal int ouncePerPound=16;
            literal double lbsToKg=1.0/2.2;
public:
 Weight(int pounds,int ounces)
 {
  lbs=pounds;
  oz=ounces;
 }
 property int pounds
 {
  int get()
  {
   return oz;
  }
  void set(int value)
  {
   lbs=value;
  }
 }
 property double kilograms
 {
  double get()
  {
   return lbsToKg*(lbs+oz/ouncePerPound);
  }
 }
 virtual String^ Tostring() override
 {
  return lbs+L"pounds"+oz+L"ounces";
 }
 };
 ref class Person
 {
 private:
  Height ht;
  Weight wt;
 public:
  property String^ Name;
  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^his=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"Shi 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}",his->Name);
 Console::WriteLine(L"His Weight is {0:F2} kilograms.",his->getWeight().kilograms);
 Console::WriteLine(L"His Height is {0} which is {1:F2} meters.",his->getHeight(),his->getHeight().meters);
}
我的难题 | 阅读 460 次
文章评论,共0条
游客请输入验证码
文章归档
最新评论