-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
44 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
class A { | ||
public: // es reicht, wenn alles public ist (hier nur, damit das Beispiel mit g++ kompiliert) | ||
A(int x) { aval = x; } | ||
|
||
void foo() { print_char('A'); print_char('f'); print_int(aval); } | ||
|
||
int aval; | ||
}; | ||
|
||
class B : public A { | ||
public: // es reicht, wenn alles public ist (hier nur, damit das Beispiel mit g++ kompiliert) | ||
// C'tor muss Basisklasse initialisieren | ||
B(int x) : A(x+3) { bval = x; } | ||
|
||
// überschriebene Methode aus A | ||
void foo() { print_char('B'); print_char('f'); print_int(aval); print_int(bval); } | ||
|
||
// eigene Methode | ||
void bar() { print_char('B'); print_char('b'); print_int(aval); print_int(bval); } | ||
|
||
int bval; | ||
}; | ||
|
||
|
||
int main() { | ||
// Vererbung: Initialisierung Basisklasse; Überschreiben von Methoden | ||
A x(2); | ||
B y(7); | ||
|
||
x.foo(); // A, f, 2 | ||
y.foo(); // B, f, 10, 7 | ||
y.bar(); // B, b, 10, 7 | ||
|
||
x.aval = 8; | ||
y.bval = 4; | ||
|
||
x.foo(); // A, f, 8 | ||
y.foo(); // B, f, 10, 4 | ||
y.bar(); // B, b, 10, 4 | ||
|
||
|
||
return 0; | ||
} |