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.
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.