Module 3_OOPs
Module 3_OOPs
Syntax-: and then there is a narrow focus on the wave coming there are
class base_class{
we create the object of last derived class always and a specific class which doesn’t consider
objects is known as abstract class.
1. SIMPLE INHERITANCE-:
B
Mirror In simple inheritance there will be only one super class and one subclass.
2. Multilevel inheritance-:
In multi level inheritance one super class can have only one direction class and many indirect
classes.
One sub class can have only one direction super class and many indirect super class
3. Hierarchical inheritance:-
B C D
In this one super class can have many direct super class but one super class can
have only one sub class.
4. Multiple inheritances:-
A B C
D
In this one sub class can have many direct super class but one super class can
have only one sub class.
5. Hybrid inheritance:-
B C D
B C D
E F
It is the combination of more than one type of inheritance i.e hybrid variety of
inheritance.
Abstract Class
A class that contains a pure virtual function is known as an abstract class. In the
above example, the class.
Ambiguity resolution using scope resolution operator and
Virtual base class
Ambiguity Resolution:
When a derived class inherits from multiple base classes and those base
classes have members with the same name, ambiguity arises.
To specify which base class's member you want to access, use the scope
resolution operator.
#include <iostream>
class Base1 {
public:
void print() {
};
class Base2 {
public:
void print() {
};
public:
void print() {
};
int main() {
Derived d;
d.print();
return 0;
In C++ inheritance, ambiguity resolution using the scope resolution operator (::)
and virtual base classes are both techniques used to address situations where a
derived class inherits from multiple base classes that have members with the
same name, allowing you to explicitly specify which base class member you
want to access when needed, while virtual base classes ensure that only a single
instance of a common base class exists in a multiple inheritance hierarchy,
effectively preventing ambiguity in the "diamond problem" scenario.
Explanation:
public:
void callPrint() {
}
};
Virtual Base Class:
Function: By declaring a base class as "virtual" in the inheritance hierarchy, you
ensure that only one instance of that class exists in the derived class, even when
multiple inheritance paths lead to the same base class.
Example:
public:
void callPrint() {
};
Key Points:
Method Overriding:-
the process of implementing super class method in the sub class method
called method overriding.
Since function calling is mode under the function body thus it is named function or method
overriding.
Calling of function is done with the help of class name and scope
resolution operator.
Class name:: function name();
Sub class can be written for three reasons.
1) To add new functionality.
2) To overwrite the existing functionality.
3) To inherit the existing functionality.
When you overriding super class method in sub class you need to follow the
following rules.
1) Sub class method is same as super class method name.
2) Sub class method return type must be same as super method parame
3) Sub class method parameter is same as super class method return type-:
Ex-:
#include<iostream.h>
#include<conio.h>
class student{
protected:
char sname[20],course[20];
int roll;
public:
void getdata(){
cout<<"Enter name”:-
cin>>sname;
cout<<"Enter course-:”
cin>>course ;
cout << “ enter rollno:-";
cin>>roll;
}
Void show(){
cout<<endl<<"student name:- "<<sname;
cout<<endl<<"Roll no:- "<<roll;
cout<<endl<<"course:- "<<course;
};
class Hostal:public student{
int hostalno, fee;
public:
void getdata(){
student::getdata();
cout<<"Enter Hostal number:- ";
cin>>hostalno;
cout<<"Enter course fee:- ";
cin>>fee;
}
void show(){
student::show();
cout<<endl<<"hostal number:- "<<hostalno;
cout<<endl<<"course fee:- "<<fee;
}
};
int main(){
Hostal h;
h.getdata();
h.show();
getch();
return 0;
}
Since we know that constructor is a group of member functions mainly used for
initial id purpose for the data members
In the case of communities there is a criteria behind the use of the structure That in
base class you constructed then it become necessary that each derived class also
used otherwise constructor is not used
Here for use of constructor in case of inheritance is totally depend upon the base
class
Points
When you not writing any corrupted inside the class then default concept will be
inserted by the compiler
When you have any arguments constructor inside the class then default constructor
will not be inserted for the compiler
When you create sub class of this object then sub class constructor and super class
constructor will be called and initialising sub class variable to initialise super class
variable
When you create super class object then sub class constructor will be called an super
class variable and not initialization subclass variable.
Ex-:
#include<iostream.h>
#include<conio.h>
Class demo1{
Protected:
Int n1,n2;
public:
demo1(int a, int b){
n1=a;
n2=b;
}
void show(){
cout<<endl<<"value of n1:- "<<n1;
cout<<endl<<"value of n2:- "<<n2;
{
};
class demo2:public demo1{ int n3,n4;
public:
demo2(int a, int b,int c,int d):demo1(a,b){ n3=c; n4=d; }
void show(){
demo1::show();
cout<<endl<<"value of n3:- "<<n3;
cout<<endl<<"value of n4:- "<<n4;
}
};
int main(){
demo2 d(10,20,30,40);
demo1 d2(50,60);
d2.show();
d.show();
getch();
return 0;
}
Nested class-:
Nested classes in C++ provide a way to encapsulate a class within another class, creating a
hierarchical relationship between them. Here's a breakdown of nested classes and their
inheritance in C++:
Nested Classes:
A nested class is a class defined within the scope of another class, known as the
enclosing class.
The nested class has access to all members (private, protected, and public) of the
enclosing class, but the enclosing class doesn't have direct access to the private
members of the nested class.
Nested classes can be useful for encapsulating functionality that is closely related to
the enclosing class, improving code organization and readability.
class Inner {
public:
void printX() {
std::cout << x << std::endl; // Accesses Outer::x
}
};
};
int main() {
Outer outer;
outer.x = 10;
Outer::Inner inner;
inner.printX(); // Output: 10
return 0;
}
class Outer {
public:
int x;
class Inner {
public:
virtual void print() {
std::cout << "Inner\n";
}
};
};
class Derived : public Outer::Inner {
public:
void print() override {
std::cout << "Derived\n";
}
};
int main() {
Derived derived;
derived.print(); // Output: Derived
return 0;
}
Important Points to Remember:
Access Control:
The nested class has access to the enclosing class's members, but the enclosing class doesn't
have direct access to the nested class's private members.
Scope:
The name of the nested class is within the scope of the enclosing class.
Instantiation:
To use the nested class, you need to create an object of the enclosing class first.
Inheritance:
A nested class can inherit from other classes, and the same inheritance rules apply as with
regular classes.
Benefits of Using Nested Classes and Inheritance:
Encapsulation: Improves code organization by grouping related classes together.
Code Reusability: Facilitates code reusability through inheritance.
Readability: Makes code more readable and maintainable.
ACCESS MODES
Access modifiers are used to implement an important aspect of Object-Oriented Programming known
as Data Hiding. Consider a real-life example:
The Research and Analysis Wing (R&AW), having 10 core members, has come into possession of
sensitive confidential information regarding national security. Now we can correlate these core
members to data members or member functions of a class, which in turn can be correlated to the R&A
Wing. These 10 members can directly access the confidential information from their wing (the class),
but anyone apart from these 10 members can’t access this information directly, i.e., outside functions
other than those prevalent in the class itself can’t access the information (that is not entitled to them)
without having either assigned privileges (such as those possessed by a friend class or an inherited
class, as will be seen in this article ahead) or access to one of these 10 members who is allowed direct
access to the confidential information (similar to how private members of a class can be accessed in
the outside world through public member functions of the class that have direct access to private
members). This is what data hiding is in practice.
Access Modifiers or Access Specifiers in a class are used to assign the accessibility to the class
members, i.e., they set some restrictions on the class members so that they can’t be directly accessed
by the outside functions.
There are 3 types of access modifiers available in C++:
1. Public
2. Private
3. Protected
Note: If we do not specify any access modifiers for the members inside the class, then by default the
access modifier for the members will be Private. Understanding how to use public, private, and
protected access modifiers is essential. The C++ Course provides detailed explanations and examples
to help you implement these concepts effectively.
Let us now look at each one of these access modifiers in detail:
1. Public: All the class members declared under the public specifier will be available to everyone.
The data members and member functions declared as public can be accessed by other classes and
functions too. The public members of a class can be accessed from anywhere in the program using the
direct member access operator (.) with the object of that class.
Example:
CPP
// C++ program to demonstrate public
// access modifier
#include<iostream>
using namespace std;
// class definition
class Circle
{
public:
double radius;
double compute_area()
{
return 3.14*radius*radius;
}
};
// main function
int main()
{
Circle obj;
// accessing public datamember outside class
obj.radius = 5.5;
#include<iostream>
using namespace std;
class Circle
{
// private data member
private:
double radius;
};
// main function
int main()
{
// creating object of the class
Circle obj;
Output:
In function 'int main()':
11:16: error: 'double Circle::radius' is private
double radius;
^
31:9: error: within this context
obj.radius = 1.5;
^
The output of the above program is a compile time error because we are not allowed to access the
private data members of a class directly from outside the class. Yet an access to obj.radius is
attempted, but radius being a private data member, we obtained the above compilation error.
However, we can access the private data members of a class indirectly using the public member
functions of the class.
Example:
CPP
// C++ program to demonstrate private
// access modifier
#include<iostream>
using namespace std;
class Circle
{
// private data member
private:
double radius;
};
// main function
int main()
{
// creating object of the class
Circle obj;
// trying to access private data member
// directly outside the class
obj.compute_area(1.5);
return 0;
}
Output:
Radius is: 1.5
Area is: 7.065
3. Protected: The protected access modifier is similar to the private access modifier in the sense that
it can’t be accessed outside of its class unless with the help of a friend class. The difference is that the
class members declared as Protected can be accessed by any subclass (derived class) of that class as
well.
Note: This access through inheritance can alter the access modifier of the elements of base class in
derived class depending on the mode of Inheritance.
Example:
CPP
// C++ program to demonstrate
// protected access modifier
#include <bits/stdc++.h>
using namespace std;
// base class
class Parent
{
// protected data members
protected:
int id_protected;
};
id_protected = id;
void displayId()
{
cout << "id_protected is: " << id_protected << endl;
}
};
// main function
int main() {
Child obj1;
obj1.setId(81);
obj1.displayId();
return 0;
}
Output:
id_protected is: 81
Private Protected
Private members keep implementation Protected members enhanced access for derived
details in a program. classes.
Only the member functions or the friend The class member declared as Protected are
functions are allowed to access the private inaccessible outside the class but they can be
data members of a class. accessed by any subclass(derived class) of that class.
Function Overriding
Types of Function Overriding in C++
Unlike other languages such as Java where function overriding is strictly done at compile time, C++
supports two types of function overriding:
Compile Time Overriding
Runtime Function Overriding
Compile Time Function Overriding
In compile time function overriding, the function call and the definition is binded at the compilation
of the program. Due to this, it is also called early binding or static binding.
Syntax
class Parent {
access_modifier :
// overridden function
return_type name_of_the_function() {}
};
// overriding function
return_type name_of_the_function() {}
};
Example of Compile Time Function Overriding
C++
// C++ program to demonstrate compile time function overriding
#include <iostream>
using namespace std;
class Parent {
public:
void GeeksforGeeks_Print()
{
cout << "Base Function" << endl;
}
};
int main()
{
Child Child_Derived;
Child_Derived.GeeksforGeeks_Print();
return 0;
}
Output
Derived Function
Runtime Function Overriding using Virtual Function
Unlike other languages like Java, the function overriding in C++ can be performed at both compile
time and runtime. In all the above examples, the call to the overridden function is resolved during
compile time. It is also called early binding where the function call is binded to its definition during
compilation.
Function overriding can also be performed at the runtime, which means that function call will be
binded to its definition during runtime (also known as late binding or dynamic binding). This can be
done with the help of virtual functions.
Syntax
class Base {
public:
virtual func()
{
// definition
}
};
Here, override keyword tells the compiler that the given overridden function should be declared as
virtual in the parent class. It is a kind of double check as the program can compile without errors even
if the function is not virtual. But then, it will be compile time polymorphism and we won’t get the
desired behaviour of the function overriding.
Example of Runtime Function Overriding using Virtual Function
C++
// C++ Program to illustrate how to implement runtime
// function overriding using virtual function
#include <iostream>
using namespace std;
class Base {
public:
// Declare the function as virtual to allow overriding
// in derived classes
virtual void display()
{
cout << "Display method of Base class" << endl;
}
int main()
{
Base* basePtr;
Derived derivedObj;
return 0;
}
Output
Display method of Derived class
Advantages of Runtime Function Overriding
The following are the main advantages of function overriding:
Methods are selected at runtime based on object type.
Common interfaces and implementations can be reused as we only need to implement the
interface for base class.
Changes in base classes automatically affect derived classes.
Facilitates the implementation of design patterns different design patterns.
Client code interacts with base class interface, not specific implementations reducing the
coupling.
Limitations of Runtime Function Overriding
While runtime function overriding provides numerous advantages, it also comes with certain
limitations:
Virtual function calls are generally slower than non-virtual function calls due to the
indirection involved in the virtual table lookup.
Implementing and maintaining polymorphic code can be more complex compared to simpler
non-polymorphic code.
Virtual tables and virtual pointers add to the memory overhead, especially in large systems
with many classes.
Errors related to function overriding (e.g., incorrect signatures) might only be detected at
runtime, leading to potential bugs.
Examples of Function Overriding in C++
Example 1: C++ Program to Call Overridden Function From Derived Class
C++
// C++ program to demonstrate function overriding
// by calling the overridden function
// of a member function from the child class
#include <iostream>
using namespace std;
class Parent {
public:
void GeeksforGeeks_Print()
{
cout << "Base Function" << endl;
}
};
int main()
{
Child Child_Derived;
Child_Derived.GeeksforGeeks_Print();
return 0;
}
Output
Derived Function
Base Function
int main()
{
Child Child_Derived;
return 0;
}
Output
Base Function
Example 3: C++ Program to Access of Overridden Function using Child Class Object
C++
// C++ program to access overridden function
// in main() using the scope resolution operator ::
#include <iostream>
using namespace std;
class Parent {
public:
void GeeksforGeeks()
{
cout << "Base Function" << endl;
}
};
int main()
{
Child Child_Derived;
Child_Derived.GeeksforGeeks();
Output
Derived Function
Base Function
Polymorphism
A function can be overloaded multiple times as A function cannot be overridden multiple times
it is resolved at Compile time as it is resolved at Run time