Object Oriented Programming Paradigm Lesson 04

Post on 09-Feb-2016

69 views 0 download

description

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) - PowerPoint PPT Presentation

Transcript of Object Oriented Programming Paradigm Lesson 04

1

Object Oriented

Programming Paradigm

Lesson 04

Mr. Muhammad Hanif

Lecturer Information Technology

MBBS Campus Dadu University of SIndh

2

Objects and Classes

3

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.

4

Objects & ClassesThe 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

};

5

Objects & Classesclass Rectangle

{

private:

float width, length, area;

public:

void setData(float, float);

void calcArea(void);

float getWidth(void);

float getLength(void);

float getArea(void);

};

6

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

7

Access SpecifiersThe 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.

8

Importance of ClassWhy 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.

9

Class Member FunctionsA 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

Privateprivate:

public:

data1data2

functiond()

functiona()functionb()functionc()

Private part of class canbe accessed by memberfunctions of the sameclass only

Public part of class constitutesthe interface that can be usedby other parts of the program

Typically member data is privatewhereas member functions aremostly public

11

Simple Program using Classes & Objects

12

Objects & Classes Example Program1

//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);}

Program: Write a program using classes, which take three variables and provide day, month and year.

13

Objects & Classes Example Program2

//cl_one.cpp#include <iostream.h>class smallobj //define a class{

private:int somedata; //class datapublic: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 smallobjs1.setdata(1066); //call member function to set datas2.setdata(1776);s1.showdata(); //call member function to display datas2.showdata();

}

Program: Make a class with two member functions, also call those functions in main function.

14

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.

15

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

16

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

17

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

18

Objects & Classes (Calling Member Functions)

19

C++ Objects as a Physical ObjectsC++ 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.

20

Objects & Classes (widget example)

//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

}

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.

21

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