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

Basic Principles of OOP

The document provides an introduction to Object-Oriented Programming (OOP) using C++, outlining its definition, principles, and advantages over procedural programming. Key concepts such as encapsulation, abstraction, inheritance, and polymorphism are explained, along with practical activities to reinforce understanding. The document emphasizes the benefits of OOP in terms of code structure, reusability, and maintainability.

Uploaded by

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

Basic Principles of OOP

The document provides an introduction to Object-Oriented Programming (OOP) using C++, outlining its definition, principles, and advantages over procedural programming. Key concepts such as encapsulation, abstraction, inheritance, and polymorphism are explained, along with practical activities to reinforce understanding. The document emphasizes the benefits of OOP in terms of code structure, reusability, and maintainability.

Uploaded by

christian.daga10
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Introduction to

Object Oriented
Programming

CPEOOP12Z
Content

Definition and Scope

Basic Principles of C++ Object-Oriented


Programming

Class Diagrams

Class Relationships
What is Object
Oriented
Programming?
• Object-oriented
programming (OOP) is a
computer programming
model that organizes
software design around
data, or objects, rather than
functions and logic. An
object can be defined as a
data field that has unique
attributes and behavior.
Object-oriented
programming has several
advantages over
procedural programming:

• OOP is faster and easier to execute


• OOP provides a clear structure for
the programs
• OOP helps to keep the C++ code
DRY "Don't Repeat Yourself", and
makes the code easier to maintain,
modify and debug
• OOP makes it possible to create full
reusable applications with less
code and shorter development time
Object Oriented
Programming
Basic Principles of C++
Object-Oriented
Programming
• Encapsulation
In C++, object-oriented
programming allows us to bundle
together data members (such as
variables, arrays, etc.) and its
related functions into a single
entity. This programming feature
is known as encapsulation.
Features of Encapsulation
1.We can not access any function from the class directly.
We need an object to access that function that is using
the member variables of that class.
2.The function which we are making inside the class
must use only member variables, only then it is
called encapsulation.
3.If we don’t make a function inside the class which is
using the member variable of the class, then we don’t
call it encapsulation.
4.Encapsulation improves readability, maintainability,
and security by grouping data and methods together.
5.It helps to control the modification of our data
members.
Activity #1: Understanding Encapsulation in C++ with Getters and
Setters

• Create a Class – Define a Person class with a private name


variable.
• Add Setter – Create setName(string n) to assign a value to
name.
• Add Getter – Create getName() to return name.
• Use in main() – Create a Person object, set a name, and print it
using getName().
• Run and Test – Verify the output displays the assigned name.
Abstraction

• In object-oriented
programming, abstraction
refers to the concept of
showing only the
necessary information to
the user i.e. hiding the
complex details of
program implementation
and execution.
Advantages of Data Abstraction

• Helps the user to avoid writing the low-level code


• Avoids code duplication and increases reusability.
• Can change the internal implementation of the class
independently without affecting the user.
• Helps to increase the security of an application or program as
only important details are provided to the user.
• It reduces the complexity as well as the redundancy of the
code, therefore increasing the readability.
• New features or changes can be added to the system with
minimal impact on existing code.
Inheritance

• Inheritance in C++ allows us to


create a new class (derived
class) from an existing class
(base class). The derived class
inherits features from the base
class and can have additional
features of its own.
LABORATORY ACTIVITY #2:

1.Create a base class Shape with a function


area().
2.Create a child class Rectangle that
inherits from the shape.
3.In main(), create an object and call the
area() function.
Polymorphism

• Polymorphism is the ability


to use a common function
(or operator) in multiple
ways. In C++, polymorphism
is implemented with the
help of function overloading,
operator overloading,
function overriding, and
virtual functions.
LABORATORY ACTIVITY #3:

1.Define a base class Computer with a


virtual function specs()
2.Create a derived classes Laptop &
Desktop that override specs().
3.Use base class pointers to call
overridden functions dynamically.

You might also like