Last active
August 31, 2015 21:25
-
-
Save W4RH4WK/cdaff03df6e14bc9be46 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { | |
protected: | |
int count; | |
public: | |
A(void) : count(0) {} | |
int getCount(void) { | |
return this->count; | |
} | |
void add(void) { | |
this->count++; | |
} | |
void add2(void) { | |
this->add(); | |
this->add(); | |
} | |
}; | |
class B : public A { | |
public: | |
void add(void) { | |
this->count++; | |
this->count++; | |
} | |
}; | |
int main(int argc, char *argv[]) | |
{ | |
A a; | |
a.add2(); | |
cout << "a.count = " << a.getCount() << endl; | |
// a.count = 2 | |
B b; | |
b.add2(); | |
cout << "b.count = " << b.getCount() << endl; | |
// b.count = 2 | |
return 0; | |
} |
This file contains hidden or 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
class A(object): | |
def __init__(self): | |
self.count = 0 | |
def add(self): | |
self.count += 1 | |
def add2(self): | |
self.add() | |
self.add() | |
class B(A): | |
def add(self): | |
self.count += 2 | |
if __name__ == '__main__': | |
a = A() | |
a.add2() | |
print 'a.count =', a.count | |
# a.count = 2 | |
b = B() | |
b.add2() | |
print 'b.count =', b.count | |
# b.count = 4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment