1 / 12

CLASSES & OBJECTS

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.

emma-levine
Télécharger la présentation

CLASSES & OBJECTS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CLASSES & OBJECTS PREPARED BY: Roop Narayan PGT(Comp.Sc.) KV Kokrajhar (Assam)

  2. 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.

  3. 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

  4. 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)

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

  6. 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.

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

  8. 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) }

  9. 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);

  10. 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

  11. 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.

  12. THANKS

More Related