-
Notifications
You must be signed in to change notification settings - Fork 0
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
6 changed files
with
131 additions
and
5 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
//54-Override | ||
|
||
#include <iostream> | ||
|
||
class A{ | ||
public: | ||
virtual void print(){ | ||
std::cout << "class A print function." << std::endl; | ||
} | ||
}; | ||
|
||
class B: public A{ | ||
public: | ||
void print() override{ //also virtual. no need to use virtual again. | ||
std::cout << "class B print function." << std::endl; | ||
A::print(); //explicit qualification. | ||
} | ||
}; | ||
|
||
int main(){ | ||
B b; | ||
b.print(); | ||
|
||
A& a = b; | ||
a.print(); | ||
} |
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,33 @@ | ||
//55-UsingDirective | ||
|
||
#include <iostream> | ||
|
||
class A{ | ||
public: | ||
void f(int x){ | ||
std::cout << "integer. " << x << std::endl; | ||
} | ||
}; | ||
|
||
class B: public A{ | ||
public: | ||
void f(double x){ | ||
std::cout << "double. " << x << std::endl; | ||
} | ||
}; | ||
|
||
class C: public B{ | ||
public: | ||
using A::f; | ||
using B::f; | ||
void f(char x){ | ||
std::cout << "char. " << x << std::endl; | ||
} | ||
}; | ||
|
||
int main(){ | ||
C c; | ||
c.f(1); | ||
c.f(1.2); | ||
c.f('a'); | ||
} |
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,23 @@ | ||
//56-InheritingConstructor | ||
|
||
#include <iostream> | ||
|
||
class A{ | ||
public: | ||
A(int x){ | ||
std::cout << "base class constructor. " << x << std::endl; | ||
} | ||
}; | ||
|
||
class B: A{ | ||
public: | ||
using A::A; | ||
B(): A{3}{ | ||
std::cout << "derived class constructor." << std::endl; | ||
} | ||
}; | ||
|
||
int main(){ | ||
B b1{1}; | ||
B b2; | ||
} |
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,32 @@ | ||
//57-CovariantReturn | ||
//Return Type Relaxation. | ||
|
||
#include <iostream> | ||
|
||
class A{ | ||
public: | ||
A(){}; | ||
A(int x){ | ||
std::cout << "base class constructor." << x << std::endl; | ||
} | ||
|
||
virtual A* make(int x){ | ||
return new A{x}; | ||
} | ||
}; | ||
|
||
class B: public A{ | ||
public: | ||
B(int x){ | ||
std::cout << "derived class constructor." << x << std::endl; | ||
} | ||
|
||
B* make(int x) override{ //return type is derived type, not base. | ||
return new B{x}; | ||
} | ||
}; | ||
|
||
int main(){ | ||
B b1{1}; | ||
B *b2{b1.make(2)}; | ||
} |
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