You are on page 1of 36

An introduction to Object oriented Concept

By
Birodh Rijal
Master’s student
Information and communication Technology
Asian Institute of Technology

Asian Institute Of Technology 11/8/2009 1


Structured programming
 Review
 Scenario
 Problems
Object oriented paradigm
OOP features
Data encapsulation
Data abstraction
Inheritance
polymorphism

Asian Institute Of Technology 11/8/2009 2


 Top-down approach of programming
 Algorithm based
 Divide and conquer thinking
 Emphasize on procedure
 Basic building blocks functions
 System is defined as a set of procedures that interact with
data
 Data is maintained separately from procedure
 a program is seen as a list of tasks (subroutines) to perform
 Discourages the goto statement as of procedural
programming

Asian Institute Of Technology 11/8/2009 3


 Scenario : Bank management system
◦ Models manager and employee
◦ Stores one record for each manager and employee
• Procedure printVal() prints all records in the repositories.

Example : C program code


typedef struct { void printEmp( Employee*e )
char *name; {…}
char *room; void printMang( Manager *m )
char *bankname; {…}
} Manager;
typedef struct {
char *name;
int e_id;
} Employee;

Asian Institute Of Technology 11/8/2009 4


void printAll( List l ) {
typedef struct {
int i;
enum { EMP, MNG } kind;
for ( i=0; l[ i ] != NULL; i++ )
union {
switch ( l[ i ] -> kind ) {
Employee *e;
case EMP:
Manager *m;
printEmp( l[ i ] -> u.e );
} u;
break;
} Person;
case MNG:
typedef Person **List;
printMang( l[ i ] -> u.m );
break;
}
}

Asian Institute Of Technology 11/8/2009 5


 Suppose we want some extension to our system.
 Extension : add trainee to system
◦ Add record and print function for trainee
◦ Reuse old code for repository and printing
typedef struct { void printTrain( Trainee *t )
char *name; {…}
char paid; /* ‘y‘, ‘n‘ */
} Trainee;

Asian Institute Of Technology 11/8/2009 6


typedef struct { void printAll( List l ) {
enum { EMP, MNG, TRNI} kind; int i;
union { for ( i=0; l[ i ] != NULL; i++ )
Employee *e; switch ( l[ i ] -> kind ) {
Manager *m; case EMP:
Trainee *t; printEmp( l[ i ] -> u.e );
} u; break;
} Person; case MNG:
typedef Person **List; printMang( l[ i ] -> u.m );
break;
case TRNI:
printAssi( l[ i ] -> u.t );
break;
}
}

Asian Institute Of Technology 11/8/2009 7


 Need to create more variables
 Change in one place may affect in another place
 Problem to identify the piece of code in large program.
 Difficult to debug the program
 Needs the modification in several part of the program
 More changes are need

Asian Institute Of Technology 11/8/2009 8


 No explicit language support for extension
 Adaptation usually requires reuse of existing code

 Copy and paste reuse


 Difficult to maintain
 Code duplication
 Error-prone

Asian Institute Of Technology 11/8/2009 9


Source: Peter muller konzeptaobjektorientierterprogeammierung, ws/04/-5
Asian Institute Of Technology 11/8/2009 10
 OO approach
◦ System is defined as a collection of objects that work together
to accomplish tasks
 Objects carry out actions when asked
 Each object maintains its own data
 Emphasize on data
 Bottom up approach of programming
 OOP makes it easy to maintain and modify existing
code as new objects can be created with small
differences to existing ones
 OOP provides a good framework for code libraries
where supplied software components can be easily
adapted and modified by the programmer.

Asian Institute Of Technology 11/8/2009 11


 Objects
 Class
 Data hiding
 Abstraction
 Sub-classing (Inheritance)
 Polymorphism

Asian Institute Of Technology 11/8/2009 12


 Represents the real world entity
 Real world objects have two characteristics state and
behavior.
 Indentifying the state and behavior of real world
objects while designing the software is very much
important.
 Objects in programs are similar to real world objects
as they consists of state and relative behavior.
 It is run time entity in program

Asian Institute Of Technology 11/8/2009 13


Employee
 Attributes: Methods:
◦ Name, - GetEmp()
◦ Address, - Print Val() etc.
◦ DateOfBirth,
◦ DateofJoining,
◦ Salary etc.

 Attributes are used to represent the state of an object


 Method are used to perform changes on state

Asian Institute Of Technology 11/8/2009 14


Object A Object B
Communicate
Attributes Attributes
Methods Methods

Object C

Attributes
Methods

Asian Institute Of Technology 11/8/2009 15


 A class is a set of objects that share the same
properties and behaviors.
 It’s the place where variables and methods are defied.
 Abstraction can be achieved
 Provides encapsulation
 Is the blue print for how to make the object

Asian Institute Of Technology 11/8/2009 16


 The state of object are represented as data members in a class.
 The behavior of objects are represented as member function.
C++ syntax:
Class classname
{
//Data;
//Method;
}; //must be ended with semi colon
The memory for the data is initialized after the creation of object.
C++ Syntax for object creation
classname Object_name;

Asian Institute Of Technology 11/8/2009 17


 C++ code
 Class Car
class Car
{
private:
char *model_no;
float milage;
int yearofmanufacture;
char color;
public: void GetVal();
Void Processs();
Void Print();
};
//Object of class car can be created as:
Car toyota; //toyota is the object

Asian Institute Of Technology 11/8/2009 18


 Encapsulation
 Information hiding is key aspect.
 Abstraction can be achieved by hiding the
implementation detail from the user.
 Reusability of code by means of Inheritance
 Polymorphism

Asian Institute Of Technology 11/8/2009 19


 The wrapping of attributes and methods is encapsulation
 Attributes are defined in terms of data members
 Methods are defined in terms of member functions
 Adds more security of data and prevents the flow of data in
the program
Example: C++ code
class student
{ private:
char *name, *address, grades;
public: void calcGrade();
void PrintDetail();
};

Asian Institute Of Technology 11/8/2009 20


 Abstraction is a process of representing the essential
features without including the implementation details.
 Since each data abstraction can contain many
procedures, data abstraction provides a way to group
our procedures into large units
 Whenever a type is defined in terms of operations,
rather than in terms of the storage layout, we call
that type a DATA ABSTRACTION

Asian Institute Of Technology 11/8/2009 21


 Float x, y,z;
◦ Z=x+y;
 The user need not to know about the storage details of
attributes.
 Above example is the built-in feature provided by the
compiler.
 Class and interfaces provide the abstraction for user
defined data.
 But most of the OOP provide abstraction in user defined
data Time1= 03:15:55
i.e operation in object. Time2= 07:50:10
Example : C++ code Time3 =11:06:05
time3=time1+time2;
This can be achieved by overloading operator.

Asian Institute Of Technology 11/8/2009 22


 It is the design principle
 Provides different access specifier for data for their
accessibility.
 Access depends up on programming language used
 C++ use three different access specifier private, public
and protected.
 Private data are not accessible from outside world i.e
outside from class in which they belong.
 Only the public interface are used to operate on
private data

Asian Institute Of Technology 11/8/2009 23


 Example C++ code
class string int main()
{ {
Private: char *font_type; string s;
int length;
float font_size;
s.font_size=14.5; //error;not accessible
public: set_font() s.set_font(); // accessible
{ return 0;
font_type=tahoma; }
length=8;
font_size=12.5; -Here, the data are private so they can not be altered from
} out side the class i.e. from main .
}; -only the class member functions have right to operate
on private data

Asian Institute Of Technology 11/8/2009 24


 Definition:
 Inheritance is the concept that when a class of object is
defined, any subclass that is defined can inherit the
definitions of one or more general classes.
 This means for the programmer that an object in a subclass
need not carry its own definition of data and methods that
are generic to the class (or classes) of which it is a part.
 This not only speeds up program development; it also
ensures an inherent validity to the defined subclass object
(what works and is consistent about the class will also
work for the subclass).

Asian Institute Of Technology 11/8/2009 25


 Mechanism by which a new class is derived from
existing class.
 Derived class is called sub class
 Existing class is called super class or parent class
 Generally the sub class is super class plus something
 With the help of inheritance it is possible to form
new classes using already defined and more generalized
ones

Asian Institute Of Technology 11/8/2009 26


Employee

Manager Supervisor

Fig: Inheritance

Asian Institute Of Technology 11/8/2009 27


-In real world Manager is a kind of employee and acquires some data from
employee class
-Manager class can add some special features which only belongs to it like he is
getting salary or not

class Employee class Manager: public Employee


{ {
protected: bool isSalary;
Char *name; public: Manager(char *, char *, float, bool)
Char *Address; bool getsalaried() const;
date doj, dob; // date is some class already defined };
Float salary;
public: Employee( char *, char *, float );
Void getDate();
};

Asian Institute Of Technology 11/8/2009 28


class vehicle class Car: public vehicle
{
{
protected:
protected: char type_of_fuel;
char colorname[20]; public:
int number_of_wheels; Car();
public: };
vehicle(); int main()
~vehicle(); { Car toyota;
void start(); toyota.start();
Toyoto.run();
void stop(); return 0;
void run(); }
};

Asian Institute Of Technology 11/8/2009 29


 Base class vehicle consists of two attributes(properties)
 Color and the number of wheels
 Vehicle is generic term so that it can be expanded like
car, bus, bike etc.
 The derived class car has access to the protected
members of vehicle class.
 The derived class can also use the functions start, stop
and run provided the functionalities as same.
 Also the functionalities of the above methods can be
changed as per the requirement in derived class.

Asian Institute Of Technology 11/8/2009 30


 In object-oriented programming, polymorphism is the
characteristic of being able to assign a different
meaning to a particular symbol or "operator" in
different contexts.
 The same method name or operator name can be
used in many different ways by many different objects.
 Polymorphism can be achieved in compile time as well
as run time.
 Compile time polymorphism can be achieved by
method overloading or operator overloading.

Asian Institute Of Technology 11/8/2009 31


 Compiletime polymorphism
◦ Method overloading : C++ example
double calculateArea(float, float);
float caluculateArea(int, float, float);
◦ Both of the method have same name but the compiler invokes
the particular method by matching the type and number of
parameters in it at the compilation time.
◦ Operator overloading
string operator +(string s1,string s2);
◦ Here ‘+’ is used to add two objects of string class.
◦ This enables operation on user defined data as abstract data
type.

Asian Institute Of Technology 11/8/2009 32


 Run time polymorphism
 The invocation of particular method is determined at
the of program execution so it is late binging.
 Virtual function provides run time polymorphism
 More generic classes or abstract are constructed with
pure virtual function( having no definition relative to
the base class)
 The abstract class does not have its direct instance.
 Pure virtual function are implemented in derived
classes.

Asian Institute Of Technology 11/8/2009 33


class Parent {
void calculate_area (Parent &p) { p.area (); }
public:
int main (void) {
virtual ~Parent (void) {}
ChildOne c;
virtual void area (void)
clrscr();
{ cout << "Parent::print()\n"; }
calculate_area (c);
};
ChildTwo c1;
class ChildOne : public Parent {
calculate_area(c1);
public:
getch();
virtual ~ChildOne (void) {}
return 0;
virtual void area (void)
} // area() is virtual
{ cout << "\n Area of circle"; }
};
class ChildTwo : public Parent {
public:
virtual ~ChildTwo (void) {}
virtual void area (void)
{ cout << "\n area of sphere"; }
};
Asian Institute Of Technology 11/8/2009 34
 Introduction to Object-Oriented Programming, 3rd Edition, T.
Budd
 Object oriented Programming in C++, 2nd edition, R. Lafore
 http://searchciomidmarket.techtarget.com/sDefinition/0,,sid1
83_gci866374,00.html
 http://ericlin2.tripod.com/as2/interface.html
 Introduction to object oriented programming , Craig Berntson,
www.craigberntson.com
 OOP Concepts by Example, by Randy Charles Morin
 Konzepte objektorientierter Programmierung, by
Prof. Dr. Peter Müller

Asian Institute Of Technology 11/8/2009 35


Asian Institute Of Technology 11/8/2009 36

You might also like