簡體   English   中英

How to access parent class function from derived class object in function over riding feature of C++

[英]How to access parent class function from derived class object in function over riding feature of C++

#include<iostream>
using namespace std;

class base
{
public: void getdata()
        {
            cout<<"in base"<<endl;
        }
};

class derived:public base
{
public: void getdata()
        {
            cout<<"in derived"<<endl;
        }
        void base:: getdata()
};

int main()
{
derived d;
d.getdata();
return 0;
}

我收到錯誤消息:“無法在 'derived' 中聲明成員 function 'base::getdata'”

我想從派生的 class 對象中打印這兩個函數的內容

#include<iostream>
using namespace std;

class base
{
public: 
  base() {

  }
  void getData()
  {
      cout << "in base" << endl;
  }

};

class derived :public base
{
public:

  derived(base b) {

      b.getData();
  }
  void getdata()
  {
      cout << "in derived" << endl;
  }

};

int main()
{
  base b;
  derived d(b);
  d.getdata();


 return 0;
}

嘗試這個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM