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


  • 相关阅读:
    linux下c语言实现搜索根目录下所有文件(转-wangxiangshang)
    windows编程之GDI基础--获取设备内容属性(三)
    windows编程之GDI基础--设备内容(二)
    windows编程之GDI基础(一)
    windows编程之滚动条(新式滚动条函数)
    window编程之滚动条(老式滚动条)
    来博客园混了...
    实现一个4位加减法与或运算选择器(作业)
    黑客初级知识(四)
    黑客初级知识(三)
  • 原文地址:https://www.cnblogs.com/odin-luyu/p/5371774.html
Copyright © 2011-2022 走看看