220 likes | 233 Views
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.
E N D
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. • Implementation of Encapsulation and Abstraction in C++.
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.
Example Class : Car Shared characteristics : Brand, Paint, Wheels, Engine, No of Seats, Fuel capacity, etc. Shared behaviour : Forward(), Backward(), Stop(), etc.
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).
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.
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;] };
Class Method/Function Definitions • Outside the class • Inside the class
Definition Outside the class Syntax : :: classtagname return_type function_name (parameter list) { ………………. ………………. ………………. }
Example: class student { int mark1,mark2; public: void getdata(); }; void student :: getdata() { cout<<“\nEnter the marks of student :”; cin>>marks1>>marks2; }
Definition Inside the class Syntax : return_type function_name (parameter list) { ………………….. ………………….. ………………….. }
Example: class student { int mark1,mark2; public: void getdata() { cout<<“\nEnter the marks of student:”; cin>>marks1>>marks2; } };
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>,
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.
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.
Syntax to refer public members using objects • object_name.data_member; • object_name.member_function();
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
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.
Implementation of Encapsulation and Abstraction using Classes and Objects
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.