SlideShare uma empresa Scribd logo
1 de 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.

Mais conteúdo relacionado

Mais procurados

Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member FunctionsMOHIT AGARWAL
 
class and objects
class and objectsclass and objects
class and objectsPayel Guria
 
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
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and DestructorKamal Acharya
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++Bhavik Vashi
 
Friend function
Friend functionFriend function
Friend functionzindadili
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functionsMarlom46
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member FunctionsMuhammad Hammad Waseem
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).pptAlok Kumar
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in JavaKurapati Vishwak
 
Access specifier
Access specifierAccess specifier
Access specifierzindadili
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorialsMayank Jain
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
Characteristics of OOPS
Characteristics of OOPS Characteristics of OOPS
Characteristics of OOPS abhishek kumar
 

Mais procurados (20)

Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
class and objects
class and objectsclass and objects
class and objects
 
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
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Class and object
Class and objectClass and object
Class and object
 
inheritance
inheritanceinheritance
inheritance
 
Types of Constructor in C++
Types of Constructor in C++Types of Constructor in C++
Types of Constructor in C++
 
C++ classes
C++ classesC++ classes
C++ classes
 
Friend function
Friend functionFriend function
Friend function
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
 
Access specifier
Access specifierAccess specifier
Access specifier
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Characteristics of OOPS
Characteristics of OOPS Characteristics of OOPS
Characteristics of OOPS
 

Semelhante a 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
 
+2 CS class and objects
+2 CS class and objects+2 CS class and objects
+2 CS class and objectskhaliledapal
 

Semelhante a 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
 
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
 
+2 CS class and objects
+2 CS class and objects+2 CS class and objects
+2 CS class and objects
 

Mais de 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
 

Último

80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 

Último (20)

80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 

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.

Notas do Editor

  1. Show “Rectangle” class program.