Unit - II Object Oriented Programming
Unit - II Object Oriented Programming
PUTTASWAMY B S
ASSISTANT
PROFESSOR
Agenda of this Chapters
The Fundamentals of Object-Oriented Programming:
Data Hiding,
Data Abstraction,
Encapsulation,
Procedural Abstraction,
this Keyword
Static Members
Procedural Oriented
Programming (POP)
Procedural Oriented Programming (POP) also called as
Procedural 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.
★ 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.
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
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.
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.
<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.
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>
class MyClass {
public:
};
int main() {
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).
{ {
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
};
Define a friend function
The function definition does not use either the keyword friend or scope
resolution operator.
}
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
Code Example
Introduction to Static Members
• Only one copy exists, regardless of how many objects are created
class MyClass {
};
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