fork download
  1. #include <iostream>
  2. using namespace std;
  3. class Base{
  4. protected:
  5. int x;
  6. string s;
  7. public:
  8. Base(int x, string s){
  9. this->x=x;
  10. this->s=s;
  11. cout<<"Constructor in the Base class"<<endl;
  12. }
  13. ~Base(){
  14. cout<<"Destructor in the Base class"<<endl;
  15. }
  16. void setVar(int x, string s){
  17. this->x=x;
  18. this->s=s;
  19. }
  20. int getX(){return x;}
  21. string getS(){return s;}
  22. };
  23.  
  24.  
  25. class Derived : public Base{
  26. private:
  27. int z;
  28. public:
  29. Derived(int x, string s, int z): Base(x, s){
  30. this->z=z;
  31. cout<<"Constructor in the Derived class"<<endl;
  32. }
  33. ~Derived(){
  34. cout<<"Destructor in the Derived class"<<endl;
  35. }
  36. void setVar(int z){
  37. this->z=z;
  38. }
  39. int getZ(){return z;}
  40.  
  41. void display(){
  42. cout<<x<<" "<<s<<" "<<z<<endl;
  43. }
  44. };
  45.  
  46.  
  47. int main(){
  48. Derived d(5, "abc", 10);
  49. d.display();
  50. return 0;
  51. }
  52.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Constructor in the Base class
Constructor in the Derived class
5 abc 10
Destructor in the Derived class
Destructor in the Base class