210 likes | 396 Views
Object Oriented Programming Paradigm Lesson 04. Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh. Objects and Classes. Contents (Objects & Classes). Objects & Classes Objects & Class Encapsulation / Data Hiding (Access Specifiers)
E N D
Object Oriented Programming ParadigmLesson 04 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh
Contents (Objects & Classes) • Objects & Classes • Objects & Class • Encapsulation / Data Hiding (Access Specifiers) • Access Specifiers • Importance of Class • Class Member Function • C++ Objects as a Physical Objects • Program: Write a program using classes, which take three variables and provide day, month and year. • Program: Make a class with two member functions, also call those functions in main function. • Program: Write a program using class which create two separate functions to set and show the values of modelnumber, partnumber and cost of the widget, also call them in main function. • Program: Write a program using classes, which could get feet and inches from user and display in main functions with specific format. • Constructors • Program: Write a program using class, which uses constructor and show automatic initialization of variables.
Objects & Classes • The class: It is the construct primarily used to create objects. • A class is a user defined data type. • Class variables (instances) are called as objects. • A class is similar to type and object is similar to variable. • Member Functions: Functions of a class which can manipulate the data of class and facilitate the use of class in main function. class class_name { Private: //Data Members Public: //Member Functions };
Objects & Classes class Rectangle { private: float width, length, area; public: void setData(float, float); void calcArea(void); float getWidth(void); float getLength(void); float getArea(void); };
Encapsulation/Data Hiding • Data hiding is mechanism to protect data from accidentally or deliberately being manipulated from other parts of the program • The primary mechanism for data hiding is to put it in a class and make it private. • Private data can only be accessed from within the class • Public data or functions are accessible from outside the class
Access Specifiers • The key words private and public are access specifiers. • private means they can only be accessed by the member functions within the class. • public means they can be called from statements outside the class. • Note: The default visibility of all data and functions is private, but it is still a good idea to use the private key word to explicitly declare private members. But in structure by default all data members are public.
Importance of Class • Why classes are used? Or What is the importance of classes in Object oriented programming? • Combine Execution: • Classes are used to combine similar type of data so that it could be executed together. • Data hiding: • Data will be hidden from outside class. Simply, it hides the data of one class from other classes, whenever required.
Class Member Functions • A class member function can only be called in association with a specific object of that class using the dot operator (period) • Date mydate; • mydate.set_month(10,01,2013); • A member function always operates on a specific object not the class itself. • Each objects has its own data but objects of the same class share the same member function code • In other OO languages member function calls are also called messages.
Access Control Private part of class can be accessed by member functions of the same class only Private private: public: data1 data2 functiond() Public part of class constitutes the interface that can be used by other parts of the program functiona() functionb() functionc() Typically member data is private whereas member functions are mostly public
Objects & Classes Example Program1 Program: Write a program using classes, which take three variables and provide day, month and year. //cl_d_d_m.cpp #include <iostream.h> class Date { private: //private in small letters int day; int month; int year; public: void set_month(int day,int month, int year) { cout<<day<<"-"<<month<<"-"<<year; } }; int main() { Date mydate; mydate.set_month(10,01,2013); }
Objects & Classes Example Program2 Program: Make a class with two member functions, also call those functions in main function. //cl_one.cpp #include <iostream.h> class smallobj //define a class { private: int somedata; //class data public: void setdata(int d) //member function to set data { somedata = d; } void showdata() //member function to display data { cout << "Data is " << somedata << endl; } }; int main() { smallobj s1, s2; //define two objects of class smallobj s1.setdata(1066); //call member function to set data s2.setdata(1776); s1.showdata(); //call member function to display data s2.showdata(); }
Objects & Classes (Member Functions) • Combination of data and functions is called object. • Combination of objects is called class. • Object is said to be an instance of a class, s1 & s2 are objects. • The two member functions provide the only access to the data item from outside the class. • setdata(int d) & showdata() • Note: Data constructs such as structures and classes end with a semicolon, while control constructs such as functions and loops do not.
Objects & Classes (Data Member) • Data Members: The data items within a class are called data members (or sometimes member data). • There can be any number of data members in a class. • E.g: void setdata(int d) { somedata = d; } void showdata() { cout << "Data is " << somedata << endl; }
Objects & Classes (Defining Objects) • Defining an object will set aside memory for object and it is similar to defining a variable of any data type. • Defining is also called as instantiating or creating an object. • E.g. • smallobj s1, s2; //define two objects
Objects & Classes (Calling Member Functions) • Member function of a class can be accessed by only by an object of that class. • E.g. • s1.setdata(1066); //call member function to set data • s2.setdata(1776); // call member function to set next data • s1.showdata(); //call member function to show data • s2.showdata(); //call member function to show another data • Some OOP refer to calls to member functions as a messages. • E.g: s.showdata();
C++ Objects as a Physical Objects • C++ objects are used to define physical objects. • Physical objects have: • Attributes: Which define the characteristics of physical objects, and in c++ their equivalent are Data members. • Behavior: They are used to define the actions of physical objects, and in c++ their equivalent are Member Functions. • C++ Objects are basically used to implement the real structure of world in programming.
Objects & Classes (widget example) Program: Write a program using class which create two separate functions to set and show the values of modelnumber, partnumber and cost of the widget, also call them in main function. //cl_widge.cpp #include <iostream.h> class part { private: int modelnumber; int partnumber; float cost; public: void setpart(int mn, int pn, float c) { modelnumber = mn; partnumber = pn; cost = c; } void showpart() //display dates { cout<<"Model " <<modelnumber; cout<<"\t part " <<partnumber; cout<<"\t costs Rs:"<<cost<<endl; } }; int main() { part part1; //define object of class part part1.setpart(6244,373,217.55F); part1.showpart(); //call member function }
Objects & Classes (widget example) • Now we have three features of this class part: Modelnubmer, Partnumber and Cost. • Member Functions • Setpart() member function supplies values to all data items at once. • Showpart() member function displays the values stored in all three items. • Object: Only one object of type part is created: part1