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

What's hot

Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
Vineeta Garg
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
Dr Sukhpal Singh Gill
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
Sivakumar R D .
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
Nilesh Dalvi
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ritika Sharma
 
Principles and advantages of oop ppt
Principles and advantages of oop pptPrinciples and advantages of oop ppt
Principles and advantages of oop ppt
daxesh chauhan
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
concept of oops
concept of oopsconcept of oops
concept of oops
prince sharma
 
array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++
Arpita Patel
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
Burhan Ahmed
 
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
Venugopalavarma Raja
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
Devashish Kumar
 
Class or Object
Class or ObjectClass or Object
Class or Object
Rahul Bathri
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
Muhammad Hammad Waseem
 
Templates
TemplatesTemplates
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Paumil Patel
 
Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++
aleenaguen
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
Prof. Dr. K. Adisesha
 

What's hot (20)

Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Structure of C++ - R.D.Sivakumar
Structure of C++ - R.D.SivakumarStructure of C++ - R.D.Sivakumar
Structure of C++ - R.D.Sivakumar
 
Inheritance : Extending Classes
Inheritance : Extending ClassesInheritance : Extending Classes
Inheritance : Extending Classes
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
Principles and advantages of oop ppt
Principles and advantages of oop pptPrinciples and advantages of oop ppt
Principles and advantages of oop ppt
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
concept of oops
concept of oopsconcept of oops
concept of oops
 
array of object pointer in c++
array of object pointer in c++array of object pointer in c++
array of object pointer in c++
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
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
 
Functions in python slide share
Functions in python slide shareFunctions in python slide share
Functions in python slide share
 
Class or Object
Class or ObjectClass or Object
Class or Object
 
[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers[OOP - Lec 07] Access Specifiers
[OOP - Lec 07] Access Specifiers
 
Templates
TemplatesTemplates
Templates
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Constructor and Destructor in c++
Constructor  and Destructor in c++Constructor  and Destructor in c++
Constructor and Destructor in c++
 
Function overloading ppt
Function overloading pptFunction overloading ppt
Function overloading ppt
 

Similar to Classes and objects in c++

4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
Praveen M Jigajinni
 
APL-2-classes and objects.ppt
APL-2-classes and objects.pptAPL-2-classes and objects.ppt
APL-2-classes and objects.ppt
srividyal2
 
Class-١.pptx vbdbbdndgngngndngnnfndfnngn
Class-١.pptx vbdbbdndgngngndngnnfndfnngnClass-١.pptx vbdbbdndgngngndngnnfndfnngn
Class-١.pptx vbdbbdndgngngndngnnfndfnngn
YoussefSameh20
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
manomkpsg
 
Class and object
Class and objectClass and object
Class and object
Prof. Dr. K. Adisesha
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
Eng Teong Cheah
 
C# interview
C# interviewC# interview
C# interview
Thomson Reuters
 
Introduction to C++ Programming
Introduction to C++ ProgrammingIntroduction to C++ Programming
Introduction to C++ Programming
Preeti Kashyap
 
Class objects oopm
Class objects oopmClass objects oopm
Class objects oopm
Shweta Shah
 
Oops abap fundamental
Oops abap fundamentalOops abap fundamental
Oops abap fundamental
biswajit2015
 
Classes-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdfClasses-and-Objects-in-C++.pdf
Classes-and-Objects-in-C++.pdf
ismartshanker1
 
Abap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorialsAbap object-oriented-programming-tutorials
Abap object-oriented-programming-tutorials
cesarmendez78
 
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 questions
AniketBhandare2
 
Oops
OopsOops
C++ Object Oriented Programming
C++  Object Oriented ProgrammingC++  Object Oriented Programming
C++ Object Oriented Programming
Gamindu Udayanga
 
Friend Function
Friend FunctionFriend Function
Friend Function
Mehak Tawakley
 

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

Course outline for c programming
Course outline for c  programming Course outline for c  programming
Course outline for c programming
Rokonuzzaman Rony
 
Operator Overloading & Type Conversions
Operator Overloading & Type ConversionsOperator Overloading & Type Conversions
Operator Overloading & Type Conversions
Rokonuzzaman Rony
 
Constructors & Destructors
Constructors  & DestructorsConstructors  & Destructors
Constructors & Destructors
Rokonuzzaman Rony
 
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++
Rokonuzzaman Rony
 
Humanitarian task and its importance
Humanitarian task and its importanceHumanitarian task and its importance
Humanitarian task and its importance
Rokonuzzaman Rony
 
Pointers
 Pointers Pointers
Loops
LoopsLoops
Introduction to C programming
Introduction to C programmingIntroduction to C programming
Introduction to C programming
Rokonuzzaman Rony
 
Array
ArrayArray
Constants, Variables, and Data Types
Constants, Variables, and Data TypesConstants, Variables, and Data Types
Constants, Variables, and Data Types
Rokonuzzaman Rony
 
C Programming language
C Programming languageC Programming language
C Programming language
Rokonuzzaman Rony
 
User defined functions
User defined functionsUser defined functions
User defined functions
Rokonuzzaman Rony
 

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
 
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
 
Counting DM
Counting DMCounting DM
Counting DM
 

Recently uploaded

Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
SACHIN R KONDAGURI
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Po-Chuan Chen
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 

Recently uploaded (20)

Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
"Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe..."Protectable subject matters, Protection in biotechnology, Protection of othe...
"Protectable subject matters, Protection in biotechnology, Protection of othe...
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdfAdversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
Adversarial Attention Modeling for Multi-dimensional Emotion Regression.pdf
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 

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.