C++中使用类(重载,友元函数,转换函数等)
12点半了。好久没更新C++博文了。把一个章节看完了。接下来说下C++里的操作符重载和以后的内容。时间晚了,可能打字没打好。望大家见谅。
C++中有个operator操作符概念。如果想重载+运算符,那么需要写成operator+()。一般两个数相加是这么调用的:
[cpp]
a = b+c; == a = b.operator+(c);
当调用操作符,会有一个隐式的调用。把自己的对象作为操作符的对象。然后显示调用参数c。
重点介绍友元函数。因为重载了运算符后可能出现类似:
[cpp]
a = 1.5 + c;
此时按照上面的说法。1.5不是一个对象。无法完成操作,这时候就需要友元函数了,其实友元函数就是类的非成员函数,不会隐式的将自身对象也作为参数。
然后贴三段代码,注释写的还算详细。大家可以复制后去运行下看下结果。代码是从C++Primer Plus里抄过来的。
mytime0.h
[cpp]
#ifndef MYTIME0_H_
#define MYTIME0_H_
#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 Sum(const Time & t)const;
// 接下来时重载+运算符的函数
Time operator+(const Time & t)const;
void Show()const;
// 重载-运算符的函数
Time operator-(const Time & t)const;
// 重载*运算符的函数
Time operator*(double n)const;
// 友元函数 类似 a+b 的原型是 a.operator+(b); 所以如果 5*a 则5没有那个重载的函数,这时就不能使用类的成员函数了,需要友元函数来进行操作,类友元函数也就是非成员函数
friend Time operator*(double m, const Time & t){return t * m;} //内联函数,调用该类的成员函数,这里也就是上面那个函数。
// 友元函数。对于那些非成员重载操作符函数来说,操作符左面的操作数对应函数的第一个参数。类似 c = a+b 类似于 c = operator+(a,b)
friend std::ostream & operator<<(std::ostream & os, const Time & t);
};
#endif
mytime0.cpp
[cpp]
#include <iostream>
#include "mytime0.h"
Time::Time()
{
hours = minutes = 0;
}
Time::Time(int h, int m)
{
hours = h;
minutes = m;
}
void Time::AddMin(int m)
{
minutes += m;
hours += minutes / 60;
minutes %= 60;
}
void Time::AddHr(int h)
{
hours += h;
}
void Time::Reset(int h, int m)
{
hours = h;
minutes = m;
}
Time Time::Sum(const Time & t)const
{
Time sum;
sum.minutes = minutes + t.minutes;
sum.hours = hours + t.hours + sum.minutes/60;
sum.minutes %= 60;
return sum;
}
// 重载加号运算符的版本
Time Time::operator+(const Time & t)const
{
Time sum;
sum.minutes = minutes + t.minutes;
sum.hours = hours + t.hours + sum.minutes/60;
sum.minutes %= 60;
return sum;
}
Time Time::operator-(const Time & t)const
{
Time diff;
int tot1, tot2;
tot1 = t.minutes + 60 * t.hours;
tot2 = minutes + 60 * hours;
diff.minutes = (tot2 - tot1) % 60;
diff.hours = (tot2 - tot1) / 60;
return diff;
}
Time Time::operator*(double n)const
{
Time result;
long totalminutes = hours * n * 60 + minutes * n;
result.hours = totalminutes / 60;
result.minutes = totalminutes % 60;
return result;
}
std::ostream & operator<<(std::ostream & os, const Time & t)
{
os << t.hours << "hours, " << t.minutes << " minutes";
return os;
}
void Time::Show()const
{
std::cout << hours << " hours, " << minutes << " minutes";
}
//usetime0.cpp
#include <iostream>
#include "mytime0.h"
int main()
{
using std::cout;
using std::endl;
Time planning;
Time coding(2, 40);
Time fixing(5, 55);
Time total;
cout << "planning time = ";
planning.Show();
cout << endl;
cout << "coding time = ";
coding.Show();
cout << endl;
cout << "fixing time = ";
fixing.Show();
cout << endl;
// total = coding.Sum(fixing);
// 重载加号运算符的版本
total = coding + fixing;
cout << "coding + fixing = ";
total.Show();
cout << endl;
Time morefixing(3 ,20);
cout << "more fixing time = ";
morefixing.Show();
cout << endl;
total = morefixing.operator+(total);
cout << "morefixing.operator+(total) = ";
total.Show();
cout << endl;
Time aida(3, 35);
Time tosca(2, 48);
Time temp;
cout << "Aida and TOsca:\n";
cout << aida <<"; " << tosca << endl;
temp = aida + tosca; // operator+()
cout << "Aida + Tosca: " << temp << endl;&
补充:软件开发 , C++ ,