普通运算符重载
数学运算符 | 关系运算符 |
---|---|
+ - * / ++ – | == >= <= != |
C++准许以运算符命名函数
string a = "hello";
a += "world"; //+(a, "world");
count<< "hello"; //<<(count, "hello"); 将hello传递给count打印出来
#include <stdio.h>
#include <unistd.h>
#include <iostream>
using namespace std;
class Timer{
public:
Timer()
{
hour = 0;
min = 0;
sec = 0;
}
~Timer()
{
}
void addtimer(int sec=1)
{
this->min += (this->sec+sec)/60; //计时分钟
this->sec = (this->sec+sec)%60; //计时秒,满60归零
}
void show()
{
printf("%2d:%2d:%2d\n", hour, min, sec);
}
Timer operator+(int sec) //重载+,使可以两个timer定义的数据进行相加(timer定义的不是普通的数据,所以不能直接相加)
{
Timer tem;
tem.sec = this->sec+sec;
return tem;
}
Timer operator+(Timer &x) //返回的是Timer对象,不可以作为左值使用
{
Timer tem;
tem.sec = sec+x.sec;
tem.min = min+x.min;
tem.hour = hour+x.hour;
return tem;
}
Timer operator++(int)
{
Timer tem = *this;//backup
sec++;
return tem;
}
Timer operator++()
{
sec++;
return *this;
}
bool operator==(Timer &x)
{
if(sec==x.sec && min==x.min && hour==x.hour)
return true;
return false;
}
private:
int hour;
int min;
int sec;
};
int main()
{
Timer t;
t.addtimer(3);
Timer t1;
t1.addtimer(5);
Timer t2;
t2 = t+t1;
t2.show();
t2 = t2+5;
t2.show();
Timer t3 = ++t2;
t3.show();
t2.show();
int i = 0;
for(;i<10; ++i)
printf("++\n");
if(t2 == t3)
printf("OK..time out!\n");
#if 0
while(1)
{
t.addtimer(1);
t.show();
sleep(1);
}
#endif
}
特殊运算符重载
运算符[]
int &operator[](int i) //返回的是引用,可以作为左值
{
switch(i)
{
case 0:
return hour;
case 1:
return min;
case 2:
return sec;
}
}
friend ostream &operator<<(ostream &out, const Timer &t);
private:
int hour;
int min;
int sec;
};
ostream &operator<<(ostream &out, const Timer &t)
{
out << "hour: "<<t.hour << " min: "<<t.min<<" sec: "<<t.sec<< endl;
}
int main(){
printf("hour: %d\n", t2[0]);
printf("min : %d\n", t2[1]);
printf("sec : %d\n", t2[2]);
t2[1] = 30;
printf("hour: %d\n", t2[0]);
printf("min : %d\n", t2[1]);
printf("sec : %d\n", t2[2]);
cout << t2;
}
赋值运算符 = 解决深浅拷贝的问题
举例:像Windows里面,拷贝一个文件,只拷贝了快捷方式,当通过其中一个快捷方式,删除了文件,另一个快捷方式就访问不了这个文件了,因为这两个快捷方式同时访问一片内存。赋值运算符,就能解决这个给问题,将头部和身体一起拷贝,使之成为两个一模一样内容的文件。
A & operator=(A &x)
{
printf("operator=\n");
p = new char[10];
strcpy(p, x.p);
return *this;
}
( )仿函数
在屏幕上打印:
在std这个空间中(或者某个类中),有一个对象,它支持一个运算符,只要往 << (重载符号) 后装一个东西,就会将这个东西打印输出。
std::endl 描述换行的意思
#include <iostream>
int main(){
std::cout<<"hello"<<std::endl;
}
第二种写法
#include <iostream>
using namespace std; //开放std命名空间
int main(){
//std::cout<<"hello"<<std::endl;
cout<<"hello"<<endl; //不用加前缀了
}
仿函数的应用
#include <iostream>
using namespace std;
class Converter{
public:
Converter(double rate)
{
this->rate = rate;
}
double operator()(double rmb)
{
return rmb*rate;
}
private:
double rate;
};
double RMBto(double rmb, double rate)
{
return rmb*rate;
}
int main()
{
// std::cout << "hello"<<std::endl;
// cout << "hello"<<endl;
//用仿函数的汇率转换方法
Converter RMBtoUS(6.4);
cout << RMBtoUS(10) << endl;
cout << RMBtoUS(10) << endl;
cout << RMBtoUS(10) << endl;
Converter RMBtoE(8.4);
cout << RMBtoE(100) << endl;
cout << RMBtoE(100) << endl;
cout << RMBtoE(100) << endl;
#if 0 //没用仿函数的汇率转换方法
cout << RMBto(10, 6.4) << endl;
cout << RMBto(10, 6.4) << endl;
cout << RMBto(10, 6.4) << endl;
cout << RMBto(10, 8.4) << endl;
cout << RMBto(10, 8.4) << endl;
cout << RMBto(10, 8.4) << endl;
#endif
}
<< 赋值运算符
friend ostream &operator<<(ostream &out, const Timer &t);
private:
int hour;
int min;
int sec;
};
ostream &operator<<(ostream &out, const Timer &t)
{
out << "hour: "<<t.hour << " min: "<<t.min<<" sec: "<<t.sec<< endl;
}
main(){
cout<<t2; //直接打印,会报错:编译器不认识,所以要进行上面的两个函数操作
}
标准输入输出流
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
// printf("input: ");fflush(stdout);
cout<<"input: ";
char buf[100];
// gets(buf);
cin >> buf;
// printf("%s\n", buf);
cout << buf << endl;
std::cout<<10<<std::endl;
cout<<10<<endl;
cout<< hex <<10<<endl;
}
转自网络
在输入流与输出流中使用控制符
用流对象的成员函数控制输出格式
组合
本质上是两个点,通过两个点组合成了一条线
#include "arr.h"
#include <iostream>
using namespace std;
class ARRX:public ARR{
public:
int ever(void)
{
int i = 0;
int sum = 0;
for(;i<tail; i++)
sum += data[i];
return sum/tail;
}
};
class Stuma{
public:
Stuma(){
}
~Stuma() { }
void savescore(int score)
{
scorearr.addtail(score);
}
int everscore(void)
{
return scorearr.ever();
}
void showscore(void)
{
scorearr.show();
}
private:
//ARR scorearr;
ARRX scorearr;
};
int main()
{
Stuma mmm;
mmm.savescore(23);
mmm.savescore(44);
mmm.savescore(55);
mmm.savescore(23);
mmm.showscore();
cout << mmm.everscore() <<endl;
}
继承
实现了比以前更高级的功能,但我还线,只不过这根线更高级了(粗细变得不一样)
继承作用:当看重了别人实现的功能,但是自己拿不到源代码,只能操作库函数时,就可以用继承实现功能,即不丢弃原来的功能,又升级了原来的功能。
#include <iostream>
using namespace std;
class A{
public:
A(){ }
~A(){ }
void showx()
{
cout<<"xxxxxxxxxxxxxxx"<<endl;
}
};
class AX:public A{ //A为基类,AX为派生类
public:
void showy()
{
cout<<"yyyyyyyyyyyyyy"<<endl;
}
};
int main()
{
A a;
a.showx();
// a.showy();
AX b;
b.showx();
b.showy();
}
-
public继承方式
• 基类中所有public成员在派生类中为public属性;
• 基类中所有protected成员在派生类中为protected属性;
• 基类中所有private成员在派生类中不可访问。 -
protected继承方式
• 基类中的所有public成员在派生类中为protected属性;
• 基类中的所有protected成员在派生类中为protected属性;
• 基类中的所有private成员在派生类中仍然不可访问。 -
private继承方式
• 基类中的所有public成员在派生类中均为private属性;
• 基类中的所有protected成员在派生类中均为private属性;
• 基类中的所有private成员在派生类中均不可访问。