Top Banner
Class N Objects Key Resource: Khalil Edappal +91 98464 55192
26
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: +2 CS class and objects

Class N

Objects

Key Resource:Khalil Edappal

+91 98464 55192

Page 2: +2 CS class and objects

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

Page 3: +2 CS class and objects

Local & Global Class n ObjectsLocal Global

Classes

1. Class definition occurs inside the function body.

2. Objects of this class can be created only within the function.

1.Class definition occurs outside the bodies of all functions in the program.

2.Objects of this class can be declared from anywhere in the program.

Objects

1.Objects declared within the function.

2.The objects are locally available to the function. That is, they cannot be used outside the function.

1.Objects declared outside all the function.

2.The objects are globally available to all the function in the program.

Page 4: +2 CS class and objects

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

Page 5: +2 CS class and objects

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 ( ) ;

Page 6: +2 CS class and objects

Objects as Function ArgumentsIt 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.

Page 7: +2 CS class and objects

Objects as Function Arguments

ob3.add(obl, ob2);

Page 8: +2 CS class and objects

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 ; }

Page 9: +2 CS class and objects

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.

Page 10: +2 CS class and objects

Memory Allocation for Class and Objects

Page 11: +2 CS class and objects

Types of Functions in Classes

Inline FunctionFriend FunctionConstant Member FunctionStatic Member Functions

Page 12: +2 CS class and objects

Inline FunctionsInline function, the definition which begins with the

keyword inlineThe 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}

Page 13: +2 CS class and objects

Inline FunctionsIf 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.

Page 14: +2 CS class and objects

Inline FunctionsFor 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.

Page 15: +2 CS class and objects

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.

Page 16: +2 CS class and objects

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.

Page 17: +2 CS class and objects

Member Function of Class as Friend of another ClassConsider 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

Page 18: +2 CS class and objects

Friend Classfriend FormerClassName ;The above class definitions can be modified as follows :

class PQR ; / / Prototyping of class PQR is essentialclass 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.

Page 19: +2 CS class and objects

Friends as Bridge of Classes

Consider the following definitions:class PQR ; / / Prototyping of the class PQRclass 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 ;

}

Page 20: +2 CS class and objects

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.

Page 21: +2 CS class and objects

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 ;OrObjectName. StaticDataMember = Value ;

Page 22: +2 CS class and objects

Static Member FunctionsThe 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 ( )

Page 23: +2 CS class and objects

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 ;

Page 24: +2 CS class and objects

Structure v/s Classes

Structure Class1.The keyword struct is used todefine.2.The members are data elements

only and there is no member function.

3.No access labels for the members.4.The members are public by default.5.Variable declared with structure

tag is known as structure variable.6. There is no static member.

1.The keyword class in used in definition

2.Both data and functions are used members.

3.Access labels sets the scope of members.

4.By default, the members are private.5.Variable declared with class are known as objects.6. There may be static members.

Page 25: +2 CS class and objects

Nested ClassesA 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 ;

Page 26: +2 CS class and objects

Never ENDS……………..