• 构造函数的使用


    /*<span style="font-size:32px;color:#FF0000;">在写一个简单的多态例子的时候总是出错,后来发现是
    在构造函数上面有问题  少写了new,结果依旧运行,那
    么构造函数返回了什么呢</span>*/
    
    #include "stdafx.h"
    #include<iostream>
    #include<string>
    using namespace std;
    class person {
    protected:
    	string name;
    public:
    	virtual void showinfo();
    	person(string name) :name(name) { cout << "this is person 构造函数" << endl; }
    	person(person& rhs) { cout << "this is person拷贝构造函数" << endl; }
    };
    void person::showinfo() {
    	cout << name<<endl;
    }
    class teacher :public person{
    protected:
    	string prefession;
    public:
    	void showinfo();
    	teacher(teacher& rhs):person(rhs) { cout << "this is teacher 拷贝构造函数" << endl; }
    	teacher(string name, string prefession) :person(name), prefession(prefession) { cout << "this is teacher 构造函数" << endl; }
    };
    void teacher::showinfo() {
    	cout << name << " " << prefession << endl;
    }
    class student :public person {
    protected:
    	int s_number;
    public:
    	void showinfo();
    	student(string name, int nu) :person(name), s_number(nu) { cout << "this is student 构造函数"<<endl; }
    };
    void student::showinfo() {
    	cout << name << " " << s_number << endl;
    }
    int main()
    {
    	person d = teacher("luyu", "jiaoshi");//构造函数这样写
    	d.showinfo();
    	person* b = new teacher("luyu", "jiaoshi");//或者这么写
    	b->showinfo();
            delete b;
            return 0;
    }
    
    
    

    运行结果是这样的,多调用了一个拷贝构造函数,那么构造函数返回的应该是this指针

    对汇编无感,知乎上面的朋友给力

    版权意识要浓厚


    构造函数返回了一个this指针。。。。


  • 相关阅读:
    Linux安装python3.6
    Django之Model操作
    Django
    html学习笔记-XML-Javascript
    html学习笔记-XML
    html学习笔记-DOM
    在IDEA中编辑struts国际化properties文件
    Java中的Serializable接口和transient关键字
    关于Kettle的事务和转换内步骤的顺序执行
    Mac、Linux下两个Emacs共享一个配置文件
  • 原文地址:https://www.cnblogs.com/odin-luyu/p/5371774.html
Copyright © 2020-2023  润新知