130 likes | 325 Views
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.
E N D
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.
CLASS DEFINITION classclass_name { private: variable declaration; function declaration; protected: variable declaration; function declaration; public: variable declaration; function declaration; }; Keywords: In bluecolour Identifiers: In black colour
CLASS METHOD DEFINTION • OUTSIDE THE CLASS DEFINITION return type class_name:: function_name(parameter list) { function body } Where return 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)
EXAMPLE “OUTSIDE CLASS DEFINITION” void BOOK :: input( ) { cout<<“\n Enter Book No:”; cin>>book_no; cout<<“\n Enter Book Title:”; gets(book_title); cout<<“Enter Price:”; cin>>price; }
CLASS METHOD DEFINITION • INSIDE THE CLASS DEFINITION When 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.
EXAMPLE“INSIDE CLASS DEFINITION” class Book { private: intbook_no; char book_title[20]; float price; Float total_cost(int n) //inline function { float total; total = n*price; // inline function definition return total; } public: intbook_sl_no; void book_info_display( ); };
EXAMPLE INLINE FUNCTION DEFINITION USING “inline” KEYWORD inline function definition, outside the definition of class with keyword inline: inline void maxi( int a, int b) // inline function { cout<<(a > b ? a : b) }
REFERENCING CLASS MEMBERS Class members refer with the help of object. • Creation of object: class_nameobject_name; or class_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);
EXAMPLE(REFERENCING CLASS MEMBERS) 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 member • ACCESSING/CALLING MEMBER FUNCTION(only public member function outside class definition) b1 . book_info_display( ); // book_info_display is a member function
MEMORY ALLOCATION OF 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.