Unit-3 (BT-205)
Unit-3 (BT-205)
Unit-3 (BT-205)
1. Class:
oA class is a blueprint for creating objects. It defines properties (attributes) and
methods (functions) that the objects created from the class will have.
o Think of a class as a template or a set of instructions.
2. Object:
o An object is an instance of a class. It is a concrete entity that has the attributes
and methods defined by its class.
o Objects are used to encapsulate data and functions that operate on that data.
Summary
The Dog class serves as a blueprint for creating dog objects with specific
characteristics and behaviors.
Each dog object created from the Dog class has its own unique values for the attributes
and can perform the methods defined in the class.
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
Constructors
Constructors are special member functions in a class that are automatically called when an
object of that class is created. They are primarily used to initialize the object’s attributes. Here’s
an overview of constructors, including their syntax, importance, and types.
1. Syntax of Constructors
2. Importance of Constructors
Initialization: Constructors allow for the initialization of objects at the time of their
creation, ensuring that attributes are set to valid values.
Resource Management: Constructors can allocate resources needed by the object
(e.g., dynamic memory).
Encapsulation: They help in encapsulating the initialization logic within the class
itself, making the code cleaner and more organized.
Overloading: Constructors can be overloaded to provide multiple ways to initialize
objects, enhancing flexibility.
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
Types of Constructors
Constructor Types
1. Default Constructor
Definition: A constructor that does not take any parameters and initializes members with
default values. Real-Life Example: Think of a default constructor as a generic template for a
new car. If you buy a car without specifying any features, it comes with basic attributes (like
color and model).
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
2. Parameterized Constructor
Definition: A constructor that takes parameters to initialize the object’s attributes with specific
values. Real-Life Example: This is like ordering a custom pizza where you specify the size,
toppings, and crust type. The constructor allows you to create a pizza object with specific
attributes based on your order.
3. Copy Constructor
Definition: A constructor that creates a new object as a copy of an existing object. It takes a reference
to an object of the same class as a parameter. Real-Life Example: Imagine you have a blueprint for a
house. If someone wants to build a new house with the same design as yours, they use the same
blueprint. The copy constructor allows you to create a new object based on an existing one.
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
Friend Functions
A friend function in C++ is a special type of function that is not a member of a class but has
access to the private and protected members of that class. This feature allows certain
functions to interact closely with class data while remaining outside the class’s scope. Here’s
a detailed explanation of friend functions
A friend function allows a non-member function to access the private and protected
members of a class.
It is declared using the friend keyword inside the class.
Friend functions can be useful for operator overloading, inter-class communication, and
enhancing functionality while maintaining the logical structure of classes.
1. Non-Member Function: A friend function is declared outside the class but is allowed
to access the class's private and protected members.
2. Declaration: To make a function a friend of a class, you declare it using the friend
keyword inside the class definition.
3. Access: Since friend functions can access private and protected members, they can
manipulate the class’s data directly, which can be useful in certain scenarios.
4. Not Inherited: Friend functions are not inherited by derived classes. If a function is a
friend of a base class, it doesn’t automatically become a friend of derived classes.
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
Explanation of the Example
1. Class Declaration:
o The class Box has a private member length and a constructor to initialize it.
2. Friend Function:
o The displayLength function is declared as a friend inside the Box class. This
allows it to access the private member length.
3. Function Definition:
o The displayLength function is defined outside the class. It takes a Box object as a
parameter and accesses its private member to print the length.
4. Usage:
o In the main function, an object of Box is created, and the friend function
displayLength is called to print the length.
Operator Overloading: Friend functions are often used for operator overloading,
allowing operators to work with class objects seamlessly.
Inter-Class Communication: When two or more classes need to interact closely and
access each other's private members, declaring friend functions can be a good solution.
Encapsulation Control: While friend functions can break encapsulation by accessing
private members, they can also be designed to enhance functionality while maintaining
logical separation.
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
Inheritance
Inheritance is a fundamental concept in object-oriented programming (OOP) that allows one class (the
derived class) to inherit attributes and methods from another class (the base class). This mechanism
promotes code reusability and establishes a natural hierarchy between classes.
Inheritance is a powerful feature in C++ that enables one class to inherit properties and methods
from another, promoting code reusability and logical hierarchy.
It can be implemented in various forms: single, multiple, multilevel, hierarchical, and hybrid.
Access specifiers determine how base class members can be accessed in derived classes, influencing
encapsulation and design choices.
EXMPLE: an example of inheritance using a Vehicle class as the base class, and then derive several
specialized classes such as Car, Bike, and Truck from it. This example will demonstrate how inheritance
can be used to model different types of vehicles with shared attributes and behaviours.
1. Base Class (Parent Class): The class whose properties and methods are inherited.
2. Derived Class (Child Class): The class that inherits from the base class and can have additional
properties or methods.
3. Access Specifiers: The way members of the base class are accessed in the derived class,
determined by public, protected, or private inheritance.
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
Explanation of the Example
Summary
Base Class: Vehicle with common attributes (brand, wheels) and a method
displayInfo().
Derived Classes:
o Car: Inherits Vehicle, adds doors and a method displayCarInfo().
o Bike: Inherits Vehicle, adds hasCarrier and a method displayBikeInfo().
o Truck: Inherits Vehicle, adds loadCapacity and a method
displayTruckInfo().
Types of Inheritance
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
1. Single Inheritance
Definition: Single inheritance occurs when a derived class inherits from only one base class. This
type of inheritance is straightforward and helps create a clear hierarchy.
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
Explanation:
In this example, the Car class inherits from the Vehicle class. The Car class can use the honk()
method defined in the Vehicle class, showcasing how specific types of vehicles share common
characteristics.
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
2. Multiple Inheritance
Definition: Multiple inheritance occurs when a derived class inherits from more than one base class.
This allows the derived class to inherit features from multiple sources.
Example:
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
3. Multilevel Inheritance
Definition: Multilevel inheritance occurs when a class is derived from another derived class.
This creates a multi-tiered hierarchy.
Example:
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
Explanation:
Here, ElectricCar is derived from Car, which is itself derived from Vehicle. This hierarchical
structure shows how properties and methods can be inherited through multiple levels, demonstrating
specialization at each tier.
4. Hierarchical Inheritance
Definition: Hierarchical inheritance occurs when multiple derived classes inherit from a single base
class. This allows different derived classes to share common functionality.
Example: Real-life scenario: Both Car and Bike are types of Vehicle
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
Explanation:
In this example, both Car and Bike classes inherit from the Vehicle class. They can both use the
honk() method from the base class while having their unique methods, demonstrating how different
classes can share functionality.
5. Hybrid Inheritance
Definition: Hybrid inheritance is a combination of two or more types of inheritance. This can
involve multiple inheritance, multilevel inheritance, or any combination of the above.
Example:
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
Polymorphism
The word “polymorphism” means having many forms. In simple words, we can define polymorphism
as the ability of a message to be displayed in more than one form. A real-life example of polymorphism
is a person who at the same time can have different characteristics. A man at the same time is a father,
a husband, and an employee. So the same person exhibits different behavior in different situations. This
is called polymorphism. Polymorphism is considered one of the important features of Object-Oriented
Programming.
Types of Polymorphism
Compile-time Polymorphism
Runtime Polymorphism
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
1. Compile-Time Polymorphism
This type of polymorphism is achieved by function overloading or operator overloading.
A. Function Overloading
When there are multiple functions with the same name but different parameters, then the functions are
said to be overloaded, hence this is known as Function Overloading. Functions can be overloaded by
changing the number of arguments or/and changing the type of arguments. In simple terms, it is a feature
of object-oriented programming providing many functions that have the same name but distinct
parameters when numerous tasks are listed under one function name. There are certain Rules of
Function Overloading that should be followed while overloading a function.
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
B. Operator Overloading
C++ has the ability to provide the operators with a special meaning for a data type, this ability is
known as operator overloading. For example, we can make use of the addition operator (+) for string
class to concatenate two strings. We know that the task of this operator is to add two operands. So a
single operator ‘+’, when placed between integer operands, adds them and when placed between
string operands, concatenates them.
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
2. Runtime Polymorphism
This type of polymorphism is achieved by Function Overriding. Late binding and dynamic
polymorphism are other names for runtime polymorphism. The function call is resolved at runtime in
runtime polymorphism. In contrast, with compile time polymorphism, the compiler determines which
function call to bind to the object after deducing it at runtime.
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
B. Virtual Function
A virtual function is a member function that is declared in the base class using the keyword virtual
and is re-defined (Overridden) in the derived class.
Some Key Points About Virtual Functions
-Virtual functions are Dynamic in nature.
-They are defined by inserting the keyword “virtual” inside a base class and are always declared with
a base class and overridden in a child class
-A virtual function is called during Runtime
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
Function Overloading
Definition: Function overloading is a feature in C++ (and other programming languages) that allows
multiple functions to have the same name with different parameters (different type or number of
parameters). It enables you to perform similar operations on different types or numbers of inputs,
enhancing code readability and usability.
Example: Library Management System
Imagine a library management system where you can search for books based on different criteria. You
can have a function called searchBook that can perform searches based on:
Book Title: Searching by title.
Author Name: Searching by the author's name.
ISBN Number: Searching by the unique ISBN identifier.
In this scenario, the searchBook function is overloaded to accept different parameters for each search
criteria.
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
Explanation of the Code
1. Class Definition: We have a class Library that contains three overloaded methods named
searchBook.
2. Different Parameters:
o The first searchBook function takes a std::string as an argument to search for a book
by its title.
o The second searchBook function takes another std::string to search by the author's
name.
o The third searchBook function takes an int to search for a book using its ISBN
number.
3. Function Calls: In the main function, we create an instance of the Library class and call the
searchBook method with different types of arguments. The appropriate function is called
based on the argument type.
Data Structure
Definition: A data structure is a specialized format for organizing, processing, storing, and retrieving
data. It defines a way to store and organize data in a computer so that it can be used efficiently. Data
structures are essential in computer science because they provide a means to manage large amounts of
data efficiently and enable the implementation of algorithms.
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
Data structures can be classified based on their characteristics and the operations that can be performed
on them. Choosing the appropriate data structure is crucial for optimizing performance and resource
utilization in software applications.
Data Structure: A way to organize and manage data efficiently.
Types of Data Structures:
o Primitive Data Structures: Basic types like integers, floats, characters, and booleans.
o Non-Primitive Data Structures:
Linear: Arrays, Linked Lists, Stacks, and Queues.
Non-Linear: Trees and Graphs.
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
Non-primitive data structures are more complex data structures that are built using primitive data types.
They can be categorized into two main types: Linear and Non-Linear data structures.
Linear Data Structures:
Array: A collection of elements identified by index or key. Arrays store data in contiguous
memory locations.
o Example: int arr[5] = {1, 2, 3, 4, 5};
Linked List: A sequential collection of elements, where each element points to the next.
Elements are not stored in contiguous memory.
o Example: A linked list can consist of nodes where each node contains data and a
pointer to the next node.
Stack: A collection of elements that follows the Last In First Out (LIFO) principle. Elements
are added and removed from the top.
o Example: Used in function calls (call stack).
Queue: A collection of elements that follows the First In First Out (FIFO) principle. Elements
are added at the back and removed from the front.
o Example: Used in print job scheduling.
Non-Linear Data Structures:
Tree: A hierarchical structure consisting of nodes, where each node has a value and pointers to
its child nodes. Trees have a root node and can represent hierarchical data.
o Example: A file system or organization structure.
Graph: A collection of nodes (vertices) connected by edges. Graphs can represent complex
relationships between entities.
o Example: Social networks or routing algorithms.
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
Comparison of stack and queue presented in a tabular format:
Classes:
Think of a class as a blueprint for a house. Just as a blueprint defines the structure, layout,
and features of a house, a class defines the properties and behaviors (methods) of an object.
Objects:
An object is like a specific house built from that blueprint. Each house (object) has its unique
characteristics (data) but follows the same blueprint (class). For instance, if you have a class
Car, different objects might represent specific cars like a red Toyota and a blue Ford, each
with its own properties (color, make, model).
Definition:
The scope resolution operator is like the address of a specific building in a neighborhood. In
programming, when you have a common name (like a variable) that might exist in different
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
contexts (like local and global scopes), the scope resolution operator helps clarify which one
you're referring to.
Real-Life Example:
Imagine you have two people named "John" in your community: John Smith (your neighbor)
and John Doe (a family member). If someone says "John," you need to specify which John
you are talking about, similar to how the scope resolution operator specifies which variable
to use in a given context.
Constructors:
Constructors can be likened to the process of moving into a new home. When you move in,
you arrange your furniture, decorate, and set up utilities. This initial setup is analogous to
what a constructor does when it initializes an object.
Destructors:
Destructors, on the other hand, are like the process of moving out. When you leave a house,
you clean it, remove your belongings, and ensure everything is in order before you go. This
cleanup process is similar to what a destructor does when it cleans up resources before an
object is destroyed.
4. Friend Functions
Definition:
A friend function is like a close friend who has the keys to your house. Although they do not
live there (are not part of your family), they have special access to all areas, including private
spaces (private members of a class).
Real-Life Example:
Imagine you have a safe in your house that holds valuable items (private members). Only
your family members can access it directly, but your best friend also knows the combination
and can access the safe if needed. In programming, a friend function can access the private
data of a class, similar to how your friend can access your safe.
5. Inheritance
Definition:
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
Inheritance is like a family tree. Just as children inherit traits from their parents, classes can
inherit properties and methods from other classes.
Real-Life Example:
Consider a Vehicle class with properties like wheels and engines. A Car class can inherit
from the Vehicle class, meaning it has all the attributes of a vehicle, but can also have
additional features like a trunk or air conditioning. The Car class is a specialized version of
the Vehicle class.
6. Polymorphism
Definition:
Polymorphism allows methods to do different things based on the object it is acting upon,
even if they share the same name.
Real-Life Example:
Think of the term "perform." A musician might perform music, an actor might perform a
play, and a dancer might perform a dance. All these performances are different, but they are
called "perform." Similarly, in programming, a method might behave differently depending
on the object that calls it.
Definition:
Overloading allows you to use the same function name or operator for different types or
numbers of inputs.
Real-Life Example:
Consider the word "print." You can print a document, print an image, or print a photo. In
each case, the action is similar, but the context and method differ. Similarly, you might have
a function that calculates the area of different shapes (circle, rectangle, triangle) but with
the same name.
8. Types of Inheritance
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in
Multilevel Inheritance: A class inherits from a derived class, forming a hierarchy.
o Example: A Mammal class inherits from an Animal class, and a Dog class inherits
from Mammal.
Hierarchical Inheritance: Multiple child classes inherit from the same parent class.
o Example: Dog and Cat classes both inherit from the Animal class.
9. Virtual Functions
Definition:
Virtual functions allow derived classes to override methods from the base class, enabling
polymorphism.
Real-Life Example:
Consider a Shape class with a method called draw. You can have Circle and Square
classes that override the draw method to implement their specific drawing logic. When you
call draw on a Shape pointer that points to a Circle object, the Circle’s version of draw
is executed, not the Shape’s version.
Definition:
A data structure is a specialized format for organizing and storing data in a computer,
enabling efficient access and modification.
Real-Life Example:
Think of a library as a data structure. The books (data) are organized on shelves (data
structure) by categories (like fiction, non-fiction, etc.), making it easy to find and retrieve
specific books. Similarly, data structures like arrays, linked lists, stacks, and queues organize
data in ways that facilitate specific operations.
ITM Campus, NH-75, Opposite Sithouli Railway Station, Gwalior (M.P.)- 475001, India
Email: registrar@itmgoi.in, web: www.itmgoi.in