前几天开始上课,偶尔翻了一下c++第十一章。主要讲了重载,友元,类型转换。。。
跳过了随机函数的那一小块。现在,基本上结合书可以写出这一章的内容,实现重载,友元,类型转换等。还不是很熟练,可以以后多练练书后面的习题。
//stonewt.h
#ifndef STONEWT_H_INCLUDED
#define STONEWT_H_INCLUDED
#include <iostream>
//没有用状态成员mode
//没有过多的使用转换函数
using namespace std;
class Stonewt
{
private:
enum {Lbs_per_stn = 14};
int stone;
double pds_left; //几英石几英磅
double pounds; //英磅
public:
Stonewt(double pou);
Stonewt(int sto,double pds);
Stonewt();
~Stonewt();
//operater overloading
Stonewt operator+(const Stonewt & b) const;
Stonewt operator-(const Stonewt & b) const;
Stonewt operator*(double n) const;
bool operator<(const Stonewt& b) const;
bool operator<=(const Stonewt& b) const;
bool operator==(const Stonewt& b) const;//返回类型
//friends
friend Stonewt operator*(double n,const Stonewt & b);
friend ostream & operator<<(ostream & os, Stonewt & st);
friend bool operator>(Stonewt& a, Stonewt& b);
friend bool operator>=(Stonewt& a, Stonewt& b);
friend bool operator!=(Stonewt& a, Stonewt& b);
};
#endif // STONEWT_H_INCLUDED
//stonewt
#include "stonewt.h"
Stonewt::Stonewt(double pou)
{
stone = int(pou) / Lbs_per_stn;
pds_left = int(pou) % Lbs_per_stn + pou - int(pou);
pounds = pou;
}
Stonewt::Stonewt(int sto,double pds)
{
stone = sto;
pds_left = pds;
pounds = sto * Lbs_per_stn + pds;
}
Stonewt::Stonewt()
{
stone = pounds = pds_left = 0;
}
Stonewt::~Stonewt()
{
}
//operater overloading
Stonewt Stonewt::operator+(const Stonewt & b) const
{
return Stonewt(pounds+b.pounds);
}
Stonewt Stonewt::operator-(const Stonewt & b) const
{
return Stonewt(pounds-b.pounds);
}
Stonewt Stonewt::operator*(double n) const
{
return Stonewt(pounds*n);
}
//第一次尝试重载<,==等关系运算符
bool Stonewt::operator<(const Stonewt& b) const
{
if(pounds<b.pounds)
return 1;
else
return 0;
}
bool Stonewt::operator<=(const Stonewt& b) const
{
return pounds <= b.pounds;
}
bool Stonewt::operator==(const Stonewt& b) const
{
return pounds == b.pounds;
}
//friends
bool operator>(Stonewt& a, Stonewt& b)
{
return a.pounds > b.pounds;
}
bool operator>=(Stonewt& a, Stonewt& b)
{
return a.pounds >= b.pounds;
}
bool operator!=(Stonewt& a, Stonewt& b)
{
return a.pounds != b.pounds;
}
Stonewt operator*(double n,const Stonewt & b)
{
return b*n;
}
ostream& operator<<(ostream& os, Stonewt & st)
{
os << st.pounds << " pounds,or " << st.stone << " stone and " << st.pds_left << " pounds." << std::endl;
return os;
}
//c++pp11.9.6.cpp
#include <iostream>
#include "stonewt.h"
#include <cstdlib>
using namespace std;
int main(int argc, const char * argv[]) //?
{
Stonewt a(12);
a = 154;//转换函数
cout << a;
int num=1;
Stonewt b[7] = { 123, 23.32, 3.3 };
b[3]= b[1] + b[2];
b[4]= 10 * b[3];
for (int i = 5; i < 7; i++)
{
b[i] = rand();//随机数我还不是很明白
}
cout << endl;
cout << "7个Stonewt的输出:" << endl;
for (int i = 0; i < 7; i++)
{
cout << b[i];
}
cout << endl;
Stonewt min = b[0];
Stonewt max = b[0];
for (int i = 0; i < 7; i ++)
{
if (min > b[i])
{
min = b[i];
}
if (max < b[i])
{
max = b[i];
}
if (b[i] >= a)
{
num++ ;
}
}
cout << "Min: " << min ;
cout << "Max: " << max ;
cout << "bigger than 11: " << num << "个。" << endl;
return 0;
}