Classes and objects allow bundling of related data and functions. An object is an instance of a class that contains the class's data members and can access its member functions. Classes improve on procedural programming by avoiding issues when data structures change. A class defines the data types and functions for a user-defined type using a class name, data members, and member functions. Objects are instances of classes that hold separate copies of class data. Access specifiers like public, private, and protected control access to class members.
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
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
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.