fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class TestClass{
  5. protected:
  6. int num;
  7. public:
  8. TestClass(int n):num(n){
  9. cout<<this<<" : init of : " <<this->num<<endl;
  10. }
  11. TestClass(const TestClass& t):num(t.num){
  12. cout<<this<<" : copyInit of : " <<this->num<<endl;
  13. }
  14. virtual void printNum () const { cout << "NUM: " << num << endl; }
  15. };
  16.  
  17. class TestClassDerived : public TestClass {
  18. public:
  19. TestClassDerived(int n):TestClass(n){}
  20. virtual void printNum () const { cout << "DERIVED NUM: " << num << endl; }
  21. };
  22.  
  23.  
  24. int main(int argc, const char * argv[]){
  25. const TestClass t1 = TestClass(100); //option1
  26. TestClassDerived td2(100);
  27. const TestClass &t2 = TestClassDerived(100); //option2
  28. t1.printNum();
  29. t2.printNum();
  30. }
Success #stdin #stdout 0s 2928KB
stdin
Standard input is empty
stdout
0xbfd26158 : init of : 100
0xbfd26150 : init of : 100
0xbfd26148 : init of : 100
NUM: 100
DERIVED NUM: 100