23 运算符重载(二)

++运算符重载

前置++运算符重载
成员函数的方式重载,原型为:
函数类型 & operator++();
友元函数的方式重载,原型为:
friend 函数类型 & operator++(类类型 &);
后置自增和后置自减的重载
成员函数的方式重载,原型为:
函数类型 & operator++(int);
友元函数的方式重载,原型为:
friend函数类型 & operator++(类类型 &,int);

//Integer.h

#ifndef _INTEGER_H_
#define _INTEGER_H_

class Integer
{
public:
    Integer(int n);
    ~Integer();

    Integer& operator++();
    //friend Integer& operator++(Integer& i);

    Integer operator++(int n);
    //friend Integer operator++(Integer& i, int n);

    void Display() const;
private:
    int n_;
};

#endif // _INTEGER_H_

//Integer.cpp
#include "Integer.h"
#include <iostream>
using namespace std;

Integer::Integer(int n) : n_(n)
{
}

Integer::~Integer()
{
}

Integer& Integer::operator ++()
{
    //cout<<"Integer& Integer::operator ++()"<<endl;
    ++n_;
    return *this;
}

//Integer& operator++(Integer& i)
//{
//  //cout<<"Integer& operator++(Integer& i)"<<endl;
//  ++i.n_;
//  return i;
//}

Integer Integer::operator++(int n)
{
    //cout<<"Integer& Integer::operator ++()"<<endl;
    //n_++;
    Integer tmp(n_);
    n_++;//n_本身++
    return tmp;//返回一个临时对象,其没有++
}

//Integer operator++(Integer& i, int n)
//{
//  Integer tmp(i.n_);
//  i.n_++;
//  return tmp;
//}

void Integer::Display() const
{
    cout<<n_<<endl;
}

//main.cpp

#include "Integer.h"
#include <iostream>
using namespace std;


int main(void)
{
    Integer n(100);
    n.Display();

    Integer n2 = ++n;
    n.Display();
    n2.Display();

    Integer n3 = n++;
    n.Display();
    n3.Display();



    return 0;
}

输出

//String.h
#ifndef _STRING_H_
#define _STRING_H_

class String
{
public:
    explicit String(const char* str="");
    String(const String& other);
    String& operator=(const String& other);
    String& operator=(const char* str);

    bool operator!() const;
    ~String(void);

    void Display() const;

private:
    char* AllocAndCpy(const char* str);
    char* str_;
};

#endif // _STRING_H_

//String.cpp
#pragma warning(disable:4996)
#include "String.h"
#include <string.h>
#include <iostream>
using namespace std;

String::String(const char* str)
{
    str_ = AllocAndCpy(str);
}

String::String(const String& other)
{
    str_ = AllocAndCpy(other.str_);
}

String& String::operator=(const String& other)
{
    if (this == &other)
        return *this;

    delete[] str_;
    str_ = AllocAndCpy(other.str_);
    return *this;
}

String& String::operator=(const char* str)
{
    delete[] str_;
    str_ = AllocAndCpy(str);
    return *this;
}

bool String::operator!() const
{
    return strlen(str_) != 0;
}

String::~String()
{
    delete[] str_;
}

char* String::AllocAndCpy(const char* str)
{
    int len = strlen(str) + 1;
    char* newstr = new char[len];
    memset(newstr, 0, len);
    strcpy(newstr, str);

    return newstr;
}

void String::Display() const
{
    cout<<str_<<endl;
}

//main.cpp
#include "String.h"
#include <iostream>
using namespace std;


int main(void)
{
    String s1("abc");
    String s2(s1);

    String s3;
    s3 = s1;//调用=运算符重载 String& operator=(const String& other);
    s3.Display();

    s3 = "xxxx";//调用=运算符重载 String& operator=(const char* str);
    s3.Display();

    return 0;
}

输出

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值