This commit is contained in:
Light-City
2020-04-06 00:57:02 +08:00
parent f97c156cc4
commit a4d828bb4c
120 changed files with 4413 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
一、类对象成员的构造
先构造成员
再构造自身(调用构造函数)
二、派生类构造函数
派生类可能有多个基类,也可能包括多个成员对象,在创建派生类对象时,派生类的构造函数除了要负责本类成员的初始化外,还要调用基类和成员对象的构造函数,并向它们传递参数,以完成基类子对象和成员对象的建立和初始化。
**派生类只能采用构造函数初始化列表的方式向基类或成员对象的构造函数传递参数**,形式如下:
派生类构造函数名(参数表):基类构造函数名(参数表),成员对象名1(参数表),…{
//……
}
三、构造函数和析构函数调用次序
**派生类对象的构造**
- 先构造基类
- 再构造成员
- 最后构造自身(调用构造函数)
基类构造顺序由派生层次决定:**最远的基类最先构造**
成员构造顺序和定义顺序符合
析构函数的析构顺序与构造相反

View File

@@ -0,0 +1,40 @@
//Eg6-12.cpp
#include <iostream>
using namespace std;
class A {
int x;
public:
A(int i=0) {
x=i;
cout << "A-----"<<x<<endl;
}
};
class B {
int y;
public:
B(int i) {
y=i;
cout << "B-----"<<y<<endl;
}
};
class C {
int z;
public:
C(int i) {
z=i;
cout << "C-----"<<z<<endl;
}
};
class D : public B{
public:
C c1, c2;
A *a1 = new A(10);
A a0,a4;
D( ) : a4(4),c2(2),c1(1),B(1) {
cout << "D-----5"<<endl;
}
};
int main(){
D d;
system("pause");
}

View File

@@ -0,0 +1,43 @@
#include<iostream>
using namespace std;
/*
先构造成员
再构造自身(调用构造函数)
*/
class A {
public:
A() { cout<<"Constructing A"<<endl;}
~A(){ cout<<"Destructing A"<<endl;}
};
class B {
public:
B() { cout<<"Constructing B"<<endl;}
~B(){ cout<<"Destructing B"<<endl;}
};
class C
{
public:
C() { cout<<"Constructing C"<<endl;}
~C(){ cout<<"Destructing C"<<endl;}
B b;
A a;
};
int main()
{
C c;
system("pause");
}
/*
执行结果:
Constructing B
Constructing A
Constructing C
Destructing C
Destructing A
Destructing B
*/

View File

@@ -0,0 +1,15 @@
#include <iostream>
using namespace std;
class A {
public:
A(){ cout<<"Constructing A"<<endl; }
~A(){ cout<<"Destructing A"<<endl; }
};
class B:public A {
public:
~B(){ cout<<"Destructing B"<<endl; }
};
int main(){
B b;
system("pause");
}

View File

@@ -0,0 +1,27 @@
#include <iostream>
using namespace std;
class Base{
private:
int x;
public:
Base(int a){
x=a;
cout<<"Base constructor x="<<x<<endl;
}
~Base(){ cout<<"Base destructor..."<<endl; }
};
class Derived:public Base{
private:
int y;
public:
Derived(int a,int b):Base(a){ //派生类构造函数的初始化列表
y=b;
cout<<"Derived constructor y="<<y<<endl;
}
~Derived(){ cout<<"Derived destructor..."<<endl; }
};
int main(){
Derived d(1,2);
system("pause");
return 0;
}

View File

@@ -0,0 +1,46 @@
#include<iostream>
using namespace std;
class A {
public:
A() { cout<<"Constructing A"<<endl;}
~A(){ cout<<"Destructing A"<<endl;}
};
class B {
public:
B() { cout<<"Constructing B"<<endl;}
~B(){ cout<<"Destructing B"<<endl;}
};
class C {
public:
C() { cout<<"Constructing C"<<endl;}
~C(){ cout<<"Destructing C"<<endl;}
};
class D:public C
{
public:
D() { cout<<"Constructing D"<<endl;}
~D(){ cout<<"Destructing D"<<endl;}
B b;
A a;
C c;
};
int main()
{
D d;
system("pause");
}
/*
Ö´ĐĐ˝ášűŁş
Constructing C
Constructing B
Constructing A
Constructing C
Constructing D
Destructing D
Destructing C
Destructing A
Destructing B
Destructing C
*/

View File

@@ -0,0 +1,24 @@
#include <iostream>
using namespace std;
class Point{
protected:
int x,y;
public:
Point(int a,int b=0) {
x=a; y=b;
cout<<"constructing point("<<x<<","<<y<<")"<<endl;
}
};
class Line:public Point{
protected:
int len;
public:
Line(int a,int b,int l):Point(a,b) { //构造函数初始化列表
len=l;
cout<<"Constructing Line,len ..."<<len<<endl;
}
};
int main(){
Line L1(1,2,3);
system("pause");
}