您的位置:首页 > 编程语言 > C语言/C++

学习C++——类的自动转换和强制类型转换

2015-01-13 11:22 141 查看

类的自动转换和强制类型转换

/*
本程序主要的用法是:
1、类型转换函数的定义以及使用。
(double类型赋值给对象类型,此时构造函数可以实现;
对象类型赋值给double类型,此时用转换函数来实现。)
2、成员函数实现加法的重载。
3、友元函数实现加法的重载。
*/
#ifndef STONEWT_H_
#define STONEWT_H_
class Stonewt
{
private:
enum {Lbs_per_stn = 14};//每14磅等于1英石
int stone;//英石
double pds_left;//
double pounds;//磅
public:
Stonewt(double lbs);//单位是磅
Stonewt(int stn, double lbs);//单位是英石和磅
Stonewt();
~Stonewt();
void show_lbs() const;
void show_stn() const;
/*
转换函数:因为上面可以知道,存在Stonewt wolfe(25.6);
也就是可以将一个double类型赋值给Stonewt对象,但是能否将
一个Stonewt对象赋值给一个double类型,即double host =? wolfe;
这就需要用到转换函数了。转换函数的原型是:operator double();
operator int();转换函数是类方法,需要通过类对象来调用,从而告知函数要转换的值。
*/
//转换函数的声明
operator double () const;
operator int () const;
//使用成员函数
//Stonewt operator+(const Stonewt & st) const;

//使用友元函数来重载加法
friend Stonewt operator+(const Stonewt & st1, const Stonewt & st2);
};
#endif


#include <iostream>
using std::cout;
#include "16.h"

Stonewt::Stonewt(double lbs)//构造函数
{
stone = int (lbs) / Lbs_per_stn;
pds_left = int (lbs) % Lbs_per_stn + lbs - int (lbs);
pounds = lbs;
}

Stonewt::Stonewt(int stn, double lbs)//构造函数
{
stone = stn;
pds_left = lbs;
pounds = stn * Lbs_per_stn + lbs;
}

Stonewt::Stonewt()//默认构造函数
{
stone = pounds = pds_left = 0;
}
Stonewt::~Stonewt()//析构函数
{

}

void Stonewt::show_lbs() const//显示函数,显示磅
{
cout << pounds << " pounds\n";
}

void Stonewt::show_stn() const//显示函数,显示英石
{
cout << stone << " stone, " << pds_left << " pounds\n";
}

//转换函数的定义
Stonewt::operator double () const
{
return pounds;
}
Stonewt::operator int() const
{
return int (pounds + 0.5);
}
//使用成员函数重载加法
/*Stonewt Stonewt::operator+(const Stonewt & st) const
{
double pds = pounds + st.pounds;
Stonewt sum(pds);
return sum;
}*/
//使用友元函数重载加法

Stonewt operator+(const Stonewt & st1, const Stonewt & st2)
{
double pds = st1.pounds + st2.pounds;
Stonewt sum(pds);
return sum;
}
#include <iostream>
#include "16.h"
using std::cout;

void display(const Stonewt & st, int n);

int main()
{
Stonewt incognito = 275;
Stonewt wolfe(287.7);
Stonewt taft(21, 8);

cout << "The incognito weighed ";
incognito.show_stn();
cout << "The wolfe weighed ";
wolfe.show_stn();
cout << "The taft weighed ";
taft.show_lbs();

incognito = 276.8;
/*
incognito = 276.8;使用接收double参数的构造函数,
将276.8转换为一个Stonewt值,这将把incognito的
pound成员设置为276.8。因为语句使用了构造函数,
所以还将设置shone,pds_left成员。
*/
taft = 325;
/*
将一个int值转换为double类型,然后使用Stonewt(double)
来设置全部3个成员
*/
cout << "\nincognito = 276.8;the incognito weighed ";
incognito.show_stn();
cout << "taft = 325;the taft weighed ";
taft.show_lbs();

cout << "\ndisplay(taft, 2);显示的结果:\n";
display(taft, 2);
cout << "\ndisplay(422, 2);显示的结果:\n";
display(422, 2);

/*
先将422,转换为一个double数,然后使用Stonewt(double)
将其转换为一个Stonewt对象。
*/
cout << "\n验证转换函数,定义了double变量poun:\n";
double poun;
poun = taft;
cout << "poun = taft = " << poun << "\n";
cout << "\n成员函数重载加法:\n";

Stonewt st1 (12.3);
Stonewt st2 (11.4);
Stonewt sum_st;
sum_st = st1 + st2;
/*
使用友元函数或者是使用成员函数实现加法重载,
但是不能同时定义两个函数,只能定义其中一个。
*/
cout << "sum_st = " ;
sum_st.show_lbs();
std::cin.get();
return 0;
}

void display(const Stonewt & st, int n)
{
for(int i = 0; i<n; i++)
{
cout << "wow! ";
st.show_stn();
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: