?? 運(yùn)算符重載.txt
字號:
//**************************
//** ch18_1.cpp **
//**************************
#include <iostream.h>
class RMB
{
public:
RMB(double d)
{
yuan=d;
jf=(d-yuan)*100;
}
RMB interest(double rate);
RMB add(RMB d);
void display()
{
cout<<(yuan + jf/100.0)<<endl;
}
RMB operator + (RMB d)
{
return RMB(yuan+d.yuan+(jf+d.jf)/100.0);
}
RMB operator * (double rate)
{
return RMB((yuan+jf/100)*rate);
}
private:
unsigned int yuan;
unsigned int jf;
};
RMB RMB::interest(double rate)
{
return RMB((yuan+jf/100.0)*rate);
}
RMB RMB::add(RMB d)
{
return RMB(yuan+d.yuan+jf/100.0+d.jf/100.0);
}
//functions
RMB expense1(RMB principle,double rate)
{
RMB interest=principle.interest(rate);
return principle.add(interest);
}
RMB expense2(RMB principle,double rate)
{
RMB interest = principle * rate;
return principle + interest;
}
//main function
void main()
{
RMB x=10000.0;
double yrate=0.035;
expense1(x,yrate).display();
expense2(x,yrate).display();
}
//**************************
//** ch18_2.cpp **
//**************************
#include <iostream.h>
class RMB
{
public:
RMB(unsigned int d,unsigned int c);
friend RMB operator + (RMB&, RMB&);
friend RMB operator ++ (RMB&);
void display()
{
cout<<(yuan + jf/100.0)<<endl;
}
protected:
unsigned int yuan;
unsigned int jf;
};
RMB::RMB(unsigned int d,unsigned int c)
{
yuan=d;
jf=c;
while(jf>=100)
{
yuan++;
jf-=100;
}
}
RMB operator + (RMB& s1, RMB& s2)
{
unsigned int jf=s1.jf + s2.jf;
unsigned int yuan=s1.yuan + s2.yuan;
RMB result(yuan,jf);
return result;
}
RMB operator ++ (RMB& s)
{
s.jf++;
if(s.jf>=100)
{
s.jf-=100;
s.yuan++;
}
return s;
}
void main()
{
RMB d1(1,60);
RMB d2(2,50);
RMB d3(0,0);
d3=d1 + d2;
++d3;
d3.display();
}
//for test
/*
void main()
{
RMB d1(1,60);
RMB d2(2,50);
RMB d3(0,0);
d3=d1 + d2;
++d3;
d3.display();
d3=d2++;
d3=++d2;
d3=++(++d2);
d3.display();
d2.display();
cout<<&d3<<endl;
cout<<&d2<<endl;
}
*/
//**************************
//** ch18_3.cpp **
//**************************
#include <iostream.h>
class RMB
{
public:
RMB(unsigned int d,unsigned int c);
RMB operator + (RMB&);
RMB& operator ++ ();
void display()
{
cout<<(yuan + jf/100.0)<<endl;
}
protected:
unsigned int yuan;
unsigned int jf;
};
RMB::RMB(unsigned int d,unsigned int c)
{
yuan=d;
jf=c;
while(jf>=100)
{
yuan++;
jf-=100;
}
}
RMB RMB::operator + (RMB& s)
{
unsigned int c=jf + s.jf;
unsigned int d=yuan + s.yuan;
RMB result(d,c);
return result;
}
RMB& RMB::operator ++ ()
{
jf++;
if(jf>=100)
{
jf-=100;
yuan++;
}
return *this;
}
void main()
{
RMB d1(1,60);
RMB d2(2,50);
RMB d3(0,0);
d3=d1 + d2;
++d3;
d3.display();
//for test
RMB s(5.8);
s=RMB(1.5)+s;
s.display();
//s=s+1.5;
//s.display();
s=1.5+6.4;
s.display();
}
//**************************
//** ch18_4.cpp **
//**************************
#include <iostream.h>
class Increase
{
public:
Increase(int x):value(x) {}
Increase& operator ++(); //前增量
Increase operator ++(int); //后增量
void display()
{
cout<<"the value is "<<value<<endl;
}
private:
int value;
};
Increase& Increase::operator ++()
{
value++; //先增量
return *this; //再返回原對象
}
Increase Increase::operator ++(int)
{
Increase temp(*this); //臨時對象存放原有對象值
value++; //原有對象增量修改
return temp; //返回原有對象值
}
void main()
{
Increase n(20);
n.display();
(n++).display(); //顯示臨時對象值
n.display(); //顯示原有對象
++n;
n.display();
++(++n);
n.display();
((n++)++).display(); //注意這里顯示的值
n.display();
}
//**************************
//** ch18_5.cpp **
//**************************
#include <iostream.h>
class Increase
{
public:
Increase(int x):value(x) {}
friend Increase& operator ++(Increase&a); //前增量 注意參數(shù)的修改
friend Increase operator ++(Increase&a, int); //后增量 注意參數(shù)的修改
void display()
{
cout<<"the value is "<<value<<endl;
}
private:
int value;
};
Increase& operator ++(Increase& a)
{
a.value++; //先增量
return a; //再返回原對象
}
Increase operator ++(Increase&a, int)
{
Increase temp(a); //臨時對象存放原有對象值
a.value++; //原有對象增量修改
return temp; //返回原有對象值
}
void main()
{
Increase n(20);
n.display();
(n++).display(); //顯示臨時對象值
n.display(); //顯示原有對象
++n;
n.display();
++(++n);
n.display();
((n++)++).display(); //注意這里顯示的值
n.display();
}
//**************************
//** ch18_6.cpp **
//**************************
#include <iostream.h>
class RMB
{
public:
RMB(double value=0.0);
operator double()
{
return yuan+jf/100.0;
}
void display()
{
cout<<"total value:"<<(yuan+jf/100.0)<<endl;
cout<<"yuan:"<<yuan<<endl
<<"jf:"<<jf<<endl;
}
protected:
unsigned int yuan;
unsigned int jf;
};
RMB::RMB(double value)
{
//cout<<"in constructor:"<<value<<endl;
yuan=value;//cout<<"yuan:"<<yuan<<" jf:";
jf=(value-yuan)*100.0+0.5;
//cout<<jf<<endl;
}
void main()
{
RMB d1(1.11),d2(1.32),d3;
d1.display();
d2.display();
//d3=RMB((double)d1 + (double)d2); //顯式轉(zhuǎn)換
d3 = d1 + d2;
d3.display();
}
//**************************
//** ch18_7.cpp **
//**************************
#include <iostream.h>
#include <string.h>
class Name
{
public:
Name() //默認(rèn)構(gòu)造函數(shù)
{
pName=0;
}
Name(char* pn) //構(gòu)造函數(shù)
{
copyName(pn);
}
Name(Name &s) //拷貝構(gòu)造函數(shù)
{
copyName(s.pName);
}
~Name() //析構(gòu)函數(shù)
{deleteName();}
Name& operator = (Name &s) //運(yùn)算符“=”重載,注意返回值是Name&,即調(diào)用對象本身
{
deleteName();
copyName(s.pName);
return *this;
}
void display()
{
cout<<pName<<endl;
}
protected:
void copyName(char *pN);
void deleteName();
char* pName;
};
void Name::copyName(char* pN)
{
pName= new char[strlen(pN)+1];
if(pName)
{
strcpy(pName,pN);
}
}
void Name::deleteName()
{
if(pName)
{
delete pName;
pName=0;
}
}
void main()
{
Name s("claudette");
Name t("temporary");
t.display();
t = s;
t.display();
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -