zoukankan      html  css  js  c++  java
  • 【C++学习教程09】重载

    参考

    • 范磊C++(第11课时)
    • VS 2015

    笔记

    实现成员的自加

    • 通过编写成员函数
    #include "stdafx.h"
    #include <stdlib.h>
    #include <iostream>
    using namespace std;
    class num
    {
    public:
    	num() { n = 1; }
    	~num() {}
    	int get() const { return n; }
    	void set(int x) { n = x; }
    	void add() { ++n; }//对象的自加功能(实际上是成员变量)
    private:
    	int n;
    };
    
    int main(int argc, const char * argv[]) {
    	num i;
    	cout << "i:"<<i.get()<<endl;
    	i.add();
    	cout << "i:" << i.get() << endl;//通过调用add函数实现了自加,但是由于还要在类中定义函数比较麻烦。
    	system("pause");
    	return 0;
    }
    
    • 重载运算符
    #include "stdafx.h"
    #include <stdlib.h>
    #include <iostream>
    using namespace std;
    class num
    {
    public:
    	num() { n = 1; }
    	~num() {}
    	int get() const { return n; }
    	void set(int x) { n = x; }
    	void add() { ++n; }
    	void operator ++() { ++n; }//相当于重载函数,函数体中是实现部分。
    private:
    	int n;
    };
    
    int main(int argc, const char * argv[]) {
    	num i;
    	cout << "i:"<<i.get()<<endl;
    	i.add();
    	cout << "i:" << i.get() << endl;//通过调用add函数实现了自加,但是由于还要定义函数比较麻烦。
    	++i;
    	cout << "i:" << i.get() << endl;//重载运算符。
    	system("pause");
    	return 0;
    }
    

    后置自加 :链接: csdn.

    • 创建临时对象

    将自加后的对象传递给另一个对象。

    #include "stdafx.h"
    #include <stdlib.h>
    #include <iostream>
    using namespace std;
    class num
    {
    public:
    	num() { n = 1; }
    	~num() {}
    	int get() const { return n; }
    	void set(int x) { n = x; }
    	void add() { ++n; }
    	num operator ++() //返回一个对象
    	{
    		++n;
    		num t;//临时对象
    		t.set(n);
    		return t;
    	}
    private:
    	int n;
    };
    
    int main(int argc, const char * argv[]) {
    	num i;
    	cout << "i:"<<i.get()<<endl;
    	i.add();
    	cout << "i:" << i.get() << endl;//通过调用add函数实现了自加,但是由于还要定义函数比较麻烦。
    	++i;
    	cout << "i:" << i.get() << endl;//重载运算符。
    	num n = ++i;//传递给一个对象 //返回num类的临时对象,然后初始化给n
    	cout << "n:" << n.get() << endl;
    	system("pause");
    	return 0;
    }
    
    • 创建无名临时对象【有缺陷】
      不用创建临时对象,用带参数的构造函数。
    #include <iostream>
    using namespace std;
    class num
    {
    public:
        num() { n = 1;cout<<"构造函数执行"<<endl; }
        num(int i){n=i;cout<<"带参数的构造函数执行"<<endl;}
        ~num() {cout<<"析构函数执行"<<endl;}
        int get() const { return n; }
        void set(int x) { n = x; }
        void add() { ++n; }
        num operator ++() //返回一个对象
        {
            ++n;
            return num(n);//调用并且返回带参数的构造函数创建的临时对象,并且踏实没有名字的。//这种方法仍然不可取,因为对象的创建和析构都会浪费时间
        }
    private:
        int n;
    };
    
    int main(int argc, const char * argv[]) {
        num i;
        cout << "i:"<<i.get()<<endl;
        i.add();
        cout << "i:" << i.get() << endl;//通过调用add函数实现了自加,但是由于还要定义函数比较麻烦。
        ++i;
        cout << "i:" << i.get() << endl;//重载运算符。
        num n = ++i;//传递给一个对象 //返回num类的临时对象,然后初始化给n
        cout << "n:" << n.get() << endl;
        return 0;
    }
    

    输出结果:
    在这里插入图片描述

    • 取消创建临时对象
      创建一个对象需要牺牲一定的速度和一定的内存空间,但是我们只是要实现成员变量的自加。在这里插入图片描述
    #include <iostream>
    using namespace std;
    class num
    {
    public:
        num() { n = 1;cout<<"构造函数执行"<<endl; }
        num(int i){n=i;cout<<"带参数的构造函数执行"<<endl;}
        num(const num&s){this->n=s.n;cout<<"复制造函数执行"<<endl;}
        ~num() {cout<<"析构函数执行"<<endl;}
        int get() const { return n; }
        void set(int x) { n = x; }
        void add() { ++n; }
        num operator ++() //返回一个对象
        {
            ++n;
            return *this;//用this来替换创造临时对象
            //返回的是原始对象而不是临时对象,不用调用带参数的构造函数
            //把返回的原始对象返回给新建的对象
            //i->相当于复制了
        }
    private:
        int n;
    };
    
    int main(int argc, const char * argv[]) {
        num i;
        cout << "i:"<<i.get()<<endl;
        i.add();
        cout << "i:" << i.get() << endl;//通过调用add函数实现了自加,但是由于还要定义函数比较麻烦。
       // ++i;
        //cout << "i:" << i.get() << endl;//重载运算符。
        num n = ++i;//传递给一个对象 //返回num类的临时对象,然后初始化给n
        cout << "n:" << n.get() << endl;
        return 0;
    }
    

    输出对象:
    在这里插入图片描述

    重载运算符

    • 加法运算符
      在这里插入图片描述
    #include <iostream>
    using namespace std;
    class num
    {
    public:
        num() { n = 1;cout<<"构造函数执行"<<endl; }
        num(int i){n=i;cout<<"带参数的构造函数执行"<<endl;}
        num(const num&s){this->n=s.n;cout<<"复制造函数执行"<<endl;}
        ~num() {cout<<"析构函数执行"<<endl;}
        int get() const { return n; }
        void set(int x) { n = x; }
        num add(const num&r) {
            return num(n+r.get());
            
        }
        num operator ++() //返回一个对象
        {
            ++n;
            return *this;//用this来替换创造临时对象
            //返回的是原始对象而不是临时对象,不用调用带参数的构造函数
            //把返回的原始对象返回给新建的对象
            //i->相当于复制了
        }
    private:
        int n;
    };
    
    int main(int argc, const char * argv[]) {
        num one(1), two(2), three;
        three=one.add(two);
        cout<<"one:"<<one.get()<<endl;
        cout<<"two:"<<two.get()<<endl;
        cout<<"three:"<<three.get()<<endl;
        return 0;
    }
    

    输出结果:
    在这里插入图片描述

    这种加法不提倡使用
    课件出现损坏无法观看

    • 重载赋值运算符
      课件损坏
    • 转换类型运算符
    #include <iostream>
    using namespace std;
    class A
    {
    public://构造函数只有一个参数,与下面的对应!
        A(int x){i=x;cout<<"构造函数执行"<<i<<endl;}
        ~A(){cout<<"析构函数执行"<<i<<endl;}
        void get(){cout<<i<<endl;}
    private:
        int i;
    };
    
    int main(int argc, const char * argv[]) {
        A a(33);
        a.get();
        a=1000;//这时候就会创造临时对象(带参数的构造函数)
        a.get();
        a=A(2);//强制类型表达式//也是创造临时对象
        a.get();
        return 0;
    }
    

    输出结果:
    在这里插入图片描述

    此后三节课件均损坏

    重载的限制: CSDN.

  • 相关阅读:
    Ext JS 6开发实例(三) :主界面设计
    Ext JS 6开发实例(二) :使用CMD创建应用程序
    文件夹或者文件比对工具 Beyond Compare
    LIS问题(DP解法)---poj1631(模板)
    hdoj Max Sum Plus Plus(DP)
    A* 算法详解
    hdoj1043 Eight(逆向BFS+打表+康拓展开)
    hdoj2612 Find a way (bfs)
    luoguP3366 [模板] 最小生成树
    luoguP1196(带权并查集)
  • 原文地址:https://www.cnblogs.com/vrijheid/p/14223005.html
Copyright © 2011-2022 走看看