0% found this document useful (0 votes)
38 views2 pages

9.2 Overloading

This document discusses polymorphism and function overloading in C++. It provides an example of a Date class that overloads its constructor by defining two versions - one that takes day, month, and year as arguments, and one that takes just day and month and sets the year automatically. It then instructs the reader to practice overloading a normal function outside of a class by creating multiple versions of a hello() function that output different messages depending on the number and type of arguments passed to it.

Uploaded by

Divyanshu Rawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views2 pages

9.2 Overloading

This document discusses polymorphism and function overloading in C++. It provides an example of a Date class that overloads its constructor by defining two versions - one that takes day, month, and year as arguments, and one that takes just day and month and sets the year automatically. It then instructs the reader to practice overloading a normal function outside of a class by creating multiple versions of a hello() function that output different messages depending on the number and type of arguments passed to it.

Uploaded by

Divyanshu Rawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

C++ - Udacity

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

Watch Video At: https://youtu.be/Y-SSHBtvPHo

Polymorphism
Polymorphism is means "assuming many forms".

In the context of object-oriented programming, polymorphism) describes a paradigm in


which a function may behave differently depending on how it is called. In particular, the
function will perform differently based on its inputs.

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.

This example of class Date overloads:

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.

1. Create a function hello() that outputs, "Hello, World!"


2. Create a class Human .
3. Overload hello() by creating a function hello(Human human) . This function should
output, "Hello, Human!"
4. Create 2 more classes and use those classes to further overload the hello()
function.

Menu

full screen
Expand

2/2

You might also like