CE144
OBJECT ORIENTED PROGRAMMING
WITH C++
UNIT-5
Classes and objects
N. A. Shaikh
nishatshaikh.it@charusat.ac.in
Topics to be covered
Limitation of C structure Static data members
Declaring class and defining member Static Member functions
function
Arrays of Objects
Making outside function inline
Object as a function argument
Nesting member function
Returning objects
Private member function
Friend functions
Arrays within a class
const Member functions
Memory allocation of objects
Unit 5: Classes and objects Prepared By: Nishat Shaikh
2
Introduction
The most important feature of C++ is the “class”
A class is an extension of the idea of structure used in C
It is a new way of creating and implementing a user-
defined data type.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
3
C Structure Revisited
C Structure provide a method for packing together data
of different data types.
It is user defined data type.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
4
Limitation of C Structure
1. The C structure does not allow the struct data type to be
treated like built-in data types
We cannot use operators like +,- etc. on Structure
variables
Unit 5: Classes and objects Prepared By: Nishat Shaikh
5
Limitation of C Structure
2. No Data Hiding
Structure members can be directly accessed by the
structure variables by any function anywhere in their
scope.
In other words, the structure members are public
members.
3. Functions inside Structure
C structures do not permit functions inside Structure
4. Static Members
C Structures cannot have static members inside their
body
Unit 5: Classes and objects Prepared By: Nishat Shaikh
6
Limitation of C Structure
5. Access Modifiers
C Programming language do not support access
modifiers. So they cannot be used in C Structures.
6. Construction creation in Structure
Structures in C cannot have constructor inside
Structures.
7. Direct Initialization
We cannot directly initialize structure data members
in C
8. Using struct keyword
In C, we need to use struct to declare a struct
variable
Unit 5: Classes and objects Prepared By: Nishat Shaikh
7
Limitation of C Structure
9. sizeof operator
This operator will generate 0 for an empty structure
in C whereas 1 for an empty structure in C++.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
8
C++ Structure
C++ supports all the features of structure as defined in C
But C++ has expanded its capabilities further to suit its
OOP philosophy.
1. It attempts to bring the user-defined types as close
as possible to the built-in data types
o Keyword struct can be omitted in the declaration
of structure variables and can be used like any
other type names.
2. Provides a facility to hide the data(using private
declaration) which is one of the main principle of
OOP.
3. In C++, a structure can have both data members and
functions.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
9
C++ Structure
Unit 5: Classes and objects Prepared By: Nishat Shaikh
10
C++ Structure
Create a C++ Structure Weight having private data
members: float kg, grams. A member function getvalue ()
should enter their values. Another member function
putvalue () should display their values.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
11
C++ class
C++ incorporates all these extensions in another user-
defined type known as class.
The only difference between a structure and a class in
C++ is that,
By default, the members of a class are private
By default, the members of a structure are public.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
12
Specifying a Class
A class is a way to bind the data and its associated
functions together(encapsulation).
It allows the data(and function) to be hidden, if
necessary, from external use.
A class specification has two parts:
1. Class declaration
2. Class function definitions
Unit 5: Classes and objects Prepared By: Nishat Shaikh
13
Class declaration
Syntax:
Unit 5: Classes and objects Prepared By: Nishat Shaikh
14
Class declaration
Example:
Representation of a class
Unit 5: Classes and objects Prepared By: Nishat Shaikh
15
Creating Objects
Once a class has been declared, we can create
variables(objects) of that type by using the class
name(like any other built-in type variable)
Unit 5: Classes and objects Prepared By: Nishat Shaikh
16
Accessing Class Members
Syntax:
Example:
Unit 5: Classes and objects Prepared By: Nishat Shaikh
17
Defining Member Functions
Member functions can be defined in two places:
o Outside the class definition
o Inside the class definition
Unit 5: Classes and objects Prepared By: Nishat Shaikh
18
Outside the class definition
Syntax:
The membership label (class-name::) tells the compiler
that the function function-name belongs to the class
class-name.
That is, the scope of the function is restricted to the
class-name specified in the header line.
Example:
Unit 5: Classes and objects Prepared By: Nishat Shaikh
19
Outside the class definition
Characteristics of Member Function:
Several different classes can use the same function
name. The ‘membership label’ will resolve their scope.
Member functions can access the private data of the
class. A non-member function cannot do so.(However,
an exception to this rule is a friend function)
A member function can call another member function
directly, without using the dot operator.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
20
Inside the class definition
Replace the function declaration by the actual function
definition inside the class.
NOTE:
Function defined inside a
class is treated as an
inline function.
All restrictions that apply
to an inline function are
also applicable here.
Normally, only small
functions are defined
inside the class definition
Unit 5: Classes and objects Prepared By: Nishat Shaikh
21
Unit 5: Classes and objects Prepared By: Nishat Shaikh
22
Unit 5: Classes and objects Prepared By: Nishat Shaikh
23
Making an outside Function Inline
One of the objective of OOP is to separate the details of
implementation from the class definition.
It is therefore good practice to define the member
functions outside the class.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
24
C++ Class
Create a C++ Class Weight having private data members:
float kg, grams. A member function getvalue () should enter
their values. Another member function putvalue () should
display their values. Define both functions inside the class.
Member function defined inside the class behaves like an
inline function. Illustrate the difference between C++
Structure and C++ Class.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
25
Practical 13: C++ struct and class
Define a C++ Structure
Rectangle with data
member’s width and
height. It has get_values()
member functions to get
the data from user and
area() member functions to
print the area of rectangle.
Also create a C++ Class for
the above program. Define
both functions inside the
class. Member function
defined inside the class
behaves like an inline
function and illustrate the
difference between C++
Structure and C++ Class.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
26
Nesting of member function
A member function can be called by using its name
inside another member function of the same class.
This is known as nesting of member functions.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
27
Private Member Functions
Although it is normal practice to place all the data items
in a private section and all the functions in public, some
situations may require certain functions to be hidden
(like private data) from the outside calls.
Tasks such as deleting an account in a customer file, or
providing increment to an employee are events of
serious consequences and therefore the functions
handling such tasks should have restricted access.
We can place these functions in the private section.
A private member function can only be called by
another function that is n member of its class.
Even an object cannot invoke a private function using the
dot operator.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
28
Private Member Functions
Unit 5: Classes and objects Prepared By: Nishat Shaikh
29
Practical 14:Private Member Functions
Write a C++ program having class Batsman. It has private data
members: batsman name, bcode (4 Digit Code Number), innings, not
out, runs, batting average. Innings, not out and runs are in integer and
batting average is in float.
Define following function outside the class using scope resolution
operator.
1. Public member function getdata()to read values of data members.
2. Public member function putdata()to display values of data
members.
3. Private member function calcavg() which calculates the batting
average of a batsman. Also make this outside function inline.
Hint : batting average = runs/(innings - notout)
Unit 5: Classes and objects Prepared By: Nishat Shaikh
30
Practical 14:Private Member Functions
Unit 5: Classes and objects Prepared By: Nishat Shaikh
31
Practical 14:Private Member Functions
Unit 5: Classes and objects Prepared By: Nishat Shaikh
32
Arrays within a Class
The arrays can be used as member variables in a class.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
33
Arrays within a Class
Unit 5: Classes and objects Prepared By: Nishat Shaikh
34
Arrays within a Class
WAP using class to process Shopping List for a Departmental
store. The list includes details such as the Code No and
price of each item and perform the operations like Adding,
Deleting items to the list and Printing the Total value of a
Order.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
35
Arrays within a Class
Unit 5: Classes and objects Prepared By: Nishat Shaikh
36
Arrays within a Class
Unit 5: Classes and objects Prepared By: Nishat Shaikh
37
Arrays within a Class
Unit 5: Classes and objects Prepared By: Nishat Shaikh
38
Memory Allocation for objects
“Memory is allocated when objects are declared and not
when the class is specified”
Above statement is partly true.
Since all the objects belonging to that class use the same
member functions, no separate space is allocated for
member functions when the objects are created.
Member functions are created and placed in the
memory space only once when they are defined.
As member variables will hold different data values for
different objects, space for member variables is allocated
separately for each object.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
39
Memory Allocation for objects
Common for all objects
Memory created when
functions defined
Separate for all objects
Memory created when
objects defined
Unit 5: Classes and objects Prepared By: Nishat Shaikh
40
Static Data Members
A data member of a class can be qualified as static.
Static data member has certain special characteristics:
o It is initialized to zero when the first object of its class
is created. No other initialization is permitted.
o Only one copy of that member is created for the
entire class and is shared by all the objects of that
class, no matter how many objects are created.
o It is visible only within the class, but its lifetime is the
entire program.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
41
Static Data Members
A static variable is normally used to maintain value
common to the entire class.
o For e.g, to hold the count of objects created.
Note that the type and scope of each static member
variable must be declared outside the class definition.
This is necessary because the static data members are
stored separately rather than as a part of an object.
While defining a static variable, some initial value can
also be assigned
Since they are associated with the class itself rather than
with any class objects, they are also known as class
variables.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
42
Static Data Members
Unit 5: Classes and objects Prepared By: Nishat Shaikh
43
Static Data Members
Unit 5: Classes and objects Prepared By: Nishat Shaikh
44
Static Member Functions
Like a static member variable, we can also have static
member functions.
Static member function has certain special
characteristics:
o A static function can have access to only other static
members (function or variable) declared in the same
class.
o A static member function can be called using the
class name (instead of its object) as follows:
Unit 5: Classes and objects Prepared By: Nishat Shaikh
45
Static Member Functions
Unit 5: Classes and objects Prepared By: Nishat Shaikh
46
Array of Objects
An array can be of any data type including struct.
Similarly, we can also have array of variables of class type
Such variables are called arrays of objects.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
47
Array of Objects
manager[0] manager[1] manager[2]
char name[30]; char name[30]; char name[30];
float age; float age; float age;
void getdata();
void putdata();
Unit 5: Classes and objects Prepared By: Nishat Shaikh
48
Array of Objects
Unit 5: Classes and objects Prepared By: Nishat Shaikh
49
Array of Objects
Define class Mobile having data members: year of manufacture,
price. A class has member functions to get and print data. Define
member function validity ( ) that checks whether the mobile is
older than 2005 or not. If mobile is older, it is invalid. Input data
for 4 mobiles in main ( ) and display information about the
phone which is invalid. Use the concept of Array of Objects.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
50
Practical 17
Create a Class Gate for students appearing in Gate (Graduate Aptitude
test for Engineering) exam. There are three examination center
Vadodara, Surat, and Ahmedabad where Gate exams are conducted. A
class has data members: Registration number, Name of student,
Examination center. Class also Contains static data member ECV_Cnt,
ECS_Cnt and ECA_Cnt which counts the number of students in
Vadodara, Surat and Ahmedabad exam center respectively. Class
Contains two Member function getdata () which gets all information of
students and counts total students in each exam center and pudata ()
which prints all information about the students. Class also contains
one static member function getcount () which displays the total
number of students in each examination center. Write a program for 5
students and display the total number of students in each examination
center. Use static data member, static member function and Array of
Objects.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
51
Practical 17
Unit 5: Classes and objects Prepared By: Nishat Shaikh
52
Practical 17
Unit 5: Classes and objects Prepared By: Nishat Shaikh
53
Objects as Function Arguments
Like any other data type, an object may be used as a
function argument.
This can be done in two ways:
1. Pass/Call By Value
o A copy of the entire object is passed to the
function
o Any changes made to object inside function don’t
affect original object
2. Pass/Call By Reference
o Only the address of the object is transferred to
the function.
o Any changes made to object inside function are
reflected in the original object
Unit 5: Classes and objects Prepared By: Nishat Shaikh
54
Objects as Function Arguments:Ex-1
Unit 5: Classes and objects Prepared By: Nishat Shaikh
55
Objects as Function Arguments:Ex-2
Unit 5: Classes and objects Prepared By: Nishat Shaikh
56
Objects as Function Arguments:Ex-2
Unit 5: Classes and objects Prepared By: Nishat Shaikh
57
Objects as Function Arguments:Ex-3
Define class Digit having int ‘n’ as data member. Define member
function enter() to enter the data and show() to print the data. A
class has member function compare() that displays whether the
first object is smaller, greater or same as compared to second
object. (Function compare() should support: int x =
d1.compare(d2); where d1 and d2 are objects of class Digit). Use
Concept of Object as Function Arguments.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
58
Objects as Function Arguments:Ex-3
Unit 5: Classes and objects Prepared By: Nishat Shaikh
59
Practical 16
Define a class Dist with int feet and float inches. Define member
function that displays distance in 1’-2.5” format. Also define
member function scale ( ) function that takes object by reference
and scale factor in float as an input argument. The function will
scale the distance accordingly. For example, 20’-5.5” and Scale
Factor is 0.5 then answer is 10’-2.75”
Unit 5: Classes and objects Prepared By: Nishat Shaikh
60
Practical 16
Unit 5: Classes and objects Prepared By: Nishat Shaikh
61
Objects as Function Arguments
Write a C++ program having class Dist with private data member
int feet and float inches. Define following public member
functions for it.
1) getdata( ) to take feet and inches as input
2) putdata( ) to display distance in 1’2.5” format
3) add( ) to do addition of two distances such that it can handle
function call d1.add(d2) where d1, d2 and d3 are objects of
class. Use Concept of Object as Function Arguments.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
62
Function Returning Object
A function cannot only receive objects as arguments but also can return them.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
63
Practical 15
Define class Currency having two integer data members rupee and
paisa. A class has member functions enter() to get the data and
show() to print the amount in 22.50 format. Define one member
function sum() that adds two objects of the class and stores answer in
the third object i.e. c3=c1.sum (c2). The second member function
should add two objects of type currency passed as arguments such
that it supports c3.add(c1,c2); where c1, c2 and c3 are objects of class
Currency. Also Validate your answer if paisa >100. Write a main(
)program to test all the functions. Use concepts of Object as Function
Arguments, function returning object and function overloading.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
64
Practical 15
Unit 5: Classes and objects Prepared By: Nishat Shaikh
65
Friend Function
We have seen that the private members cannot be
accessed from outside the class(non-member function)
There could be a situation when we want our private
data to be shared by a non member function.
Then, we declare something as a friend, you give it
access to your private data members.
Single functions or entire classes may be declared as
friends of a class.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
66
Friend Function
To make a non-member function friendly to a class, we
have to simply declare this function as a friend of the
class
Syntax
Unit 5: Classes and objects Prepared By: Nishat Shaikh
67
Friend Function
A friend function is a non-member function of the class
that has been granted access to all private members of
the class.
The function declaration should be preceded by the
keyword friend.
Definition of friend function is specified outside the
class body and is not treated as a part of class.
Function definition must not use keyword friend nor
scope operator ::
The major difference between member function and
friend function is that the member function is accessed
through the object while friend function requires object
to be passed as parameter.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
68
Friend Function
A friend function possesses certain special characteristics:
It is not in the scope of the class to which it has been
declared as friend.
Since it is not in the scope of the class, it cannot be called
using the object of that class.
It can be invoked like a normal function without he help of
any object.
Unlike member functions, it cannot access the member
names directly and has to use an object name and dot
membership operator with each member name (e.g. A.x).
It can be declared in either the public or the private part of
a class without affecting its meaning.
Usually, it has the objects as arguments
Unit 5: Classes and objects Prepared By: Nishat Shaikh
69
A Function Friendly To One Class
Unit 5: Classes and objects Prepared By: Nishat Shaikh
70
Practical 19: Friend Function
Create a Class Date having data members: int dd, mm, yyyy.
Class has one member function to input the dates and another
member function which prints the dates. Write a main() function
which takes two dates as input. Write a friend function
swapdates() which takes two objects by reference of type Date
and swaps both the dates. Use the concept of Friend function
which takes objects by reference
Unit 5: Classes and objects Prepared By: Nishat Shaikh
71
Practical 19: Friend Function
Unit 5: Classes and objects Prepared By: Nishat Shaikh
72
A Function Friendly To Two Classes: By Value
Unit 5: Classes and objects Prepared By: Nishat Shaikh
73
A Function Friendly To Two Classes: By Reference
Unit 5: Classes and objects Prepared By: Nishat Shaikh
74
Practical 18: Friend Function
Define a class Fahrenheit with float temp as data member.
Define another class Celsius with float temperature as data
member. Both classes have member functions to input and print
data. Write a non-member function that receives objects of
both the classes and declare which one is higher than another
according to their values. Also define main() to test the function.
Define all member functions outside the class. (Formula for
converting Celsius to Fahrenheit is F = (9C/5) + 32). Use the
concept of friend function.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
75
Practical 18: Friend Function
Unit 5: Classes and objects Prepared By: Nishat Shaikh
76
Practical 18: Friend Function
Unit 5: Classes and objects Prepared By: Nishat Shaikh
77
Friend Function
Create a class Corporate which contains name of employee and
salary in float. Create another class Education which contains
name of employee and salary in float. Both the class have
member function to get and print data. Write a non-member
function compare() which receives object of both the classes
and compares the salary of employee in education and
corporate field. Use the concept of friend function.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
78
Member function of one class,
friend function of another class
Member functions of one class can be friend function of
another class.
In such cases, they are defined using the scope
resolution operator
NOTE:
Class containing member function should be defined first
Unit 5: Classes and objects Prepared By: Nishat Shaikh
79
Member function of one class,
friend function of another class
Create a class Customer having data members: name of the
customer and customer number in integer and member
function to get customer data. Create another class
Manager having data members: name of manager and
employee id in integer and member function to get
managers data. Class Manager also have member function
get_cust_data () which takes objects of class Customer as
input and prints the customers details and is a friend
function of class Customer . Write a main () function to test
all this function. Use the concepts of Member function of
one class can be a Friend Function of another class.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
80
Member function of one class,
friend function of another class
Unit 5: Classes and objects Prepared By: Nishat Shaikh
81
Member function of one class,
friend function of another class
Unit 5: Classes and objects Prepared By: Nishat Shaikh
82
Practical 20: Friend Function
Create a class LAND having data members: length, width, area1.
Write member functions to read and display the data of land.
Also, calculates the area of the land. Create another class TILES
having data members: l, w, area2. Write a member function to
get the data of tile. Calculate the area of one tile. Class TILE has a
member function named number_of_tiles() which is a friend of
class LAND and takes the object of class LAND by reference
which calculates the number of tiles which can be put over the
land area. Write the main function to test all the functions. Use
the concept of member function of one class can be a friend
function of another class.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
83
Practical 20: Friend Function
Unit 5: Classes and objects Prepared By: Nishat Shaikh
84
Practical 20: Friend Function
Unit 5: Classes and objects Prepared By: Nishat Shaikh
85
Friend Class
We can also declare all the member functions of one class as
the friend functions of another class.
In such cases , the class is called a friend class.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
86
Practical 21: Friend class
Create a class Child having data members: name of the child and
gender and a member function to get and print child data.
Create another class Parent which is a friend class of child class.
Class Parent have member function ReadChildData() which takes
child’s object by reference as input argument and Reads the
childs data and DisplayChildData() which takes childs object as
argument and displays childs data. Use the concepts of Friend
Class.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
87
Practical 21: Friend class
Unit 5: Classes and objects Prepared By: Nishat Shaikh
88
Practical 21: Friend class
Unit 5: Classes and objects Prepared By: Nishat Shaikh
89
const Member Functions
If a member function does not alter any data in the class,
then we may declare it as const member function
Syntax:
NOTE:
The qualifier const is appended to the function
declarations well as definition.
The compiler will generate an error message if such
functions try to alter the data values.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
90
const Member Functions
Unit 5: Classes and objects Prepared By: Nishat Shaikh
91
Practical 22.1: const member functions
Unit 5: Classes and objects Prepared By: Nishat Shaikh
92
Pointer to Member(data member & member function)
One way to access the data members and member
function of a class is through the object of the class with
a dot operator.
C++ also provides us a few operators through we could
access the data members and member functions of a class
by using pointers.
These operators are known as dereferencing operators.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
93
Pointer to Member(data member & member function)
Derefencing Description
Operators
::* This operator allows us to create a pointer to a class
member, which could be data member or member
function.
->* This operator uses the pointer to the member of a class
and a pointer to the object of the same class, to access
the member of a class.
.* This operator uses the pointer to the member of a class
and an object of the same class, to access member of a
class.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
94
Pointer to Member(data member & member function)
Unit 5: Classes and objects Prepared By: Nishat Shaikh
95
Pointer to data member
Syntax of ::* dereferencing operator:
Example of ::* dereferencing operator:
Syntax of .* dereferencing operator:
Example of .* dereferencing operator:
Syntax of ->* dereferencing operator:
Example of ->* dereferencing operator:
Unit 5: Classes and objects Prepared By: Nishat Shaikh
96
Unit 5: Classes and objects Prepared By: Nishat Shaikh
97
Practical 22.2(a): Pointer to data members
Reason for Error:
1. The dereferencing operator ->* is used to access a member when
we use pointers to both the object and the member.
2. The dereferencing operator .* is used when the object itself is
used with the member pointer.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
98
Pointer to member function
Syntax of ::* dereferencing operator:
Example of ::* dereferencing operator:
Syntax of .* dereferencing operator:
Example of .* dereferencing operator:
Syntax of ->* dereferencing operator:
Example of ->* dereferencing operator:
Unit 5: Classes and objects Prepared By: Nishat Shaikh
99
Unit 5: Classes and objects Prepared By: Nishat Shaikh
10
0
Practical 22.2(b): Pointer to data members
Reason for Error:
1. The dereferencing operator ->* is used to access a member
when we use pointers to both the object and the member.
2. The dereferencing operator .* is used when the object itself is
used with the member pointer.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
10
1
Local Classes
Classes can be defined and used inside a function or a
block. Such classes are called local classes.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
10
2
Local Classes
Following are some interesting facts about local classes:
1. A local class type name can only be used in the enclosing
function.
2. All the methods of Local classes must be defined inside the class
only.
3. A Local class cannot contain static data members. It may contain
static functions though
4. Member methods of local class can only access static and enum
variables of the enclosing function. Non-static variables of the
enclosing function are not accessible inside local classes
5. Local classes can access global types(declared above the
function), variables and functions. Also, local classes can access
other local classes of same function.
https://www.geeksforgeeks.org/local-class-in-c/
Unit 5: Classes and objects Prepared By: Nishat Shaikh
10
3
Local Classes
Following are some restrictions on local classes:
A local class cannot have static data members
Member functions of a local class have to be defined
entirely inside the class body
Enclosing function cannot access the private members of
a local class.
Local classes cannot have friend templates
Local classes cannot define friend functions inside the
class definition
Unit 5: Classes and objects Prepared By: Nishat Shaikh
10
4
Practical 22.3: Local Classes
Reason for Error:
Local classes have some restriction:
1. Local classes cannot have static data member
2. Member function must be defined inside the local class.
Unit 5: Classes and objects Prepared By: Nishat Shaikh
10
5
End of Unit-5