【14】构造函数和析构函数

构造函数、默认构造函数、拷贝构造函数、深拷贝、浅拷贝、析构函数、常成员函数,blabla一坨概念,云里雾里,今天我们就来好好理一理这些概念...

1、构造函数

#include "iostream"
#include "string"

using namespace std;

class base {
public:
	int a;
	base() {}
	base(int m_a) :a(m_a) {
		cout << "base()" << endl;
	}
	base& operator+(base &b);
	void operator=(base &a);
};

class derive : public base {
public:
	int a;
	int b;
	derive(int a) :a(a) {
		cout << "init a" << endl;
	}
};

base& base::operator+(base &b)
{
	b.a= this->a + b.a;
	return b;
}

void base::operator=(base& a)
{
	this->a = a.a;
}

int main()
{
	base a(5);
	derive b(1);
	cout << b.base::a << endl;
	cout << b.a << endl;
}

对于这种父类子类有相同成员变量的情况,定义子类对象时,会通过命令空间来保留两份a,那么如何初始化基类的a值呢?

#include "iostream"
#include "string"

using namespace std;

class base {
public:
	int a;
	//base() {}
	base(int m_a) :a(m_a) {
		cout << "base()" << endl;
	}
	base& operator+(base &b);
	//void operator=(base &a);
	void printHello() {
		cout << "this is a test" << endl;
	}
};

class derive : public base {
public:
	int a;
	int b;
	derive(int base_a, int a, int b) :a(a),b(b),base(base_a) {
		cout << "init a" << endl;
	}
};

base& base::operator+(base &b)
{
	b.a= this->a + b.a;
	return b;
}


int main()
{
	base a(5);
	derive b(1,2,3);
	cout << b.base::a << endl;
	cout << b.a << endl;
}

如何父类只有带参的构造函数,则子类构造函数中必须显式调用父类的带参构造函数,如上。

默认构造函数:

2、拷贝构造函数

#include "iostream"
#include "string"

using namespace std;

class base {
public:
	int a;
	//base() {}
	base(int m_a) :a(m_a) {
		cout << "base()" << endl;
	}
	base& operator+(base &b);
	//void operator=(base &a);
	void printHello() {
		cout << "this is a test" << endl;
	}
};

class derive : public base {
public:
	int a;
	int b;
	derive(int base_a, int a, int b) :a(a),b(b),base(base_a) {
		cout << "init a" << endl;
	}
};

base& base::operator+(base &b)
{
	b.a= this->a + b.a;
	return b;
}


int main()
{
	base a(5);
	derive b(1,2,3);
	cout << b.base::a << endl;
	cout << b.a << endl;
	/*************** 拷贝构造函数 *****************/
	derive k = b;
	cout << "k.a = " << k.base::a << endl;
}

当类中没有定义拷贝构造函数时,编译器会默认提供一个拷贝构造函数,进行成员变量之间的拷贝。(这个拷贝操作是浅拷贝)

3、常成员函数

#include "iostream"
#include "string.h"

using namespace std;

class base {
public:
	int a;
	base() {
		cout << "base()" << endl;
	}
	base(int m_a) : a(m_a) {
		cout << "base(" << m_a << ")" << endl;
	}
};

class derive : public base {
public:
	int a;
	int b;
	derive(int m_a, int m_b, int m_c) : a(m_a),b(m_b),base(m_c) {
		cout << "derive(" << m_a << "," << m_b << "," << m_c << ")" << endl;
	}
	void printHello();
	void printHello() const;
};

void derive::printHello()
{
	cout << "printHello()" << endl;
}

void derive::printHello() const
{
	cout << "printHello() const" << endl;
}

int main()
{
	const derive d(1,2,3);
	d.printHello();
	cout << "d.base::a = " << d.base::a << endl;
	return 0;
}
0000000000202068 B __bss_start
0000000000202068 b completed.7594
                 U __cxa_atexit@@GLIBC_2.2.5
                 w __cxa_finalize@@GLIBC_2.2.5
0000000000000a70 t deregister_tm_clones
0000000000000b00 t __do_global_dtors_aux
0000000000201de0 t __do_global_dtors_aux_fini_array_entry
0000000000202060 d __dso_handle
0000000000201df0 d _DYNAMIC
0000000000202068 D _edata
0000000000202070 B _end
0000000000000e08 T _fini
0000000000000b40 t frame_dummy
0000000000201dd0 t __frame_dummy_init_array_entry
0000000000000fc8 r __FRAME_END__
0000000000202000 d _GLOBAL_OFFSET_TABLE_
0000000000000cc4 t _GLOBAL__sub_I_main.cpp
                 w __gmon_start__
0000000000000e50 r __GNU_EH_FRAME_HDR
0000000000000998 T _init
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
0000000000201de8 d __JCR_END__
0000000000201de8 d __JCR_LIST__
                 w _Jv_RegisterClasses
0000000000000be4 T main
0000000000000ab0 t register_tm_clones
                 U __stack_chk_fail@@GLIBC_2.4
0000000000202068 d __TMC_END__
0000000000000c7b t _Z41__static_initialization_and_destruction_0ii
0000000000000cda W _ZN4baseC1Ei
0000000000000cda W _ZN4baseC2Ei
0000000000000b70 T _ZN6derive10printHelloEv
0000000000000d40 W _ZN6deriveC1Eiii
0000000000000d40 W _ZN6deriveC2Eiii
0000000000000baa T _ZNK6derive10printHelloEv
                 U _ZNSolsEi@@GLIBCXX_3.4
                 U _ZNSolsEPFRSoS_E@@GLIBCXX_3.4
                 U _ZNSt8ios_base4InitC1Ev@@GLIBCXX_3.4
                 U _ZNSt8ios_base4InitD1Ev@@GLIBCXX_3.4
                 U _ZSt4cout@@GLIBCXX_3.4
                 U _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@@GLIBCXX_3.4
0000000000202069 b _ZStL8__ioinit
                 U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@@GLIBCXX_3.4
parallels@parallels-vm:~$ g++ main.cpp -o main;./main
base(3)
derive(1,2,3)
printHello() const
d.base::a = 3

猜你喜欢

转载自blog.csdn.net/Utotao/article/details/115425518