SlideShare a Scribd company logo
Classes and objects
MRI 1
Key Point
 An object or class contains the data and the
functions that operate on that data. Objects are
similar to structs but contain functions, as well.
MRI 2
Limitations of Procedural Programming
• If the data structures change, many functions must
also be changed
• Programs that are based on complex function
hierarchies are:
–difficult to understand and maintain
–difficult to modify and extend
–easy to break
MRI 3
Class
 class: like a struct (allows bundling of related
variables), but variables and functions in the class can
have different properties than in a struct
 A class is a building block of OOP. It is the way to bind
the data and its logically related functions together. An
abstract data type that can be treated like any other
built in data type.
 Class definition: Class head class name_of_class.
Class body { data members; member functions; };
MRI 4
Class Features
 Class name must start with an uppercase letter. If class name is made of
more than one word, then first letter of each word must be in uppercase.
 Classes contain, data members and member functions, and the access of
these data members and variable depends on the access specifiers
(discussed in next section).
 Class's member functions can be defined inside the class definition or
outside the class definition.
 Class in C++ are similar to structures in C, the only difference being, class
defaults to private access control, where as structure defaults to public.
 All the features of OOPS, revolve around classes in C++. Inheritance,
Encapsulation, Abstraction etc.
 Objects of class holds separate copies of data members. We can create as
many objects of a class as we need.
 Classes do posses more characteristics, like we can create abstract
classes, immutable classes, all this we will study later.
MRI 5
Object
Object is an instance of a class, which holds the
data variables declared in class and the member
functions work on these class objects.
Object is an abstraction of real wold entity. Objects
are the variables/instances of classes.
MRI 6
Defining an Instance of a Class
 An object is an instance of a class
 Defined like structure variables:
Rectangle r;
 Access members using dot operator:
r.setWidth(5.2);
cout << r.getWidth();
 Compiler error if you attempt to access a private
member using dot operator
MRI 7
Classes and Objects
 A Class is like a blueprint and objects are like
houses built from the blueprint
MRI 8
Object-Oriented Programming
Terminology
 attributes: members of a class
 methods or behaviors: member functions of a class
MRI 9
More Object Terms
 data hiding: restricting access to certain members of
an object
 public interface: members of an object that are
available outside of the object. This allows the
object to provide access to some data and functions
without sharing its internal details and design, and
provides some protection from data corruption
MRI 10
Creating a Class
 Objects are created from a class
 Format:
class ClassName
{
declaration;
declaration;
};
MRI 11
Classic Class Example
MRI 12
Access Control in Classes
Access specifiers in C++ class defines the access
control rules. C++ has 3 new keywords introduced,
namely,
1. public
2. private
3. protected
These access specifiers are used to set boundaries
for availability of members of class be it data
members or member functions
MRI 13
Access Specifiers
 Used to control access to members of the class
 public: can be accessed by functions outside of
the class
 private: can only be called by or accessed by
functions that are members of the class
 Protected, is the last access specifier, and it is
similar to private, it makes class member
inaccessible outside the class. But they can be
accessed by any subclass of that class. (If class A is
inherited by class B, then class B is subclass of
class A. We will learn this later.)
MRI 14
Class Example
MRI 15
Access Specifiers
MRI 16
Private Members
Public Members
Types of Member Functions
We already know what member functions are and
what they do. Now lets study some special member
functions present in the class. Following are different
types of Member functions,
1. Simple functions
2. Static functions
3. Const functions
4. Inline functions
5. Friend functions
MRI 17
Simple Member functions
These are the basic member function, which don't
have any special keyword like static etc as prefix. All
the general member functions, which are of below
given form, are termed as simple and basic member
functions.
return_type functionName(parameter_list)
{
function body;
}
MRI 18
Static Member functions
Static is something that holds its position. Static is a
keyword which can be used with data members as
well as the member functions.
A function is made static by using static keyword with
function name. These functions work for the class as
whole rather than for a particular object of a class.
It can be called using the object and the direct
member access . operator. But, its more typical to
call a static member function by itself, using class
name and scope resolution :: operator.
MRI 19
Static Member functions
Static is a keyword in C++ used to give special
characteristics to an element. Static elements are
allocated storage only once in a program lifetime in
static storage area. And they have a scope till the
program lifetime. Static Keyword can be used with
following,
 Static variable in functions
 Static Class Objects
 Static member Variable in class
 Static Methods in class MRI 20
Static variables
 Static variables when used inside function are initialized only
once, and then they hold there value even through function
calls.
 These static variables are stored on static storage area , not
in stack.
void counter()
{
static int count=0;
cout << count++;
}
int main()
{
for(int i=0;i<5;i++)
{ counter(); } }
MRI 21
Const Member functions
Const keyword makes variables constant, that means
once defined, there values can't be changed.
When used with member function, such member
functions can never modify the object or it’s related
data members.
//Basic Syntax of const Member Function
void fun() const {}
MRI 22
Inline function
C++ inline function is powerful concept that is commonly used
with classes. If a function is inline, the compiler places a copy
of the code of that function at each point where the function
is called at compile time.
To inline a function, place the keyword inline before the
function name and define the function before any calls are
made to the function.
The compiler can ignore the inline qualifier in case defined
function is more than a line.
Some Important points about Inline
Functions
1. We must keep inline functions small, small inline functions
have better efficiency.
2. Inline functions do increase efficiency, but we should not
make all the functions inline. Because if we make large
functions inline, it may lead to code bloat, and might affect
the speed too.
3. Hence, it is adviced to define large functions outside the
class definition using scope resolution ::operator, because if
we define such functions inside class definition, then they
become inline automatically.
4. Inline functions are kept in the Symbol Table by the
compiler, and all the call for such functions is taken care at
Limitations of Inline Functions
1. Large Inline functions cause Cache misses and affect
performance negatively.
2. Compilation overhead of copying the function body
everywhere in the code on compilation, which is
negligible for small programs, but it makes a difference
in large code bases.
3. Also, if we require address of the function in program,
compiler cannot perform inlining on such functions.
Friend function
A friend function of a class is defined outside that class' scope
but it has the right to access all private and protected
members of the class. Even though the prototypes for friend
functions appear in the class definition, friends are not
member functions.
A friend can be a function, function template, or member
function, or a class or class template, in which case the entire
class and all of its members are friends.

More Related Content

PPT
Class and object in C++
PDF
Class and object
PPTX
Member Function in C++
PPT
Abstract class in java
PPTX
Friend function
PPTX
classes and objects in C++
PPTX
Inheritance in c++
PPTX
Abstract class in c++
Class and object in C++
Class and object
Member Function in C++
Abstract class in java
Friend function
classes and objects in C++
Inheritance in c++
Abstract class in c++

What's hot (20)

PPTX
07. Virtual Functions
PPT
Operator Overloading
PPTX
PPTX
Virtual base class
PDF
Constructors and destructors
PDF
Classes and objects
PPT
Function overloading(c++)
PPTX
Destructors
PPTX
Static Data Members and Member Functions
PPTX
Polymorphism in java
PPSX
Function in c
PDF
Constructors and Destructors
PPT
Inheritance C#
PPTX
Object Oriented Programming Using C++
PPTX
Friend function & friend class
PPTX
Access specifier
PPT
08 c++ Operator Overloading.ppt
PPTX
Super Keyword in Java.pptx
PPTX
User defined functions in C
PPTX
This pointer
07. Virtual Functions
Operator Overloading
Virtual base class
Constructors and destructors
Classes and objects
Function overloading(c++)
Destructors
Static Data Members and Member Functions
Polymorphism in java
Function in c
Constructors and Destructors
Inheritance C#
Object Oriented Programming Using C++
Friend function & friend class
Access specifier
08 c++ Operator Overloading.ppt
Super Keyword in Java.pptx
User defined functions in C
This pointer
Ad

Similar to Classes and objects in c++ (20)

PPT
Object and class presentation
PDF
Introduction to C++ Class & Objects. Book Notes
PPT
classandobjectunit2-150824133722-lva1-app6891.ppt
PDF
22 scheme OOPs with C++ BCS306B_module1.pdf
PPTX
Classes and objects
PPT
Classes in C++ computer language presentation.ppt
PPT
APL-2-classes and objects.ppt
PPT
APL-2-classes and objects.ppt data structures using c++
PDF
Class object
PPT
Class objects oopm
PPT
static member and static member fumctions.ppt
PPTX
OOPs & C++ UNIT 3
PPTX
Class and objects
PPTX
object oriented programming language.pptx
PPTX
Class and object
PPT
classes & objects.ppt
PPTX
object oriented programming-classes and objects.pptx
PPTX
Classes and objects
PPTX
class c++
PPTX
OOP.pptx
Object and class presentation
Introduction to C++ Class & Objects. Book Notes
classandobjectunit2-150824133722-lva1-app6891.ppt
22 scheme OOPs with C++ BCS306B_module1.pdf
Classes and objects
Classes in C++ computer language presentation.ppt
APL-2-classes and objects.ppt
APL-2-classes and objects.ppt data structures using c++
Class object
Class objects oopm
static member and static member fumctions.ppt
OOPs & C++ UNIT 3
Class and objects
object oriented programming language.pptx
Class and object
classes & objects.ppt
object oriented programming-classes and objects.pptx
Classes and objects
class c++
OOP.pptx
Ad

More from Rokonuzzaman Rony (20)

PPTX
Course outline for c programming
PPTX
PPTX
Operator Overloading & Type Conversions
PPTX
Constructors & Destructors
PPTX
Functions in c++
PPTX
Object Oriented Programming with C++
PPTX
Humanitarian task and its importance
PPTX
PPTX
PPTX
Introduction to C programming
PPTX
Constants, Variables, and Data Types
PPTX
C Programming language
PPTX
User defined functions
PPTX
Numerical Method 2
PPT
Numerical Method
PPTX
Data structures
PPT
Data structures
PPTX
Data structures
Course outline for c programming
Operator Overloading & Type Conversions
Constructors & Destructors
Functions in c++
Object Oriented Programming with C++
Humanitarian task and its importance
Introduction to C programming
Constants, Variables, and Data Types
C Programming language
User defined functions
Numerical Method 2
Numerical Method
Data structures
Data structures
Data structures

Recently uploaded (20)

PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Classroom Observation Tools for Teachers
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
master seminar digital applications in india
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
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 Đ...
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
01-Introduction-to-Information-Management.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Classroom Observation Tools for Teachers
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
TR - Agricultural Crops Production NC III.pdf
master seminar digital applications in india
FourierSeries-QuestionsWithAnswers(Part-A).pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Basic Mud Logging Guide for educational purpose
Saundersa Comprehensive Review for the NCLEX-RN Examination.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 Đ...
Renaissance Architecture: A Journey from Faith to Humanism
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
human mycosis Human fungal infections are called human mycosis..pptx
Cell Structure & Organelles in detailed.
O5-L3 Freight Transport Ops (International) V1.pdf
01-Introduction-to-Information-Management.pdf

Classes and objects in c++

  • 2. Key Point  An object or class contains the data and the functions that operate on that data. Objects are similar to structs but contain functions, as well. MRI 2
  • 3. Limitations of Procedural Programming • If the data structures change, many functions must also be changed • Programs that are based on complex function hierarchies are: –difficult to understand and maintain –difficult to modify and extend –easy to break MRI 3
  • 4. Class  class: like a struct (allows bundling of related variables), but variables and functions in the class can have different properties than in a struct  A class is a building block of OOP. It is the way to bind the data and its logically related functions together. An abstract data type that can be treated like any other built in data type.  Class definition: Class head class name_of_class. Class body { data members; member functions; }; MRI 4
  • 5. Class Features  Class name must start with an uppercase letter. If class name is made of more than one word, then first letter of each word must be in uppercase.  Classes contain, data members and member functions, and the access of these data members and variable depends on the access specifiers (discussed in next section).  Class's member functions can be defined inside the class definition or outside the class definition.  Class in C++ are similar to structures in C, the only difference being, class defaults to private access control, where as structure defaults to public.  All the features of OOPS, revolve around classes in C++. Inheritance, Encapsulation, Abstraction etc.  Objects of class holds separate copies of data members. We can create as many objects of a class as we need.  Classes do posses more characteristics, like we can create abstract classes, immutable classes, all this we will study later. MRI 5
  • 6. Object Object is an instance of a class, which holds the data variables declared in class and the member functions work on these class objects. Object is an abstraction of real wold entity. Objects are the variables/instances of classes. MRI 6
  • 7. Defining an Instance of a Class  An object is an instance of a class  Defined like structure variables: Rectangle r;  Access members using dot operator: r.setWidth(5.2); cout << r.getWidth();  Compiler error if you attempt to access a private member using dot operator MRI 7
  • 8. Classes and Objects  A Class is like a blueprint and objects are like houses built from the blueprint MRI 8
  • 9. Object-Oriented Programming Terminology  attributes: members of a class  methods or behaviors: member functions of a class MRI 9
  • 10. More Object Terms  data hiding: restricting access to certain members of an object  public interface: members of an object that are available outside of the object. This allows the object to provide access to some data and functions without sharing its internal details and design, and provides some protection from data corruption MRI 10
  • 11. Creating a Class  Objects are created from a class  Format: class ClassName { declaration; declaration; }; MRI 11
  • 13. Access Control in Classes Access specifiers in C++ class defines the access control rules. C++ has 3 new keywords introduced, namely, 1. public 2. private 3. protected These access specifiers are used to set boundaries for availability of members of class be it data members or member functions MRI 13
  • 14. Access Specifiers  Used to control access to members of the class  public: can be accessed by functions outside of the class  private: can only be called by or accessed by functions that are members of the class  Protected, is the last access specifier, and it is similar to private, it makes class member inaccessible outside the class. But they can be accessed by any subclass of that class. (If class A is inherited by class B, then class B is subclass of class A. We will learn this later.) MRI 14
  • 16. Access Specifiers MRI 16 Private Members Public Members
  • 17. Types of Member Functions We already know what member functions are and what they do. Now lets study some special member functions present in the class. Following are different types of Member functions, 1. Simple functions 2. Static functions 3. Const functions 4. Inline functions 5. Friend functions MRI 17
  • 18. Simple Member functions These are the basic member function, which don't have any special keyword like static etc as prefix. All the general member functions, which are of below given form, are termed as simple and basic member functions. return_type functionName(parameter_list) { function body; } MRI 18
  • 19. Static Member functions Static is something that holds its position. Static is a keyword which can be used with data members as well as the member functions. A function is made static by using static keyword with function name. These functions work for the class as whole rather than for a particular object of a class. It can be called using the object and the direct member access . operator. But, its more typical to call a static member function by itself, using class name and scope resolution :: operator. MRI 19
  • 20. Static Member functions Static is a keyword in C++ used to give special characteristics to an element. Static elements are allocated storage only once in a program lifetime in static storage area. And they have a scope till the program lifetime. Static Keyword can be used with following,  Static variable in functions  Static Class Objects  Static member Variable in class  Static Methods in class MRI 20
  • 21. Static variables  Static variables when used inside function are initialized only once, and then they hold there value even through function calls.  These static variables are stored on static storage area , not in stack. void counter() { static int count=0; cout << count++; } int main() { for(int i=0;i<5;i++) { counter(); } } MRI 21
  • 22. Const Member functions Const keyword makes variables constant, that means once defined, there values can't be changed. When used with member function, such member functions can never modify the object or it’s related data members. //Basic Syntax of const Member Function void fun() const {} MRI 22
  • 23. Inline function C++ inline function is powerful concept that is commonly used with classes. If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time. To inline a function, place the keyword inline before the function name and define the function before any calls are made to the function. The compiler can ignore the inline qualifier in case defined function is more than a line.
  • 24. Some Important points about Inline Functions 1. We must keep inline functions small, small inline functions have better efficiency. 2. Inline functions do increase efficiency, but we should not make all the functions inline. Because if we make large functions inline, it may lead to code bloat, and might affect the speed too. 3. Hence, it is adviced to define large functions outside the class definition using scope resolution ::operator, because if we define such functions inside class definition, then they become inline automatically. 4. Inline functions are kept in the Symbol Table by the compiler, and all the call for such functions is taken care at
  • 25. Limitations of Inline Functions 1. Large Inline functions cause Cache misses and affect performance negatively. 2. Compilation overhead of copying the function body everywhere in the code on compilation, which is negligible for small programs, but it makes a difference in large code bases. 3. Also, if we require address of the function in program, compiler cannot perform inlining on such functions.
  • 26. Friend function A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions. A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends.

Editor's Notes

  • #8: Show “Rectangle” class program.