Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 86c6260

Browse files
committed
overloading: C++
1 parent 5db09d4 commit 86c6260

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

‎book/cite.bib‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,4 +577,13 @@ @online{2013-manura
577577
url = {http://lua-users.org/wiki/TypeIntrospection},
578578
urldate = {Apr 6, 2013},
579579
organization = {http://lua-users.org},
580+
}
581+
582+
@online{2009-bailey,
583+
author = {CB Bailey},
584+
title = {Overloading a method in a subclass in C++},
585+
year = {2009},
586+
url = {https://stackoverflow.com/a/1734905},
587+
urldate = {Oct 6, 2009},
588+
organization = {stackoverflow.com},
580589
}

‎book/polymorphism.tex‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,9 @@ \subsection{C++}
242242

243243
\lstinputlisting[language={[KB]C++}, linerange={76-76}, style=codeStyle]{../codes/cpp/polymorphism/type_manip.cpp}
244244

245-
There are two types of cast: at compilation time (\keyword[C++]{static\_cast}) or at runtime (\keyword[C++]{dynamic\_cast}).
245+
There are two types of down-casting (among others): \keyword[C++]{static\_cast} and \keyword[C++]{dynamic\_cast}.
246246
Using the first one, type check is performed during compilation.
247-
The dynamic cast check types at runtime and return \keyword[C++]{NULL} in case of pointers, or throw an exception in case of references.
247+
The dynamic cast checks types at runtime and return \keyword[C++]{NULL} in case of pointers, or throw an exception in case of references.
248248

249249
\lstinputlisting[language={[KB]C++}, linerange={83-87}, style=codeStyle]{../codes/cpp/polymorphism/type_manip.cpp}
250250

@@ -440,6 +440,27 @@ \subsection{Ruby}
440440

441441
\section{Methods overloading}
442442

443+
It is the feature of a class having multiple methods defined by the same identifier and different parameters.
444+
Statically-typed programming languages are, usually, the ones affording methods overloading since they enforce type checking during function calls.
445+
In this section, we will verify these properties:
446+
\begin{itemize}
447+
\item If the programming language supports methods overloading.
448+
If not, how to afford something similar.
449+
\item If we can overload methods over inheritance
450+
\end{itemize}
451+
452+
\subsection{C++}
453+
454+
In C++, overloading is permitted in the same class
455+
456+
\lstinputlisting[language={[KB]C++}, linerange={4-8,16-18,22-23,31-32}, style=codeStyle]{../codes/cpp/polymorphism/overloading.cpp}
457+
458+
When the same method is overloaded in a subclass, the new definition will hide those of the parent class;
459+
if they are called, this will generate a compilation error.
460+
To call them, there are two ways: import them using \keyword[C++]{using} declaration, or by calling them explicitly \citep{2009-bailey}.
461+
\lstinputlisting[language={[KB]C++}, linerange={10-17,19-19,26-32}, style=codeStyle]{../codes/cpp/polymorphism/overloading.cpp}
462+
463+
443464

444465
\section{Methods overriding}
445466

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
class Person {
5+
public:
6+
void read(){ std::cout << "I am a reading" << std::endl; }
7+
void read(std::string text){ std::cout << "A text: " << text << std::endl; }
8+
};
9+
10+
class Student: public Person {
11+
public:
12+
using Person::read;
13+
void read(int nbr){ std::cout << "I read on table n°: " << nbr << std::endl; }
14+
};
15+
16+
int main()
17+
{
18+
Person pe;
19+
Student st;
20+
21+
std::cout << "PERSON" << std::endl;
22+
pe.read();
23+
pe.read("I am a person");
24+
25+
std::cout << "STUDENT" << std::endl;
26+
st.read(); // using Person::read; or error
27+
st.Person::read(); // Without using Person::read;
28+
st.read("I am a student"); // using Person::read; or error
29+
st.read(5);
30+
31+
return 0;
32+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /