#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; //满60秒进1分钟
++minute;
}
return *this; //返回当前对象值
}
int main( )
{
Time time1(34,0);
for (int i=0;i<61;i++)
{
++time1;
time1.display( );
}
return 0;
}
34:1 34:2 ┆ 34:59 35:0 35:1 (共输出61行)
#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++; //将自加前的对象的值赋给time2
cout<<"time1++: ";
time1.display( );
cout<<" time2 :";
time2.display( ); //输出time2对象的值
}
time1 : 34:59(time1原值) ++time1: 35:0 (执行++time1后time1的值) time1++: 35:1 (再执行time1++后time1的值) time2 : 35:0 (time2保存的是执行time1++前time1的值)
#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;} //使p指向实参字符串
void String::display( ) //输出p所指向的字符串
{cout<<p;}
int main( )
{
String string1("Hello"),string2("Book");
string1.display( );
cout<<endl;
string2.display( );
return 0;
}
Hello Book
#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( ) //输出p所指向的字符串
{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;
}
friend bool operator> (String &string1, String &string2); friend bool operator< (String &string1, String &string2); friend bool operator==(String &string1, String& string2);
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"), string3("Computer");
cout<<(string1>string2)<<endl; //比较结果应该为true
cout<<(string1<string3)<<endl; //比较结果应该为false
cout<<(string1==string2)<<endl; //比较结果应该为false
return 0;
}
1 0 0
#include <iostream>
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( ) //输出p所指向的字符串
{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;
}
Hello>Book Book<Computer Hello==Hello
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有