OOP-Lecture 06
OOP-Lecture 06
Programming
Dr. Mujeeb Ur Rehman
mujeeb.rehman@skt.umt.edu.pk
Recommended Books:
1.C++ How to Program ( Deitel & Deitel );
2.Object-Oriented Software Engineering By Jacobson, Christerson,
Jonsson, Overgaard
Hazrat/Sir Allama Muhammad Iqbal (R.A)
Object-Oriented
Programming
(OOP)
Lecture No. 6
Class Compatibility
►A class is behaviorally compatible with
another if it supports all the operations
of the other class
Circle Triangle
radius Line angle
length
draw draw
computeArea draw computeArea
getLength
Example – Class
Compatibility
File
size
…
open
print
…
► In
general, polymorphism refers to
existence of different forms of a single
entity
► Consequently,sender of a message
does not need to know exact class of
the receiver
Example – Polymorphism
draw Shape
View
draw
print File
Editor
print
draw Shape
View
draw
draw Shape
View
draw
► In
general, polymorphism is a powerful
tool to develop flexible and reusable
systems
Class
► Class is a tool to realize objects
► Class is a tool for defining a new type
Example
► Lionis an object
► Student is an object
► Both has some attributes and some
behaviors
Uses
► The problem becomes easy to
understand
► Interactions can be easily modeled
Type in C++
► Mechanism for user defined types are
Structures
Classes
► Built-in types are like int, float and
double
► User defined type can be
Student in student management system
Circle in a drawing software
Abstraction
► Only include details in the system that
are required for making a functional
system
► Student
Relevant to our problem
Name
Address
Not relevant to our problem
Sibling
Father Business
Defining a New User Defined
Type
class ClassName
{ Syntax
…
DataType MemberVariable;
ReturnType MemberFunction();
…
}; Syntax
Example
class Student
{
int rollNo;
char *name; Member variables
float CGPA;
Member Functions
char *address;
…
void setName(char *newName);
void setRollNo(int newRollNo);
…
};
Why Member Function
► They model the behaviors of an object
► Objects can make their data invisible
► Object remains in consistent state
Example
Student aStudent;
aStudent.rollNo = 514;
TypeName VaraibaleName;
int var;
Student aStudent;
Accessing members
► Members of an object can be accessed
using
dot operator (.) to access via the variable
name
arrow operator (->) to access via a pointer
to an object
► Member variables and member
functions are accessed in a similar
fashion
Example
class Student{
int rollNo;
void setRollNo(int
aNo);
};