c++ 继承以及析构和构造函数过程

该文通过C++代码展示了类的构造函数、析构函数、拷贝构造函数的使用,以及继承的概念。创建了Hero和MonkeyKing类,MonkeyKing继承自Hero,并重写了部分方法。在main函数中,创建并操作了这些类的对象,演示了对象生命周期中的构造与析构过程。
摘要由CSDN通过智能技术生成

继承

实例代码

#ifndef _HERO_H
#define _HERO_H

#include <iostream>

using namespace std;

class hero
{
	public:
		hero();
		hero(const string &name);
		~hero();
		hero(const hero&other);
		void showskill();
		void heroname();
	private:
		string name;

};
#endif

#include <iostream>
#include "hero.hpp"
using namespace std;


hero::hero() : name("NULL")
{
	cout << "默认构造函数" << endl;
}

hero::hero(const string &name) : name(name)
{
	cout << "全參构造函数" <<endl;
}

hero::~hero()
{
	cout << "析构函数" << endl;
}

hero::hero(const hero&other) : name(other.name)
{
	cout << "拷贝构造函数" <<endl;

}

void hero::showskill()
{
	cout << "飞天" <<endl;
}

void hero::heroname()
{
	cout << "hero_name " << name << endl;
}
#ifndef _MONKEYKING_HPP
#define _MONKEYKING_HPP
#include <iostream>
#include "hero.hpp"
using namespace std;
class monkeyking : public hero
{
	public:
		monkeyking();
		~monkeyking();

		void showskill();
		void  showweapon();
	private:
		string weapon;
};

#endif

#include "monkeyking.hpp"
#include <iostream>

using namespace std;

monkeyking::monkeyking() : hero("悟空"),weapon("棒子"//继承了基类的名字,在参数列表当中进行初始化
{//名字是基类的成员变量,只能通过成员函数构造函数进行初始化,如果是protected的话可以直接对成员数据初始化
	cout << "childern Default constructor" << endl;
}
monkeyking::~monkeyking()
{
	cout << "childern Destructor" <<endl;
}

void monkeyking::showskill()//重写了基类的公有成员函数,隐藏了基类的成员函数
{
	cout << "火眼" << endl;
}

void monkeyking::showweapon()
{
	cout << weapon <<endl;
}

#include "monkeyking.hpp"

int main(int argc, const char *argv[])
{
	hero h;
	h.showskill();
	h.heroname();
	cout << "-----------" <<endl;
	monkeyking mk;
	mk.showskill();
	mk.heroname();
	mk.showweapon();
	cout << "-----------" << endl;
	hero h1 = mk;
	cout << &mk << endl;
	cout << sizeof(mk) << endl;
	cout << &h1 << endl;
	hero h2 = h1;
	cout << &h1 << endl;
	cout << &h2 << endl;
	return 0;
}

private,public,protected

在这里插入图片描述## 运行结果
在这里插入图片描述

构造函数以及析构函数过程

#include <iostream>

using namespace std;


class A
{
	public:
		A(){
			cout << "A default constructor" << endl;
		}
		A(int){
			cout << "A argument constructor" << endl;
		}
		~A(){
			cout << "A destructor" << endl;
		}
	private:

};
class B
{
	public:
		B(){
			cout << "B default constructor" << endl;
		}
		B(int){
			cout << "B argument constructor" << endl;
		}
		~B(){
			cout << "B destructor" << endl;
		}
	private:

};


class C : public A , public B
{
	public:
#if 1
		C() : a2(10),A(),B(),b(),a1()
		{//先初始化基类的成员变量,再初始化自己成员数据变量
			cout << "C default constructor" << endl;
		}
#endif
#if 0
		C()
		{
			cout << "C default constructor" << endl;
		}
#endif
		C(int){
			cout << "C argument constructor" << endl;
		}
		~C(){
			cout << "C destructor" << endl;
		}
	private:
		A a1;//
		B b;
		A a2;
		static A a3;
};

int main(int argc, const char *argv[])
{
	C c;
	cout << "------" << endl;
	return 0;
}

运行实例

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值