Created
February 5, 2009 20:17
-
-
Save zwily/58974 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
class A { | |
public: | |
A(int val) : m_val(val) {} | |
A& operator=(A& a) { | |
m_val += a.m_val; | |
return *this; | |
} | |
int val() { return m_val; } | |
protected: | |
int m_val; | |
}; | |
class B : public A { | |
public: | |
B(int val) : A(val) {} | |
B& operator=(B& b) { | |
A::operator=(b); | |
m_val += 5; | |
return *this; | |
} | |
}; | |
int main(int argc, char** argv) { | |
A a1(1); | |
A a2(2); | |
a1 = a2; | |
cout << "should be 3: "; | |
cout << a1.val() << endl; | |
B b1(1); | |
B b2(2); | |
b1 = b2; | |
cout << "should be 8: "; | |
cout << b1.val() << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment