Classes and Objects Presented by Neha Tyagi PGT
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.
Mammals
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: 1. Data Members - Properties that describe characteristics of a class. 2. Member Functions - Set of operations that may be applied to objects of the class. 3. Program Access Levels - controls the access of members from within the program. 4. Class Tagname - name of class given by user, used to create objects of a class.
Syntax of defining a class name_of class { private : [variable declarations; ] [function declarations; ] protected : [variable declarations; ] [function declarations; ] public : [variable declarations; ] [function declarations; ] }; class mobile { private: char brand[20]; char colour[20]; int RAM, Memory; char OS[10]; public: void Calling(); void Browsing(); void Photography(); };
Class Method/Function Definitions 1. Outside the class 2. Inside the class
Definition Outside the class Syntax : return_type class tagname { ………………. } : : function_name (parameter list)
Example: class student { int mark 1, mark 2; public: void getdata(); }; void student : : getdata() { cout<<“n. Enter the marks of student : ”; cin>>marks 1>>marks 2; }
Definition Inside the class Syntax : return_type function_name (parameter list) { …………………. . }
Example: class student { int mark 1, mark 2; public: void getdata() { cout<<“n. Enter the marks of student: ”; cin>>marks 1>>marks 2; } };
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 Class_tagname <Object 1>, <Object 2>, …. <Object n>; Method II clas <tagname> {s …………. }<Object 1>, <Object 2>, ……. . <Object n>;
Example Method II mobile Iphone_7, One. Plus_3 T; class mobile { [data members; ] }Iphone_7, One. PLus_3 T; It is similar to declaring an integer variable. Example : int a; variable ‘a’ is of integer type while Iphone_7 and One. Plus_3 T 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; } }O 1, O 2; void main() { O 1. sum_func(2, 3); O 2. sum_func(7, 2); } //Sum = 5 //Sum = 9 If we type the following statements : O 1. z=10; //Valid O 2. 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 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. class mobile { private: char brand[20]; char colour[20]; int RAM, Memory; char OS[10]; public: void Calling(); void Browsing(); void Photography(); };
Thank you
- Slides: 22