fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Foo
  5. {
  6. int number;
  7. public:
  8. Foo (int number) {
  9. this->number = number;
  10. }
  11.  
  12. ~Foo() {
  13. cout << "Destroy Foo: " << this->number << endl;
  14. }
  15.  
  16. virtual void print() {
  17. cout << "Foo!" << endl;
  18. }
  19.  
  20. void print(int x) {
  21. cout << "Foo: Number: " << x << endl;
  22. }
  23.  
  24. virtual void printNumber() {
  25. cout << "Number: " << this->number << endl;
  26. }
  27. };
  28.  
  29. class Hoge : public Foo
  30. {
  31. friend void printValue(Hoge &hoge);
  32.  
  33. private:
  34. int value;
  35.  
  36.  
  37. public:
  38. Hoge(int value) : Foo(value * 3) {
  39. this->value = value;
  40. }
  41. ~Hoge() {
  42. cout << "Destroy Hoge: " << this->value << endl;
  43. }
  44.  
  45. int setValue(int value);
  46. int getValue();
  47. int operator + (Hoge & hoge);
  48. int operator ++();
  49. int operator ++(int n);
  50. void print();
  51. void print(int x);
  52. };
  53.  
  54.  
  55. int Hoge::setValue(int value) {
  56. int oldvalue = this->value;
  57. this->value = value;
  58. return oldvalue;
  59. }
  60.  
  61. int Hoge::getValue() {
  62. return this->value;
  63. }
  64.  
  65. int Hoge::operator + (Hoge & hoge) {
  66. return this->value + hoge.value;
  67. }
  68.  
  69. int Hoge::operator ++() {
  70. return ++this->value;
  71. }
  72.  
  73. int Hoge::operator ++(int n) {
  74. return this->value++;
  75. }
  76.  
  77. void Hoge::print() {
  78. cout << "Hoge!" << endl;
  79. }
  80.  
  81. void Hoge::print(int x) {
  82. cout << "Hoge: Number: " << x << endl;
  83. }
  84.  
  85. void printValue(Hoge & hoge) {
  86. cout << "value: " << hoge.value << endl;
  87. }
  88.  
  89. int main() {
  90. Hoge hoge(13), fuga(22);
  91. Foo &foo = hoge;
  92.  
  93. cout << hoge.getValue() << endl;
  94. cout << hoge.setValue(55) << endl;
  95. cout << hoge.getValue() << endl;
  96. cout << (hoge + fuga) << endl;
  97. cout << ++fuga << endl;
  98. cout << fuga++ << endl;
  99.  
  100. foo.print();
  101. foo.print(123);
  102. foo.printNumber();
  103.  
  104. hoge.print();
  105. hoge.print(123);
  106. hoge.printNumber();
  107.  
  108. printValue(fuga);
  109.  
  110. return 0;
  111. }
Success #stdin #stdout 0s 3344KB
stdin
Standard input is empty
stdout
13
13
55
77
23
23
Hoge!
Foo: Number: 123
Number: 39
Hoge!
Hoge: Number: 123
Number: 39
value: 24
Destroy Hoge: 24
Destroy Foo: 66
Destroy Hoge: 55
Destroy Foo: 39