构造函数的使用

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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值