建構式、默認建構式、拷貝建構式、深拷貝、淺拷貝、解構式、常成員函式,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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/272540.html
標籤:區塊鏈
