2. Scope Resolution Operator
Scope resolution operator is also used define and
initialize the static data members of a class.
The syntax for accessing a global variable using
the scope resolution operator is as follows:
: : GlobalVariableName
3. Local & Global Class n Objects
Local Global
1. Class definition occurs inside 1.Class definition
the function body. occurs outside the
bodies of all functions
2. Objects of this class can be in the program.
Classes created only within the function.
2.Objects of this class
can be declared from
anywhere in the
program.
1.Objects declared within the 1.Objects declared
function. outside all the
function.
2.The objects are locally available
Objects 2.The objects are
to the function. That is, they
cannot be used outside the globally available to all
function. the function in the
program.
4. Array of Objects
It is a collection of objects of the same class type
referenced under common name. It is declared with
the usual syntax:
className ArrayName [Size] ;
A member of the class in accessed by specifying the
object followed by the dot operator and member name
as follows;
ArrayName [Subcript]. memberName
5. Array of Objects
subcriber ob [50] ;
for (int i = 0; i < 50 ; i + +)
ob [i]. read_data ( ) ;
for (int i = 0; i < 50 ; i + +)
ob [i]. print_data ( ) ;
6. Objects as Function Arguments
It is possible to have functions which object of classes as
arguments, just as there are functions which accept other
variable as arguments.
Like any other data type, an object can be passed as an argument
to the function by the following ways.
Pass-by value, a copy of the entire object is passed to the
function.
Pass-by-reference, only the address of the object is passed
implicitly to the function.
In the case of pass-by-value, a copy of the object is passed to the
function and any modification made to the object inside the
function is not reflected in the object used to call the function.
But in pass-by-reference the address of an object is passed to the
function and any changes made to the object inside the function
are reflected in the actual object.
8. Returning Objects from Functions
Even functions can return reference to the object if the
function header has the format:
ClassName & FunctionName (ArgumentList)
Examole: complex complex : : add (complex &dc)
{
complex temp ;
temp.real=real+dc.real ;
temp.imag=imag=iamg+dc.imag ;
return temp ;
}
9. Memory Allocation for Class and Objects
When an object of a particular class is created, memory is
allocated to both its data members and member function.
Member functions are created and stored in memory only
when a class specification is completed.
All objects of that class have access to the same area in the
memory where the member functions are stored.
It is also logically true that the member functions are same
for all the objects.
However, separate storage is allocated for every object’s
data members since they contain different values.
It allows different objects to handle their data in a manner
that suits them.
11. Types of Functions in Classes
Inline Function
Friend Function
Constant Member Function
Static Member Functions
12. Inline Functions
Inline function, the definition which begins with the
keyword inline
The inline functions are C++ enhancement designed to speed
up the execution of programs.
An inline member function is treated like a macro; any call to
this function in a program is replaced by the function itself.
This is called inline expansion.
inline ReturnType ClassName : : FunctionName (Arguments)
{
Body_of Function
}
13. Inline Functions
If there is an inline function is the program the
compiler replaces the function calling statement by
the function code itself and compiles the entire code
so that the program looks like a single function, which
is stored in some other memory location and then
jump back to the calling function.
The disadvantage is in the case of memory wastage.
That is, if an inline function is called ten times, there
will be ten copies of the function code, which makes
the program large in size and requires more memory.
14. Inline Functions
For functions that return and are having a loop or a
switch or a go to.
For functions not returning values, if a return
statement exists.
For functions containing static variables.
For recursive functions.
15. Friend Function
It is required to allow functions outside a class to
access and manipulate the private members of the class.
In C++, this is achieved by using the concept of friends.
The friends come in three forms: friend functions,
member functions of other classes as friend, and friend
class.
Friend function is a non member function that is granted
access to the private and protected members of a class.
Now let us brief features of friend functions as follows:
A function may be declared friend of more than one
class.
It does not have the class scope; rather it depends upon
its original declaration and definition.
16. Friend Function
A friend function cannot be called using the object of
that class; it is not in the scope of a class.
It can be invoked like a normal function. That is, it
does not require that dot operator prefixed by the
object.
Since it is not a member function, it cannot access the
class members directly but has to use the object name
as the parameter and the membership operator (.)
with each member name.
It can be declared privately or publicly in the class
without affecting its meaning.
17. Member Function of Class as Friend of another Class
Consider the following class definitions :
class PQR ; / / Prototyping of class PQR
class ABC / / Definition of class ABC
{ int a ;
float b:
public :
void abcprint(PQR) : / / friend of
PQR
class PQR
{ int p ;
float q ;
public :
void pqrfun ( ) ;
friend void ABC : : abcprintint (PQR) ;
/ / prototype of friend
18. Friend Class
friend FormerClassName ;
The above class definitions can be modified as follows :
class PQR ; / / Prototyping of class PQR is essential
class ABC
{ int a ;
flaot b ;
public :
int abcsum (PQR obl)
{
return (a + obl.p) ; / / private member of PQR is accessed by
function of ABC
}
in abcdif ( )
{
PQR ob2 ;
return (b - ob2.q) ;
}
};
Class PQR
{
int p ;
float q ;
public :
void pqrfun ( ) ;
friend ABC ; / / ABC is declared as friend of PQR
};
Friend class is one, all the member functions of which are friends another class.
19. Friends as Bridge of Classes
Consider the following definitions:
class PQR ; / / Prototyping of the class PQR
class ABC
{ int a ;
float b :
public :
int r ;
void pqrfun ( ) ;
friend void com_fun (ABC, PQR) ;
};
void com_ fun (ABC obl, PQR ob2)
{
cout<<obl.a * obl.b + ob2.p * ob2.q ;
}
20. Uses of Friend Functions
Friend functions are useful in the following situations:
Function operating on objects of two different classes.
This is the ideal situation where the friend function
can be used to bridge two classes.
Friend functions can be used to increase the versatility
of overloaded operators.
Sometimes, a friend allows a more obvious syntax for
calling a function, rather than what a member
function can do.
21. Static Members of Class
The declaration of such members begins with the keyword static.
Static Data Members
Only one copy of such member will be maintained in the memory for
the entire class, which will be shared by all the objects of that class.
They are visible only within the class; however their lifetime is the
entire program.
The static data members declared as private can be a accessed within
class by the static member function. (i.e; they are hidden from outside
the class).
The static data members declared as public can be accessed outside the
class definition by using the following syntax:
ClassName : : StaticDataMember = Value ;
Or
ObjectName. StaticDataMember = Value ;
22. Static Member Functions
The following are the features of static member functions.
They can access only the static data members of the same
class. (That, is; non-static data members are unavailable to
these functions)
Only static member functions declared public can be
invoked outside the class by using the following syntax :
ClassName : : MemberFunction ( )
Or
ObjectName. MemberFunction ( )
23. Constant Member Function
If a member function does not alter any data in the
class, then this member function may be declared as a
constant member function using the keyword const as
in the following format:
ReturnType MemberFunctionName
(ArgumentList) const ;
24. Structure v/s Classes
Structure Class
1.The keyword struct is used to 1.The keyword class in used in
define. definition
2.The members are data elements 2.Both data and functions are used
only and there is no member members.
function. 3.Access labels sets the scope of
3.No access labels for the members.
members. 4.By default, the members are
4.The members are public by private.
default. 5.Variable declared with class are
5.Variable declared with structure known as objects.
tag is known as structure 6. There may be static members.
variable.
6. There is no static member.
25. Nested Classes
A class declared inside another class is called nested class. The outer class is known as enclosing class.
Consider the following class definition:
Class outer
{
int p ;
class inner / / nesting of a class
{
int q,r ;
public :
void prinfun ( )
{
cout<<q*r ;
}
} obl ; / / object creation of nested class
public :
void outprint ( )
{
obl. prinfun ( ) ;
cout << p ;
}
};
The objects of the nested class can be declared outside the enclosing class by specifying the full name of the
nested class as :
EnclosindgClassName : : NestedClassName ObjectName ;
An object say ob would have been created outiside the clas outer with the expression.
outer : : inner ob ;