Upload
others
View
10
Download
0
Embed Size (px)
Citation preview
Object Oriented
Programming
Instructor: M.Imran khalil
©University of Sargodha Canal Campus
Lahore
What is OOP?
• Why oop? – Limitations of procedural approch – A litle background or functions
• Oop is real word modeling • A technique for system modeling
©University of Sargodha Canal Campus
Lahore
1
What is an Object?
An object is
• Something tangible (Ali, Car) • Something that can be apprehended
intellectually (Time, Date)
©University of Sargodha Canal Campus
Lahore
What is an Object?
An object has
• State (attributes) • Well-defined behaviour (operations) • Unique identity
©University of Sargodha Canal Campus Lahore
2
Example – Car is a Tangible Object
• State (attributes) - Color - Model
• behaviour (operations)
- Accelerate - Start Car
- Change Gear • Identity
- Its registration number
©University of Sargodha Canal Campus
Lahore
Class
• Class is a tool to realize objects • Class is a tool for defining a new type • Class is user defined data type
©University of Sargodha Canal Campus Lahore
3
Object Oriented Programing
Instructor: M.Imran khalil
©University of Sargodha Canal Campus Lahore
Review
• What is oop? • Class • Object
– Attributes/Data Members – Factions/ methods
• Syntax of defining class • Creating objects of class
©University of Sargodha Canal Campus Lahore
1
Class
• A class has
–Data
–Function
©University of Sargodha Canal Campus
Lahore
Today Topic
• How to access a member of class • Member Functions • Access modifier • Constructor • Destructor
©University of Sargodha Canal Campus Lahore
2
Today Example
class Date
{
int day ; int month ; int year ;
} ;
©University of Sargodha Canal Campus
Lahore
Example cont..
main ( )
{
Date mydate; mydate.month = 10 ; // Error
}
©University of Sargodha Canal Campus Lahore
3
Object Oriented Programing
Instructor: M.Imran Khalil
Resource:Imrankhalil3.wordpress.com
©University of Sargodha Canal Campus Lahore
A small tip
1
A little Review
• Member Functions • Access modifier
• Public • Private
• Constructor
©University of Sargodha Canal Campus Lahore
Constructor
• A constructor is a member function that is executed automatically whenever an object is created.
• It has following special features Has Same name as class name Has no return Type Is usually public
• Why we use constructor??
©University of Sargodha Canal Campus Lahore
2
Constructor cont.…
• Constructor is used to make object in well defined form.
Example
class Student {
int
rollNo; public: Student()
{ rollNo = 100;
} }; int main() { Student aStudent;/*constructor is explicit called at this
point*/ } };
3
Object Oriented Programing
Instructor: M.Imran Khalil
Resource:Imrankhalil3.wordpress.com
©University of Sargodha Canal Campus Lahore
A small tip
1
A little Review
• Member Functions • Access modifier
• Public • Private
• Constructor
©University of Sargodha Canal Campus Lahore
Constructor
• A constructor is a member function that is executed automatically whenever an object is created.
• It has following special features Has Same name as class name Has no return Type Is usually public
• Why we use constructor??
©University of Sargodha Canal Campus Lahore
2
Constructor cont.…
• Constructor is used to make object in well defined form.
Example
class Student {
int
rollNo; public: Student()
{ rollNo = 100;
} }; int main() { Student aStudent;/*constructor is explicit called at this
point*/ } };
3
Lecture 4
Object Oriented Programing
Instructor: M.Imran Khalil
Resource: Imrankhalil3.wordpress.com
©University of Sargodha Canal Campus Lahore
Review
•Constructor
•Default constructor
•Destructor
•Inline functions
1
Now Its time to self assessment !
• Create a class Rectangle with attributes length and width, each of which defaults to 1. Provide member functions that calculate the perimeter and the area of the rectangle. Also, provide set and get functions for the length and width attributes. The set functions should verify that length and width are each floating-point numbers larger than 0.0and less than 20.0.
Today’s lecture
• Todays lecture contents
• UML diagrams to represent classes • UML Symbols for classes and
their relationships • Composition • Aggregation
2
What’s UML
• The UML is a graphical “language” for modeling computer programs.
• “Modeling” means to create a simplified representation of something, as a blueprint models a house.
• The UML provides a way to visualize the higher-level organization of programs without getting mired down in the details of actual code.
• UML is simply a way to look at the software being • developed
Graphical Representation of Classes
(Class Name)
(attributes)
(operations)
(Class Name)
Suppressed
Form
Normal Form 3
Lecture 5
Object Oriented
Programing
Instructor: M.Imran Khalil
Resource: Imrankhalil3.wordpress.com ©University of Sargodha Canal Campus Lahore
Review
UML diagrams to represent classes
Function overloading
Same function performing similar task
©University of Sargodha Canal Campus Lahore
1
14/10/2013
Today’s Lecture
Constructor Overloading
Scope Resolution Operator
©University of Sargodha Canal Campus Lahore
Constructor Overloading Constructors can have parameters
These parameters are used to initialize the data members with user supplied data
Constructors with different parameters are called overloaded constructors
©University of Sargodha Canal Campus Lahore
2
14/10/2013
Example
class Date{
int day, month, year;
public: Date() {
day=0; month=0; year=0; } Date(int d, int m, int y) {
day=d; month=m; year=y; }
};
Void main()
{
Date mydate;
Date urdate(10,12,2013);
}
©University of Sargodha Canal Campus Lahore
3
15/5/2013
Lecture 6
Object Oriented
Programing
Instructor: M.Imran Khalil
Resource: Imrankhalil3.wordpress.com ©University of Sargodha Canal Campus Lahore
Today’s lecture
Operators
Syntax for overloading operators
How to overload operators
©University of Sargodha Canal Campus Lahore
1
15/5/2013
Getting Start Operator Overloading Operator overloading is one of the most exciting features of
object-oriented programming.
It can transform complex, obscure program listings obvious ones. For example
statements like
d3.addobjects(d1, d2);
or the similar but equally
obscure d3 = d1.addobjects(d2);
can be changed to the much more
readable d3 = d1 + d2; ©University of Sargodha Canal Campus Lahore
Overloading Using operator overloading we can perform basic
operations on our own defined classes objects in the similar way as we perform them on basic built-in types(like int, float, long, double)
For example we can write
Object3=Object1+object2;
©University of Sargodha Canal Campus Lahore
2
15/5/2013
Example
Date Class
©University of Sargodha Canal Campus Lahore
class Date
{
int day; int month; int year;
};
©University of Sargodha Canal Campus Lahore
3
13/5/2013
Lecture 7
Object Oriented
Programing
Instructor: M.Imran Khalil
Resource: Imrankhalil3.wordpress.com ©University of Sargodha Canal Campus Lahore
Review
In Last Lecture we studied about
Syntax of Overloading
Overloading Unary Operator
Overloading postfix and postfix ++ operator
©University of Sargodha Canal Campus Lahore
1
13/5/2013
Review of Last Lecture Types for operator overloading
Built in (int, char) or user-defined (classes)
Can use existing operators with user-defined types
Cannot create new operators
Overloading operators
Create a function for the class
Name of operator function
Keyword operator followed by symbol
Example
operator+ for the addition operator +
©University of Sargodha Canal Campus Lahore
Restrictions on Operator Overloading Overloading restrictions
Precedence of an operator cannot be changed
Associativity of an operator cannot be changed
Arity (number of operands) cannot be changed
Unary operators remain unary, and binary operators remain binary
No new operators can be created
Use only existing operators
No overloading operators for built-in types
Cannot change how two integers are added
©University
of SargodhaProducesCanalCampusLahorea syntax error
2
13/5/2013
Today’s lecture
A little more discussion on overloading
Overloading binary operators
Overloading + operator
Overloading -+ operator
©University of Sargodha Canal Campus Lahore
Operator Overloading
Operator Overloading enables to apply standard
operators (such as +,-,*,<, and so on) to objects of the programmer defined type.
int a = b +
c;
Function name
operator+
Returns an int int data type
Calling Party
Stored in a
L-Value
int data type
Argument to function R-
Value , Called Party
It helps to enhance simplicity in program structure.
©University of Sargodha Canal Campus Lahore
3
Lecture 9
Object Oriented
Programing
Instructor: M.Imran Khalil
Resource: Imrankhalil3.wordpress.com
Today’s Lecture
Static Data Member
Static Member function
2 1
Static Class Members
When a class variable is static, only one memory
location is allocated
All members of the class share a single storage location for a static data member of that same class
When you create a non-static variable within a function, a new variable is created every time you call that function
When you create a static variable, the variable maintains its memory address and previous value
for the life of the program.
3
When you Need it?
When you need it ?
A static data item is useful when all objects of the same class must share a common item of information.
static class data member is used to share information among the objects of a class.
4 2
Example class foo { private: static int count; //only one data item for all
objects //note: “declaration” only! public: foo() //increments count when object created {
count++; }
int getcount() //returns count { return count;
} }; //End of class
5 int foo::count = 0; //*definition* of count
int main() { foo f1, f2, f3; //create three objects cout << “count is “ << f1.getcount() << endl; //each
object cout << “count is “ << f2.getcount() << endl;
//sees the cout << “count is “ << f3.getcount() << endl;
//same value return 0; }
6 3
5/12/2013
Lecture 10
Object Oriented
Programing
Instructor: M.Imran Khalil
Resource: Imrankhalil3.wordpress.com ©University of Sargodha Canal Campus Lahore
Revision of Previous lecture
In previous lecture we discussed
Static Data Member
Static Member Function
©University of Sargodha Canal Campus Lahore
1
5/12/2013
Inheritance
A child inherits characteristics of its parents
Besides inherited characteristics, a child may have its own unique characteristics
©University of Sargodha Canal Campus Lahore
Inheritance in Classes If a class B inherits from class A then it contains
all the characteristics (information structure and behavior) of class A
The parent class is called base class and the child class is called derived class
Besides inherited characteristics, derived class may have its own unique characteristics
©University of Sargodha Canal Campus Lahore
2
5/12/2013
Example – Inheritance
Person
Doctor
Student
Teacher ©University of Sargodha Canal Campus Lahore
Inheritance
Inheritance is the process of creating new classes, called derived classes, from existing or base classes.
©University of Sargodha Canal Campus Lahore
3
08/12/2014
Lecture 11
Object Oriented
Programing
Instructor: M.Imran Khalil
Resource: Imrankhalil3.wordpress.com ©University of Sargodha Canal Campus Lahore
Review of Last Lecture
In Previous lecture we learned
Inheritance between classes
Parent class is called Base Class and Child Class is called Derived class
There is a relation called “Is A” Or “Is a Kind of” between Base class and child class
Generalization
Extension
Specialization
©University of Sargodha Canal Campus Lahore
1
08/12/2014
Todays Topic
Practically implementation of Inheritance in C++
©University of Sargodha Canal Campus Lahore
Inheritance
It is the mechanism of deriving new class from existing
class.It provides the idea of reusability.
A B
Base , super,parent class
derived , sub, child class
2
08/12/2014
Inheritance Syntax The simplest example of inheritance requires two classes: a base
class and a derived class.
The base class OR super-class does not need any special syntax.
The derived class OR subclass on the other hand, must indicate that it’s derived from the base class.
This is done by placing a colon after the name of the
derived class, followed by a keyword such as public and then the base class name.
Syntax of inheritance in c++
Keyword Child class name
Parent class name
Class Child: public parent
MODE OF
INHARITACE
3
Lecture 12
Object Oriented
Programing INSTRUCTOR: M.IMRAN KHALIL [email protected] RESOURCE: IMRANKHALIL3.WORDPRESS.COM
Todays topic
Stream Classes
File handling
16/6/2013
©U
niv
ersity
o
f Sa
rgo
dh
a C
an
al C
am
pu
s Lah
ore
1
16/6/2013
Stream Classes
A stream is a general name given to a flow of
data
In C++ a stream is represented by an object of a particular class.
So far we’ve used the cin and cout stream objects.
Why using stream classes ??
Stream class hierarchy
2
16/6/2013
Cont…
The ios class is the base class for the hierarchy.
contains many constants and member functions common to input and output operations of all kinds.
The istream and ostream classes are derived from ios and are dedicated to input and output,
The istream class contains such functions as get(), getline(), read(), and the overloaded extraction (>>) operators, while ostream contains put()and write(), and the over-loaded insertion (<<) operators.
Disk File I/O with Streams Most programs need to save data to disk files and read it back in.
Working with disk files requires another set of classes:
ifstream for input, fstream for both input and output.
ofstream for output. Objects of these classes can be associated with disk files, and we can use
their member functions to read and write to the files.
Ifstream is derived from istream.
fstream is derived from iostream, and ofstream is derived from ostream.
3
17/6/2013
Lecture 13
Object Oriented
Programing
Instructor: M.Imran Khalil
Resource: Imrankhalil3.wordpress.com ©University of Sargodha Canal Campus Lahore
Todays Lecture
Composition
Aggregation
©University of Sargodha Canal Campus Lahore
1
17/6/2013
Composition
An object may be composed of other smaller objects
The relationship between the “part” objects and the “whole” object is known as Composition
Composition is represented by a line with a filled-diamond head towards the composer object
©University of Sargodha Canal Campus Lahore
Example – Composition of Ali
Head
1
Arm 2
Ali 2
Leg
1
©University of Sargodha Canal Campus Lahore Body
2
17/6/2013
Example – Composition of Chair
Back
1
Chair
2 1 4
©UniversityArm of Sargodha Canal Campus Lahore Seat Leg
Composition: Objects as Members of
Classes
Composition
Composition is “consists of” relation Ship
A class can have objects of other classes as members
©University of Sargodha Canal Campus Lahore 3
15/12/2014
Lecture 15
Object Oriented
Programing
Instructor: M.Imran Khalil
Resource: Imrankhalil3.wordpress.com ©University of Sargodha Canal Campus Lahore
Today’s Lecture
Static and Dynamic Binding
Virtual and Pure Virtual Functions
Abstract Class
Multiple Inheritance
Problems with Multiple inheritance
©University of Sargodha Canal Campus Lahore
1
15/12/2014
Dynamic Method Binding Inheritance in OOP opens up many interesting properties.
One of the main advantages of inheritance is that some derived class D has all of the members of its base class B.
Once D is not hiding any of the public members of B, then it is possible for an object of D to represent B in any context where a B could be used.
This feature is known as subtype polymorphism.
Lets illustrate an example:
©University of Sargodha Canal Campus Lahore
An Example of Dynamic Binding
class person {
… void person :: print() { // prints details for the person }
}; class student : public person {
… void student :: print () { //adds more specific information }
}; class lecturer : public person {
… void lecturer :: print () { //adds more details }
}; …
new student (); person *x = s;
lecturer (); person *y = t; s -> print (); x -> print () ;
©University of Sargodha Canal Campus Lahore t -> print (); y -> print () ;
This is a polymorphic call
2
15/12/2014
Example cont…
person *x = s;
person *y = t; • -> print () ; • -> print () ;
Does the choice of the method to be called depend on the types of x and y ?
If so then this is known as static binding.
Does the choice of the method to be called depend on the classes of the objects s and t to which those variables refer ?
If so then is known as dynamic binding.
Dynamic binding is central to OOP and ensures that even if we are using the base class to refer to a child class, the correct methods will always be called.
©University of Sargodha Canal Campus Lahore
Virtual Methods
C++ uses static binding by default.
Hence we must explicitly state that we wish to use dynamic binding.
To do this we can use the virtual keyword.
By changing the code in the previous example to look like
this: class person { … virtual void print () { // print person details
} }; With virtual methods, calls are dispatched to the appropriate implementation at
runtime, based upon the class of the object.
©University of Sargodha Canal Campus Lahore
3
A
c
t
i
v
a
t
e
y
o
u
r
s
o
f
t
w
a
r
e
f
o
r
l
e
s
s
t