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));
?????? }
}
轉載于:https://www.cnblogs.com/sdluReaLo/archive/2013/03/08/2949874.html
總結
- 上一篇: 获取css样式
- 下一篇: c++中在堆和栈中申请空间的差别