9.2 Overloading
9.2 Overloading
classroom.udacity.com/nanodegrees/nd213/parts/f9fffe8e-1984-4045-92b6-
64854de4df2b/modules/70db33ed-8e7e-45e9-ab1d-3d0b257fc196/lessons/15cd39a7-3fda-495d-af24-
c5ccd45826a8/concepts/e185eaf8-ae1d-4799-995b-4302a2fca6bb
Polymorphism
Polymorphism is means "assuming many forms".
Polymorphism can be achieved in two ways in C++: overloading and overriding. In this
exercise we will focus on overloading.
Overloading
In C++, you can write two (or more) versions of a function with the same name. This is
called "overloading". Overloading requires that we leave the function name the same,
but we modify the function signature. For example, we might define the same function
name with multiple different configurations of input arguments.
1/2
#include <ctime>
class Date {
public:
Date(int day, int month, int year) : day_(day), month_(month), year_(year) {}
Date(int day, int month) : day_(day), month_(month)
{
time_t t = time(NULL);
tm* timePtr = localtime(&t);
year_ = timePtr->tm_year;
}
private:
int day_;
int month_;
int year_;
};
Instructions
Overloading can happen outside of an object-oriented context, too. In this exercise, you
will practice overloading a normal function that is not a class member.
Menu
full screen
Expand
2/2