0% found this document useful (0 votes)
109 views4 pages

Gombe State University

The document discusses method overriding in C++. It defines method overriding as redefining a function in a derived class that has the same declaration as a function in the base class. It lists two requirements for overriding: 1) inheritance must be present, and 2) the function must have the same name, return type, and parameter list in both classes. Examples are provided to demonstrate how overriding works, showing that calling an overridden function through a derived class object calls the derived class's version, while calling through a base class pointer calls the base class version.

Uploaded by

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

Gombe State University

The document discusses method overriding in C++. It defines method overriding as redefining a function in a derived class that has the same declaration as a function in the base class. It lists two requirements for overriding: 1) inheritance must be present, and 2) the function must have the same name, return type, and parameter list in both classes. Examples are provided to demonstrate how overriding works, showing that calling an overridden function through a derived class object calls the derived class's version, while calling through a base class pointer calls the base class version.

Uploaded by

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

GOMBE STATE UNIVERSITY

FACULTY OF SCIENE
DEPARTMENT OF MATHEMATICS
COURSE CODE:
COSC 301
COURSE TITLE:
OBJECT ORIENTED PROGRAMMING AND
FUNDAMENTAL OF C++
QUESTION:
WITH GOOD AND EXECUTABLE EXAMPLES
CLEARLY GIVE THE SYNTAX AND SEMANTICS OF
METHOD OVERRIDING
ASSIGNMENT
BY
ABUBAKAR MUSTAPHA
UG14/SCCS/1066
JANUARY 2017
Polymorphism

Polymorphism means having multiple forms of one thing. In inheritance,


polymorphism is done, by method overriding, when both super and sub class have
member function with same declaration but different definition.

Method Overriding

If we inherit a class into the derived class and provide a definition for one of the
base class's function again inside the derived class, then that function is said to be
overridden, and this mechanism is called Method Overriding

Requirements for Overriding

1. Inheritance should be there. Function overriding cannot be done within a


class. For this we require a derived class and a base class.
2. Function that is redefined must have exactly the same declaration in both
base and derived class, that means same name, same return type and same
parameter list.

Example of function of overriding


class Base
{
public:
void show()
{
cout << "Base class";
}
};
class Derived:public Base
{
public:
void show()
{
cout << "Derived Class";
}
}
class Base
{
public:
void shaow()
{
cout << "Base class\t";
}
};
class Derived:public Base
{
public:
void show()
{
cout << "Derived Class";
}
}

int main()
{
Base b; //Base class object
Derived d; //Derived class object
b.show(); //Early Binding Ocuurs
d.show();
}

Therefore the output of the above code is:

Output :

Base class Derived class

In the above example, we are calling the overrided function using Base class and
Derived class object. Base class object will call base version of the function and
derived class's object will call the derived version of the function.

But when we use a Base class's pointer or reference to hold Derived class's object,
then Function call Binding gives some unexpected results.
Another example of function/ method overriding:

class Base
{
public:
void show()
{
cout << "Base class";
}
};
class Derived:public Base
{
public:
void show()
{
cout << "Derived Class";
}
}

int main()
{
Base* b; //Base class pointer
Derived d; //Derived class object
b = &d;
b->show(); //Early Binding Occurs
}
Therefore the output of the above code is:

Output:

Base class

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy