SlideShare a Scribd company logo
Topic 2 : Object-oriented
concept and programming
4000CEM Object Oriented Programing (OOP)
Topic Outlines
• Introduction to OOP
• Object-oriented concept and programming
• Encapsulation, information hiding, classes, inheritance
Object Oriented Programming
Classes & Objects
• Class defines what data and method should the object have.
• The object contains the actual data
• Creating an object using the class blueprint.
• Each object behave in the same way but has it own data.
• Behaviour define inside the class is shares by all the objects,
but data is not.
• Each object behave in the same way but has it own data.
Create a Class in Python
class Person: //class object is created
pass //don’t have any data/method
p1 = Person() //instance object created
p2 = Person()
class Person:
def display(self):
print (‘I am a Person’)
def greet(self):
print (‘Hello, how are you doing?’)
p1 = Person() //instance object created
p2 = Person()
p1.display() //no parameter is send because the object is send
when the function is called
p1.greet()
class Person:
def set_details(self, name, age):
self.name = name
self.age = age
def display(self):
print (‘I am a Person’)
def greet(self):
print (‘Hello, how are you doing?’)
p1 = Person() //instance object created
p2 = Person()
p1.set_details(‘Bob’, 20)
p2.set_details(‘Ted’, 60)
Classes and Objects
INTRODUCTION TO OOP
• OOP stands for Object-Oriented Programming.
• Procedural programming is about writing procedures or functions that perform
operations on the data, while object-oriented programming is about creating
objects that contain both data and functions.
• 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“ (avoid repeating
the code), 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 is a fundamental concept in Python,
empowering developers to build modular, maintainable, and scalable
applications. By understanding the core OOP principles (classes,
objects, inheritance, encapsulation, polymorphism, and abstraction),
programmers can leverage the full potential of Python OOP capabilities
to design elegant and efficient solutions to complex problems.
• OOPs is a way of organizing code that uses objects and classes to
represent real-world entities and their behavior. In OOPs, object has
attributes thing that has specific data and can perform certain actions
using methods.
OOPs Concepts in Python
• Class in Python
• Objects in Python
• Polymorphism in Python
• Encapsulation in Python
• Inheritance in Python
• Data Abstraction in Python
C++ Classes and Objects?
• Classes and objects are the two main aspects of object-
oriented programming.
CLASSES OBJECT
Vehicles • Car
• Truck
• Bicycle
Animals • Dog
• Cat
• Bird
• A class is a template for objects, and an object is an
instance of a class.
• When the individual objects are created, they inherit all
the variables and functions from the class.
Python Class
• A class is a collection of objects. Classes are blueprints for
creating objects. A class defines a set of attributes and
methods that the created objects (instances) can have.
• Classes are created by keyword class.
• Attributes are the variables that belong to a class.
• Attributes are always public and can be accessed using the
dot (.) operator. Example: Myclass.Myattribute
Creating a Class
• Here, the class keyword indicates that we are creating a class
followed by name of the class (Dog in this case).
Python Objects
• An Object is an instance of a Class. It represents a specific implementation of
the class and holds its own data.
• An object consists of:
• State: It is represented by the attributes and reflects the properties of an
object.
• Behavior: It is represented by the methods of an object and reflects the
response of an object to other objects.
• Identity: It gives a unique name to an object and enables one object to
interact with other objects.
Creating Object
• Creating an object in Python involves instantiating a class to
create a new instance of that class. This process is also
referred to as object instantiation.
Self Parameter
• self parameter is a reference to the current instance of the
class. It allows us to access the attributes and methods of the
object.
class Dog:
species = "Canine" # Class attribute
def __init__(self, name, age):
self.name = name # Instance attribute
self.age = age # Instance attribute
dog1 = Dog("Buddy", 3) # Create an instance of Dog
dog2 = Dog("Charlie", 5) # Create another instance of Dog
print(dog1.name, dog1.age, dog1.species) # Access instance and class attributes
print(dog2.name, dog2.age, dog2.species) # Access instance and class attributes
print(Dog.species) # Access class attribute directly
__init__ Method
• __init__ method is the constructor in Python, automatically
called when a new object is created. It initializes the
attributes of the class.
Class and Instance Variables
• In Python, variables defined in a class can be either class
variables or instance variables, and understanding the
distinction between them is crucial for object-oriented
programming.
• Class Variables
• These are the variables that are shared across all instances of a class. It is
defined at the class level, outside any methods. All objects of the class
share the same value for a class variable unless explicitly overridden in an
object.
• Instance Variables
• Variables that are unique to each instance (object) of a class. These are
defined within the __init__ method or other instance methods. Each object
maintains its own copy of instance variables, independent of other objects.
Object Oriented Programming Class and Objects
C++ Classes/Objects
• C++ is an object-oriented programming language.
• Everything in C++ is associated with classes and objects, along with its attributes and
methods.
• For example: in real life, a car is an object. The car has attributes, such as weight and color,
and methods, such as drive and brake.
• Attributes and methods are basically variables and functions that belongs to the class. These
are often referred to as "class members".
• A class is a user-defined data type that we can use in our program, and it works as an object
constructor, or a "blueprint" for creating objects.
Example - Create a Class
• The class keyword is used to create a class called MyClass.
• The public keyword is an access specifier, which specifies that members
(attributes and methods) of the class are accessible from outside the class.
• Inside the class, there is an integer variable myNum and a string
variable myString. When variables are declared within a class, they are
called attributes.
• At last, end the class definition with a semicolon ;.
Create an Object
• In C++, an object is created from a class. We have already
created the class named MyClass, so now we can use this to
create objects.
• To create an object of MyClass, specify the class name,
followed by the object name.
• To access the class attributes (myNum and myString), use the
dot syntax (.) on the object.
Example
Multiple Objects
C++ Class Methods
• Methods are functions that belongs to the class.
• There are two ways to define functions that belongs to a class :
• Inside class definition
• Outside class definition
• In the next example, we define a function inside the class, and we
named it “myMethod”.
Note : You can access methods just like you access attributes, by
creating an object of the class and using the dot syntax (.)
Object Oriented Programming Class and Objects
To define a function outside the class definition, you must declare it inside the class and then
define it outside of the class. This is done by specifying the name of the class, followed the
scope resolution :: operator , followed by the name of the function.
Parameters
C++ Constructors
• A constructor in C++ is a special method that is automatically called when an object of a class is
created.
• To create a constructor, use the same name as the class, followed by paratheses ()
• The constructor has the same name as the class, it is always public, and it does not have any
return value.
Constructor Parameters
• Constructors can also take parameters (just like regular functions), which can be useful for
setting initial values for attributes.
• The following class have brand, model and year attributes, and a constructor with different
parameters. Inside the constructor we set the attributes equal to the constructor parameters
(brand=x, etc). When we call the constructor (by creating an object of the class), we pass
parameters to the constructor, which will set the value of the corresponding attributes to the
same:
Object Oriented Programming Class and Objects
Just like functions, constructors can also be defined outside the class. First, declare the constructor inside
the class, and then define it outside of the class by specifying the name of the class, followed by the scope
resolution :: operator, followed by the name of the constructor (which is the same as the class):
C++ Access Specifiers
By now, you are quite familiar with the public keyword that appears in all of our class examples:
• The public keyword is an access specifier. Access specifiers define how 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 code.
• However, what if we want members to be private and hidden from the outside world?
• 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.
The following example shows the differences between public and private members:
Object Oriented Programming Class and Objects
End of Topic
Practice Exercise 1
Solution
Definition of object
Elements of Object
Elements of Object
Object Oriented Programming Class and Objects
Object Oriented Programming Class and Objects
Object Oriented Programming Class and Objects
Object Oriented Programming Class and Objects
Object Oriented Programming Class and Objects
Object Oriented Programming Class and Objects
Object Oriented Programming Class and Objects
Object Oriented Programming Class and Objects

More Related Content

Similar to Object Oriented Programming Class and Objects (20)

OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
OOPS-PYTHON.pptx    OOPS IN PYTHON APPLIED PROGRAMMINGOOPS-PYTHON.pptx    OOPS IN PYTHON APPLIED PROGRAMMING
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
NagarathnaRajur2
 
Python Programming - Object-Oriented
Python Programming - Object-OrientedPython Programming - Object-Oriented
Python Programming - Object-Oriented
Omid AmirGhiasvand
 
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHONUNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
drkangurajuphd
 
Unit - 3.pptx
Unit - 3.pptxUnit - 3.pptx
Unit - 3.pptx
Kongunadu College of Engineering and Technology
 
week 2 - classes and objects of object ori9ented programming .pptx
week 2 - classes and objects of object ori9ented programming .pptxweek 2 - classes and objects of object ori9ented programming .pptx
week 2 - classes and objects of object ori9ented programming .pptx
farooqabubakar4000
 
Object oriented programming. (1).pptx
Object oriented programming.      (1).pptxObject oriented programming.      (1).pptx
Object oriented programming. (1).pptx
baadshahyash
 
c++.pptxwjwjsijsnsksomammaoansnksooskskk
c++.pptxwjwjsijsnsksomammaoansnksooskskkc++.pptxwjwjsijsnsksomammaoansnksooskskk
c++.pptxwjwjsijsnsksomammaoansnksooskskk
mitivete
 
OOP and C++Classes
OOP and C++ClassesOOP and C++Classes
OOP and C++Classes
MuhammadHuzaifa981023
 
Lecture 01 - Basic Concept About OOP With Python
Lecture 01 - Basic Concept About OOP With PythonLecture 01 - Basic Concept About OOP With Python
Lecture 01 - Basic Concept About OOP With Python
National College of Business Administration & Economics ( NCBA&E)
 
Oop
OopOop
Oop
志明 陳
 
Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptx
SAICHARANREDDYN
 
Python programming Concepts (Functions, classes and Oops concept
Python programming Concepts (Functions, classes and Oops conceptPython programming Concepts (Functions, classes and Oops concept
Python programming Concepts (Functions, classes and Oops concept
Lipika Sharma
 
slides11-objects_and_classes in python.pptx
slides11-objects_and_classes in python.pptxslides11-objects_and_classes in python.pptx
slides11-objects_and_classes in python.pptx
debasisdas225831
 
Python-Classes.pptx
Python-Classes.pptxPython-Classes.pptx
Python-Classes.pptx
Karudaiyar Ganapathy
 
Object oriented programming 6 oop with c++
Object oriented programming 6  oop with c++Object oriented programming 6  oop with c++
Object oriented programming 6 oop with c++
Vaibhav Khanna
 
مقدمة بايثون .pptx
مقدمة بايثون .pptxمقدمة بايثون .pptx
مقدمة بايثون .pptx
AlmutasemBillahAlwas
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
sana younas
 
software construction and development week 3 Python lists, tuples, dictionari...
software construction and development week 3 Python lists, tuples, dictionari...software construction and development week 3 Python lists, tuples, dictionari...
software construction and development week 3 Python lists, tuples, dictionari...
MuhammadBilalAjmal2
 
Rajib Ali Presentation on object oreitation oop.pptx
Rajib Ali Presentation on object oreitation oop.pptxRajib Ali Presentation on object oreitation oop.pptx
Rajib Ali Presentation on object oreitation oop.pptx
domefe4146
 
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptxObject Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
RashidFaridChishti
 
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
OOPS-PYTHON.pptx    OOPS IN PYTHON APPLIED PROGRAMMINGOOPS-PYTHON.pptx    OOPS IN PYTHON APPLIED PROGRAMMING
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
NagarathnaRajur2
 
Python Programming - Object-Oriented
Python Programming - Object-OrientedPython Programming - Object-Oriented
Python Programming - Object-Oriented
Omid AmirGhiasvand
 
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHONUNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
drkangurajuphd
 
week 2 - classes and objects of object ori9ented programming .pptx
week 2 - classes and objects of object ori9ented programming .pptxweek 2 - classes and objects of object ori9ented programming .pptx
week 2 - classes and objects of object ori9ented programming .pptx
farooqabubakar4000
 
Object oriented programming. (1).pptx
Object oriented programming.      (1).pptxObject oriented programming.      (1).pptx
Object oriented programming. (1).pptx
baadshahyash
 
c++.pptxwjwjsijsnsksomammaoansnksooskskk
c++.pptxwjwjsijsnsksomammaoansnksooskskkc++.pptxwjwjsijsnsksomammaoansnksooskskk
c++.pptxwjwjsijsnsksomammaoansnksooskskk
mitivete
 
Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptx
SAICHARANREDDYN
 
Python programming Concepts (Functions, classes and Oops concept
Python programming Concepts (Functions, classes and Oops conceptPython programming Concepts (Functions, classes and Oops concept
Python programming Concepts (Functions, classes and Oops concept
Lipika Sharma
 
slides11-objects_and_classes in python.pptx
slides11-objects_and_classes in python.pptxslides11-objects_and_classes in python.pptx
slides11-objects_and_classes in python.pptx
debasisdas225831
 
Object oriented programming 6 oop with c++
Object oriented programming 6  oop with c++Object oriented programming 6  oop with c++
Object oriented programming 6 oop with c++
Vaibhav Khanna
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
sana younas
 
software construction and development week 3 Python lists, tuples, dictionari...
software construction and development week 3 Python lists, tuples, dictionari...software construction and development week 3 Python lists, tuples, dictionari...
software construction and development week 3 Python lists, tuples, dictionari...
MuhammadBilalAjmal2
 
Rajib Ali Presentation on object oreitation oop.pptx
Rajib Ali Presentation on object oreitation oop.pptxRajib Ali Presentation on object oreitation oop.pptx
Rajib Ali Presentation on object oreitation oop.pptx
domefe4146
 
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptxObject Oriented Programming using C++: Ch06 Objects and Classes.pptx
Object Oriented Programming using C++: Ch06 Objects and Classes.pptx
RashidFaridChishti
 

Recently uploaded (20)

Jira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : IntroductionJira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : Introduction
Ravi Teja
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
Soulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate reviewSoulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate review
Soulmaite
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptxISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
AyilurRamnath1
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | BluebashMCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
Bluebash
 
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdfHow Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
Rejig Digital
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdfvertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOMEstablish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Anchore
 
7 Salesforce Data Cloud Best Practices.pdf
7 Salesforce Data Cloud Best Practices.pdf7 Salesforce Data Cloud Best Practices.pdf
7 Salesforce Data Cloud Best Practices.pdf
Minuscule Technologies
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to KnowWhat is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
SMACT Works
 
Jira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : IntroductionJira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : Introduction
Ravi Teja
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
Soulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate reviewSoulmaite review - Find Real AI soulmate review
Soulmaite review - Find Real AI soulmate review
Soulmaite
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdfcnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptxISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
ISOIEC 42005 Revolutionalises AI Impact Assessment.pptx
AyilurRamnath1
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | BluebashMCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
Bluebash
 
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdfHow Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
Rejig Digital
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdfvertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOMEstablish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Establish Visibility and Manage Risk in the Supply Chain with Anchore SBOM
Anchore
 
7 Salesforce Data Cloud Best Practices.pdf
7 Salesforce Data Cloud Best Practices.pdf7 Salesforce Data Cloud Best Practices.pdf
7 Salesforce Data Cloud Best Practices.pdf
Minuscule Technologies
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to KnowWhat is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
SMACT Works
 
Ad

Object Oriented Programming Class and Objects

  • 1. Topic 2 : Object-oriented concept and programming 4000CEM Object Oriented Programing (OOP)
  • 2. Topic Outlines • Introduction to OOP • Object-oriented concept and programming • Encapsulation, information hiding, classes, inheritance
  • 4. Classes & Objects • Class defines what data and method should the object have. • The object contains the actual data • Creating an object using the class blueprint. • Each object behave in the same way but has it own data. • Behaviour define inside the class is shares by all the objects, but data is not. • Each object behave in the same way but has it own data.
  • 5. Create a Class in Python class Person: //class object is created pass //don’t have any data/method p1 = Person() //instance object created p2 = Person()
  • 6. class Person: def display(self): print (‘I am a Person’) def greet(self): print (‘Hello, how are you doing?’) p1 = Person() //instance object created p2 = Person() p1.display() //no parameter is send because the object is send when the function is called p1.greet()
  • 7. class Person: def set_details(self, name, age): self.name = name self.age = age def display(self): print (‘I am a Person’) def greet(self): print (‘Hello, how are you doing?’) p1 = Person() //instance object created p2 = Person() p1.set_details(‘Bob’, 20) p2.set_details(‘Ted’, 60)
  • 9. INTRODUCTION TO OOP • OOP stands for Object-Oriented Programming. • Procedural programming is about writing procedures or functions that perform operations on the data, while object-oriented programming is about creating objects that contain both data and functions. • 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“ (avoid repeating the code), 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
  • 10. • Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full potential of Python OOP capabilities to design elegant and efficient solutions to complex problems. • OOPs is a way of organizing code that uses objects and classes to represent real-world entities and their behavior. In OOPs, object has attributes thing that has specific data and can perform certain actions using methods.
  • 11. OOPs Concepts in Python • Class in Python • Objects in Python • Polymorphism in Python • Encapsulation in Python • Inheritance in Python • Data Abstraction in Python
  • 12. C++ Classes and Objects? • Classes and objects are the two main aspects of object- oriented programming. CLASSES OBJECT Vehicles • Car • Truck • Bicycle Animals • Dog • Cat • Bird
  • 13. • A class is a template for objects, and an object is an instance of a class. • When the individual objects are created, they inherit all the variables and functions from the class.
  • 14. Python Class • A class is a collection of objects. Classes are blueprints for creating objects. A class defines a set of attributes and methods that the created objects (instances) can have. • Classes are created by keyword class. • Attributes are the variables that belong to a class. • Attributes are always public and can be accessed using the dot (.) operator. Example: Myclass.Myattribute
  • 15. Creating a Class • Here, the class keyword indicates that we are creating a class followed by name of the class (Dog in this case).
  • 16. Python Objects • An Object is an instance of a Class. It represents a specific implementation of the class and holds its own data. • An object consists of: • State: It is represented by the attributes and reflects the properties of an object. • Behavior: It is represented by the methods of an object and reflects the response of an object to other objects. • Identity: It gives a unique name to an object and enables one object to interact with other objects.
  • 17. Creating Object • Creating an object in Python involves instantiating a class to create a new instance of that class. This process is also referred to as object instantiation.
  • 18. Self Parameter • self parameter is a reference to the current instance of the class. It allows us to access the attributes and methods of the object. class Dog: species = "Canine" # Class attribute def __init__(self, name, age): self.name = name # Instance attribute self.age = age # Instance attribute dog1 = Dog("Buddy", 3) # Create an instance of Dog dog2 = Dog("Charlie", 5) # Create another instance of Dog print(dog1.name, dog1.age, dog1.species) # Access instance and class attributes print(dog2.name, dog2.age, dog2.species) # Access instance and class attributes print(Dog.species) # Access class attribute directly
  • 19. __init__ Method • __init__ method is the constructor in Python, automatically called when a new object is created. It initializes the attributes of the class.
  • 20. Class and Instance Variables • In Python, variables defined in a class can be either class variables or instance variables, and understanding the distinction between them is crucial for object-oriented programming.
  • 21. • Class Variables • These are the variables that are shared across all instances of a class. It is defined at the class level, outside any methods. All objects of the class share the same value for a class variable unless explicitly overridden in an object. • Instance Variables • Variables that are unique to each instance (object) of a class. These are defined within the __init__ method or other instance methods. Each object maintains its own copy of instance variables, independent of other objects.
  • 23. C++ Classes/Objects • C++ is an object-oriented programming language. • Everything in C++ is associated with classes and objects, along with its attributes and methods. • For example: in real life, a car is an object. The car has attributes, such as weight and color, and methods, such as drive and brake. • Attributes and methods are basically variables and functions that belongs to the class. These are often referred to as "class members". • A class is a user-defined data type that we can use in our program, and it works as an object constructor, or a "blueprint" for creating objects.
  • 24. Example - Create a Class
  • 25. • The class keyword is used to create a class called MyClass. • The public keyword is an access specifier, which specifies that members (attributes and methods) of the class are accessible from outside the class. • Inside the class, there is an integer variable myNum and a string variable myString. When variables are declared within a class, they are called attributes. • At last, end the class definition with a semicolon ;.
  • 26. Create an Object • In C++, an object is created from a class. We have already created the class named MyClass, so now we can use this to create objects. • To create an object of MyClass, specify the class name, followed by the object name. • To access the class attributes (myNum and myString), use the dot syntax (.) on the object.
  • 29. C++ Class Methods • Methods are functions that belongs to the class. • There are two ways to define functions that belongs to a class : • Inside class definition • Outside class definition • In the next example, we define a function inside the class, and we named it “myMethod”. Note : You can access methods just like you access attributes, by creating an object of the class and using the dot syntax (.)
  • 31. To define a function outside the class definition, you must declare it inside the class and then define it outside of the class. This is done by specifying the name of the class, followed the scope resolution :: operator , followed by the name of the function.
  • 33. C++ Constructors • A constructor in C++ is a special method that is automatically called when an object of a class is created. • To create a constructor, use the same name as the class, followed by paratheses () • The constructor has the same name as the class, it is always public, and it does not have any return value.
  • 34. Constructor Parameters • Constructors can also take parameters (just like regular functions), which can be useful for setting initial values for attributes. • The following class have brand, model and year attributes, and a constructor with different parameters. Inside the constructor we set the attributes equal to the constructor parameters (brand=x, etc). When we call the constructor (by creating an object of the class), we pass parameters to the constructor, which will set the value of the corresponding attributes to the same:
  • 36. Just like functions, constructors can also be defined outside the class. First, declare the constructor inside the class, and then define it outside of the class by specifying the name of the class, followed by the scope resolution :: operator, followed by the name of the constructor (which is the same as the class):
  • 37. C++ Access Specifiers By now, you are quite familiar with the public keyword that appears in all of our class examples:
  • 38. • The public keyword is an access specifier. Access specifiers define how 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 code. • However, what if we want members to be private and hidden from the outside world? • 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.
  • 39. The following example shows the differences between public and private members: