C++ 复制构造函数和运算符重载示例

string1.h

//
// Created by lance on 10/16/18.
//

#ifndef CPP_PRIMER_STRING1_H
#define CPP_PRIMER_STRING1_H

#include <iostream>

using std::ostream;
using std::istream;

class String {
private:
    // pointer to string
    char *str;

    // length of string
    int len;

    // number of objects
    static int numStrings;

    // cin input limit
    static const int CIN_LIM = 80;
public:

    // constructors
    String();

    String(const char *s);

    String(const String &st);

    ~String();

    // other methods
    int length() const {
        return this->len;
    }

    void showAddr();

    // overloaded operator methods
    String &operator=(const String &st);

    String &operator=(const char *s);

    char &operator[](int i);

    const char &operator[](int i) const;

    // overloaded operator friends
    friend bool operator<(const String &st1, const String &st2);

    friend bool operator>(const String &st1, const String &st2);

    friend bool operator==(const String &st1, const String &st2);

    friend ostream &operator<<(ostream &os, const String &st);

    friend istream &operator>>(istream &is, String &st);

    // static function
    static int howMany();

};


#endif //CPP_PRIMER_STRING1_H

string1.cpp

//
// Created by lance on 10/16/18.
//
#include <cstring>
#include "string1.h"

using std::cout;
using std::endl;

// initializing static class member
int String::numStrings = 0;

int String::howMany() {
    return numStrings;
}

void String::showAddr() {
    cout << "memory addr: " << this << endl;
}

// class methods
String::String() {
    cout << "========call String()" << endl;
    this->len = 4;
    str = new char[1];
    str[0] = '\0';
    numStrings++;
}

String::String(const char *s) {
    cout << "========call String(const char *s)" << endl;
    len = std::strlen(s);
    str = new char[len + 1];
    std::strcpy(str, s);
    numStrings++;
}

String::String(const String &st) {
    cout << "========call String(const String &st)" << endl;
    numStrings++;
    len = st.len;
    str = new char[len + 1];
    std::strcpy(str, st.str);
}

String::~String() {
    --numStrings;
    delete[] str;
}

// overloaded operator methods

// assign a String to a String
String &String::operator=(const String &st) {
    cout << "========call operator=(const String &st)" << endl;
    if (this == &st) {
        return *this;
    } else {
        delete[] str;
        len = st.len;
        str = new char[len + 1];
        std::strcpy(str, st.str);
        return *this;
    }
}

// assign a C string to a String
String &String::operator=(const char *s) {
    cout << "========call operator=(const char *s)" << endl;
    delete[] str;
    len = std::strlen(s);
    str = new char[len + 1];
    std::strcpy(str, s);
    return *this;
}

// read-write char access for non-const String
char &String::operator[](int i) {
    return str[i];
}

// read-only char access for const String
const char &String::operator[](int i) const {
    return str[i];
}

// overloaded operator friends

bool operator<(const String &st1, const String &st2) {
    return (std::strcmp(st1.str, st2.str) < 0);
}

bool operator>(const String &st1, const String &st2) {
    return st2 < st1;
}

bool operator==(const String &st1, const String &st2) {
    return (std::strcmp(st1.str, st2.str) == 0);
}

// simple String output
ostream &operator<<(ostream &os, const String &st) {
    os << st.str;
    return os;
}

// quick and dirty String input
istream &operator>>(istream &is, String &st) {
    char temp[String::CIN_LIM];
    is.get(temp, String::CIN_LIM);
    if (is) {
        st = temp;
    }
    while (is && is.get() != '\n') {
        continue;
    }
    return is;
}

vegnews.cpp

//
// Created by lance on 10/17/18.
//

#include <iostream>
#include "string1.h"

using std::cout;
using std::endl;

void callme1(String &str);

void callme2(String str);

int main(void) {
    cout << "Starting an inner block." << endl;

    String headline1("Celery Stalks at Midnight");
    cout << "memory addr: " << &headline1 << endl;
    headline1.showAddr();

    callme1(headline1);
    callme2(headline1);

    String headlineCp1 = headline1;
    callme1(headlineCp1);

    String headlineCp2;
    headlineCp2 = headline1;
    callme1(headlineCp2);

    String headline2 = "Lettuce Prey";
    callme1(headline2);

    String sports;
    sports = "Spinach Leaves Bowl for Dollars";
    callme1(sports);

    return 0;
}


void callme1(String &str) {
    cout << "String passed by reference: " << endl;
    cout << "   \"" << str << "\"" << endl;
    str.showAddr();
}

void callme2(String str) {
    cout << "String passed by value: " << endl;
    cout << "   \"" << str << "\"" << endl;
    str.showAddr();
}


特别注意

    String headline2 = "Lettuce Prey";

    String sports;
    sports = "Spinach Leaves Bowl for Dollars";

是完全不同的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值