「C++」子クラスに親クラスのインスタンスを呼び出すサンプル
書式
クラス名::メソッド名
使用例
#include <iostream>
using namespace std;
//親クラスを定義
class TestA
{
public:
void getFuncA();
};
//子クラスを定義
class TestB : public TestA
{
public:
void getFuncB();
};
//メンバ関数の実装
void TestA::getFuncA()
{
cout << "study skill in arkgame\n";
return;
}
void TestB::getFuncB()
{
//親クラスのメソッド
TestA::getFuncA();
return;
}
//クラスを使用
int main() {
TestB tb;
//親クラスのメソッド
tb.getFuncA();
//子クラスのメソッド
tb.getFuncB();
return 0;
}
#include <iostream>
using namespace std;
//親クラスを定義
class TestA
{
public:
void getFuncA();
};
//子クラスを定義
class TestB : public TestA
{
public:
void getFuncB();
};
//メンバ関数の実装
void TestA::getFuncA()
{
cout << "study skill in arkgame\n";
return;
}
void TestB::getFuncB()
{
//親クラスのメソッド
TestA::getFuncA();
return;
}
//クラスを使用
int main() {
TestB tb;
//親クラスのメソッド
tb.getFuncA();
//子クラスのメソッド
tb.getFuncB();
return 0;
}
#include <iostream> using namespace std; //親クラスを定義 class TestA { public: void getFuncA(); }; //子クラスを定義 class TestB : public TestA { public: void getFuncB(); }; //メンバ関数の実装 void TestA::getFuncA() { cout << "study skill in arkgame\n"; return; } void TestB::getFuncB() { //親クラスのメソッド TestA::getFuncA(); return; } //クラスを使用 int main() { TestB tb; //親クラスのメソッド tb.getFuncA(); //子クラスのメソッド tb.getFuncB(); return 0; }
実行結果
study skill in arkgame
study skill in arkgame