Docsity

Object Oriented Programing, Slides of Computer Engineering and Programming

Partial preview of the text

Download Object Oriented Programing and more Slides Computer Engineering and Programming in PDF only on Docsity!

Classes and Classes and

Objects Objects

Objectives of this session Objectives 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

Structures in C Structures in C

A structure is a convenient tool for handlingA structure is a convenient tool for handling

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

It is a user defined data type with aIt 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.

Structures in C Structures in C

struct student struct student

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

int int roll_number;roll_number;

float total_marks; float total_marks;

The keyword The keyword structstruct declaresdeclares studentstudent asas aa

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; struct student A; // C declaration// C declaration

continue… Structure members or elements Structure name or structure tag

Structures and Classes in Structures and Classes in C++ C++  C++ supports all the features ofC++ supports all the features of structures as defined in C. structures as defined in C.  In C++, a structure can have bothIn C++, a structure can have both variables and functions as members. variables and functions as members.  It can declare some of its members asIt 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; student A; //// C++ declarationC++ declaration

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

By default the members of a classBy default the members of a class

are are privateprivate , while, by default, the, while, by default, the

members of a structure are members of a structure are publicpublic ..

continue…

Class Declaration Class Declaration

class class_name class 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 adeclaration is similar to a structstruct

declaration. declaration.

Class Declaration Class Declaration  (^) The body of a class isThe body of a class is enclosed within enclosed within braces and terminated braces and terminated by a semicolon. by a semicolon.  The class bodyThe class body contains the contains the declaration of declaration of variables and variables and functions. functions.  These functions andThese functions and variables collectively variables collectively called class members. called class members. continue… class class_name class class_name { { private : private : variable declarations; variable declarations; function declarations; function declarations; public : public : variable declarations; variable declarations; function declarations; function declarations; }; };

Class Declaration Class Declaration

The class membersThe 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 class. from within the class.

Public members canPublic members can

be accessed from be accessed from

outside the class also. outside the class also.

KeywordKeyword privateprivate isis

optional. By default, optional. By default,

the members of a the members of a

class are class are privateprivate ..

continue… class class_name class class_name { { private : private : variable declarations; variable declarations; function declarations; function declarations; public : public : variable declarations; variable declarations; function declarations; function declarations; }; };

Class Declaration Class Declaration class class_name class class_name { { private : private : variable declarations; variable declarations; function declarations; function declarations; public : public : variable declarations; variable declarations; function declarations; function declarations; }; };  The variablesThe variables declared inside the declared inside the class are known as class are known as data members data members ..  and the functions areand the functions are known as known as membermember functions functions ..  Only the memberOnly 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…

Class Declaration Class Declaration continue… Data Functions Private area Data Functions Public area No entry to private area X Entry allowed to public area Data hiding in CLASS

Class Example Class Example class item class item { { int int number;number; // variable declaration// variable declaration float float cost;cost; // private by default// private by default public : public : void void getdata( int a, float b);getdata( int a, float b); (^) //// function declarationfunction declaration void void putdata( void );putdata( void ); // using prototype// using prototype }; };

Class Example Class Example

The data membersThe data members

are private by default are private by default

While both theWhile both the

functions are public functions are public

by declaration. by declaration.

 The functions areThe functions are

declared but not declared but not

defined. defined.

 Actual functionActual function

definition will appear definition will appear

later in the program. later in the program.

continue… class item class item { { int int number;number; float float cost;cost; public : public : void void getdata(intgetdata(int a, float b); a, float b); void void putdata(void); putdata(void); }; };

Class Example Class Example continue… class item class item { { int int number;number; float float cost;cost; public : public : void void getdata(intgetdata(int a, float b); a, float b); void void putdata(void); putdata(void); }; }; Class : ITEM DATA number cost ……… FUNCTIONS getdata( ) putdata( ) ……… Representation of a class