zoukankan      html  css  js  c++  java
  • 在继承类中,父类在子类中初始化问题,已解决

    #include<iostream>
    
    using namespace std;
    
    //继承与组合混搭情况下,构造和析构调用原则
    class Grandfather
    {
    public:
    	Grandfather( char * g)
    	{
    		this->G_t1=g;
    		cout<<"G_t1====== "<<G_t1;
    		cout<<"我是顶头的爷爷的构造函数
    ";
    	}
    
    	~Grandfather()
    	{
    		cout<<"我是顶头的爷爷的析构函数
    ";
    	}
    protected:
    	char *G_t1;
    private:
    };
    class Father:public Grandfather
    {
    public:
    	Father( char *f):Grandfather("hahahah")
    	{
    		F_t1=f;
    		cout<<"F_t1====== "<<F_t1;
    		cout<<"这是父亲的构造函数
    ";
    	}
    	~Father()
    	{
    		cout<<"这是父亲的析构函数 
    ";
    	}
    protected:
    	char *F_t1;
    private:
    };
    class Child:public Father
    {
    public:
    	Child():Father("dsa"),g1("sfsdfds"),g2("g2")//不懂
    	{
    
    		cout<<"这是孩子的构造函数 
    ";
    	}
    	~Child()
    	{
    		cout<<"这是孩子的析构函数 
    ";
    	}
    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<<"我是顶头的爷爷的构造函数
    ";
    	}
    
    	~Grandfather()
    	{
    		cout<<"我是顶头的爷爷的析构函数
    ";
    	}
    protected:
    	char *G_t1;
    private:
    };
    class Father:public Grandfather
    {
    public:
    	Father( char *f):Grandfather("hahahah")
    	{
    		F_t1=f;
    		cout<<"F_t1====== "<<F_t1;
    		cout<<"这是父亲的构造函数
    ";
    	}
    	~Father()
    	{
    		cout<<"这是父亲的析构函数 
    ";
    	}
    protected:
    	char *F_t1;
    private:
    };
    class Child:public Father
    {
    public:
    	Child():Father("dsa"),g1("sfsdfds"),g2("g2"),f1("f1")//不懂
    	{
    
    		cout<<"这是孩子的构造函数 
    ";
    	}
    	~Child()
    	{
    		cout<<"这是孩子的析构函数 
    ";
    	}
    protected:
    	int C_t1;
    	Father f1;
    	Grandfather g1;//这里这两句有疑问,为什么Grandfather明明必须有形参,但是为什么这里初始化参数反而不对了呢?
    	Grandfather g2;
    private:
    };
    
    void objplay()
    {
    	Child c1;
    }
    int main()
    {
    	
    	objplay();
    	system("pause");
    	return 0;		
    }
    

      运行结果是:

    发现一个真理没?只要调用一个Father就必须调用一个爷爷啊

    套路

  • 相关阅读:
    Java for LeetCode 025 Reverse Nodes in k-Group
    Java for LeetCode 024 Swap Nodes in Pairs
    Java for LeetCode 023 Merge k Sorted Lists
    【JAVA、C++】LeetCode 022 Generate Parentheses
    【JAVA、C++】LeetCode 021 Merge Two Sorted Lists
    【JAVA、C++】LeetCode 020 Valid Parentheses
    【JAVA、C++】LeetCode 019 Remove Nth Node From End of List
    9-[记录操作]--数据的增删改,权限管理
    8-[表操作]--foreign key、表与表的关系
    7-[表操作]--完整性约束
  • 原文地址:https://www.cnblogs.com/xiaochige/p/6613022.html
Copyright © 2011-2022 走看看