C++ <四>
//运算符函数, 重载。
#include<iostream>
using namespace std;
class A{
int data;
public:
A(int d=0):data(d){
}
void show(){
cout<<"data1="<<data<<endl;
}
//friend A operator-(const A& a1,const A& a2);//授权,声明在类的内部声明,声明可以和定义写在一起。
A operator-(const A& o)
{
cout<<"相信我,没错。";
int dif=data-o.data;
return A(dif);
}
};
// A operator-(const A& a1,const A& a2){//友元不是成员,其中没有this。没有当前对象
// cout<<"相信我,没错。"<<endl;
// int sum=a1.data-a2.data;//一般用引用而不是复制一份。
// return A(sum);//初始化:A a1=obj1; A a2(obj2);
//}
int main()
{
A a1(80),a2(50);
A a3,a4;
// a3= operator-(a1,a2);//可以使用operator运算符作为函数名
//a3.show();
//a4=a1-a2;//这种类型的函数可以直接写成运算符的形式
//a4.show();//更像数学上的式子
a3=a1.operator-(a2);
a3.show();
a4=a1-a2;
a4.show();
}
//运算符重载就是自己写运算符函数,来规定也能算符如何工作。
*/
/*
//写一个分数相乘的运算符函数。
#include<iostream>
using namespace std;
class Fenshu{
int fz;
int fm;
public:
Fenshu(int fz=0,int fm=1):fz(fz),fm(fm){}
friend Fenshu operator *( const Fenshu& a1,const Fenshu& a2);
//友元函数不是成员函数。不能继承;
friend ostream& operator<< (ostream& os,const Fenshu& f)
{
os<<f.fz<<'/'<<f.fm;
return os;
}
friend istream& operator>>(istream& is,Fenshu &f)
{
char c;
is>>f.fz>>c>>f.fm;
return is;//变量
}
};
Fenshu operator *( const Fenshu& a1,const Fenshu& a2)
{
int zo=a1.fz*a2.fz;
int mo=a1.fm*a2.fm;
return Fenshu(zo,mo);
}
int main()
{
//int a,b;
//cin>>a>>b;//自定义<<操作符。
Fenshu a1(2,3);
Fenshu a2(4,5);
Fenshu a3;
a3=a1*a2;
cout<<a3<<endl;
Fenshu f1,f2;
cin>>f1>>f2;
cout<<f1*f2<<endl;
}
*/
/*
#include<iostream>
using namespace std;
class A{
int data;
public:
A(int d=0):data(d){}
friend ostream& operator<<(ostream& os,const A&a)
{
os<<a.data;
return os;
}
friend istream& operator>>(istream & is,A &a)
{
is>>a.data;
return is;
}
friend A& operator++(A& a)
{
a.data+=10;
return a;
}
A& operator--()
{
data-=1;
return *this;
}
operator int ()//自动转换类型
{
return data;
}
operator bool()
{
return data!=0;
}
operator char()
{
return (char)data;
}
};
int main()
{
A a(100);//至少有一个操作数是自定义类型的
cout<<(++a)<<endl;
cout<<(--a)<<endl;// a--/a++ 可看成双目运算符
A c(65),c2(290);
cout<<"c="<<(char)c<<endl;
int d=c2;
if(c2)
cout<<"good"<<endl;
else
cout<<"bad"<endl;
*/
//对自定义类型的对象,使用运算符时总是调用相应运算符函数。
//强制类型能够转换 类型(数据) 不需要写返回类型 operator 类型()
//单目运算符不能重载。
// =,(),[],->,->* ,类型转换只能利用成员
/* 运算符的重载:
双目: 友元形式: 返回类型 operator X(形参1,形参2)
成员形式: 返回类型 operator X(形参)
单目:
友元 返回 operator x(形参)
返回 operator X()
输入、输出只能以友元函数
作业: 复数类 Complex 构造函数: 实部,虚部,重载运算符: +,-,》》,《《,
类型转换 (double) 实部平方+虚部平方 再sqrt ,
~用来交换实部和虚部
*/
/*
#include<iostream>
#include<math.h>
using namespace std;
class Complex{
int as;
int ax;
public:
Complex(int a=0,int b=0):as(a),ax(b){}
Complex& operator+(const Complex& f)
{
return Complex(as+f.as,ax+f.ax);
}
Complex& operator-(const Complex& f)
{
return Complex(as-f.as,ax-f.ax);
}
friend ostream& operator<<(ostream & os,Complex f)//输入输出只能用友元形式来实现运算符的重载;
{
cout<<f.as<<"+j"<<f.ax;
return os;
}
friend istream& operator >>(istream& is,Complex f)
{
char c1,c2;
if(f.ax>=0)
cin>>f.as>>c1>>c2>>f.ax;
else if(f.ax<0)
cin>>f.as>>c1>>f.ax;
return is;
}
operator double()
{
return (double)sqrt(ax*ax+as*as);
}
Complex& operator~()
{
// int x;
// x=as;
// as=ax;
// ax=x;
swap(as,ax);
return Complex(as,ax);
}
};
int main()
{
Complex A(2,1);
Complex B(3,4);
cout<<A<<endl;
cout<<A<<"+"<<B<<'='<<A+B<<endl;
cout<<~A<<","<<~B<<endl;
}
*/
//-----------------------day_12_am 做好平凡的事情----------------------
// 【day12_am】
//回顾: 一. 拷贝构造函数:有创建对象的时候调用其的参数形式来
//决定调用哪一个拷贝构造函数,A(const A&0), 默认构造函数回绝
//形参对象的类容全部复制一份,函数语句块, 执行函数中的东西。
// 运算符的重载: 双目,单目,成员形式(单形参),友元形式(双形参);
//特殊的运算符:>>,<<只能运用友元形式;后++,后--;类型转换:不写返回类型
// 运算符重载的作用是:更适合人们的使用习惯, 让程序更加清晰;
// 重载的条件为: 至少有一个操作数为自定义类型;运算符重载也能继承;
/*
#include<iostream>
using namespace std;
//尽力而为,对个人的最大收获;定位很重要, 确定可实现的位置。
//踏实就好,理解,理解
// 把平凡的事情做好了便不再平凡;
//
class A{
public:
operator int(){
cout<<"[int]";
return 100;
}
};
int main()
{
A obj;
int n=(int)obj;//这个时候有输出【int】
cout<<"n="<<n<<endl;
cout<<"obj="<<obj<<endl;
}
*/
// I/o, I: 控制台-c,文件-f; O: (C、f控制台完全统一的)
//cout/fout
// O:输出【写出】,向屏幕【控制台】【写】送出数据,向文件写
//I:输入【读入】,从键盘【控制台】【读】、从文件读
// 数据的传输,stream 全部有关I/O的声明都在<iostream>头文件中;
//cin 键盘 标准输入设备
//cout屏幕 标准输出对象
// 数据流【stream】可以改向,缓冲;输出<—满/清-> 显示
//输入的过程: 键盘->键盘缓冲区->(回车)输入缓冲区->程序读取
/*
#include<iostream>
using namespace std;
#include<ctime>
int main()
{
/*
char ch,ch2,ch3;
cout<<"input a char :"<<endl;
cin>>ch;
cout<<"ch="<<ch<<endl;
cout<<"input a char :"<<endl;
cin>> ch2;
cout<<"ch2="<<ch2<<endl;
cout<<"input a char :"<<endl;
cin>>ch3;
cout<<ch3<<endl;
cerr<<ch3<<"hello"<<endl;
//cout<<"hello";
cerr<<"world";
for(int i=0;i<5;i++){
time_t t=time(NULL);
while(time(NULL)==t);
}
cout<<endl;
//回车后进入【输入缓冲区】中,只要缓冲区中还有数据则直接从缓冲区中读取;
//cerr,clog用来输出时没有缓冲,也不能重定向 其用法和cout完全相同;
}//文件IO和控制台的IO完全一致;
*/
// 文件类型的准备:读入,写出; ofstream fout("a.text");
// ofstream 变量名 ("文件名");fout<</ fout.close();
// ifstream fin("a.text"); fin>>变量名/ fin.close();
/*
#include<iostream>
using namespace std;
#include<fstream>
#include<string>
int main()
{
ofstream fout("a.txt");//当前目录下生成文件a.txt
fout<<"hello, world!"<<endl;// (void*)
fout<<123<<endl;
fout<<4.5*6<<endl;
fout<<'V'<<endl;
fout.close();
string str=" ";
int n=0;
double d=0.0;
char c='/0';
ifstream fin("a.txt");
getline(fin,str);
fin>>n>>d>>c;
cout<<"n="<<n<<endl;
cout<<"d="<<d<<endl;
cout<<"c="<<c<<endl;
fin.close();
//cout<<cout<<endl;//输出的为地址、、类型转换(void*),
//如果输入输出出错了则输出为NULL
//cout<<cin<<endl; // 输出为地址
//一个O流对象在没有出错的时候可以当成真,在出错的时候可以当成假;
//即可以看成是bool类型
}
*/
/*
#include<iostream>
using namespace std;
#include<fstream>
int main()
{
ofstream fout("\abc");//不可以的时候执行出错, 输出为NULL
ifstream fin("\abc");
cout<<fout<<endl;
cout<<fin<<endl;
ofstream fout1("abc");
ifstream fin1("abc");
cout<<fout1<<endl;
cout<<fin1<<endl;
}
*/
// --------------------------态度决定一切-----------------------
/*
统计文件的行数
#include<iostream>
using namespace std;
#include<fstream>
#include<string>
int main()
{
char name[256];
cin>>name;
ifstream fin(name);
int n=0;
for(;;){
string str;
getline(fin,str);
if(!fin) break;
n++;
}
fin.close();
cout<<n<<endl;
}
*/
/*
#include<iostream>
using namespace std;
class A{
int n;
public:
A(int d):n(d){}
friend ostream& operator<<(ostream& os,A&o)//运算符>>,<<只能以友元形式
{
cout<<o.n;
return os;
}
};
int main()
{
A s(100);
cout<<s<<endl;
//输入:将键盘输入的各种字符转换成数据,
//输出: 将各种数据以一串字符从屏幕输出
//格式化输入输出
cout<<endl;
}
*/
//1)非格式化输入:get(),getline(),read(),ignore(),peek(),putback();
//get()操作(无参),从输入流中读取一个字读,返回字符的ASCII码。从流中取走,不再存在、
//
/*
#include<iostream>
using namespace std;
int main()
{
cout<<"please Input 2 chars:;
//若输入: aB
cout<<cin.get()<<endl;//取值:a
cout<< (char)cin.get()<<endl;//取值 :B
}
*/
//2)【get(char&)】操作:从输入字符流中读取一个字符保存到形参中;
//用">>"输入时会跳过空白字符; 例如:空格, 换行等
/*
#include<iostream>
using namespace std;
int main(){
char a,b,c,d;
cout<<"input 4 char:";
a=cin.get();
cin.get(b);
cin.get(c).get(d);//返回任然为istream类型的引用:但不提倡这样使用
cout<<"a=["<<a<<"]"<<endl;
cout<<"b=]"<<b<<"]"<<endl;
cout<<"c=["<<c<<"]"<<endl;
cout<<"d=["<<d<<"]"<<endl;
}
*/
/*
#include<iostream>
using namespace std;
#include<fstream>
#include<ctime>
void wait_n_minute(int n);
int main()
{
ifstream fin("a.txt");
for(;;){
if(!fin) break;//没出错的情况下;
wait_n_minute(2);
#define COUT cout<<(char)fin.get()<<flush;
COUT;COUT;COUT;COUT;COUT;
//cout<<(char)fin.get()<<flush;
//cout<<(char)fin.get()<<flush;
//cout<<(char)fin.get()<<flush;
//cout<<(char)fin.get()<<flush;
}
fin.close();
return 0;
}
void wait_n_minute(int n)
{
for(int i=n;i!=0;i--)
{
time_t t=time(NULL);
while(t==time(NULL));
}
}