[go: up one dir, main page]

0% found this document useful (0 votes)
15 views36 pages

OOP-Lecture 06

C++

Uploaded by

shahwaizarts
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views36 pages

OOP-Lecture 06

C++

Uploaded by

shahwaizarts
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 36

Object Oriented

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

► Such a class is called subtype

►A class can be replaced by its subtype


…Class Compatibility
► Derived class is usually a subtype of
the base class

► Itcan handle all the legal messages


(operations) of the base class

► Therefore,base class can always be


replaced by the derived class
Example – Class
Compatibility
Shape
color
vertices
move
setColor
draw

Circle Triangle
radius Line angle
length
draw draw
computeArea draw computeArea
getLength
Example – Class
Compatibility
File
size

open
print

ASCII File PS File


… PDF File …

print print
… print …

Polymorphism

► In
general, polymorphism refers to
existence of different forms of a single
entity

► Forexample, both Diamond and Coal


are different forms of Carbon
Polymorphism in OO Model

► InOO model, polymorphism means


that different objects can behave in
different ways for the same message
(stimulus)

► Consequently,sender of a message
does not need to know exact class of
the receiver
Example – Polymorphism

draw Shape
View
draw

Line Circle Triangle


draw draw draw
Example – Polymorphism

print File
Editor
print

ASCII File PDF File PS File


print print print
Polymorphism – Advantages
► Messagescan be interpreted in different
ways depending upon the receiver class

draw Shape
View
draw

Line Circle Triangle


draw draw draw
Polymorphism – Advantages
► Newclasses can be added without
changing the existing model

draw Shape
View
draw

Square Line Circle Triangle


draw draw draw draw
Polymorphism – Advantages

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

aStudent.rollNo = -514; //Error


Object and Class
► Objectis an instantiation of a user
defined type or a class
Declaring class variables
► Variablesof classes (objects) are
declared just like variables of
structures and built-in data types

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

Student aStudent; Error


aStudent.rollNo;
Access specifiers
Access specifiers
► There are three access specifiers
 ‘public’ is used to tell that member can be
accessed whenever you have access to
the object
 ‘private’ is used to tell that member can
only be accessed from a member function
 ‘protected’ to be discussed when we
cover inheritance
Example
class Student{
private:
char * name;
Cannot be accessed outside class
int rollNo;
public:
void setName(char *); Can be
accessed
void setRollNo(int);
outside class
...
};
Example
class Student{
...
int rollNo;
public:
void setRollNo(int aNo);
};
int main(){
Student aStudent;
aStudent.SetRollNo(1);
}
Default access specifiers
► When no access specifier is mentioned
then by default the member is
considered private member
Example
class Student class Student
{ {
char * name; private:
int RollNo; char * name;
}; int RollNo;
};
Example
class Student
{
char * name;
int RollNo;
void SetName(char *);
}; Error
Student aStudent;
aStudent.SetName(Ali);
Example
class Student
{
char * name;
int RollNo;
public:
void setName(char *);
};
Student aStudent;
aStudent.SetName(“Ali”);

You might also like