zoukankan      html  css  js  c++  java
  • 构造函数的使用

    /*<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指针。。。。


  • 相关阅读:
    c++开发之对应Linux下的sem_t和lock
    嵌入式开发之davinci--- 8148/8168/8127 中的图像处理算法优化库vlib
    crc32 冗余加密校验
    快速安装 GitLab 并汉化
    oracle-6-密码文件
    oracle-5-的升级步骤
    Linux大文件已删除,但df查看已使用的空间并未减少解决
    cygwin 的不同文件类型显示不同的颜色
    PLSQL的安装
    网络流量监控工具----iftop
  • 原文地址:https://www.cnblogs.com/odin-luyu/p/5371774.html
Copyright © 2011-2022 走看看