zoukankan      html  css  js  c++  java
  • 面向对象程序设计-C++ Default constructor & Copy constructor& Destructor & Operator Overloading【第九次上课笔记】

    先上笔记内容吧:

    这次上课的内容有关

    1. 构造函数
    2. 析构函数
    3. 运算符重载
    4. return * this

      内容很细,大家好好回顾笔记再照应程序复习吧 :)

    #include <iostream>
    
    using namespace std;
    
    class Integer {
    public:
        int i;
        int geti () const {return this->i;}
        void seti (int i) {this->i = i;}
        Integer(int j = 0);
        Integer(Integer & c);
        ~Integer();
    };
    
    Integer::Integer (Integer & c) {    //Constructer Function
        i = 18;
        cout << "Integer (Integer & c)" << endl;
    }
    
    Integer::Integer (int j) : i(j) {   //Copy Constructer Function
        cout << "Integer (int j)" << endl;
    }
    
    Integer::~Integer () {              //Destructer Function
        cout << "~Integer () " << endl;
    }
    
    const Integer & fun (const Integer & in) {
        cout << in.geti () << endl;
        return in;
    }
    
    int main (){
    
        Integer b(8);
        fun(b).geti();
        Integer a(b);
    
        return 0;
    }

    下面这个程序是有关运算符重载

    通过 log print 可以跟踪观察到析构的顺序

    同时还有引用返回、友元函数的例子

    #include <iostream>
    
    using namespace std;
    
    class Integer {
    private:
        int i;
    public:
        int geti () const {return this->i;}
        void seti (int i) {this->i = i;}
        Integer(int j = 0);
        Integer(Integer & c);
        ~Integer();
    
        const Integer & operator = (const Integer & in);
        friend const Integer operator + (const Integer & lhs, const Integer & rhs);
    };
    
    const Integer & Integer::operator = (const Integer & in) {  //Member Function
        i = in.i + 50;
        //return in;
        return *this;
    }
    
    const Integer operator + (const Integer & lhs, const Integer & rhs) { //全局函数
        Integer out;
        out.i = lhs.i + rhs.i;
        return out;
    }
    
    Integer::Integer (Integer & c) {    //Constructer Function
        i = 18;
        cout << "Integer (Integer & c)" << endl;
    }
    
    Integer::Integer (int j) : i(j) {   //Copy Constructer Function
        cout << "Integer (int j)" << ' ' << j << endl;
    }
    
    Integer::~Integer () {              //Destructer Function
        cout << "~Integer () " << ' ' << i << endl;
    }
    
    int main (){
    
        Integer b(8), a, c, d;
        cout << "a.geti()" << a.geti() << endl;
        d = c = a = b;
        cout << "a.geti()" << a.geti() << endl;
        cout << "c.geti()" << c.geti() << endl;
        cout << "d.geti()" << d.geti() << endl;
    
        d = a + b;
        cout << "d.geti()" << d.geti() << endl;
    
        return 0;
    }

     接下来运算符重载作一些附加说明

    • =
    • &
    • ,

    这三个运算符可以直接使用

    运算符重载的时候,如果既有 成员函数,又有全局函数, 优先(?)  

    eg.

    ++i (前加) Integer & operator ++ ()
    i++ (后加) Integer & operator ++ (int)
    括号中的 int 仅仅表示后加,无其他含义

    下面的这个例子可以帮助大家更好的理解

    #include <iostream>
    
    using namespace std;
    
    class Integer {
    private:
        int i;
    public:
        int geti () const {return this->i;}
        void seti (int i) {this->i = i;}
        Integer(int j = 0);
        Integer(Integer & c);
        ~Integer();
    
        Integer & operator ++ ();
        Integer operator ++ (int);
        const Integer & operator = (const Integer & in);
        friend const Integer operator + (const Integer & lhs, const Integer & rhs);
    };
    
    Integer & Integer::operator ++ () {
    	++i;
    	return *this;
    }
    
    Integer Integer::operator ++ (int) {
    	Integer temp (this->i);
    	++temp.i;
    	return temp;
    }
    
    const Integer & Integer::operator = (const Integer & in) {  //Member Function
        i = in.i;
        //return in;
        return *this;
    }
    
    const Integer operator + (const Integer & lhs, const Integer & rhs) { //全局函数
        Integer out;
        out.i = lhs.i + rhs.i;
        return out;
    }
    
    Integer::Integer (Integer & c) {    //Constructer Function
        i = 18;
        cout << "Integer (Integer & c)" << endl;
    }
    
    Integer::Integer (int j) : i(j) {   //Copy Constructer Function
        cout << "Integer (int j)" << ' ' << j << endl;
    }
    
    Integer::~Integer () {              //Destructer Function
        cout << "~Integer () " << ' ' << i << endl;
    }
    
    int main (){
    
    	Integer b(8), a;
    	cout << b.geti () << "," << a.geti () << endl;
    	a = ++b;
    	cout << b.geti () << "," << a.geti () << endl;
        a = b++;
        cout << b.geti () << "," << a.geti () << endl;
    
        return 0;
    }
    

      

  • 相关阅读:
    C++
    C语言获取系统时间的几种方式
    我在学习编程中犯的两个最大错误
    《十天学会单片机和C语言编程》
    socket 编程通信实例
    比尔·盖茨给年轻人的十一个忠告 句句是哲理
    程序员有八个级别。
    DOS debug 命令的详细用法
    从命令行模式运行Windows管理工具。
    无法删除DLL文件解决方法(转)
  • 原文地址:https://www.cnblogs.com/wushuaiyi/p/4464093.html
Copyright © 2011-2022 走看看