59
Learners Support Publications www.lsp4you.com Classes and Classes and Objects Objects

Learners Support Publications Classes and Objects

Embed Size (px)

Citation preview

Page 1: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Classes and Classes and ObjectsObjects

Page 2: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Objectives of this sessionObjectives of this session

Structures in C and its limitationsStructures in C and its limitations Specifying a ClassSpecifying a Class Creating ObjectsCreating Objects Accessing Class MembersAccessing Class Members Defining Member FunctionsDefining Member Functions Making an outside Function InlineMaking an outside Function Inline Nesting of Member FunctionsNesting of Member Functions Private Member FunctionsPrivate Member Functions

Page 3: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

IntroductionIntroduction

Classes is an extension of the idea of Classes is an extension of the idea of structure used in C.structure used in C.

It is a new way of creating and It is a new way of creating and implementing a user-defined data implementing a user-defined data type.type.

Page 4: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Structures in CStructures in C A structure is a convenient tool for A structure is a convenient tool for

handling a group of logically related data handling a group of logically related data items.items.

It is a user defined data type with a It is a user defined data type with a template.template.

Once the structure type has been defined, Once the structure type has been defined, we can create variables of that type using we can create variables of that type using declarations, that are similar to the built-in declarations, that are similar to the built-in type declarations.type declarations.

Page 5: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Structures in CStructures in C

struct studentstruct student

{{

char name[20];char name[20];

int roll_number;int roll_number;

float total_marks;float total_marks;

};};

The keyword The keyword structstruct declares declares studentstudent as a as a new data type that can hold three fields of new data type that can hold three fields of different data types.different data types.

struct student A; // C declarationstruct student A; // C declaration

continue…

Structure members or elements

Structure name or structure tag

Page 6: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Limitations of Structures Limitations of Structures in Cin C

The standard C does not allow the The standard C does not allow the struct data type to be treated like struct data type to be treated like built-in types.built-in types.

They do not permit data hiding.They do not permit data hiding. Structure members can be directly Structure members can be directly

accessed by the structure variables accessed by the structure variables by any function anywhere in their by any function anywhere in their scope.scope.

Page 7: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Structures and Classes in Structures and Classes in C++C++

C++ supports all the features of C++ supports all the features of structures as defined in C.structures as defined in C.

In C++, a structure can have both In C++, a structure can have both variables and functions as members.variables and functions as members.

It can declare some of its members as It can declare some of its members as ‘private’.‘private’.

In C++, the structure names are stand-In C++, the structure names are stand-alone and can be used like any other alone and can be used like any other type names.type names.

student A; // C++ declarationstudent A; // C++ declaration

Page 8: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Structures and Classes in Structures and Classes in C++C++

By default the members of a class By default the members of a class areare privateprivate, while, by default, the , while, by default, the members of a structure are members of a structure are publicpublic..

continue…

Page 9: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

CLASSCLASS

A class is a way to bind the data and A class is a way to bind the data and its associated functions together.its associated functions together.

It allows the data( and functions ) to It allows the data( and functions ) to be hidden, if necessary, from be hidden, if necessary, from external use.external use.

A CLASS specification has two parts:A CLASS specification has two parts: Class DeclarationClass Declaration Class Function DefinitionsClass Function Definitions

Describes the type and scope of its members

Describes how the class functions are implemented

Page 10: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Class DeclarationClass Declaration

class class_nameclass class_name{{ private :private : variable declarations;variable declarations;

function declarations;function declarations; public :public :

variable declarations;variable declarations;function declarations;function declarations;

};};The The classclass declaration is similar to a declaration is similar to a structstruct

declaration.declaration.

Page 11: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Class DeclarationClass Declaration

The body of a class is The body of a class is enclosed within enclosed within braces and braces and terminated by a terminated by a semicolon.semicolon.

The class body The class body contains the contains the declaration of declaration of variables and variables and functions.functions.

These functions and These functions and variables collectively variables collectively called class members.called class members.

continue…

class class_nameclass class_name

{{

private :private :

variable declarations;variable declarations;

function declarations;function declarations;

public :public :

variable declarations;variable declarations;

function declarations;function declarations;

};};

Page 12: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Class DeclarationClass Declaration

Members grouped Members grouped into two sections :into two sections : Private - visibility Private - visibility

labelslabels PublicPublic

The keyword are The keyword are followed by colon.followed by colon.

continue…

class class_nameclass class_name

{{

private :private :

variable declarations;variable declarations;

function declarations;function declarations;

public :public :

variable declarations;variable declarations;

function declarations;function declarations;

};};

Page 13: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Class DeclarationClass Declaration

The class members The class members that have been that have been declared as private declared as private can be accessed only can be accessed only from within the from within the class.class.

Public members can Public members can be accessed from be accessed from outside the class outside the class also.also.

Keyword Keyword privateprivate is is optional. By default, optional. By default, the members of a the members of a class are class are privateprivate..

continue…

class class_nameclass class_name

{{

private :private :

variable declarations;variable declarations;

function declarations;function declarations;

public :public :

variable declarations;variable declarations;

function declarations;function declarations;

};};

Page 14: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Class DeclarationClass Declaration

class class_nameclass class_name

{{

private :private :

variable declarations;variable declarations;

function declarations;function declarations;

public :public :

variable declarations;variable declarations;

function declarations;function declarations;

};};

The variables The variables declared inside the declared inside the class are known as class are known as data membersdata members..

and the functions are and the functions are known as known as member member functionsfunctions..

Only the member Only the member functions can have functions can have access to the private access to the private data members and data members and private functions.private functions.

continue…

Page 15: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Class DeclarationClass Declaration

class class_nameclass class_name

{{

private :private :

variable declarations;variable declarations;

function declarations;function declarations;

public :public :

variable declarations;variable declarations;

function declarations;function declarations;

};};

The public members The public members (both functions and (both functions and data) can be data) can be accessed from outside accessed from outside the class.the class.

The binding of data The binding of data and functions and functions together into a single together into a single class-type variable is class-type variable is referred to as referred to as encapsulationencapsulation..

continue…

Page 16: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Class DeclarationClass Declarationcontinue…

Data

Functions

Private area

Data

Functions

Public area

No entry toprivate area

X

Entry allowed topublic area

Data hiding in CLASS

Page 17: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Class ExampleClass Example

class itemclass item

{{ int int number; number; // //

variable declarationvariable declaration

float float cost;cost; // private by // private by defaultdefault

public :public :void void getdata( int a, float b);getdata( int a, float b); // // function declarationfunction declaration

voidvoid putdata( void );putdata( void ); // using prototype// using prototype

};};

Page 18: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Class ExampleClass Example

Give meaningful Give meaningful names to classes.names to classes.

Names become the Names become the new type identifier new type identifier that can be used to that can be used to declare instances of declare instances of that class type.that class type.

The class The class itemitem contains two data contains two data members and two members and two member functions.member functions.

continue…

class itemclass item

{{

intint number;number;

floatfloat cost;cost;

public :public :

voidvoid getdata(int a, getdata(int a, float b);float b);

voidvoid putdata(void);putdata(void);

};};

Page 19: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Class ExampleClass Example

The data members The data members are private by defaultare private by default

While both the While both the functions are public functions are public by declaration.by declaration.

The functions are The functions are declared but not declared but not defined.defined.

Actual function Actual function definition will appear definition will appear later in the program.later in the program.

continue…

class itemclass item

{{

intint number;number;

floatfloat cost;cost;

public :public :

voidvoid getdata(int a, getdata(int a, float b);float b);

voidvoid putdata(void);putdata(void);

};};

Page 20: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Class ExampleClass Examplecontinue…

class itemclass item

{{

intint number;number;

floatfloat cost;cost;

public :public :

voidvoid getdata(int a, getdata(int a, float b);float b);

voidvoid putdata(void);putdata(void);

};};

Class : ITEM

DATAnumbercost………

FUNCTIONSgetdata( )putdata( )………

Representation of a class

Page 21: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Creating ObjectsCreating Objects

Once a class has been declared, we can Once a class has been declared, we can create variables of that type by using the create variables of that type by using the class name.class name.

item x ; item x ; // create a variable // create a variable xx of type of type itemitem..

In C++, the class variables are known as In C++, the class variables are known as objectsobjects..

item x, y, z ; item x, y, z ; // declare more than one objects in one // declare more than one objects in one statementstatement

Page 22: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Creating ObjectsCreating Objects

The declaration of an object is similar The declaration of an object is similar to that of any basic type. to that of any basic type.

The necessary memory space is The necessary memory space is allocated to an object at this stage.allocated to an object at this stage.

Class specification, like a structure, Class specification, like a structure, provides only a template and does not provides only a template and does not create any memory space for the create any memory space for the objects.objects.

continue…

Page 23: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Creating ObjectsCreating Objects

Object can also be created when a Object can also be created when a class is defined by placing their class is defined by placing their names immediately after the closing names immediately after the closing brace.brace.

class itemclass item{{

………………………………

} x, y, z ;} x, y, z ;

continue…

Page 24: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Accessing Class Accessing Class MembersMembers

The private data of a class can be The private data of a class can be accessed only through the member accessed only through the member functions of that class.functions of that class.

object-name . function-name ( actual-object-name . function-name ( actual-arguments);arguments);

In our example, although In our example, although x x is an object of the typeis an object of the type itemitem to which number belongs, the number to which number belongs, the number can be accessed only through a member can be accessed only through a member function and not by the object directly.function and not by the object directly.

Page 25: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Defining Member Defining Member FunctionsFunctions

Member functions can be defined in Member functions can be defined in two places:two places: Outside the class definition.Outside the class definition. Inside the class definition.Inside the class definition.

Page 26: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Defining Member Defining Member FunctionsFunctions

Outside the Class DefinitionOutside the Class Definition Member functions that are declared inside Member functions that are declared inside

a class have to be defined separately a class have to be defined separately outside the class.outside the class.

Their definitions are very much like the Their definitions are very much like the normal functions.normal functions.

They should have a function header and a They should have a function header and a function body.function body.

An important difference between a An important difference between a member function and a normal function is member function and a normal function is that a member function incorporates a that a member function incorporates a membership “identity label” in the header.membership “identity label” in the header.

continue…

This label tells the compiler which class the

function belongs to.

Page 27: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Defining Member Defining Member FunctionsFunctions

Outside the Class DefinitionOutside the Class Definition

return-type class-name : : function-name (argument return-type class-name : : function-name (argument declaration)declaration)

{{Function bodyFunction body

}}

The membership label class-name : : tells The membership label class-name : : tells the compiler that the function function-the compiler that the function function-name belongs to the class class-name.name belongs to the class class-name.

The scope of the function is restricted to The scope of the function is restricted to the class-name specified in the header the class-name specified in the header line.line.

continue…

Page 28: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Defining Member Defining Member FunctionsFunctions

Inside the Class DefinitionInside the Class Definition Replace the function declaration with Replace the function declaration with

the definition of the function inside the the definition of the function inside the class.class.

When a function is defined inside a class, When a function is defined inside a class, it is treated as an inline function.it is treated as an inline function.

All the restrictions and limitations that All the restrictions and limitations that apply to an inline function are also apply to an inline function are also applicable to the functions defined inside applicable to the functions defined inside a class.a class.

continue…

Page 29: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Making an Outside Making an Outside Functions InlineFunctions Inline

The member functions defined outside a The member functions defined outside a class can be made inline by using the class can be made inline by using the qualifier qualifier inlineinline in the header line of in the header line of function definition.function definition.

class itemclass item{{

……………………

public :public :void getdata (int a, float b);void getdata (int a, float b);

};};inline void item : : getdata (int a, float b)inline void item : : getdata (int a, float b){{

number = a ;number = a ;cost = b ;cost = b ;

}}

Page 30: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Nesting of Member Nesting of Member FunctionsFunctions

The member function of a class can be The member function of a class can be called only by an object of that class called only by an object of that class using a dot operator.using a dot operator.

But a member function can be called by But a member function can be called by using its name inside another member using its name inside another member function of the same class.function of the same class.

This is known as nesting of member This is known as nesting of member functions.functions.

Page 31: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Private Member Private Member FunctionsFunctions

Private member functions can be Private member functions can be created for making them to be created for making them to be hidden.hidden.

A private member function can only A private member function can only be called by another function that is be called by another function that is a member of its class.a member of its class.

Even an object cannot invoke a Even an object cannot invoke a private function using the dot private function using the dot operator.operator.

Page 32: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Private Member Private Member FunctionsFunctions continue…

class productclass product

{{

intint code ;code ;

float stock ;float stock ;

void read ( void ) ;void read ( void ) ;

public :public :

voidvoid update( void ) ;update( void ) ;

voidvoid display( void ) ;display( void ) ;

};};

If p1 is an object, thenIf p1 is an object, thenp1.read ( ) is illegal.p1.read ( ) is illegal.

However, the function However, the function read( ) can be called read( ) can be called by any of the public by any of the public functions of this functions of this class.class.

void product : : update void product : : update ( void)( void)

{{ read ( ) ;read ( ) ;};};

Page 33: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Arrays within a CLASSArrays within a CLASS

The arrays can be The arrays can be used as member used as member variables in a class.variables in a class.

const int size = 10;const int size = 10;

class matrixclass matrix

{{

int mat [ size ] ;int mat [ size ] ;

public:public:

void getval ( ) ;void getval ( ) ;

void putval ( ) ;void putval ( ) ;

};};

Page 34: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Memory Allocation for Memory Allocation for ObjectsObjects

The member functions are created and placed The member functions are created and placed in the memory space only once when they are in the memory space only once when they are defined.defined.

Since all the objects belongs to that class use Since all the objects belongs to that class use the same member functions, no separate the same member functions, no separate space is allocated for member functions space is allocated for member functions when the objects are created.when the objects are created.

Only space for member variables is allocated Only space for member variables is allocated separately for each object.separately for each object.

Separate memory locations for the objects are Separate memory locations for the objects are essential, because the member variables hold essential, because the member variables hold different data values for different objects.different data values for different objects.

Page 35: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Static Data MembersStatic Data Members A data member of a class can be qualified as A data member of a class can be qualified as

static.static. Characteristics of static member variables:Characteristics of static member variables:

It is initialized to zero when the first object of its It is initialized to zero when the first object of its class is created. No other initialization is class is created. No other initialization is permitted.permitted.

Only one copy of that member is created for the Only one copy of that member is created for the entire class and is shared by all the objects of that entire class and is shared by all the objects of that class, no matter how many objects are created.class, no matter how many objects are created.

It is visible only within the class, but its lifetime is It is visible only within the class, but its lifetime is the entire program.the entire program.

Static variables are normally used to Static variables are normally used to maintain values common to the entire class.maintain values common to the entire class.

Page 36: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Static Data MembersStatic Data Members

The type and scope of each static The type and scope of each static member variable must be defined member variable must be defined outside the class definition.outside the class definition.

This is because the static data This is because the static data members are stored separately members are stored separately rather than as a part of an object.rather than as a part of an object.

Since they are associated with class Since they are associated with class itself rather than with any class itself rather than with any class object, they are also known as class object, they are also known as class variables.variables.

continue…

Page 37: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Static Data MembersStatic Data Members

Static variables are like non-inline Static variables are like non-inline member functions as they are declared in member functions as they are declared in a class declaration and defined in the a class declaration and defined in the source file.source file.

While defining a static variable, some While defining a static variable, some initial value can also be assigned to the initial value can also be assigned to the variable.variable.

type class-name : : static-variable = initial type class-name : : static-variable = initial value;value;

continue…

Page 38: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Static Member FunctionsStatic Member Functions

Like static member variable, we can Like static member variable, we can also have static member functions.also have static member functions.

Properties of member functions:Properties of member functions: A static function can have access to A static function can have access to

only other static members ( functions only other static members ( functions or variables ).or variables ).

A static member function can be called A static member function can be called using the class name ( instead of its using the class name ( instead of its objects ) as:objects ) as:

class-name : : function-name;class-name : : function-name;

Page 39: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Arrays of ObjectsArrays of Objects

Arrays of variables that are of type Arrays of variables that are of type class are called class are called arrays of objectsarrays of objects..

class employee{ char name [30]; float age; public: void getdata (void); void putdata (void);};

employee manager [5];employee worker [25];

• The array manager contains five objects, viz manager[0], manager[1], manager[2], manager[3] & manager[4].

• Array of objects behave like any other array.

• manager [i]. putdata( ); to execute the putdata( ) member function of the ith

element of the array manager.

Page 40: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Objects as Function Objects as Function ArgumentsArguments

An object can be used as a function An object can be used as a function argument like any other data type.argument like any other data type.

Two ways:Two ways: A copy of the entire object is passed to the A copy of the entire object is passed to the

function. function. ( Pass-by-Value)( Pass-by-Value) Only the address of the object is Only the address of the object is

transferred to the function. transferred to the function. (Pass-by-(Pass-by-Reference)Reference)

The pass-by-reference method is more The pass-by-reference method is more efficient since it requires to pass only efficient since it requires to pass only the address of the object and not the the address of the object and not the entire object.entire object.

Page 41: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Objects as Function Objects as Function ArgumentsArguments

An object can also be passed as an An object can also be passed as an argument to a non-member function.argument to a non-member function.

Such functions can have access to the Such functions can have access to the public member functions only through public member functions only through the objects passed as arguments to it.the objects passed as arguments to it.

These functions cannot have access to These functions cannot have access to the private data members.the private data members.

continue…

Page 42: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Friendly FunctionsFriendly Functions

The private members can not be The private members can not be accessed from outside the class.accessed from outside the class.

A non-member function can not A non-member function can not have an access to the private data of have an access to the private data of a class.a class.

However ……. ?However ……. ?

Page 43: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Friendly FunctionsFriendly Functions

C++ allows a common function to be C++ allows a common function to be made friendly with more than one made friendly with more than one classes, thereby allowing the function classes, thereby allowing the function to have access to the private data of to have access to the private data of these classes.these classes.

Such a function need not be a Such a function need not be a member of these classes.member of these classes.

To make an outside function friendly To make an outside function friendly to a class, we have to simply declare to a class, we have to simply declare this function as a friend of the class.this function as a friend of the class.

continue…

Page 44: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Friendly FunctionsFriendly Functions The function The function

declaration should be declaration should be preceded by the preceded by the keyword keyword friendfriend..

The function is The function is defined elsewhere in defined elsewhere in the program like a the program like a normal C++ function.normal C++ function.

The function The function definition does not definition does not use either the use either the keyword friend or the keyword friend or the scope operator : :.scope operator : :.

class employeeclass employee{{ ------ ------ public :public : ------ ------ friend void it_cal friend void it_cal

(void);(void);}}

continue…

Page 45: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Friendly FunctionsFriendly Functions

The functions that are declared with The functions that are declared with the keyword friend are known as the keyword friend are known as friend functionfriend function..

A function can be declared as a A function can be declared as a friend in any number of classes.friend in any number of classes.

A friend function, although not a A friend function, although not a member function, has full access member function, has full access right to the private members of the right to the private members of the class.class.

continue…

Page 46: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Friendly FunctionsFriendly Functions

Special Characteristics:Special Characteristics: It is not in the scope of the class to It is not in the scope of the class to

which it has been declared as friend.which it has been declared as friend.

Since it is not in the scope of the class, Since it is not in the scope of the class, it cannot be called using the object of it cannot be called using the object of the class.the class.

It can be invoked like a normal function It can be invoked like a normal function without the help of any object.without the help of any object.

continue…

Page 47: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Friendly FunctionsFriendly Functions

Special Characteristics:Special Characteristics: Unlike member functions, it cannot Unlike member functions, it cannot

access the member names directly and access the member names directly and has to use an object name and dot has to use an object name and dot membership operator with each member membership operator with each member name.name.

It can be declared either in the public or It can be declared either in the public or private part of a class without affecting private part of a class without affecting its meaning.its meaning.

Usually, it has objects as arguments.Usually, it has objects as arguments.

continue…

Page 48: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Friendly FunctionsFriendly Functions

Member function of one class can be Member function of one class can be friend functions of another class.friend functions of another class.

In such cases, they are defined using In such cases, they are defined using the scope resolution operator as:the scope resolution operator as:

continue…

Page 49: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Friendly FunctionsFriendly Functions

class Xclass X

{{

… …

… …

int fun1 ( );int fun1 ( );

… …

};};

class Yclass Y

{{

… …

… …

friend int X : : friend int X : : fun1 ( );fun1 ( );

… …

};};

continue…

Page 50: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Friendly FunctionsFriendly Functions

We can also declare We can also declare all the member all the member functions of one functions of one class as the friend class as the friend functions of another functions of another class. class.

In such cases, the In such cases, the class is called a class is called a friend classfriend class..

class Zclass Z

{{

… …

… …

friend class X ;friend class X ;

… …

};};

continue…

Page 51: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Returning ObjectsReturning Objects

Like a function can receive objects Like a function can receive objects as arguments, it can also return as arguments, it can also return objects.objects.

Page 52: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Const Member FunctionsConst Member FunctionsIf a member function does not alter any If a member function does not alter any data in the class, then it is called a const data in the class, then it is called a const member function.member function.

void mul (int, int) const ;void mul (int, int) const ;

void get_balance( ) const ;void get_balance( ) const ;

The qualifier const is appended to the The qualifier const is appended to the function prototypes ( in both declaration function prototypes ( in both declaration and definition). The compiler will generate and definition). The compiler will generate an error message if such functions try to an error message if such functions try to alter the data values.alter the data values.

Page 53: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Pointer To MembersPointer To Members It is possible to take the address of It is possible to take the address of

a member of a class and assign it to a member of a class and assign it to a pointer.a pointer.

The address of a member can be The address of a member can be obtained by applying the operator & obtained by applying the operator & to a fully qualified class member to a fully qualified class member name.name.

A class member pointer can be A class member pointer can be declared using the operator : : * declared using the operator : : * with the class name.with the class name.

Page 54: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Pointer To MembersPointer To Members

We can define a pointer We can define a pointer to the member m as to the member m as follows:follows:

int A : : * pm = &A : : m;int A : : * pm = &A : : m;

A : : * A : : * “pointer-to-“pointer-to-membermember

of A class”.of A class”.

&A : : m means “address &A : : m means “address of the m member of A of the m member of A class”.class”.

class Aclass A

{{

private :private :

int m ;int m ;

public :public :

void show( ) ;void show( ) ;

} ;} ;

continue…

Page 55: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Pointer To MembersPointer To Members

The dereferencing The dereferencing operatoroperator

.* is used when the .* is used when the object itself is used object itself is used with the member with the member pointer.pointer.

The dereferencing The dereferencing operatoroperator

->* is used to access a ->* is used to access a member when we use member when we use pointers to both the pointers to both the object and the member.object and the member.

class Aclass A

{{

int m ;int m ;

public :public :

void show( ) ;void show( ) ;

} ;} ;

A a ;A a ;

int A : : * pm = & A : int A : : * pm = & A : : m ;: m ;

A * pa = & a ;A * pa = & a ;

continue…

Page 56: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Pointer To MembersPointer To Members

The dereferencing The dereferencing operatoroperator

.* is used when the .* is used when the object itself is used object itself is used with the member with the member pointer.pointer.

The dereferencing The dereferencing operatoroperator

->* is used to access a ->* is used to access a member when we use member when we use pointers to both the pointers to both the object and the member.object and the member.

class Aclass A

{{

int m ;int m ;

public :public :

void show( ) ;void show( ) ;

} ;} ;

A a ;A a ;

int A : : * pm = & A : int A : : * pm = & A : : m ;: m ;

A * pa = & a ;A * pa = & a ;

continue…

To refer the member m

a .* pm

To refer the member mpa -> * pm

Page 57: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Pointer To MembersPointer To Members

We can also design pointers to member We can also design pointers to member functions which, then, can be invoked functions which, then, can be invoked using the dereferencing operators in the using the dereferencing operators in the main.main.

(object-name . * pointer-to-member (object-name . * pointer-to-member function) ( )function) ( )

(pointer-to-object -> * pointer-to-member (pointer-to-object -> * pointer-to-member function) ( )function) ( )

continue…

The precedence of ( ) is higher than The precedence of ( ) is higher than that of . * and -> * , so the that of . * and -> * , so the parentheses are necessary.parentheses are necessary.

Page 58: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Local ClassesLocal Classes

Classes can be defined and used inside a Classes can be defined and used inside a function or a block. Such classes are called function or a block. Such classes are called local classes.local classes.

Local classes can be used global variables and Local classes can be used global variables and static variables but can not use automatic static variables but can not use automatic variables. The global variables should be used variables. The global variables should be used with the scope operator ( : : ).with the scope operator ( : : ).

They cannot have static data members and They cannot have static data members and member functions must be defined inside the member functions must be defined inside the local classes.local classes.

Page 59: Learners Support Publications  Classes and Objects

Learners Support Publications www.lsp4you.com

Thank YouThank You

Learners Support PublicationsLearners Support Publicationswww.lsp4you.comwww.lsp4you.com