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

EXP-7 Inheritance and Function Overriding

The document explains the concepts of inheritance and function overriding in C++. Inheritance allows a child class to acquire properties and behaviors from a parent class, while function overriding enables the child class to redefine a function already present in the parent class, facilitating runtime polymorphism. Two C++ code examples illustrate these concepts, demonstrating how function calls are resolved at runtime and compile time.

Uploaded by

shiv.prasad2049
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)
29 views2 pages

EXP-7 Inheritance and Function Overriding

The document explains the concepts of inheritance and function overriding in C++. Inheritance allows a child class to acquire properties and behaviors from a parent class, while function overriding enables the child class to redefine a function already present in the parent class, facilitating runtime polymorphism. Two C++ code examples illustrate these concepts, demonstrating how function calls are resolved at runtime and compile time.

Uploaded by

shiv.prasad2049
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/ 2

7.

Programs to implement Inheritance and Function overriding


 In C++, inheritance is a process in which one object acquires all the properties and behaviors of
its parent object automatically.
 In such a way, you can reuse, extend or modify the attributes and behaviors which are defined in
other classes.
 The function overriding concept exists due to the inheritance in C++.
 The function overriding in C++ aids us to use a function with the same name that is in the child
class that is already present in its parent class.
 The function overriding in C++ is used to achieve runtime polymorphism. Runtime
polymorphism is also known as dynamic polymorphism or late binding.
 In runtime polymorphism, the function call is resolved at run time. In contrast, to compile-time
or static polymorphism, the compiler deduces the object at run time and then decides which
function call to bind to the object.

//Example 1: C++ program to demonstrate function overriding

#include <iostream>
using namespace std;

class Animal {
public:
void sound() {
cout << "Animal sound" << endl;
}
};

class Lion : public Animal {


public:
void sound() {
cout << "Roar" << endl;
}
};

int main() {
Animal a;
a.sound();

Lion l;
l.sound();
return 0;
}
Output:
"Animal sound"
"Roar"

// Example 2: C++ program to demonstrate compile time function overriding


#include <iostream>
using namespace std;

class Parent {
public:
void GeeksforGeeks_Print()
{
cout << "Base Function" << endl;
}
};

class Child : public Parent {


public:
void GeeksforGeeks_Print()
{
cout << "Derived Function" << endl;
}
};

int main()
{
Child Child_Derived;
Child_Derived.GeeksforGeeks_Print();
return 0;
}

Output
Derived Function

You might also like