0% found this document useful (0 votes)
19 views

Unit - II Object Oriented Programming

Uploaded by

Putta Swamy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Unit - II Object Oriented Programming

Uploaded by

Putta Swamy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 87

Unit – II

The Fundamentals of Object-Oriented


Programming and More extensions to C
in C++ to provide OOP Facilities

PUTTASWAMY B S
ASSISTANT
PROFESSOR
Agenda of this Chapters
The Fundamentals of Object-Oriented Programming:

Necessity for OOP,

Data Hiding,

 Data Abstraction,

Encapsulation,

 Procedural Abstraction,

Class and Object.


Agenda of this Chapters
(Cont…)
More extensions to C in C++ to provide OOP Facilities:

Scope of Class and Scope Resolution Operator

Member Function of a Class

private, protected and public Access Specifier

 this Keyword

Constructors and Destructors

friend functions and class

Static Members
Procedural Oriented
Programming (POP)
 Procedural Oriented Programming (POP) also called as
Procedural programming.

 A programming paradigm (model) that emphasizes a linear,


top-down approach to problem-solving.

 A series (set) of procedures or functions to accomplish tasks.

 Each procedure is a self-contained unit with a clear purpose


that can be called independently.
Key Features of Procedural
Programming
Languages Used in Procedural
Programming
 The languages used in Procedural Programming
are FORTRAN, ALGOL, COBOL, BASIC, Pascal,
and C.

These languages use a list of instructions to


tell the computer what to do step-by-step.
Advantages of Procedural
Programming
It is good (excellent) for general-purpose
programming.
We can access the same code at different points
in the software program without repeating it.
The memory need is also reduced by using the
Procedural Programming technique.
Disadvantages of Procedural
Programming
 The program code is harder to write when Procedural Programming is employed
 The Procedural code is often not reusable, which may pose the need to recreate
the code if is needed to use in another application
 Difficult to relate with real-world objects
 The importance is given to the operation rather than the data, which might
pose issues in some data-sensitive cases
 The data is exposed to the whole program, making it not so much security
friendly
Object-Oriented Programming
(OOP)
 Type of Programming paradigm
Code is organized not around actions, and data rather than
logic, but around objects.
 This makes your code easier to understand, reuse, and
maintain.
 Class and Objects
 Inheritance and Polymorphism
Abstraction and Encapsulation
Object
 An entity with some characteristics and behaviour or anything
that exists physically in the world.

An object is an instance of the class. In Object-Oriented


Programming, when a class is defined, no memory is allocated,
but when an object is created, the memory is allocated.
 You can call a member function with the help object and use the dot operator.
Class
Class is a collection of objects.

It is like a blueprint for an object.

In the C++ programming language.

 The foundational element of object-oriented programming.

An instance of a class is created for accessing its data types and
member functions.
 ★ public The public members are accessible from outside the class
through an object of the class, typically with the use of the dot operator.

 ★ protected The protected members can be accessed from outside the


class but only in a class derived from it.

 ★ private The private members are only accessible from within the class
or in it's member functions.
Inheritance
 When one object acquires all the properties and behaviors of
its parent object, then it is known as Inheritance.
It helps to reduce the code size.
Polymorphism
 Ability to take more than one form.

With this feature, you can use the same function to perform
different tasks thus increasing code reusability.
Abstraction
 The process of data hiding.

 It assists the program in showing the essential features


without showing the functionality or the details of the program
to its users.

 It generally avoids unwanted information or irrelevant details


but shows the important part of the program.
Encapsulation
 Encapsulation helps to wrap up the functions and data
together in a single unit.
By privatizing the scope of the data members it can be
achieved.
This particular feature makes the program inaccessible to the
outside world.
Advantages of Object-Oriented
Programming (OOP)
It helps in data hiding, keeping the data and information safe
from leaking or exposure.

Because OOPs provide reusability to the code, we can use a


class many times.

In OOPs, it is easy to maintain code as there are classes and


objects, which help in making it easy to maintain rather than
restructure.
Disadvantages of Object-
Oriented Programming (OOP)
 As OOP programs are large, they need more time to run,
resulting in slower execution.

 OOP is not a universal language, so we can only use it in some


places. It is only used when necessary. It is not appropriate for
all sorts of issues.

 Everything is represented as an object in OOP, so before


applying it, we need to think about objects well.
Scope of Class and Scope
Resolution Operator
 The accessibility or visibility of class variables or functions.

 The term scope is defined as a place where in variable name,


function name and typedef are used inside a program.
Local scope Function scope File scope

Class scope Prototype scope.


Local Scope

 The variables that are declared in a function block are called local variable
which have local scope.

The variables defined inside the function will have function scope. These

variables can only be accessed inside the function.

The data members and member functions defined in a class will have class

scope.

They are local only to that class. Each member possesses unique scope.
 The identifiers which are present outside the definition of function or class will have file scope.

This scope consist of local scope as well as class scope and it will be a in the outermost part of program.

 The variables defined with file scope are known as global variables.

The variables that are declared inside a function prototype have prototype scope, They are local only up to

that prototype.
Scope Resolution
Operator
 One of the essential operator (symbol) represented by two colons
(::)
 To identify and access(disambiguate) the entities (identifiers) that
are defined outside the current scope of a program.
 This operator is also known as the global scope operator.
scope resolution operator (::) is
uses in several Reasons
 If the global variable name is same as local variable name, the
scope resolution operator will be used to call the global variable.

 It is also used to define a function outside the class

 It is used to access the static variables of class.


Member Function of a
Class
 Also called as method is a function
 It can be declared inside the class definition and can be defined either inside or outside
the class definition.
 Declared within the class as public, private, or protected functions.
Generally, member functions are declared as public because they
have to be called outside of the class.
 Member Functions may be used to read, manipulate, or display all
types of data members (private, public, and protected).
How to define Member Functions
in C++?
1. Definition within the Class
2. Definition outside the Class
this keyword
A keyword or implicit pointer variable non-static member functions of a class

 Points (representing) to the current instance of the class which calls the member

function.

An implicit pointer that holds the memory address of the class instance that is being

called by a member function, allowing those functions to correctly access the object’s

data members.

Distinguish data members of the class and member function when having the same

name.
Syntax
 The 'this' keyword is used in conjunction with the arrow operator (->)
along with the name of the member variable or method being referred
to.
 this->var_name;

 this.var_name;

void functionName()

{
this->memberName = value;
}
#include<iostream.h>

#include<conio.h>

class rectangle

private:

int l,b;

public:
Constructors
&
Destructors
What is Constructor?
 A specially defined method or member function, that is
automatically executed when an instance (object) of class is
created.

 The main purpose of a constructor is to construct an object


and assign values to the object’s members (It is used to
initialize the data members of new objects generally).
Characteristics of
Constructor

 The name of the constructor is the same as its class name to


which it belongs.

 Constructors are usually declared in the public section (scope).


However, constructor can be declared in private section, as well.

 Constructors do not return values; hence they do not have a


return type, not even void.
Characteristics of Constructor(Cont.…)

 Constructors are invoked (gets called) automatically when we


create the object of the class and can take arguments to
initialize the object’s data members.

 It can have any number of parameters and a class may have


any number of overloaded constructors.

 Constructors may have any accessibility, public, private or


protected.
Constructors can be defined either inside

or outside class definition.


General Syntax

<class-name> ()

...

}
Types of constructors
Default constructor
 A unique constructor that is created and called by the compiler
automatically without any parameters, when no other user-
defined constructor is explicitly declared within a class.

 It is called for the default initialization of class internal


members.
Parameterized Constructor

 A particular kind of constructor that accepts one or


more parameters as arguments when creating an object

 In other words, parameterized constructor is a constructor


that accepts parameters to initialize an object with specific
values at the time of its creation.
Copy Constructor

 A special type of constructor which creates an object


by initializing it with an object of the same class, which
has been created previously.
In other words, a
copy constructor is a special type of constructor that
makes a copy of an existing object as an argument
while creating a new object.
 This type of constructor is used to copy values of
data members of one object into another object.
Destructor in C++
Understanding Object Cleanup and
Memory Management in C++
What is a Destructor?
•A destructor is a special member function of a class in C++.
•It is automatically called when an object goes out of scope or is
explicitly deleted.
•Purpose: To release resources allocated during the object's
lifetime (e.g., memory, file handles, etc.).
Key Characteristics of
Destructors
•Automatic invocation: Destructor is automatically called
when the object is destroyed.
•No arguments: Destructors cannot take parameters.
•No return type: Destructors do not return a value.
•No overloading: A class can have only one destructor.
class MyClass

public:

// Constructor

MyClass() { }

// Destructor

~MyClass() { }

};
When is a Destructor Called?
•Automatic destruction: When an object goes out of
scope (e.g., a local object).
•Explicit deletion: When delete is called on an object
created with new.
#include <iostream>

using namespace std;

class MyClass {

public:

MyClass() { cout << "Constructor called." << endl; }

~MyClass() { cout << "Destructor called." << endl; }

};

int main() {

MyClass obj; // Destructor is called when obj goes out of scope

return 0;

}
Friend Function
and Class
Friend Function
 A non-member function can be either a member
function of another class or a global function
(defined outside the scope of the class).

 Privileges to access all private and protected


members (variables and functions) of the class even
though it is not a member function of the class.
Why do we need a friend
function?
 Generally, the private data members of a class can
accessible within that class only and protected members
can only be accessed by derived classes.

 A class cannot access the private members of another


class. Similarly, a class that doesn’t inherit another class
cannot access its protected members.
Why do we need a friend
function?
class MyClass int main()

{ {
MyClass obj;
private:
obj.member1 = 5;
int member1;
}; }
Declare and define a friend
function
 Friend functions are declared inside the class scope with the keyword 'friend.’

class class_Name

// declartion of class properties

friend data_type/return type function_name(argument/s);

};
Define a friend function
The function definition does not use either the keyword friend or scope
resolution operator.

return_type friend_function_name(arguments / objects)

// Friend function has privileges to access all private and


protected members of the class

}
Types of Friend Function
1. Global function as friend function
2. Member function of another class
 A standalone function outside any class can access private
members of a class by using Global Function as a Friend
Function.
 This kind is useful for operations that involve various classes,
allowing for data manipulation without restrictions from classes.
Member Function of Another
Class as a Friend Function
 Allowing a member function from a different class to access
private members is achieved by declaring it as a friend function.
 This category promotes intimate connections among classes,
improving adaptability and teamwork.
It supports encapsulation while enabling targeted access,
simplifying communication and ensuring seamless operation
among interconnected classes in C++.
Friend Class
 A friend class can access private and protected (also public)
members of other classes in which it is declared as a friend.
It is sometimes useful to allow a particular class to access
private and protected members of other classes.
Static Data Members
Understanding Static Data Members and Static Member
Functions
Agenda
 Introduction to Static Members

 Static Data Members

 Static Member Functions

 Use Cases of Static Members

 Code Example
Introduction to Static Members

• Definition: Static members are shared across all instances of a class.


1. Static Data Members
2. Static Member Functions
Why Use Static Members?
• Shared state or behavior
• Reduce memory usage when data is common for all objects
Static Data Members
• Belongs to the class, not any individual object

• Shared among all objects of the class

• Only one copy exists, regardless of how many objects are created
class MyClass {

public: static int count; // Declaration

};

// Definition outside class

int MyClass::count = 0;
•Must be initialized outside the class
•Can be accessed using the class name:
MyClass::count
•Scope is global for all instances of the class
MyClass obj1;
MyClass obj2;
obj1.count = 5; // Changes count for all objects
std::cout << obj2.count; // Outputs: 5
Static Member Functions
Key Characteristics:
• Can be called without an object instance using the class
name
• Can only access static data members or other static
functions
• Do not have access to this pointer (since no object context)
class MyClass {
public:
static void showCount() {
std::cout << count << std::endl;
}
static int count;
};
•Can be called using the class name: MyClass::showCount();
•Can be useful for utility functions that don't need object-specific
data

MyClass::showCount(); // Outputs static member count


Use Cases of Static Members
•When to use Static Data Members:
• When a value needs to be shared across all objects (e.g.,
object count, configuration settings)

•When to use Static Member Functions:


• When a function does not require object-specific data (e.g.,
factory methods, utility methods)
Any Questions

You might also like