fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <class T>
  5. void out(T t) { cout << t << endl; }
  6. void out(char *str) { cout << str << endl; }
  7. template <class T>
  8. void out(char *str, T t) { cout << str << ' ' << t << endl; }
  9.  
  10. class VirtualClass {
  11. public:
  12. void virtual_func() { out("vir"); }
  13. };
  14.  
  15. class PublicClass : public virtual VirtualClass {
  16. private:
  17. void pub_private_func() {}
  18. protected:
  19. void pub_protected_func() {}
  20. public:
  21. void virtual_func() { out("pub"); }
  22. void pub_public_func() {}
  23. void override_fail_func() { out("pub"); }
  24. virtual void override_success_func() { out("pub"); }
  25. virtual void need_override_func() = 0;
  26. };
  27.  
  28. class PrivateClass : public virtual VirtualClass {
  29. private:
  30. void prv_private_func();
  31. protected:
  32. void prv_protected_func();
  33. public:
  34. void virtual_func() { out("prv"); }
  35. void prv_public_func();
  36. PrivateClass(int x) {}
  37. };
  38.  
  39. class ProtectedClass {
  40. private:
  41. void prt_private_func();
  42. protected:
  43. void prt_protected_func();
  44. public:
  45. void prt_public_func();
  46. };
  47.  
  48. class MyClass : public PublicClass, private PrivateClass,
  49. protected ProtectedClass, public virtual VirtualClass
  50. {
  51. friend class FriendClass;
  52. friend void friend_func(MyClass& mc);
  53. private:
  54. static const int static_const_value;
  55. static int static_private_value;
  56. const int const_value;
  57. int private_value;
  58. void private_func();
  59. void func_access_prv() {
  60. // pub_private_func(); //NG
  61. pub_protected_func(); //OK
  62. pub_public_func(); //OK
  63.  
  64. // prv_private_func(); //NG
  65. prv_protected_func(); //OK?
  66. prv_public_func(); //OK?
  67.  
  68. // prt_private_func(); //NG
  69. prt_protected_func(); //OK?
  70. prt_public_func(); //OK?
  71. }
  72. void fa_prv() { func_access_prv(); }
  73. protected:
  74. int protected_value;
  75. void protected_func();
  76. void func_access_prt() {
  77. // pub_private_func(); //NG
  78. pub_protected_func(); //OK
  79. pub_public_func(); //OK
  80.  
  81. // prv_private_func(); //NG
  82. prv_protected_func(); //OK?
  83. prv_public_func(); //OK?
  84.  
  85. // prt_private_func(); //NG
  86. prt_protected_func(); //OK?
  87. prt_public_func(); //OK?
  88. }
  89. void fa_prt() { func_access_prt(); }
  90. public:
  91. MyClass(); //コンストラクタ
  92. MyClass(int x); //コンストラクタ(オーバーロード)
  93. MyClass(const MyClass& mc) : PrivateClass(5), const_value(mc.const_value) {
  94. private_value = MyClass::static_private_value++;
  95. }
  96. ~MyClass(); //デストラクタ
  97. static void static_public_func();
  98. int public_value;
  99. void public_func();
  100. void virtual_func() { out("my"); }
  101. void override_fail_func() { out("my"); }
  102. void override_success_func() { out("my"); }
  103. void need_override_func() {}
  104. void const_ng_func() {}
  105. void const_ok_func() const {}
  106. inline int getPrivateValue() const {
  107. return private_value;
  108. }
  109.  
  110. operator int() const {
  111. return const_value;
  112. }
  113.  
  114. MyClass operator + (const MyClass& mc) {
  115. out("op+ l pv", private_value);
  116. out("op+ l cv", const_value);
  117. out("op+ r pv", mc.private_value);
  118. out("op+ r cv", mc.const_value);
  119. MyClass mc2(const_value + mc.const_value);
  120. out("op+ pv", mc2.private_value);
  121. out("op+ cv", mc2.const_value);
  122. return mc2;
  123. }
  124.  
  125. void func_access_pub() {
  126. // func_access_prv(); //NG
  127. // func_access_prt(); //NG
  128.  
  129. // pub_private_func(); //NG
  130. pub_protected_func(); //OK
  131. pub_public_func(); //OK
  132.  
  133. // prv_private_func(); //NG
  134. // prv_protected_func(); //NG
  135. // prv_public_func(); //NG
  136.  
  137. // prt_private_func(); //NG
  138. // prt_protected_func(); //NG
  139. // prt_public_func(); //NG
  140. }
  141. };
  142.  
  143. const int MyClass::static_const_value = 123;
  144. int MyClass::static_private_value = 1;
  145.  
  146. void MyClass::static_public_func() { out("spf"); }
  147. void MyClass::private_func() {}
  148. void MyClass::protected_func() {}
  149. void MyClass::public_func() {}
  150.  
  151. MyClass::MyClass() : PrivateClass(1), const_value(1) {
  152. private_value = MyClass::static_private_value++;
  153. }
  154. MyClass::MyClass(int x) : PrivateClass(x * 2), const_value(x) {
  155. private_value = MyClass::static_private_value++;
  156. }
  157. MyClass::~MyClass() { out("des:", private_value); }
  158.  
  159. void friend_func(MyClass& mc) {
  160. mc.private_value = 777;
  161. }
  162.  
  163. int main() {
  164. MyClass mc;
  165. MyClass mc1(123);
  166. const MyClass mc2(999);
  167. MyClass mc3 = 9876;
  168. MyClass *mc4 = new MyClass; //動的割り当て
  169. MyClass *mc5 = new MyClass(789); //動的割り当て
  170. PublicClass *pub = &mc; //OK
  171. // PrivateClass *prv = &mc; //NG
  172. // ProtectedClass *prt = &mc; //NG
  173. VirtualClass *vir = &mc; //OK
  174.  
  175. out("Public Value");
  176. out(mc.public_value = 333);
  177.  
  178. out("Virtual Class.");
  179. pub->virtual_func(); //pub
  180. vir->virtual_func(); //vir
  181. mc.virtual_func(); //my
  182. mc.VirtualClass::virtual_func(); //vir
  183. mc.PublicClass::virtual_func(); //pub
  184. // mc.PrivateClass::virtual_func(); //NG
  185. // mc.ProtectedClass::virtual_func(); //NG
  186.  
  187.  
  188. out("Static Function");
  189. MyClass::static_public_func();
  190.  
  191. out("Friend Function");
  192. friend_func(mc);
  193.  
  194. out("Inline Function");
  195. out(mc.getPrivateValue());
  196.  
  197. out("Override Fail Function");
  198. pub->override_fail_func(); //pub
  199. mc.override_fail_func(); //my
  200. mc.PublicClass::override_fail_func(); //pub
  201. out("Override success Function");
  202. pub->override_success_func(); //my
  203. mc.override_success_func(); //my
  204. mc.PublicClass::override_success_func(); //pub
  205.  
  206. out("Base Class");
  207. // mc.pub_private_func(); //NG
  208. // mc.pub_protected_func(); //NG
  209. mc.pub_public_func(); //OK
  210.  
  211. // mc.prv_private_func(); //NG
  212. // mc.prv_protected_func(); //NG
  213. // mc.prv_public_func(); //NG
  214.  
  215. // mc.prt_private_func(); //NG
  216. // mc.prt_protected_func(); //NG
  217. // mc.prt_public_func(); //NG
  218.  
  219. out("Function Access");
  220. mc.func_access_pub();
  221.  
  222. out("Override Operator");
  223. out("mc3 pv", mc3.getPrivateValue());
  224. out("mc3 cv", (int)mc3);
  225. MyClass mc6;
  226. out("mc6 pv", mc6.getPrivateValue());
  227. out("mc6 cv", (int)mc6);
  228. out(mc6 + mc3 + *mc5);
  229.  
  230. out("Const Class");
  231. // mc2.const_ng_func(); //NG
  232. mc2.const_ok_func(); //OK
  233.  
  234. out("delete.");
  235. delete mc4;
  236. delete mc5;
  237. out("deleted.");
  238.  
  239. return 0;
  240. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
Public Value
333
Virtual Class.
pub
vir
my
vir
pub
Static Function
spf
Friend Function
Inline Function
777
Override Fail Function
pub
my
pub
Override success Function
my
my
pub
Base Class
Function Access
Override Operator
mc3 pv 4
mc3 cv 9876
mc6 pv 7
mc6 cv 1
op+ l pv 7
op+ l cv 1
op+ r pv 4
op+ r cv 9876
op+ pv 8
op+ cv 9877
op+ l pv 8
op+ l cv 9877
op+ r pv 6
op+ r cv 789
op+ pv 9
op+ cv 10666
10666
des: 9
des: 8
Const Class
delete.
des: 5
des: 6
deleted.
des: 7
des: 4
des: 3
des: 2
des: 777