SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
Classes and Objects
Muhammad Hammad Waseem
m.hammad.wasim@gmail.com
Introduction
• An object has the same relationship to a class that a variable has to a
data type.
• An object is said to be an instance of a class, in the same way
• my 1954 Chevrolet is an instance of a vehicle.
• A class is a template to create many similar objects.
• As my BMW and Chevrolet is an instance of vehicle class.
Objects
• An object can be a variable, a data structure, or a function.
• In the class-based object-oriented programming paradigm, "object"
refers to a particular instance of a class
• where the object can be a combination of
• variables,
• functions, and
• data structures.
Classes
• A class is a blueprint for creating many similar objects.
• The created object is an instance of that class.
• Objects created from the same class will have the same basic
structure and functionality.
• All cars created from the same Ford Escort blueprints will look and work
basically the same.
• Many instances can be created from a single class.
• Just as many Ford Escorts can be created from the same set of Ford Escort
blueprints.
Classes and Instances
Data Elements of Class
• A class may contain attributes and behaviors for a particular object.
• In OOP, these are called as:
• Data members/ instance variables (attributes)
• Member functions/ instance methods/ functions/ methods
(behaviors/actions)
Instance Variables
• An instance variable (attribute) of an object is a piece of information
attached to an instance (object).
• The name of a Person object, the model and year of a Car object, etc.
• The instance variables that an object has are defined in the object's
class:
• An object can usually have many instance variables, of many different types.
• Each object is given its own private space to hold its instance
variables.
• Assigning a new value to an instance variable of one object does not
affect the instance variables of any other object.
Instance Methods
• When we define objects, we usually have an idea of what we want to
do with them...
• – I'm dealing with Person objects in an employee database... I want to be able
to ask each Person object their name, weight, and age.
• – I'm dealing with Car objects in a driving simulation... I want to be able to
start a Car, change its speed, turn its steering wheel, etc.
• An action that involves a single object is usually implemented as a
special kind of function/subroutine attached to that object's class,
called an instance method (or, more commonly, just a method).
Defining the Class
• Here’s the definition (sometimes called a specifier) for the class:
class ClassName //define a class
{
private: //Access Specifier
Data Members; //class data
public:
Member Functions //class data
};
Defining a Class: ClassName
• The definition starts with the keyword class, followed by the class
name.
• Like a structure, the body of the class is delimited by braces and
terminated by a semicolon. (Don’t forget the semicolon).
• Remember,
• Data constructs such as structures and classes end with a semicolon,
• While control constructs such as functions and loops do not.
Defining a Class: Access Specifiers
• The body of the class contains two unfamiliar keywords:
• private and
• public.
• A key feature of object-oriented programming is data hiding.
• This term does not refer to the activities of particularly paranoid
programmers;
• rather it means that data is concealed within a class so that it cannot be accessed
mistakenly by functions outside the class.
• The primary mechanism for hiding data is to put it in a class and make it
private.
• Private data or functions can only be accessed from within the class.
• Public data or functions, on the other hand, are accessible from outside the
class.
Defining a Class: Class Data
• The data items within a class are called data members (or sometimes
member data).
• There can be any number of data members in a class, just as there
can be any number of data items in a structure.
• The data member follows the keyword private, so it can be accessed
from within the class, but not from outside.
Defining a Class: Member Functions
• Member functions are functions that are included within a class.
• (In some object-oriented languages, such as Smalltalk, member functions are
called methods; some writers use this term in C++ as well.)
• The function bodies of these functions have been written on the
same line as the braces that delimit them. You could also use the
more traditional format for these function definitions.
• However, when member functions are small, it is common to
compress their definitions this way to save space.
• Functions follow the keyword public, they can be accessed from
outside the class.
Defining a Class: Functions Are Public, Data Is
Private
• Usually the data within a class is private and the functions are public.
• The data is hidden so it will be safe from accidental manipulation,
while the functions that operate on the data are public so they can be
accessed from outside the class.
• However, there is no rule that says data must be private and
functions public; in some circumstances you may find you’ll need to
use private functions and public data.
Defining a Class: Member Functions Within
Class Definition
• The member functions definitions may be done within the class
definition.
• Member functions defined inside a class this way are created as inline
functions by default.
• We’ll see later that it is also possible to declare a function within a
class but to define it elsewhere.
• Functions defined outside the class are not normally inline.
Defining Objects
• Class only describes how they will look when they are created, just as a
structure definition describes how a structure will look but doesn’t create
any structure variables.
• It is objects that participate in program operations.
• Defining an object is similar to defining a variable of any data type
• When an object of a class is created, the space for all data members
defined in the class is allocated in the memory according to their data
types.
• An object is also known as instance.
• The process of creating an object of a class is also called instantiation.
• Syntax: ClassName ObjectName;
Defining Objects: Executing Member Functions
• An object of a particular class contains all data members as well as
member functions defined in that class
• The data members contains the value related to the object.
• The functions are used to manipulate data members.
• The member functions can be executed only after creating an object.
• Syntax: ObjectName.memberfunction();

More Related Content

PPTX
[OOP - Lec 19] Static Member Functions
PPTX
[OOP - Lec 18] Static Data Member
PPTX
[OOP - Lec 04,05] Basic Building Blocks of OOP
PPTX
Classes and objects
PPT
Classes cpp intro thomson bayan college
PPTX
object oriented programming using c++
PDF
Java Programming Paradigms Chapter 1
PPTX
Static keyword ppt
[OOP - Lec 19] Static Member Functions
[OOP - Lec 18] Static Data Member
[OOP - Lec 04,05] Basic Building Blocks of OOP
Classes and objects
Classes cpp intro thomson bayan college
object oriented programming using c++
Java Programming Paradigms Chapter 1
Static keyword ppt

What's hot (20)

PDF
Classes and Nested Classes in Java
PPTX
CPP14 - Encapsulation
PPSX
Object Oriented Programming Overview for the PeopleSoft Developer
PPTX
C++ training
PPTX
Object as function argument , friend and static function by shahzad younas
PPTX
object oriented programing lecture 1
PPTX
Characteristics of OOPS
PPTX
Characteristics of oop
PPTX
[OOP - Lec 07] Access Specifiers
PPTX
OOPS Characteristics (With Examples in PHP)
PPTX
Inner classes in java
PPTX
Oo ps concepts in c++
PPTX
Java static keyword
PPTX
Inner Classes & Multi Threading in JAVA
PPTX
oop lecture 3
PPT
11 Using classes and objects
PPTX
Object Oriented Programming
PPTX
Object oriented programming concept- Saurabh Upadhyay
PPTX
Oops concepts
PPT
Ashish oot
Classes and Nested Classes in Java
CPP14 - Encapsulation
Object Oriented Programming Overview for the PeopleSoft Developer
C++ training
Object as function argument , friend and static function by shahzad younas
object oriented programing lecture 1
Characteristics of OOPS
Characteristics of oop
[OOP - Lec 07] Access Specifiers
OOPS Characteristics (With Examples in PHP)
Inner classes in java
Oo ps concepts in c++
Java static keyword
Inner Classes & Multi Threading in JAVA
oop lecture 3
11 Using classes and objects
Object Oriented Programming
Object oriented programming concept- Saurabh Upadhyay
Oops concepts
Ashish oot
Ad

Viewers also liked (11)

PPTX
[OOP - Lec 09,10,11] Class Members & their Accessing
PPTX
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
PPTX
[OOP - Lec 03] Programming Paradigms
PPTX
[OOP - Lec 01] Introduction to OOP
PPTX
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
PPTX
[OOP - Lec 02] Why do we need OOP
PPTX
[OOP - Lec 08] Encapsulation (Information Hiding)
PPTX
[OOP - Lec 20,21] Inheritance
PDF
2015 Upload Campaigns Calendar - SlideShare
PPTX
What to Upload to SlideShare
PDF
Getting Started With SlideShare
[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
[OOP - Lec 03] Programming Paradigms
[OOP - Lec 01] Introduction to OOP
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
[OOP - Lec 02] Why do we need OOP
[OOP - Lec 08] Encapsulation (Information Hiding)
[OOP - Lec 20,21] Inheritance
2015 Upload Campaigns Calendar - SlideShare
What to Upload to SlideShare
Getting Started With SlideShare
Ad

Similar to [OOP - Lec 06] Classes and Objects (20)

PPTX
Concept of Object-Oriented in C++
PPTX
Class and objects
PPTX
week 2 - classes and objects of object ori9ented programming .pptx
PPTX
object oriented programming-classes and objects.pptx
PPTX
Presentation on class and object in Object Oriented programming.
PPT
Classes and objects
PPT
classandobjectunit2-150824133722-lva1-app6891.ppt
PPT
4 Classes & Objects
PDF
Introduction to C++ Class & Objects. Book Notes
PPTX
B.sc CSIT 2nd semester C++ Unit3
PPTX
Object Oriented Programming Class and Objects
PPTX
Classes and objects
PPTX
Object oriented programming in C++
PPTX
PPTX
PDF
Class and object
PPTX
OOP-Advanced_Programming.pptx
PPTX
PPTX
class and object in c++.pptx
PPTX
Class-١.pptx vbdbbdndgngngndngnnfndfnngn
Concept of Object-Oriented in C++
Class and objects
week 2 - classes and objects of object ori9ented programming .pptx
object oriented programming-classes and objects.pptx
Presentation on class and object in Object Oriented programming.
Classes and objects
classandobjectunit2-150824133722-lva1-app6891.ppt
4 Classes & Objects
Introduction to C++ Class & Objects. Book Notes
B.sc CSIT 2nd semester C++ Unit3
Object Oriented Programming Class and Objects
Classes and objects
Object oriented programming in C++
Class and object
OOP-Advanced_Programming.pptx
class and object in c++.pptx
Class-١.pptx vbdbbdndgngngndngnnfndfnngn

More from Muhammad Hammad Waseem (18)

PDF
[ITP - Lecture 17] Strings in C/C++
PDF
[ITP - Lecture 16] Structures in C/C++
PDF
[ITP - Lecture 15] Arrays & its Types
PDF
[ITP - Lecture 14] Recursion
PDF
[ITP - Lecture 13] Introduction to Pointers
PDF
[ITP - Lecture 12] Functions in C/C++
PDF
[ITP - Lecture 11] Loops in C/C++
PDF
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
PDF
[ITP - Lecture 09] Conditional Operator in C/C++
PDF
[ITP - Lecture 08] Decision Control Structures (If Statement)
PDF
[ITP - Lecture 07] Comments in C/C++
PDF
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
PDF
[ITP - Lecture 05] Datatypes
PDF
[ITP - Lecture 04] Variables and Constants in C/C++
PDF
[ITP - Lecture 03] Introduction to C/C++
PDF
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
PDF
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
PPTX
Data Structures - Lecture 10 [Graphs]
[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 14] Recursion
[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 07] Comments in C/C++
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 05] Datatypes
[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 03] Introduction to C/C++
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
Data Structures - Lecture 10 [Graphs]

Recently uploaded (20)

PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Basic Mud Logging Guide for educational purpose
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Pre independence Education in Inndia.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Cell Structure & Organelles in detailed.
PPTX
Institutional Correction lecture only . . .
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Cell Types and Its function , kingdom of life
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Basic Mud Logging Guide for educational purpose
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
GDM (1) (1).pptx small presentation for students
O5-L3 Freight Transport Ops (International) V1.pdf
Insiders guide to clinical Medicine.pdf
TR - Agricultural Crops Production NC III.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Final Presentation General Medicine 03-08-2024.pptx
Pre independence Education in Inndia.pdf
VCE English Exam - Section C Student Revision Booklet
Cell Structure & Organelles in detailed.
Institutional Correction lecture only . . .
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Abdominal Access Techniques with Prof. Dr. R K Mishra
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Cell Types and Its function , kingdom of life
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape

[OOP - Lec 06] Classes and Objects

  • 1. Classes and Objects Muhammad Hammad Waseem m.hammad.wasim@gmail.com
  • 2. Introduction • An object has the same relationship to a class that a variable has to a data type. • An object is said to be an instance of a class, in the same way • my 1954 Chevrolet is an instance of a vehicle. • A class is a template to create many similar objects. • As my BMW and Chevrolet is an instance of vehicle class.
  • 3. Objects • An object can be a variable, a data structure, or a function. • In the class-based object-oriented programming paradigm, "object" refers to a particular instance of a class • where the object can be a combination of • variables, • functions, and • data structures.
  • 4. Classes • A class is a blueprint for creating many similar objects. • The created object is an instance of that class. • Objects created from the same class will have the same basic structure and functionality. • All cars created from the same Ford Escort blueprints will look and work basically the same. • Many instances can be created from a single class. • Just as many Ford Escorts can be created from the same set of Ford Escort blueprints.
  • 6. Data Elements of Class • A class may contain attributes and behaviors for a particular object. • In OOP, these are called as: • Data members/ instance variables (attributes) • Member functions/ instance methods/ functions/ methods (behaviors/actions)
  • 7. Instance Variables • An instance variable (attribute) of an object is a piece of information attached to an instance (object). • The name of a Person object, the model and year of a Car object, etc. • The instance variables that an object has are defined in the object's class: • An object can usually have many instance variables, of many different types. • Each object is given its own private space to hold its instance variables. • Assigning a new value to an instance variable of one object does not affect the instance variables of any other object.
  • 8. Instance Methods • When we define objects, we usually have an idea of what we want to do with them... • – I'm dealing with Person objects in an employee database... I want to be able to ask each Person object their name, weight, and age. • – I'm dealing with Car objects in a driving simulation... I want to be able to start a Car, change its speed, turn its steering wheel, etc. • An action that involves a single object is usually implemented as a special kind of function/subroutine attached to that object's class, called an instance method (or, more commonly, just a method).
  • 9. Defining the Class • Here’s the definition (sometimes called a specifier) for the class: class ClassName //define a class { private: //Access Specifier Data Members; //class data public: Member Functions //class data };
  • 10. Defining a Class: ClassName • The definition starts with the keyword class, followed by the class name. • Like a structure, the body of the class is delimited by braces and terminated by a semicolon. (Don’t forget the semicolon). • Remember, • Data constructs such as structures and classes end with a semicolon, • While control constructs such as functions and loops do not.
  • 11. Defining a Class: Access Specifiers • The body of the class contains two unfamiliar keywords: • private and • public. • A key feature of object-oriented programming is data hiding. • This term does not refer to the activities of particularly paranoid programmers; • rather it means that data is concealed within a class so that it cannot be accessed mistakenly by functions outside the class. • The primary mechanism for hiding data is to put it in a class and make it private. • Private data or functions can only be accessed from within the class. • Public data or functions, on the other hand, are accessible from outside the class.
  • 12. Defining a Class: Class Data • The data items within a class are called data members (or sometimes member data). • There can be any number of data members in a class, just as there can be any number of data items in a structure. • The data member follows the keyword private, so it can be accessed from within the class, but not from outside.
  • 13. Defining a Class: Member Functions • Member functions are functions that are included within a class. • (In some object-oriented languages, such as Smalltalk, member functions are called methods; some writers use this term in C++ as well.) • The function bodies of these functions have been written on the same line as the braces that delimit them. You could also use the more traditional format for these function definitions. • However, when member functions are small, it is common to compress their definitions this way to save space. • Functions follow the keyword public, they can be accessed from outside the class.
  • 14. Defining a Class: Functions Are Public, Data Is Private • Usually the data within a class is private and the functions are public. • The data is hidden so it will be safe from accidental manipulation, while the functions that operate on the data are public so they can be accessed from outside the class. • However, there is no rule that says data must be private and functions public; in some circumstances you may find you’ll need to use private functions and public data.
  • 15. Defining a Class: Member Functions Within Class Definition • The member functions definitions may be done within the class definition. • Member functions defined inside a class this way are created as inline functions by default. • We’ll see later that it is also possible to declare a function within a class but to define it elsewhere. • Functions defined outside the class are not normally inline.
  • 16. Defining Objects • Class only describes how they will look when they are created, just as a structure definition describes how a structure will look but doesn’t create any structure variables. • It is objects that participate in program operations. • Defining an object is similar to defining a variable of any data type • When an object of a class is created, the space for all data members defined in the class is allocated in the memory according to their data types. • An object is also known as instance. • The process of creating an object of a class is also called instantiation. • Syntax: ClassName ObjectName;
  • 17. Defining Objects: Executing Member Functions • An object of a particular class contains all data members as well as member functions defined in that class • The data members contains the value related to the object. • The functions are used to manipulate data members. • The member functions can be executed only after creating an object. • Syntax: ObjectName.memberfunction();