fork download
  1. //==============================================================================
  2. // Include Files
  3. //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  4. #include <iostream>
  5. #include <string>
  6. //==============================================================================
  7. // Name Space
  8. //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  9. using namespace std;
  10. //==============================================================================
  11. // Class
  12. //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  13. class ParentClass {
  14. //--------------------------------------------------------------------------
  15. public:
  16. ParentClass() { // Constructor
  17. //this->SetName(); // ERROR!!
  18. }
  19. ~ParentClass() { // Destructor
  20. }
  21. virtual void ShowName(void) {
  22. cout<<"The name is "<<this->name<<endl;
  23. }
  24. //--------------------------------------------------------------------------
  25. protected:
  26. virtual void SetName(void)=0;
  27. string name;
  28. //--------------------------------------------------------------------------
  29. };
  30. //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  31. class ChildClassA:public ParentClass {
  32. //--------------------------------------------------------------------------
  33. public:
  34. ChildClassA() { // Constructor
  35. cout<<"ChildClassA be created."<<endl;
  36. //this->SetName();
  37. }
  38. ~ChildClassA() { // Destructor
  39. cout<<"ChildClassA be killed."<<endl;
  40. }
  41. //--------------------------------------------------------------------------
  42. protected:
  43. virtual void SetName(void) {
  44. this->name = "Tom";
  45. }
  46. //--------------------------------------------------------------------------
  47. };
  48. //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  49. class ChildClassB:public ParentClass {
  50. //--------------------------------------------------------------------------
  51. public:
  52. ChildClassB() { // Constructor
  53. cout<<"ChildClassB be created."<<endl;
  54. //this->SetName();
  55. }
  56. ~ChildClassB() { // Destructor
  57. cout<<"ChildClassB be killed."<<endl;
  58. }
  59. //--------------------------------------------------------------------------
  60. protected:
  61. virtual void SetName(void) {
  62. this->name = "Sam";
  63. }
  64. //--------------------------------------------------------------------------
  65. };
  66. //==============================================================================
  67. // Main
  68. //=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  69. int main() {
  70. //--------------------------------------------------------------------------
  71. ChildClassA *child_a;
  72. child_a = new ChildClassA();
  73. child_a->ShowName();
  74. delete child_a;
  75. //--------------------------------------------------------------------------
  76. ChildClassB *child_b;
  77. child_b = new ChildClassB();
  78. child_b->ShowName();
  79. delete child_b;
  80. //--------------------------------------------------------------------------
  81. return (0);
  82. }
  83. //==============================================================================
  84.  
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
ChildClassA be created.
The name is 
ChildClassA be killed.
ChildClassB be created.
The name is 
ChildClassB be killed.