Chapter-4 (3) Function Overriding, Member Classes Nesting of Classes
Chapter-4 (3) Function Overriding, Member Classes Nesting of Classes
Course Objectives
2
Course Outcomes
CO Course Outcome
Number
CO1 Understand the concepts of object-oriented programming
including programming process and compilation process.
CO2 Apply different techniques to decompose a problem and
programmed a solution with its sub modules.
CO3 Analyze and explain the behavior of simple programs involving the
programming addressed in the course.
CO4 Implement and evaluate the programs using the syntax and
semantics of object-oriented programming.
CO5 Design the solution of real-world problems in order to determine
that the program performs as expected.
3
Scheme of Evaluation
4
• Defining derived class,
• modes of inheritance,
• types of inheritance,
CONTENTS • ambiguity in inheritance,
• virtual base class,
• Function overriding,
The capability of a class to derive properties and • Member Classes: Nesting of Classes.
characteristics from another class is called
Inheritance.
Inheritance is one of the most important feature
of Object Oriented Programming
5
FUNCTION OVERRIDING
If the derived class has the function with same name as the function in the base class, then it will hide the function of base
class.
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 Function Overriding
• IN SHORT: If derived class defines same function as defined in its base class, it is known as function overriding in C++. It
is used to achieve runtime polymorphism. It enables you to provide specific implementation of the function which is
already provided by its base class.
6
FUNCTION OVERLOADING VS.
FUNCTION OVERRIDING
• Function Overloading happens in the same class when we declare
same functions with different arguments in the same class.
• Function Overriding is happens in the child class when child class
overrides parent class function.
• Function overriding is a feature that allows us to have a same function
in child class which is already present in the parent class. A child class
inherits the data members and member functions of parent class, but
when you want to override a functionality in the child class then you
can use function overriding.
7
8
EXAMPLE # 1 - FUNCTION
OVERRIDING
11
SOLUTION
12
NESTING OF CLASSES
• A nested class is a member and as such has the same access rights as any other member.
• The name of the nested class exists in the scope of the enclosing class, and name lookup from a member function of a
nested class visits the scope of the enclosing class after examining the scope of the nested class.
• Like any member of its enclosing class, the nested class has access to all names (private, protected, etc) to which the
enclosing class has access, but it is otherwise independent and has no special access to the this pointer of the enclosing
class.
• A nested class is declared within the scope of another class. The name of a nested class is local to its enclosing class.
• A member function can call another member function of the same class directly without using the dot operator. This is
called as nesting of member functions. Nesting of member functions. You know that only the public members of a class can
be accessed by the object of that class, using dot operator
• Why do we need nested classes?
• Nested classes represent a special type of relationship that is it can access all the members (data members and methods) of outer class including private.
• Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only.
13
NESTING OF CLASSES
• A nested class is a class that is declared in another class. The nested class is also a member variable of the enclosing class
and has the same access rights as the other members
• Nested classes can be forward-declared and later defined, either within the same enclosing class body, or outside of it[4]:
class enclose {
class nested1; // forward declaration
class nested2; // forward declaration
class nested1 {}; // definition of nested class
};
class enclose::nested2 { }; // definition of nested class
• They enable you to logically group classes that are only used in one place, thus this increases the use of encapsulation, and
create more readable and maintainable code.
14
NESTING OF CLASSES
#include <iostream>
using namespace std; int main()
class outer {
{
outer obj;
outer::inner obj1;
public: cout<<obj.a<<endl;
int a=10; cout<<obj1.b;
class inner{
return 0;
public:
}
int b=20;
};
}; Output:
10
20
15
NESTING OF CLASSES-EXAMPLE 1
• A nested class is a class which is declared in another enclosing class.
• A nested class is a member and as such has the same access rights as any other member.
• The members of an enclosing class have no special access to members of a nested class; the usual access rules shall be
obeyed.
20
APPLICATIONS
• We use nested classes to reflect and to enforce the relationship between two classes.
• We should define a class within another class when the nested class makes sense only in the context of its
enclosing class or when it relies on the enclosing class for its function.
• For example, a text cursor might make sense only in the context of a text component.
• A nested class is a class that is declared in another class.
• The nested class is also a member variable of the enclosing class and has the same access rights as the other
members.
• However, the member functions of the enclosing class have no special access to the members of a nested
class.
21
Frequently Asked Questions: -
1. How do you declare a nested class in C++?
2. Can you avoid complex nesting?
3. Which feature of OOP reduces the use of nested classes?
4. How many categories are nested classes divided into?
5. Non-static nested classes have access to which members from
enclosing class?
6. What are the advantages/disadvantages of nested classes?
22
SUMMARY
1. If a class is defined inside another class, the inner class is termed as nested class. The inner class is local to
the enclosing class. Scope matters a lot here.
2. Using inheritance we can have the security of the class being inherited. The subclass can access the
members of parent class. And have more feature than a nested class being used.
3. The nested classes are divided into two main categories. Namely, Static and non-static. The categories
define how the classes can be used inside another class.
4. The non-static nested class can access all the members of the enclosing class. All the data members and
member functions can be accessed from the nested class. Even if the members are private, they can be
accessed.
5. The static nested class doesn’t have access to any other members of the enclosing class. This is a rule that
is made to ensure that only the data which can be common to all the object is being accessed by the static
nested class.
23
REFERENCES
Reference Website
[1] https://www.geeksforgeeks.org/inheritance-in-c/
[2] https://www.geeksforgeeks.org/object-oriented-programming-in-cpp/
[3] https://www.geeksforgeeks.org/copy-constructor-argument-const/
[4] https://en.cppreference.com/w/cpp/language/nested_types
References on YouTubes
[1] https://www.youtube.com/watch?v=rr7HVs4d1Qo
[2] https://www.youtube.com/watch?v=5pJyKzON8Ww
[3] https://www.youtube.com/watch?v=RO1ZYW9NAzg
References books
[1] Object-Oriented Programming in C++, by Robert Lafore
[2] Object-Oriented Programming Using C++, by Joyce Farrell
[3] Object-Oriented Programming Using C++, by Pohl
24
THANK YOU