classes & objects

12
PREPARED BY: Roop Narayan PGT(Comp.Sc.) KV Kokrajhar (Assam)

Upload: emma-levine

Post on 30-Dec-2015

45 views

Category:

Documents


3 download

DESCRIPTION

CLASSES & OBJECTS. PREPARED BY: Roop Narayan PGT( Comp.Sc .) KV Kokrajhar (Assam). CLASS. DEFINITION A CLASS IS A WAY TO WIND THE DATA DESCRIBING AN ENTITIY AND ITS ASSOCIATED FUNCTIONS TOGETHER. IN C++, CLASS MAKES A DATA TYPE THAT IS USED TO CREATE OBJECTS OF THIS TYPE. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CLASSES & OBJECTS

PREPARED BY: Roop Narayan PGT(Comp.Sc.)

KV Kokrajhar (Assam)

Page 2: CLASSES & OBJECTS

DEFINITIONA CLASS IS A WAY TO WIND THE DATA DESCRIBING AN ENTITIY AND ITS ASSOCIATED FUNCTIONS TOGETHER. IN C++, CLASS MAKES A DATA TYPE THAT IS USED TO CREATE OBJECTS OF THIS TYPE.

Page 3: CLASSES & OBJECTS

class class_name{

private:variable declaration;function declaration;

protected:variable declaration;function declaration;

public:variable declaration;function declaration;

};Keywords: In blue colour Identifiers: In black colour

Page 4: CLASSES & OBJECTS

1. OUTSIDE THE CLASS DEFINITIONreturn type class_name :: function_name(parameter list){

function body}

Wherereturn type (Any fundamental data types like void, int, float…): : (scope resolution operator, specifies that the scope of the

function is restricted to the class name)

Page 5: CLASSES & OBJECTS

void BOOK :: input( ){

cout<<“\n Enter Book No:”;cin>>book_no;cout<<“\n Enter Book Title:”;gets(book_title);cout<<“Enter Price:”;cin>>price;

}

Page 6: CLASSES & OBJECTS

2. INSIDE THE CLASS DEFINITIONWhen a member function is defined inside a class definition.

A function defined inside a class is an inline function. A function can be declared inline by placing the keyword

inline before it. Inline function are best for small functions that are called

often. An inline function definition should be placed above all

the functions that call it. Inline functions executes faster than the normal functions

as function calling overheads are saved, however is a memory penalty.

Page 7: CLASSES & OBJECTS

EXAMPLE“INSIDE CLASS DEFINITION”

class Book{private:

int book_no;char book_title[20];float price;Float total_cost(int n) //inline function{

float total;total = n*price; // inline function definitionreturn total;

}public:

int book_sl_no;void book_info_display( );

};

Page 8: CLASSES & OBJECTS

inline function definition, outside the definitionof class with keyword inline:

inline void maxi( int a, int b) // inline function{

cout<<(a > b ? a : b)}

Page 9: CLASSES & OBJECTS

Class members refer with the help of object.Creation of object:

class_name object_name; orclass_name obj1, obj2, obj3,…………….;

Referring data member(by dot operator)object_name . data_member_name = value;

Referring member function(with dot operator) object_name . Function_name(actual_arguments);

Page 10: CLASSES & OBJECTS

CREATION OF AN OBJECT:Book b1; // Book is a class, b1 is an object ACCESSING DATA MEMBER(only public data

member outside class definition)b1 . bool_sl_no = 1; //book_sl_no is data memberACCESSING/CALLING MEMBER FUNCTION(only

public member function outside class definition)b1 . book_info_display( ); // book_info_display is a member function

Page 11: CLASSES & OBJECTS

Member functions are created and placed in the memory space only once when the class is defined.

The memory space is allocated for objects data members only when the objects are declared.

No separate space is allocated for member functions when the objects are created.

Page 12: CLASSES & OBJECTS

THANKS