SlideShare a Scribd company logo
1 of 26
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

What's hot

Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Michelle Anne Meralpis
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vishal Patil
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingMoutaz Haddara
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programmingSachin Sharma
 
Characteristics of OOPS
Characteristics of OOPS Characteristics of OOPS
Characteristics of OOPS abhishek kumar
 
Classes and objects
Classes and objectsClasses and objects
Classes and objectsNilesh Dalvi
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS ConceptBoopathi K
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functionsHarsh Patel
 
Inline function
Inline functionInline function
Inline functionTech_MX
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend classAbhishek Wadhwa
 
Introduction to oop
Introduction to oop Introduction to oop
Introduction to oop Kumar
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++Muhammad Waqas
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member FunctionsMOHIT AGARWAL
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVAAnkita Totala
 
Friend functions
Friend functions Friend functions
Friend functions Megha Singh
 
Friend function
Friend functionFriend function
Friend functionzindadili
 
Virtual base class
Virtual base classVirtual base class
Virtual base classTech_MX
 

What's hot (20)

Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programming
 
Characteristics of OOPS
Characteristics of OOPS Characteristics of OOPS
Characteristics of OOPS
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
C++ OOPS Concept
C++ OOPS ConceptC++ OOPS Concept
C++ OOPS Concept
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
Inline function
Inline functionInline function
Inline function
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
Friend function & friend class
Friend function & friend classFriend function & friend class
Friend function & friend class
 
Introduction to oop
Introduction to oop Introduction to oop
Introduction to oop
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
 
Friend functions
Friend functions Friend functions
Friend functions
 
Friend function
Friend functionFriend function
Friend function
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 

Similar to Classes and objects in c++

APL-2-classes and objects.ppt
APL-2-classes and objects.pptAPL-2-classes and objects.ppt
APL-2-classes and objects.pptsrividyal2
 
Class-١.pptx vbdbbdndgngngndngnnfndfnngn
Class-١.pptx vbdbbdndgngngndngnnfndfnngnClass-١.pptx vbdbbdndgngngndngnnfndfnngn
Class-١.pptx vbdbbdndgngngndngnnfndfnngnYoussefSameh20
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptmanomkpsg
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceEng Teong Cheah
 
Introduction to C++ Programming
Introduction to C++ ProgrammingIntroduction to C++ Programming
Introduction to C++ ProgrammingPreeti Kashyap
 
Class objects oopm
Class objects oopmClass objects oopm
Class objects oopmShweta Shah
 
Oops abap fundamental
Oops abap fundamentalOops abap fundamental
Oops abap fundamentalbiswajit2015
 
Classes-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfClasses-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfismartshanker1
 
Abap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorialsAbap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorialscesarmendez78
 
Object oriented basics
Object oriented basicsObject oriented basics
Object oriented basicsvamshimahi
 
Tcs NQTExam technical questions
Tcs NQTExam technical questionsTcs NQTExam technical questions
Tcs NQTExam technical questionsAniketBhandare2
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented ProgrammingGamindu Udayanga
 

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

4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
APL-2-classes and objects.ppt
APL-2-classes and objects.pptAPL-2-classes and objects.ppt
APL-2-classes and objects.ppt
 
Class-١.pptx vbdbbdndgngngndngnnfndfnngn
Class-١.pptx vbdbbdndgngngndngnnfndfnngnClass-١.pptx vbdbbdndgngngndngnnfndfnngn
Class-١.pptx vbdbbdndgngngndngnnfndfnngn
 
My c++
My c++My c++
My c++
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
 
Class and object
Class and objectClass and object
Class and object
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
 
C# interview
C# interviewC# interview
C# interview
 
Introduction to C++ Programming
Introduction to C++ ProgrammingIntroduction to C++ Programming
Introduction to C++ Programming
 
Class objects oopm
Class objects oopmClass objects oopm
Class objects oopm
 
Oops abap fundamental
Oops abap fundamentalOops abap fundamental
Oops abap fundamental
 
Classes-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfClasses-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdf
 
Abap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorialsAbap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorials
 
Object oriented basics
Object oriented basicsObject oriented basics
Object oriented basics
 
Tcs NQTExam technical questions
Tcs NQTExam technical questionsTcs NQTExam technical questions
Tcs NQTExam technical questions
 
Oops
OopsOops
Oops
 
Oops
OopsOops
Oops
 
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented Programming
 
Friend Function
Friend FunctionFriend Function
Friend Function
 

More from Rokonuzzaman Rony (20)

Course outline for c programming
Course outline for c  programming Course outline for c  programming
Course outline for c programming
 
Pointer
PointerPointer
Pointer
 
Operator Overloading & Type Conversions
Operator Overloading & Type ConversionsOperator Overloading & Type Conversions
Operator Overloading & Type Conversions
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++
 
Humanitarian task and its importance
Humanitarian task and its importanceHumanitarian task and its importance
Humanitarian task and its importance
 
Structure
StructureStructure
Structure
 
Pointers
 Pointers Pointers
Pointers
 
Loops
LoopsLoops
Loops
 
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
 
Array
ArrayArray
Array
 
Constants, Variables, and Data Types
Constants, Variables, and Data TypesConstants, Variables, and Data Types
Constants, Variables, and Data Types
 
C Programming language
C Programming languageC Programming language
C Programming language
 
User defined functions
User defined functionsUser defined functions
User defined functions
 
Numerical Method 2
Numerical Method 2Numerical Method 2
Numerical Method 2
 
Numerical Method
Numerical Method Numerical Method
Numerical Method
 
Data structures
Data structuresData structures
Data structures
 
Data structures
Data structures Data structures
Data structures
 
Data structures
Data structures Data structures
Data structures
 

Recently uploaded

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 

Recently uploaded (20)

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.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

  1. Show “Rectangle” class program.