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)

PPTX
[OOP - Lec 06] Classes and Objects
Muhammad Hammad Waseem
 
PPT
Object -oriented analysis and design.ppt
pierrerj05
 
PPTX
object oriented programing lecture 1
Geophery sanga
 
PPTX
Object Oriented Programming.pptx
SAICHARANREDDYN
 
PPTX
IPP-M5-C1-Classes _ Objects python -S2.pptx
DhavalaShreeBJain
 
PPTX
OOP Presentation.pptx
DurgaPrasadVasantati
 
PPTX
OOP Presentation.pptx
DurgaPrasadVasantati
 
PPTX
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
NagarathnaRajur2
 
PPTX
Class_and_Object_with_Example_Python.pptx janbsbznnsbxghzbbshvxnxhnwn
bandiranvitha
 
PPTX
Advanced Topics on Database - Unit-2 AU17
LOGANATHANK24
 
PPTX
OOSD1-unit1_1_16_09.pptx
ShobhitSrivastava15887
 
PPTX
introduction_OOP for the java courses [Autosaved].pptx
DrShamimAlMamun
 
PPTX
Object Oriented Programming in Python.pptx
grpvasundhara1993
 
PPTX
[OOP - Lec 04,05] Basic Building Blocks of OOP
Muhammad Hammad Waseem
 
PPTX
Object oriented Programming in Python.pptx
SHAIKIRFAN715544
 
PPT
Core Java unit no. 1 object and class ppt
Mochi263119
 
PPT
Core Java unit no. 1 object and class ppt
Mochi263119
 
PPTX
Creating class, self variables in Python
AditiPawaskar5
 
PPTX
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
drkangurajuphd
 
PPTX
Python-Classes.pptx
Karudaiyar Ganapathy
 
[OOP - Lec 06] Classes and Objects
Muhammad Hammad Waseem
 
Object -oriented analysis and design.ppt
pierrerj05
 
object oriented programing lecture 1
Geophery sanga
 
Object Oriented Programming.pptx
SAICHARANREDDYN
 
IPP-M5-C1-Classes _ Objects python -S2.pptx
DhavalaShreeBJain
 
OOP Presentation.pptx
DurgaPrasadVasantati
 
OOP Presentation.pptx
DurgaPrasadVasantati
 
OOPS-PYTHON.pptx OOPS IN PYTHON APPLIED PROGRAMMING
NagarathnaRajur2
 
Class_and_Object_with_Example_Python.pptx janbsbznnsbxghzbbshvxnxhnwn
bandiranvitha
 
Advanced Topics on Database - Unit-2 AU17
LOGANATHANK24
 
OOSD1-unit1_1_16_09.pptx
ShobhitSrivastava15887
 
introduction_OOP for the java courses [Autosaved].pptx
DrShamimAlMamun
 
Object Oriented Programming in Python.pptx
grpvasundhara1993
 
[OOP - Lec 04,05] Basic Building Blocks of OOP
Muhammad Hammad Waseem
 
Object oriented Programming in Python.pptx
SHAIKIRFAN715544
 
Core Java unit no. 1 object and class ppt
Mochi263119
 
Core Java unit no. 1 object and class ppt
Mochi263119
 
Creating class, self variables in Python
AditiPawaskar5
 
UNIT 3 PY.pptx - OOPS CONCEPTS IN PYTHON
drkangurajuphd
 
Python-Classes.pptx
Karudaiyar Ganapathy
 

Recently uploaded (20)

PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PPT
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
Ericsson LTE presentation SEMINAR 2010.ppt
npat3
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
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: