#include<iostream>
using namespace std;
//继承与组合混搭情况下,构造和析构调用原则
class Grandfather
{
public:
Grandfather( char * g)
{
this->G_t1=g;
cout<<"G_t1====== "<<G_t1;
cout<<"我是顶头的爷爷的构造函数\n";
}
~Grandfather()
{
cout<<"我是顶头的爷爷的析构函数\n";
}
protected:
char *G_t1;
private:
};
class Father:public Grandfather
{
public:
Father( char *f):Grandfather("hahahah")
{
F_t1=f;
cout<<"F_t1====== "<<F_t1;
cout<<"这是父亲的构造函数\n";
}
~Father()
{
cout<<"这是父亲的析构函数 \n";
}
protected:
char *F_t1;
private:
};
class Child:public Father
{
public:
Child():Father("dsa"),g1("sfsdfds"),g2("g2")//不懂
{
cout<<"这是孩子的构造函数 \n";
}
~Child()
{
cout<<"这是孩子的析构函数 \n";
}
protected:
int C_t1;
Grandfather g1;//这里这两句有疑问,为什么Grandfather明明必须有形参,但是为什么这里初始化参数反而不对了呢?
Grandfather g2;
private:
};
void objplay()
{
Child c1;
}
int main()
{
objplay();
system("pause");
return 0;
}
不明白的是这两句
但是这好像是套路,必须这么做呢
就像例如:
#include<iostream>
using namespace std;
//继承与组合混搭情况下,构造和析构调用原则
class Grandfather
{
public:
Grandfather( char * g)
{
this->G_t1=g;
cout<<"G_t1====== "<<G_t1;
cout<<"我是顶头的爷爷的构造函数\n";
}
~Grandfather()
{
cout<<"我是顶头的爷爷的析构函数\n";
}
protected:
char *G_t1;
private:
};
class Father:public Grandfather
{
public:
Father( char *f):Grandfather("hahahah")
{
F_t1=f;
cout<<"F_t1====== "<<F_t1;
cout<<"这是父亲的构造函数\n";
}
~Father()
{
cout<<"这是父亲的析构函数 \n";
}
protected:
char *F_t1;
private:
};
class Child:public Father
{
public:
Child():Father("dsa"),g1("sfsdfds"),g2("g2"),f1("f1")//不懂
{
cout<<"这是孩子的构造函数 \n";
}
~Child()
{
cout<<"这是孩子的析构函数 \n";
}
protected:
int C_t1;
Father f1;
Grandfather g1;//这里这两句有疑问,为什么Grandfather明明必须有形参,但是为什么这里初始化参数反而不对了呢?
Grandfather g2;
private:
};
void objplay()
{
Child c1;
}
int main()
{
objplay();
system("pause");
return 0;
}
运行结果是:
发现一个真理没?只要调用一个Father就必须调用一个爷爷啊
套路