//4.1通过函数来实现复数的相加(没用运算符的重载)
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(){real=0;imag=0;};
Complex(double r,double i){real=r;imag=i;};
Complex complex_add(Complex &c2);
void display();
private:
double real;
double imag;
};
Complex Complex::complex_add(Complex &c2)
{
Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
void Complex::display()
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main()
{
Complex c1(3,4),c2(5,-10),c3;
c3=c1.complex_add(c2);
cout<<"c1=";
c1.display();
cout<<"c2=";
c2.display();
cout<<"c1+c3=";
c3.display();
return 0;
}
//4.2改写4.1重载运算符“+”。使之能用于两个负数相加
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
Complex operator +(Complex &c2); //重载“+”
void display();
private:
double real;
double imag;
};
Complex Complex::operator +(Complex &c2)
{
Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
void Complex::display()
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main()
{
Complex c1(2,4),c2(3,-12),c3;
c3=c1+c2;
cout<<"c1=";
c1.display();
cout<<"c2=";
c2.display();
cout<<"c1+c2=";
c3.display();
return 0;
}
//4.3将运算符“+”重载为适用于复数加法;重载函数不作为成员函数;而放在类外;作为complex类的友元函数
#include <iostream.h>//当使用友元函数的时候,visual c++6.0不能用标准的c++格式 要加.h 去掉using namespace std
//using namespace std;
class Complex
{
private:
double real;
double imag;
public:
Complex(){real =0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
friend Complex operator +(Complex &c1,Complex &c2);
void display();
};
Complex operator +(Complex &c1,Complex &c2)
{
return Complex(c1.real+c2.real,c1.imag+c2.imag);
}
void Complex::display()
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main()
{
Complex c1(3,4),c2(5,-10),c3;
c3=c1+c2;
cout<<"c1=";
c1.display();
cout<<"c2=";
c2.display();
cout<<"c1+c2=";
c3.display();
return 0;
}
//4.4定义一个字符串类string;用来存放不定长的字符串;
//重载运算符“==”;“<”;“>”用于两个字符串的等于、小于和大于的比较运算,注意友元函数的时候 开头<iostream.h>
//no.1 先建立一个string类
#include <iostream>
using namespace std;
class String
{
public:
String(){p=NULL;}
String(char *str);
void display();
private:
char *p;
};
String ::String(char *str)
{
p=str;
}
void String::display()
{
cout<<p;
}
int main()
{
String string1("Hello"),string("Book");
string1.display();
cout<<endl;
string2.display();
return 0;
}
//no.2 先重载运算符“>”
#include <iostream>
#include <string>
using namespace std;
class String
{
public:
String(){p=NULL;}
String(char *str);
friend bool operator >(String &string1,String &string2);
void display();
private:
char *p;
};
String ::String(char *str)
{
p=str;
}
void String::display()
{
cout<<p;
}
bool operator >(String &string1,String &string2)
{
if((strcmp(string1.p,string2.p)>0)
return true;
else
return false;
}
int main()
{
String string1("Hello"),string2("Book");
cout<<(string1>string2)<<endl;
return 0;
}
//no.3扩展到对3个运算符重载
#include <iostream>
#include <string>
using namespace std;
class String
{
public:
String(){p=NULL;}
String(char *str);
friend bool operator >(String &string1,String &string2);
friend bool operator <(String &string1,String &string2);
friend bool operator ==(String &string1,String &string2);
void display();
private:
char *p;
};
String ::String(char *str)
{
p=str;
}
void String::display()
{
cout<<p;
}
bool operator >(String &string1,String &string2)
{
if((strcmp(string1.p,string2.p)>0)
return true;
else
return false;
}
bool operator <(String &string1,String &string2)
{
if((strcmp(string1.p,string2.p)<0)
return true;
else
return false;
}
bool operator ==(String &string1,String &string2)
{
if((strcmp(string1.p,string2.p)==0)
return true;
else
return false;
}
int main()
{
String string1("Hello"),string2("Book"),string("Computer");
cout<<(string1>string2)<<endl;
cout<<(string1<string3)<<endl;
cout<<(string1==string2)<<endl;
return 0;
}
//no.4最后的完善
#include <iostream.h>
#include <string>
class String
{
public:
String(){p=NULL;}
String(char *str);
friend bool operator >(String &string1,String &string2);
friend bool operator <(String &string1,String &string2);
friend bool operator ==(String &string1,String &string2);
void display();
private:
char * p;
};
String ::String(char *str)
{
p=str;
}
void String::display()
{
cout<<p;
}
bool operator >(String &string1,String &string2)
{
if(strcmp(string1.p,string2.p)>0)
return true;
else
return false;
}
bool operator <(String &string1,String &string2)
{
if(strcmp(string1.p,string2.p)<0)
return true;
else
return false;
}
bool operator ==(String &string1,String &string2)
{
if(strcmp(string1.p,string2.p)==0)
return true;
else
return false;
}
void compare(String &string1,String &string2)
{
if(operator >(string1,string2)==1)
{
string1.display();
cout<<">";
string2.display();
}
else
if(operator <(string1,string2)==1)
{
string1.display();
cout<<"<";
string2.display();
}
else
if(operator ==(string1,string2)==1)
{
string1.display();
cout<<"==";
string2.display();
}
cout<<endl;
}
int main()
{
String string1("Hello"),string2("Book"),string3("Computer"),string4("Hello");
compare(string1,string2);
compare(string2,string3);
compare(string1,string4);
return 0;
}
//4.5有一个Time类,包含数据成员minute(分)和sec(秒),模拟秒表,每次走一秒,满60秒进一分钟,此时秒又从0
//开始算,要求输出分和秒的值
#include <iostream>
using namespace std;
class Time
{
public:
Time(){minute=0;sec=0;}
Time(int m,int s):minute(m),sec(s){}
Time operator++();
void display()
{
cout<<minute<<":"<<sec<<endl;
}
private:
int minute;
int sec;
};
Time Time::operator++()
{
if(++sec>=60)
{
sec-=60;
++minute;
}
return *this;
}
int main()
{
Time time1(34,0);
for(int i=0;i<61;i++)
{
++time1;
time1.display();
}
return 0;
}
//4.6在例4.5的基础上增加对后置自增运算符的重载
#include <iostream>
using namespace std;
class Time
{
public:
Time(){minute=0;sec=0;}
Time(int m,int s):minute(m),sec(s){}
Time operator++();
Time operator++(int);//后置++
void display()
{
cout<<minute<<":"<<sec<<endl;
}
private:
int minute;
int sec;
};
Time Time::operator++()
{
if(++sec>=60)
{
sec-=60;
++minute;
}
return *this;
}
Time Time::operator++(int)//后置++
{
Time temp(*this);
sec++;
if(sec>=60)
{
sec-=60;
++minute;
}
return temp;
}
int main()
{
Time time1(34,59),time2;
cout<<"time1:";
time1.display();
++time1;
cout<<"++time1:";
time1.display();
time2=time1++;
cout<<"time1++";
time1.display();
cout<<"time2:";
time2.display();
return 0;
}
//4.7在4.2的基础上。用重载“<<”输出复数
#include <iostream.h>
class Complex
{
public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
Complex operator +(Complex &c2);
friend ostream& operator<<(ostream&,Complex&);
private:
double real;
double imag;
};
Complex Complex::operator +(Complex &c2)
{
Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
ostream & operator<<(ostream& output,Complex& c)
{
output<<"("<<c.real<<"+"<<c.imag<<"i)"<<endl;
return output;
}
int main()
{
Complex c1(2,4),c2(6,10),c3;
c3=c1+c2;
cout<<c3;
return 0;
}
//4.8在4.7的基础上。增加重载流提取运算符“>>”,用“cin>>”输入复数;用“cout<<”
//输出复数
#include <iostream.h>
class Complex
{
public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
friend istream& operator>>(istream&,Complex&);
friend ostream& operator<<(ostream&,Complex&);
private:
double real;
double imag;
};
istream & operator>>(istream & input,Complex& c)
{
cout<<"inpyt real part and imaginary part of complex number:";
input>>c.real>>c.imag;
return input;
}
ostream & operator<<(ostream& output,Complex& c)
{
output<<"("<<c.real<<"+"<<c.imag<<"i)"<<endl;
return output;
}
int main()
{
Complex c1,c2;
cin>>c1>>c2;
cout<<"c1="<<c1<<endl;
cout<<"c2="<<c2<<endl;
return 0;
}
//4.9使用类型转换函数的简单例子
#include <iostream>
using namespace std;
class Complex
{
public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
operator double(){return real;}//类型转换函数,把类对象转化为double型数据
private:
double real;
double imag;
};
int main()
{
Complex c1(3,4),c2(5,-10),c3;
double d;
d=2.5+c1;//调用double() 函数
cout<<d<<endl;
return 0;
}
//4.10包含转换构造函数、运算符重载函数和类型转换函数的程序
#include <iostream.h>
class Complex
{
public:
Complex(){real=0;imag=0;}
Complex(double r){real=r;imag=0;}//有时带参数的默认函数可以当转换构造函数
Complex(double r,double i){real=r;imag=i;}
friend Complex operator +(Complex c1,Complex c2);
void display();
private:
double real;
double imag;
};
Complex operator +(Complex c1,Complex c2)
{
return Complex(c1.real+c2.real,c1.imag+c2.imag);
}
void Complex::display()
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main()
{
Complex c1(3,4),c2(5,-10),c3;
c3=2.5+c1;
c3.display();
return 0;
}