作业交晚了 半抄袭的吧23333

//Measurement.h

#pragma once
#include
#include

using namespace std;

struct meter
{
    double value;
    const string symtol = "m";
    meter(double v)
    :value(v)
{}
};

struct minute
{
double value;
string symtol = "min"
};

struct second
{
double value;
string symtol = "s";
second(double v)
    :value(v)
{}
    };

  struct kilometer
{
double value;
string symtol = "km";
};

ostream& operator << (ostream& os, meter m)
{
os << m.value << m.symtol;
return os;
}

ostream& operator << (ostream& os, second s)
{
os << s.value << s.symtol;
return os;
}

template 

struct MeasurementTraits
{
typedef double UnitType;
static constexpr double scalar = 1;
};

template<>

struct MeasurementTraits
{
typedef meter UnitType;
static constexpr double scalar = 1;
};

template<>

struct MeasurementTraits
{
typedef meter UnitType;
static constexpr double scalar = 1000;
};

template<>

struct MeasurementTraits
{
typedef second UnitType;
static constexpr double scalar = 1;
};

template<>

struct MeasurementTraits
{
typedef second UnitType;
static constexpr double scalar = 60;
};

template

struct Measurement
{
double value;
typedef typename MeasurementTraits::UnitType UnitType;
UnitType description()
{
    return UnitType{ value *MeasurementTraits::scalar };
}

Measurement(double v)
{
    value = v;
}
};



//main.cpp
#include 
#include"Measurement.h"

using namespace std;

int main()
{
Measurement m1 = 20;
Measurement m2 = 11.2;
Measurement m3 = 20;
Measurement m4 = 10;
Measurement m5 = 10;

cout << m1.description() << endl;
cout << m2.description() << endl;
cout << m3.description() << endl;
cout << m4.description() << endl;
cout << m5.description() << endl;

system("pause");
return 0;
}

你可能感兴趣的:(作业交晚了 半抄袭的吧23333)