1 / 22

Classes and Objects

Classes and Objects. Presented by: Neha Tyagi PGT Comp. Sc. KV Nagercoil Chennai Region. Learning Objectives. Concept of Classes and Objects. Defining a Class and its members. Defining Member Functions. Creation of Objects. Referencing Data Members.

ernestkelly
Télécharger la présentation

Classes and 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 and Objects Presented by: Neha Tyagi PGT Comp. Sc. KV Nagercoil Chennai Region

  2. Learning Objectives • Concept of Classes and Objects. • Defining a Class and its members. • Defining Member Functions. • Creation of Objects. • Referencing Data Members. • Implementation of Encapsulation and Abstraction in C++.

  3. An Introduction to a Class • A CLASS is a building block of OOP. It is a way to bind data and its associated functions together. • In C++, class makes a data type that is used to create OBJECTS of that CLASS type.

  4. Example Class : Car Shared characteristics : Brand, Paint, Wheels, Engine, No of Seats, Fuel capacity, etc. Shared behaviour : Forward(), Backward(), Stop(), etc.

  5. Mammals

  6. Need for Classes and Objects Classes are needed in programming languages to represent real world entities that not only have data type properties(their characteristics) but also have associated operations (their behaviour).

  7. Defining Classes in C++ It involves declaration of four associated attributes: Data Members - Properties that describe characteristics of a class. Member Functions - Set of operations that may be applied to objects of the class. Program Access Levels - controls the access of members from within the program. Class Tagname- name of class given by user, used to create objects of a class.

  8. Syntax of defining a class class mobile { private: char brand[20]; char colour[20]; int RAM, Memory; char OS[10]; public: void Calling(); void Browsing(); void Photography(); }; classname_of class { private : [variable declarations;] [function declarations;] protected : [variable declarations;] [function declarations;] public : [variable declarations;] [function declarations;] };

  9. Class Method/Function Definitions • Outside the class • Inside the class

  10. Definition Outside the class Syntax : :: classtagname return_type function_name (parameter list) { ………………. ………………. ………………. }

  11. Example: class student { int mark1,mark2; public: void getdata(); }; void student :: getdata() { cout<<“\nEnter the marks of student :”; cin>>marks1>>marks2; }

  12. Definition Inside the class Syntax : return_type function_name (parameter list) { ………………….. ………………….. ………………….. }

  13. Example: class student { int mark1,mark2; public: void getdata() { cout<<“\nEnter the marks of student:”; cin>>marks1>>marks2; } };

  14. Creating Objects of a class The declaration of an object is similar to that of a variable declaration of any basic data type. Syntax Method I <Object n>; <Object 2>,…. <Object 1>, Class_tagname Method II class <tagname> { …………. …………. <Object n>; <Object 2>,…….. }<Object 1>,

  15. Example Method II class mobile { [data members;] }Iphone_7,OnePLus_3T; Method I mobile Iphone_7, OnePlus_3T; It is similar to declaring an integer variable. Example : int a; variable ‘a’ is of integer type while Iphone_7 and OnePlus_3T can be said to be of mobile type.

  16. Referencing class members • The members of a class can be accessed in a program using objects of that class. • The private data members can be accessed only through member functions of that class. They are not accessible directly outside the class. • The objects can access only the public data members and member functions directly.

  17. Syntax to refer public members using objects • object_name.data_member; • object_name.member_function();

  18. Example class Addition { int a, b; public: int z; void sum_func(int x, int y) { a=x; b=y; z=a+b; cout<<“Sum = “<<z; } }O1, O2; void main() { O1.sum_func(2,3); //Sum = 5 O2.sum_func(7,2); //Sum = 9 } If we type the following statements : //Valid O1.z=10; O2.a=20; //Invalid sum_func(9,23); //Invalid

  19. Characteristics of Access Specifiers • Private section of the class can have both data members and member functions, usually data members are made private for data security. • It is not mandatory that private section has to be declared before protected or public section. • If no member access specifier is specified by default the members are private for the class. • Protected specifier is used for declaring the class members which can be accessed by its own class and its derived class.

  20. Implementation of Encapsulation and Abstraction using Classes and Objects

  21. Example class mobile { private: char brand[20]; char colour[20]; int RAM, Memory; char OS[10]; public: void Calling(); void Browsing(); void Photography(); }; • Encapsulation : Data members and member functions are wrapped up in a single unit. • Abstraction : The private members are hidden from the non members of the class. Only member functions of the same class will be able to access the private members.

  22. Thank you

More Related