OBJECT ORIENTED
PROGRAMMING
Object and Classes
CLASSES
CLASSES
 A class is a blueprint or a template that defines
the structure and behavior of objects. It is a
blueprint that encapsulates data and methods
(functions) into a single unit.
 A class is an entity that determines how an
object will behave and what the object will
contain.
 Think of a class as a blueprint for a house, which
defines the structure and characteristics of the
house.
CLASSES
 When you define a class you define a blueprint
for data type.
 This doesn’t actually define any data but it does
define what the class name means, what an
object of the class will consist of and what
operations can be performed on such an object.
SYNTAX OF CLASS
 A class is defined in C++ using
keyword class followed by the name of the class.
 The body of the class is defined inside the curly
brackets and terminated by a semicolon at the
end.
CLASS STRUCTURE
 A Class is a 3-Compartment Box encapsulating
Data and Functions
1. Classname (or identifier): identifies the class.
2. Data Members or Variables (or attributes,
states, fields): contains the static attributes of
the class.
3. Member Functions (or methods, behaviors,
operations): contains the dynamic operations of
the class
CLASS STRUCTURE
 In other words, a class encapsulates the static
attributes (data) and dynamic behaviors
(operations t hat operate on the data) in a box.
 Class Members
 The data members and member functions are
collectively called class members.
CLASS STRUCTURE
 Data Members (Variables)
 A data member (variable) has a name (or identifier) and a type;
and holds a value of that particular type (as descried in the
earlier chapter). A data member can also be an instance of a
certain class
Member Functions
A member function
 receives parameters from the caller
 performs the operations defined in the function body
 returns a piece of result (or void) to the caller
CLASS STRUCTURE
Class UML diagram
CLASS STRUCTURE
Instance UML diagram
EXAMPLE
Data members
Class
Member functions
OBJECTS
OBJECT
 An object is an instance of a class. It is a concrete
representation of a class that is created at
runtime.
 Basic unit of Object Oriented Programming that
is both data and function that operate on data
are bundled as unit called as object.
 Think of an object as an actual house built from
a blueprint. It has its own unique identity and
can have its own data and behavior.
OBJECT
 When a class is defined, only the specification for
the object is defined; no memory or storage is
allocated.
 To use the data and access functions defined in
the class, we need to create objects.
SYNTAX OF OBJECTS
EXAMPLE
objects room3 and room4 are
created in main()
SAMPLE CODE
SAMPLE CODE
 We can think of a class as the technical design
(prototype) of a car.
 It contains all the details about the brand, model,
mileage, etc.
 We can then build different cars based on these
descriptions.
 Here, each distinct car is an object.
SAMPLE CODE
SAMPLE CODE
• used the class keyword to
create a class named Car.
• brand and model are class
attributes used to store data
• drive() is a class function
used to perform some
operation
• The public keyword
represents an access
modifier.
SAMPLE CODE
 For example, the Car class defines the model,
brand, and mileage. Now, based on the definition,
 suv, sedan, and van are objects of the Car class
SAMPLE CODE
SAMPLE CODE
OUTPUT
LAB EXERCISE
LAB EXERCISE 1
 Write a C++ program that defines a class
representing a simple bank account. The class
should have data members such as account
number, account holder name, and balance. It
should also have member functions such as
deposit, withdrawal, and display account details.
Create objects of the class and perform various
operations on them, such as depositing money,
withdrawing money, and displaying account
details.
LAB EXERCISE 2
 Write a C++ program that defines a class
representing a student. The class should have
data members such as name, roll number, and
marks in different subjects. It should also have
member functions to accept and display student
information. Create objects of the class for
multiple students and input their details. Use
member functions to display the information of
the students.
LAB EXERCISE 3
 Write a C++ program that defines classes
representing a circle and a rectangle. The circle
class should have data members such as radius
and member functions to calculate and display
the area and circumference of the circle. The
rectangle class should have data members such
as length and breadth, and member functions to
calculate and display the area and perimeter of
the rectangle. Create objects of the circle and
rectangle classes and perform various operations
on them, such as calculating area, circumference,
perimeter, and displaying the details of the
shapes.

week 2 - classes and objects of object ori9ented programming .pptx

  • 1.
  • 2.
  • 3.
    CLASSES  A classis a blueprint or a template that defines the structure and behavior of objects. It is a blueprint that encapsulates data and methods (functions) into a single unit.  A class is an entity that determines how an object will behave and what the object will contain.  Think of a class as a blueprint for a house, which defines the structure and characteristics of the house.
  • 4.
    CLASSES  When youdefine a class you define a blueprint for data type.  This doesn’t actually define any data but it does define what the class name means, what an object of the class will consist of and what operations can be performed on such an object.
  • 5.
    SYNTAX OF CLASS A class is defined in C++ using keyword class followed by the name of the class.  The body of the class is defined inside the curly brackets and terminated by a semicolon at the end.
  • 6.
    CLASS STRUCTURE  AClass is a 3-Compartment Box encapsulating Data and Functions 1. Classname (or identifier): identifies the class. 2. Data Members or Variables (or attributes, states, fields): contains the static attributes of the class. 3. Member Functions (or methods, behaviors, operations): contains the dynamic operations of the class
  • 7.
    CLASS STRUCTURE  Inother words, a class encapsulates the static attributes (data) and dynamic behaviors (operations t hat operate on the data) in a box.  Class Members  The data members and member functions are collectively called class members.
  • 8.
    CLASS STRUCTURE  DataMembers (Variables)  A data member (variable) has a name (or identifier) and a type; and holds a value of that particular type (as descried in the earlier chapter). A data member can also be an instance of a certain class Member Functions A member function  receives parameters from the caller  performs the operations defined in the function body  returns a piece of result (or void) to the caller
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
    OBJECT  An objectis an instance of a class. It is a concrete representation of a class that is created at runtime.  Basic unit of Object Oriented Programming that is both data and function that operate on data are bundled as unit called as object.  Think of an object as an actual house built from a blueprint. It has its own unique identity and can have its own data and behavior.
  • 14.
    OBJECT  When aclass is defined, only the specification for the object is defined; no memory or storage is allocated.  To use the data and access functions defined in the class, we need to create objects.
  • 15.
  • 16.
    EXAMPLE objects room3 androom4 are created in main()
  • 17.
  • 18.
    SAMPLE CODE  Wecan think of a class as the technical design (prototype) of a car.  It contains all the details about the brand, model, mileage, etc.  We can then build different cars based on these descriptions.  Here, each distinct car is an object.
  • 19.
  • 20.
    SAMPLE CODE • usedthe class keyword to create a class named Car. • brand and model are class attributes used to store data • drive() is a class function used to perform some operation • The public keyword represents an access modifier.
  • 21.
    SAMPLE CODE  Forexample, the Car class defines the model, brand, and mileage. Now, based on the definition,  suv, sedan, and van are objects of the Car class
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
    LAB EXERCISE 1 Write a C++ program that defines a class representing a simple bank account. The class should have data members such as account number, account holder name, and balance. It should also have member functions such as deposit, withdrawal, and display account details. Create objects of the class and perform various operations on them, such as depositing money, withdrawing money, and displaying account details.
  • 27.
    LAB EXERCISE 2 Write a C++ program that defines a class representing a student. The class should have data members such as name, roll number, and marks in different subjects. It should also have member functions to accept and display student information. Create objects of the class for multiple students and input their details. Use member functions to display the information of the students.
  • 28.
    LAB EXERCISE 3 Write a C++ program that defines classes representing a circle and a rectangle. The circle class should have data members such as radius and member functions to calculate and display the area and circumference of the circle. The rectangle class should have data members such as length and breadth, and member functions to calculate and display the area and perimeter of the rectangle. Create objects of the circle and rectangle classes and perform various operations on them, such as calculating area, circumference, perimeter, and displaying the details of the shapes.