C++语言基础 例程 重载双目运算符

贺老师的教学链接  本课讲解

String类运算符重载函数

#include<iostream>
#include<cstring>
using namespace std;
class String
{
public:
    String( ){p=NULL;}
    String(char *str);
    friend bool operator>(String &s1, String &s2);
    friend bool operator<(String &s1, String &s2);
    friend bool operator==(String &s1, String &s2);
        friend bool operator>=(String &s1, String &s2);
    friend bool operator<=(String &s1, String &s2);
    friend bool operator!=(String &s1, String &s2);
    void display( );
private:
    char *p;
};
String::String(char *str)
{
    p = new char[strlen(str)+1];
    strcpy(p, str);
}

void String::display( )
{
    cout << p;
}

bool operator>(String &s1,String &s2)
{
    if(strcmp(s1.p,s2.p)>0)
        return true;
    else
        return false;
}

bool operator<(String &s1,String &s2)
{
    if(strcmp(s1.p,s2.p)<0)
        return true;
    else
        return false;
}

bool operator==(String &s1,String &s2)
{
    return !((s1>s2)||(s1<s2));
}

bool operator>=(String &s1,String &s2)
{
    return !(s1<s2);
}

bool operator<=(String &s1,String &s2)
{
    return !(s1>s2);
}

bool operator!=(String &s1,String &s2)
{
    return ((s1>s2)||(s1<s2));
}

void compare(String &s1,String &s2)
{
    if(s1>s2)
    {
        s1.display( );
        cout<<" > ";
        s2.display( );
    }
    else if(s1<s2)
    {
        s1.display( );
        cout<<" < ";
        s2.display( );
    }
    else if(s1==s2)
    {
        s1.display( );
        cout<<" = ";
        s2.display( );
    }
    cout<<endl;
}

int main( )
{
    String s1("Hello"),s2("Money"),s3("Girl"),s4("Hello");
    compare(s1,s2);
    compare(s2,s3);
    compare(s1,s4);
    return 0;
}
时间: 2024-07-30 08:49:54

C++语言基础 例程 重载双目运算符的相关文章

C++语言基础 例程 重载流插入运算符和流提取运算符

贺老师的教学链接  本课讲解 重载流插入运算符"<<" #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); //运算符"+"重载为成员函

C++语言基础 例程 重载单目运算符

贺老师的教学链接  本课讲解 示例1:分数类对象的相反数 class CFraction { private: int nume; // 分子 int deno; // 分母 public: CFraction(int nu=0,int de=1):nume(nu),deno(de) {} CFraction operator-(const CFraction &c); //两个分数相减,结果要化简 CFraction operator-(); //取反一目运算 }; // 分数相减 CFrac

C++语言基础 例程 函数重载

贺老师的教学链接 重载函数:同名同体,但接口不同 #include <iostream> using namespace std; int max(int a,int b,int c); //函数声明 double max(double a,double b,double c); long max(long a,long b,long c); int main( ) { int i1,i2,i3,i; cin>>i1>>i2>>i3; //输入3个整数 i=

C++语言基础 例程 文件的随机读写

贺老师的教学链接  本课讲解 示例:写到尾再从头读 #include <iostream> #include <fstream> #include <cstdlib> using namespace std; int main( ) { int a[10], b[10]; fstream iofile("f1.dat",ios::in|ios::out); if(!iofile) { cerr<<"open error!&quo

C++语言基础 例程 不同类型数据间的转换

贺老师的教学链接  本课讲解 //类型转换函数应用 #include <iostream> using namespace std; class Complex { public: Complex( ) { real=0; imag=0; } Complex(double r,double i) { real=r; imag=i; } operator double( ); friend Complex operator + (Complex c1,Complex c2); private:

C++语言基础 例程 案例:Time类的设计

贺老师的教学链接  本课讲解 #include <iostream> using namespace std; class CTime { private: unsigned short int hour; // 时 unsigned short int minute; // 分 unsigned short int second; // 秒 public: CTime(int h=0,int m=0,int s=0); void setTime(int h,int m,int s); //输

C++语言基础 例程 虚析构函数

贺老师的教学链接  本课讲解 问题的由来 #include <iostream> using namespace std; class Point { public: Point( ) { } ~Point() { cout<<"executing Point destructor"<<endl; } }; class Circle:public Point { public: Circle( ) { } ~Circle( ) { cout<&

C++语言基础 例程 应用系统开发:银行储蓄系统

贺老师的教学链接  本课讲解 说明:(1)下面的代码,只演示了利用链表作为存储结构的可选处理方法,本讲提到的其他方面的拓展,请感兴趣做下去的同学自行使用相关技术组合起来,形成一个完整的系统.(2)运行程序,登录用户名和密码,请阅读程序,从程序中找出.建议建立多文件项目,将代码拷贝到IDE中看.(3)本程序由我的2011级学生刘镇参加企业组织的实训中完成,原文在:点击打开链接 Record.h #ifndef HEADER_RECORD //条件编译 #define HEADER_RECORD #

C++语言基础 例程 C++中的输入和输出

贺老师的教学链接 程序将自行识别符号 #include <iostream> using namespace std; int main() { int a,b; char op; cin>>a>>op>>b cout<<"result: "<<'\n'; cout<<"a: "<<a<<'\n'; cout<<"b: "<