您现在的位置是:首页 >

派生类访问基类成员的关键字 怎么在派生类中初始化基类中的成员?

火烧 2021-06-26 17:39:58 1050
怎么在派生类中初始化基类中的成员? 怎么在派生类中初始化基类中的成员?如果是初始化当然是呼叫基类的构造函数了,所以在建立派生类物件,引用或者指标的时候,就能呼叫到基类的建构函式来初始化基类成员!C++

怎么在派生类中初始化基类中的成员?  

怎么在派生类中初始化基类中的成员?

如果是初始化当然是呼叫基类的构造函数了,所以在建立
派生类物件,引用或者指标的时候,就能呼叫到基类的建构函式来初始化
基类成员!

C++派生类中如何初始化基类物件

今天收到盛大的面试,问我一个问题,关于派生类中如何初始化基类物件,我在想派生类对于建构函式不都是先构造基类物件,然后在构造子类物件,但是如果我们在成员初始化列表先初始化派生类的私有成员,在函式内去呼叫基类的建构函式,能编译通过吗?或者当我们定义了基类的预设建构函式,而没有去在派生类的建构函式中显示的去呼叫基类的建构函式,会出现什么状况,我想派生类肯定会自动去呼叫基类的预设建构函式,那么解构函式又怎么样呢?我们都知道派生类的解构函式会先被呼叫,然后基类的解构函式后被呼叫,但是我不知道我们是否需要在派生类的解构函式中显示的去呼叫基类的解构函式吗?这个有待我去验证。程式码一:在派生类中成员初始化列表先初始化派生类的私有成员,不显示的呼叫基类的建构函式#include <iostream> usingnamespace std; class Base { private: int n; public: Base(int m):n(m){ cout<<"constructor is called
";} ~Base(){} }; class Derive:public Base { private: int n; public: Derive(int m):n(m) { } ~Derive(){} }; int main() { Derive* a = new Derive(10);return0; }结果:编译错误,error C2512: “Base”: 没有合适的预设建构函式可用 程式码二:在派生类中成员初始化列表先初始化派生类的私有成员,显示的呼叫基类的建构函式#include <iostream> usingnamespace std; class Base { private: int n; public: Base(){ cout<<"default constructor is called
"; n = 8;} Base(int m):n(m){ cout<<"constructor is called
";} ~Base(){} }; class Derive:public Base { private: int n; public: Derive(int m):Base(m),n(m) { } ~Derive(){} }; int main() { Derive* a = new Derive(10);return0; }执行结果: 程式码三:在派生类中成员初始化列表先初始化派生类的私有成员,不显示的呼叫基类的建构函式,则会呼叫预设的建构函式#include <iostream> usingnamespace std; class Base { private: int n; public: Base(){ cout<<"default constructor is called
"; n = 8;} Base(int m):n(m){ cout<<"constructor is called
";} ~Base(){} }; class Derive:public Base { private: int n; public: Derive(int m):n(m) { } ~Derive(){} }; int main() { Derive* a = new Derive(10); return0; }执行结果: 程式码四:派生类解构函式的呼叫过程中会不会自动去呼叫基类的解构函式呢?答案是,肯定的,所以千万不要在派生类的解构函式中再去呼叫基类的解构函式,这种去释放已经释放的记忆体,系统是不允许的。#include <iostream> usingnamespace std; class Base { private: int n; public: Base(){ cout<<"default constructor is called
"; n = 8;} Base(int m):n(m){ cout<<"constructor is called
";} ~Base(){ cout<<"Base distructor is called
"; } }; class Derive:public Base { private: int n; public: Derive(int m):Base(m),n(m) { } ~Derive(){ cout<<"Derive distructor is called
"; } }; int main() { Derive* a = new Derive(10); delete a; return0; }执行结果: 程式码5:如果我们去试试在派生类的解构函式中去呼叫基类的解构函式,看看结果如何?当我想这么做的时候,我突然发现这个程式码我写出来,因为我们都知道,对于C++的一个物件要么将物件分配在栈中,要么将物件分配在堆中,而对于分配在栈中的物件我们过程结束后,自动呼叫类的解构函式,而分配在堆中的物件,得我们去delete,但是你必须拿到指向物件在堆中记忆体的控制代码,也就是指标,但是我发现不可能拿得到,除非你在派生类的建构函式中去new基类物件,但是又有个问题,在派生类建构函式中去new出这个基类物件,那么基类物件是派生类的区域性变数,还是派生类继承而来的呢?我发现肯定是派生类的区域性变数,那么也就是说,如果new出一个派生类物件,那么派生类本身的私有成员是在堆中,而继承而来的属性,也就是基类的东西分配的栈中,好吧,这样,难道派生物件在记忆体上竟然不是连续的?#include <iostream> usingnamespace std; class Base { private: int n; public: Base(){ cout<<"default constructor is called
"; n = 8;} Base(int m):n(m){ cout<<"constructor is called
";} ~Base(){ cout<<"Base distructor is called
"; } }; class Derive:public Base { private: int n; public: Derive(int m):Base(m),n(m) 在这里构造继承属性,即派生类的基类部分 { new Base(m); 这个仅仅在派生类中建立了一个基类的变数而已 } ~Derive(){ cout<<"Derive distructor is called
"; } }; int main() { Derive* a = new Derive(10); delete a; return0; }执行结果如下:构造两次基类,一次构造派生类中的基类成分,还有一次仅仅是派生类的变数而已。同时析构派生类,自动呼叫基类解构函式。 程式码六:在派生类建构函式中用new分配一个基类物件,然后析构掉,在main函式中去呼叫基类的成员函式,发现仍可排程,说明在派生类建构函式中用new分配基类物件,不是派生类的基类成分。 #include <iostream> usingnamespace std; class Base { private: int n; public: Base(){ cout<<"default constructor is called
"; n = 8;} Base(int m):n(m){ cout<<"constructor is called
";} voidget(){ cout<<"get() is called
"; } ~Base(){ cout<<"Base distructor is called
"; } }; class Derive:public Base { private: int n; public: Derive(int m):Base(m),n(m) 在这里构造继承属性,即派生类的基类部分 { Base* a =new Base(m); 这个仅仅在派生类中建立了一个基类的变数而已 delete a; } ~Derive(){ cout<<"Derive distructor is called
"; } }; int main() { Derive* a = new Derive(10); a->get(); delete a; return0; }执行如下: 下面我就有一个疑问了,派生类中只能将基类成分分配在栈中吗?而如果去new出派生类物件,那么岂不记忆体不连续了,这是个问题?我验证了一下,与我的想法并非一致,记忆体还是连续的。只不过在派生类构造基类的过程中(new出派生类方式),只是在堆中去分配基类的成分,虽然使用非new来建立基类物件。程式码如下:#include <iostream> usingnamespace std; class Base { public: int n; public: Base(){ cout<<"default constructor is called
"; n = 8;} Base(int m):n(m){ cout<<"constructor is called
";} voidget(){ cout<<"get() is called
"; } ~Base(){ cout<<"Base distructor is called
"; } }; class Derive:public Base { private: int n; public: Derive(int m):Base(m),n(m) 在这里构造继承属性,即派生类的基类部分 { cout<<"Base address: "<<&(Base::n)<<endl; 地址 cout<<"Derive address: "<<&n<<endl; 地址 } ~Derive(){ cout<<"Derive distructor is called
"; } }; int main() { Derive* a = new Derive(10); delete a; return0; }执行结果如下:以上可以看出地址连续,说明派生类中基类成分和派生类成分地址是连续的。

派生类访问基类成员的关键字 怎么在派生类中初始化基类中的成员?

或者当我们定义了基类的预设建构函式,而没有去在派生类的建构函式中显示的去呼叫基类的建构函式,会出现什么状况,我想派生类肯定会自动去呼叫基类的预设建构函式,那么解构函式又怎么样呢?我们都知道派生类的解构函式会先被呼叫,然后基类的解构函式后被呼叫,但是我不知道我们是否需要在派生类的解构函式中显示的去呼叫基类的解构函式吗?这个有待我去验证。程式码一:在派生类中成员初始化列表先初始化派生类的私有成员,不显示的呼叫基类的建构函式#include usingnamespace std; class Base { private: int n; public: Base(int m):n(m){ cout

在保护派生中,基类许可权为private的成员在派生类中______。

不能访问,类的继承特性不管为public,protected,private都是不能访问

公用继承时基类中的private成员在派生类中是?

私有成员不能被派生类使用的。派生类看不到的基类的私有成员。

派生类中不对基类中的资料初始化,怎样写建构函式

class B
{
B();
}
Class A:public B
{
A();
}
A::A()
{
B::B();
}

c++基类中的成员函式的引数是派生类怎么办

先宣告派生类,将成员函式引数设为派生类的引用

#include<iostream>
using namespace std;

class B;
class A{
public:
void f(const B& b){
cout<<"f(B&)"<<endl;
}
};

class B:public A{
};

int main(){
A a;
B b;
a.f(b);
}

不知道你是不是要问这个

在派生类中怎么对物件进行初始化

是基类的物件的话可以直接初始化,实际上就算你不初始化也子类也会用预设值帮你自动初始化的

在派生类中如何实现访问基类中的私有成员

基类中加一个访问的函式
或把基类中的私有成员改为公有

  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码