【c++ primer】第十一章 使用类

本文详细解释了C++中操作符重载的概念、限制、示例及注意事项,包括函数重载、操作符优先级、局部变量引用问题等,并通过一个Time类实例展示了操作符重载的应用。此外,还介绍了友元函数的作用、访问权限以及如何使用友元函数进行操作符重载。最后,文章涵盖了类的自动转换、强制类型转换、成员函数与友元函数的区别等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一,操作符重载

1)函数重载(多态)

名称相同,特征标(参数列表)不同的函数。完成相同的基本操作

2)操作符左侧的对像是调用对象,操作符右侧的作为参数被传递的对象

3)重载限制:1>重载后的操作符至少有一个操作数是用户定义的类型。防止用户为标准类型重载操作符

2>使用操作符,不能违反操作符原来的句法规则。

3>不能修改操作符优先级,不能定义新的操作符

4>可被重载的操作符:

+ - * / % ^ & | ~
! = < > + = -= *= /= %=
^= &= | =<< >> > >= << == = !=
<= >= && || ++ -- [ ] ( ) ->
->* , new new[ ] delete delete[ ]

4)不要返回局部变量的引用,因为函数执行完毕后,局部变量将会消失

二,操作符重载示例

#include <iostream>
class Time
{
private:
    int hours;
    int minutes;
public:
    Time();
    Time(int h,int m=0);
    void addmin(int m);
    void addhr(int h);
    void reset(int h=0,int m=0);
    Time operator+ (const Time &t)const;
    Time operator- (const Time &t)const;
    Time operator* (double mult)const;
    void show()const;
};

Time::Time()
{
    hours=minutes=0;
}
Time::Time(int h, int m)
{
    hours=h;
    minutes=m;
}
void Time::addhr(int h)
{
    hours=hours+h;
}
void Time::addmin(int m)
{
    minutes=minutes+m;
    hours=minutes/60;
    minutes=minutes%60;
}
void Time::reset(int h, int m)
{
    hours=h;
    minutes=m;
}
Time Time::operator +(const Time& t) const
{
    Time sum;
    sum.minutes=minutes+t.minutes;
    sum.hours=hours+t.hours+sum.minutes/60;
    sum.minutes=sum.minutes%60;
    return sum;
}
Time Time::operator -(const Time& t) const
{
    Time diff;
    int tot1,tot2;
    tot1=minutes+60*hours;
    tot2=t.minutes+60*t.hours;
    diff.minutes=(tot1-tot2)%60;
    diff.hours=(tot1-tot2)/60;
    return diff;
}
Time Time::operator *(double mult) const
{
    Time result;
    long totalminutes = hours*mult*60+minutes*mult;
    result.hours=totalminutes/60;
    result.minutes=totalminutes%60;
    return result;
}
void Time::show() const
{
    std::cout<<hours<<" hours "<<minutes<<" minutes "<<std::endl;
}

int main()
{
    using std::cout;
    using std::endl;
    Time weeding(4,35);
    Time waxing(2,27);
    Time total;
    Time diff;
    Time adjusted;
    
    cout<<"weeding Time = ";
    weeding.show();
    cout<<endl;
    
    cout<<"waxing Time = ";
    waxing.show();
    cout<<endl;       
    
    cout<<"total work Time=";
    total=weeding + waxing;
    total.show();
    cout<<endl;
    
    diff=weeding -waxing;
    cout<<"weeding - waxing =";
    diff.show();
    cout<<endl;
    
    adjusted=total*1.5;
    cout<<"adjusted work Time =";
    adjusted.show();
    cout<<endl;
    
    
    
    return 0;
}

程序注意事项:1>不能使用关键字 time作为类名

2>操作符重载total = weeding + waxing;执行为: weeding.operator+(waxing),然后返回一个Time对象

3>将参数声明为引用的目的:提高效率

三,友元函数(只有在声明中的原型中使用)

1,定义:友元函数是指某些虽然不是类成员却能够访问类的所有成员的函数。类授予它的友元特别访问权。通常同一个开发者会出于技术和非技术的原因,控制类的友元和成员函数(否则当你想更新你的类时,还要征得其它部分的拥有者的同意)

2,例子说明:Time重载操作符* A=B*1.5;对应调用步骤B.operator*(1.5);

操作符左边对应对象,为成员函数调用法则

如果A=1.5*B;不适用于 操作符重载函数(成员函数)

则须定义Time operator* (double m,const Time &t);非成员函数

但是非成员函数不能直接使用私有成员

所以使用友元函数friend Timeoperator* (double m,const Time &t)

3,友元函数:1>在类中声明,但它不是成员函数。不能使用成员操作符“.”(B.operator*)来调用

2>但有与成员函数访问权限相同

3>不要使用Time::限定符

4>调用时候:A=operator(1.5,B)

4,定义如下

Time operator *(double m,const &t)
{
Time result;
long totalminutes = hours*mult*60+minutes*mult;
result.hours=totalminutes/60;
result.minutes=totalminutes%60;
return result;
}

如果写成如下形式可以不用友元函数(不用访问私有成员)

Time operator* (double m,const &t)

{

return t*m;

}

5,常用的友元:重载<<操作符

1>"<<" 重载之输出对象

void operator<< (ostream &os,Time &t)

{

cout<<t.hours<<" hours "<<t.minutes<<" minutes ";

}

使用:cout<<B;

不能这样使用:cout<<"the time is:"<<B<<"ok";

如果想这样使用

void &operator<<(ostream &os,Time &t)

{

cout<<t.hours<<" hours "<<t.minutes<<" minutes ";

return os;

}

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

1,例子:long count = 8; 将整型自动转化为long 型

int *p=10; //非法的

int *p=(int *)10;//将指针设置为地址10

2,接受一个参数的构造函数 可以作为转换函数

explicit 关键字用来关闭自动转换特性

int 强制转换 将待转换的值 四舍五入为最接近的值

五,复习

1)成员函数和友元函数区别

成员函数:类的组成部分,可以直接访问对象的成员而不用使用成员操作符

友元函数:友元函数不是类组成部分,不能隐式访问类成员

2)非成员函数访问类成员,必须是友元么?

要访问私有成员,必须是友元。而访问共有成员可以不是友元

3)重载操作符= () [ ] ->必须使用成员函数来定义

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值