Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. class Base {
  4. public:
  5.     int val;
  6.     Base(int initialization);
  7.     virtual void which();
  8. };
  9.  
  10. Base::Base(int init) {
  11.     val = init;
  12. }
  13.  
  14. void Base::which() {
  15.     std::cout << "Base" << "\n";
  16. }
  17.  
  18. class Derived : public Base {
  19. public:
  20.     Derived(int init);
  21.     void which();
  22. };
  23.  
  24. Derived::Derived(int init) : Base(init) {
  25.    
  26. }
  27.  
  28. void Derived::which() {
  29.     std::cout << "Derived" << "\n";
  30. }
  31.  
  32. int main() {
  33.     Base b = Derived(5);
  34.     b.which(); //prints base, but I want Derived
  35.     Base *pb = new Derived(5);
  36.     pb->which(); //prints derived
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement