0% found this document useful (0 votes)
25 views32 pages

4.2 Lecture - 15

The document discusses Object-Oriented Programming (OOP) concepts, contrasting it with Procedural Programming (POP). It highlights key OOP principles such as classes, objects, encapsulation, inheritance, and polymorphism, along with access specifiers and constructors. Additionally, it provides examples and outlines the benefits of OOP in terms of data protection, modularity, and code reusability.

Uploaded by

Kabir Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views32 pages

4.2 Lecture - 15

The document discusses Object-Oriented Programming (OOP) concepts, contrasting it with Procedural Programming (POP). It highlights key OOP principles such as classes, objects, encapsulation, inheritance, and polymorphism, along with access specifiers and constructors. Additionally, it provides examples and outlines the benefits of OOP in terms of data protection, modularity, and code reusability.

Uploaded by

Kabir Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

BTCS0104

PROGRAMMING CONCEPTS USING C & C++

UNIT- 4
STRUCTURES & OBJECT ORIENTED
PROGRAMMING

ANKIT JAIN
4.2
OBJECT ORIENTED
PROGRAMMING
PROGRAMMING PARADIGMS

➢Programming paradigm is an approach to solve


problem using some programming language.
➢Or also, we can say it is a method to solve a problem
using tools and techniques that are available to us
following some approach.
➢Two popular program approaches are POP & OOP
PROCEDURAL VS. OBJECT-ORIENTED
PROGRAMMING
• In Procedural programming, program is divided into
multiple small functions.
• Procedural programming focus on how to do the task but
don’t focus on data. [Locker example]
• In Object-Oriented programming program is divided into
multiple classes (objects).
• OOP focus on what to do, including both how & the data.
POP VS. OOP
LIMITATIONS OF POP

• If the data structures change, many functions


must also be changed

• Programs that are based on complex function


hierarchies are:
– difficult to understand and maintain
– difficult to modify and extend (interdependency in functions)
– easy to break
Aspect OOP POP
Focus on objects and Focus on functions and
Core Concept
classes. procedures.
Emphasizes encapsulation, Data may be global and
Data
data is accessed through accessible to all functions.
Accessibility
methods. Hence, less data security.
Well-suited for modelling Less intuitive for modelling
Real-World
real-world entities and real-world objects and their
Modelling
relationships. interactions.
Encourages modularity
Modularity can be achieved
through classes and objects,
Modularity through functions, but not
promoting easy maintenance
as naturally as in OOP.
and updates.
Promotes code reusability
Code Functions can be reused,
through inheritance and
Reusability but code is less modular.
polymorphism.
OBJECT ORIENTED PROGRAMMING (OOP)

• Object-Oriented Programming refers to languages


that use objects in programming.
• The main aim of OOP is to bind together the data
and the functions that operate on them so that no
other part of the code can access this data except
that function.
• C++ is object oriented programming (not purely),
whereas C is procedure oriented.
REAL WORLD EXAMPLE

STUDENT MANAGEMNT SYSTEM


1. Admission
2. Fees
3. Attendance
4. Examinations
5. Cultural
6. And many more…
OOP CONCEPTS

• Class
• Objects
• Data Abstraction
• Encapsulation
• Inheritance
• Polymorphism
• Dynamic Binding
• Message Passing
CLASS

A class is a user-defined data type, which holds its own


data members and member functions.

• A C++ class is like a blueprint for an object.

• Members of a class can be accessed and used by


creating an instance (object) of that class.

• Class is a group of similar objects. It is a template from


which objects are created.
• Classes are similar to structs but contain functions, as well.
OBJECTS

• Object is an instance of a class.

• When a class is defined, only the specification for the


object is defined; no memory or storage is allocated.

• Object is a real world entity, for example, chair, car, pen,


mobile, laptop etc.
CLASSES AND OBJECTS

• A Class is like a blueprint and objects are like houses built


from the blueprint
OOP CONCEPTS

• Data member/Attribute - description of objects in a class


Example: manufacturer’s name, model name, year made, color, number of
doors, size of engine, etc.

• Member function/Method - an action performed by an object


(a verb)
Example: Define data items (specify manufacturer’s name, model, year,
etc.), Change a data item (color, engine, etc.), Display data items,
Calculate cost, etc.
• Another example: Student Class
Access
specifier
(Discussed in
next slide)
ACCESS SPECIFIERS

• Access specifiers define how/where the members


(attributes and methods) of a class can be accessed.

• In the example above, the members are public -


which means that they can be accessed and modified
from outside the class.

• However, we can restrict the access of members


from the outside world.
ACCESS SPECIFIER

In C++, there are three access specifiers:


• public - members are accessible from outside the class
• private - members cannot be accessed (or viewed) from
outside the class
• protected - members cannot be accessed from outside
the class, however, they can be accessed in inherited
classes. (You will learn more about Inheritance later).
• If not specified, the default is private
ANOTHER CLASS EXAMPLE

Const (optional)
indicates that
this function will
not change any
data member
during call.
Private Members

Public Members
PRIVATE MEMBERS

• Making data members private provides data


protection

• Data can be accessed only through public


functions

• Public functions define the class’s public interface


PRIVATE MEMBERS

Code outside the class must use the class's


public member functions to interact with the
object.
MEMBER FUNCTION

• A Member function is a function that is declared as a


member of a class.
• A member function can access all the data members
of the class.
• If the member function is defined inside the class
definition, it can be defined directly inside the class;
• Otherwise, we need to use the scope resolution
operator (::) to define the member function outside
the class.
DEFINING MEMBER FUNCTIONS

• Either define inside the class itself (as done in student


class example)
OR
• Define outside the class using scope resolution operator
(::), as given below:
– First, put prototype in class declaration (as done for
rectangle class), then
CREATE OBJECTS

• We have already created the class named Rectangle, so


now we can use this to create objects.
• To create an object of , specify the class name, followed
by the object name.
Rectangle r;
• To access the class members, use the dot syntax (.) on
the object.
r.setData(4.5,6.5)
r.width = 5.5;
CONSTRUCTOR

• Constructor is a special method that is invoked


automatically at the time an object of a class is
created.
• It constructs the values i.e. provides data for the
object which is why it is known as a constructor.
OR
• It initializes the data members of new objects
generally.
• The constructor has the same name as the class
or structure. Int has no return type.
TYPES OF CONSTRUCTORS

• Default Constructor: No parameters. They are used


to create an object with default values.
• Parameterized Constructor: Takes parameters.
Used to create an object with specific initial values.
• Copy Constructor: Takes a reference to another
object of the same class. Used to create a copy of an
object.
RULES FOR DEFAULT CONSTRUCTOR

• If there is no constructors defined in the class, then


a default constructor will be invoked but do not
initialize the members (garbage values).
• If there is a parameterized constructor is defined
then you must invoke only parametrized constructor
(default constructor will give error until or unless
not defined).
DESTRUCTORS

• A destructor is also a special member function like a


constructor. Destructor destroys the objects created
by the constructor.
• It is automatically called when an object goes out of
scope.
• Destructor name is ~classname, e.g., ~Rectangle
• Has no return type; takes no arguments
• Only one destructor per class.
• Either you can declare your own destructor, or the
compiler will define one for you implicitly.

You might also like