fork download
  1. #include <iostream>
  2.  
  3. class Base
  4. {
  5. protected:
  6. int x;
  7. std::string s;
  8.  
  9. public:
  10. Base() = default;
  11. Base(int x, std::string s)
  12. {
  13. this->x = x;
  14. this->s = s;
  15.  
  16. std::cout << "Constuctor in Base Class Called" << std::endl;
  17. }
  18.  
  19. int getX()
  20. {
  21. return x;
  22. }
  23.  
  24. std::string getS()
  25. {
  26. return s;
  27. }
  28.  
  29. ~Base()
  30. {
  31. std::cout << "Destructor in Base Class" << std ::endl;
  32. }
  33. };
  34.  
  35. class Derived : public Base
  36. {
  37. private:
  38. int z;
  39.  
  40. public:
  41. Derived() = default;
  42.  
  43. Derived(int x, std::string s, int z) : Base(x, s)
  44. {
  45. this->z = z;
  46. std::cout << "Constuctor in Derived Class Called" << std::endl;
  47. }
  48.  
  49. int getZ()
  50. {
  51. return z;
  52. }
  53.  
  54. void display()
  55. {
  56. std::cout << getX() << getS() << z << std::endl;
  57. }
  58.  
  59. ~Derived()
  60. {
  61. std::cout << "Destructor in Derived Class" << std::endl;
  62. }
  63. };
  64.  
  65. int main()
  66. {
  67.  
  68. Derived d(5, "Hello", 55);
  69. d.display();
  70. return 0;
  71. }
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
Constuctor in Base Class Called
Constuctor in Derived Class Called
5Hello55
Destructor in Derived Class
Destructor in Base Class