Last active
December 7, 2018 04:03
-
-
Save baobao/625cf2cc574f9fdf2623 to your computer and use it in GitHub Desktop.
C++の基礎構文を学ぶメモ ◆タイトル
継承・オーバーライド
スーパークラスのプロパティ、メソッドへのアクセス周り
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
C++の基礎構文を学ぶプロジェクト | |
◆タイトル | |
継承・オーバーライド | |
スーパークラスのプロパティ、メソッドへのアクセス周り | |
*/ | |
#include <iostream> | |
//========================================================= | |
// スーパークラス | |
//========================================================= | |
class SuperClass | |
{ | |
int outOfProp = 9999; | |
public: | |
SuperClass(){ | |
//コンストラクタ | |
std::cout<<"SuperClass::constructor"<<"\n"; | |
} | |
~SuperClass(){ | |
//デストラクタ | |
std::cout<<"SuperClass::destructor"<<"\n"; | |
} | |
//継承するメソッド・関数はvirtualをつける | |
virtual void update(); | |
int publicSuperProp = 100; | |
protected: | |
int protectedSuperProp = 222; | |
private: | |
int privateSuperProp = 802; | |
}; | |
void SuperClass::update() | |
{ | |
std::cout << "SuperClass::publicSuperProp::" << publicSuperProp << "\n"; | |
std::cout << "SuperClass::protectedSuperProp::" << protectedSuperProp << "\n"; | |
std::cout << "SuperClass::privateSuperProp::"<< privateSuperProp << "\n"; | |
std::cout << "SuperClass::outOfProp::" << outOfProp << "\n"; | |
std::cout << "-----------------------\n\n"; | |
} | |
//========================================================= | |
// サブクラス | |
//========================================================= | |
//SuperClassを継承する | |
class SubClass:public SuperClass | |
{ | |
public: | |
SubClass(){ | |
std::cout<<"SubClass::constructor"<<"\n"; | |
} | |
~SubClass(){ | |
std::cout<<"SubClass::destructor"<<"\n"; | |
} | |
void update(); | |
int _id = 0; | |
}; | |
void SubClass::update() | |
{ | |
//スーパークラスのpublicプロパティにアクセス | |
std::cout << "SubClass::publicSuperProp::" << publicSuperProp << "\n"; | |
//スーパークラスのprotectedプロパティにアクセス | |
std::cout << "SubClass::protectedSuperProp::" << protectedSuperProp << "\n"; | |
//スーパークラスのprivateプロパティにアクセス | |
//std::cout << "SubClass::privateSuperProp::" << privateSuperProp << "\n"; //アクセス指定子がprivateなのでエラー | |
//スーパークラスのprivateプロパティにアクセス(明示的にアクセス指定子をつけていないプロパティ) | |
//何もアクセス指定子がついていないプロパティは、private扱いになる | |
//std::cout << "SubClass::outOfProp::" << outOfProp << "\n"; | |
std::cout << "-----------------------\n\n"; | |
//親クラスのupdateをコールする | |
SuperClass::update(); | |
} | |
//========================================================= | |
// メイン | |
//========================================================= | |
int main(int argc, const char * argv[]) | |
{ | |
//newとはメモリ領域を確保する作業 | |
//[参考]http://www.geocities.jp/ky_webid/cpp/language/012.html | |
//newした際は、必ずポインタ型になる | |
//なぜなら確保するメモリ量がわからないためだと思う | |
SubClass *sub = new SubClass; | |
std::cout << "-----------------------\n\n"; | |
sub->update(); | |
std::cout << "main::publicSuperProp::" << sub->publicSuperProp << "\n"; | |
//std::cout << sub->protectedSuperProp:: << "\n"; //protectedプロパティなのでエラー | |
//std::cout << sub->privateSuperProp:: << "\n"; //privateプロパティなのでエラー | |
//std::cout << sub->outOfProp:: << "\n"; //privateプロパティなのでエラー | |
//要らなくなったら削除(デストラクタが走る) | |
delete sub; | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment