Date post: | 13-Dec-2015 |
Category: |
Documents |
Upload: | preston-norris |
View: | 217 times |
Download: | 1 times |
What is PP ?
• Revision of Programming concepts learned in CPLB
• Learning how to perform “Object-Oriented Programming”
• This course doesn’t teach C• However we will be using C only as a tool to
understand OOP
What is Programming ?
• Computers are designed to execute a set of instructions.
• Programming is the act of creating a set of instructions for a computer to execute.
• A set of instructions that performs a specific task is called an algorithm.
Types of Programming Language
• Procedural Languages– C, Pascal, Fortran, Basic
• Object-Oriented Languages– C++, C#, JAVA, Python
Procedural Languages
• A program in a procedural language is basically a list of instructions
• As programs become larger and complex they are divided into functions
• Breaking program into different independent functions is called Structural Programming
Structured Programming
• Large number of potential connections between functions and • Data (everything is related to everything, no clear boundaries)
• makes it difficult to conceptualize program structure• makes it difficult to modify and maintain the program
• For e.g. It is difficult to tell which functions access the data
global data Y
Function A:
local data
Function B:
local data
Function C:
local data
global data X global data Z
Problems with Structured Programming
• Data and function are considered as two separate entities• Makes it difficult to model things in the real world
• Complex real world objects have both attributes and behaviours
“Object” in OOP
What is an Object ?
An object is a single identity which compromises of following concepts;• characteristics• responsibilities (or behaviors)
• Or simply
Object = Data + Function
Object• Attributes/Data/Facts
• People: Name, DOB, Height, Weight ...• Cars: Brand, Model, Horse Power, Color ...
• Behaviours/Actions/Functions• People: Ask a person to bring glass of water• Cars: Apply the brakes
ObjectProblem Computation modeling in biology
Write a program that simulates the growth of virus population in humans over time. Each virus cell reproduces itself at some time interval. Patients may undergo drug treatment to inhibit the reproduction process, and clear the virus cells from their body. However, some of the cells are resistant to drugs and may survive.
ObjectProblem Computation modeling in biology
Write a program that simulates the growth of virus population in humans over time. Each virus cell reproduces itself at some time interval. Patients may undergo drug treatment to inhibit the reproduction process, and clear the virus cells from their body. However, some of the cells are resistant to drugs and may survive.
What are objects? Characteristics? Responsibilities?
ObjectProblem Computation modeling in biology
Write a program that simulates the growth of virus population in humans over time. Each virus cell reproduces itself at some time interval. Patients may undergo drug treatment to inhibit the reproduction process, and clear the virus cells from their body. However, some of the cells are resistant to drugs and may survive.
What are objects? Characteristics? Responsibilities?
Classes
• A class is like a cookie-cutter; it defines the shape of object
• Objects are like cookies; they are instances of the class
Classes versus Objects
• A class is a prototype specification from which one can generate a number of similar objects
• A class can be considered as an object factory
• An object is said to be a instance of a class
Example of a Class in C++
class student //declares a class { private: int id, age; //class data public: int getage() //method to get age of object {
return (age); }
}
Class and its Objects
person data: name, address, age methods: getage()
Class
person data: Khalid, Clifton, 24
person data: Rashid, Tariq Road, 25
person data: Ali, N.Nazimabad, 28
Object Oriented ApproachObject
data
functions
• Encapsulation:Integration data and functions into one object.
•Data hiding: Data is hidden to the outside world and can only be accessed via the object functions
• The functions within the object are called Methods
• Data items are called Attributes
Object Oriented Approach
Object A
data
functions
Object C
data
functions
Object B
data
functions
Separation: Objects interact with each other only via the their methods
Characteristics of OOP
• Abstraction is the representation of the essential features of an object. These are then used to create a Class
• Encapsulation is the practice of including in an object everything it needs hidden from other objects. The internal state is usually not accessible by other objects.
Characteristics of OOP
• Inheritance means that one class inherits the characteristics of another class.
This is also called a “is a” relationship:
• A car is a vehicle• A teacher is a person• A student is a person
Inheritance
Vehiclewheelsengine
Carwheelsenginetrunk
Truck
wheelsenginetrailertrunk trailer
Super-class
sub-classes orderived classes
•The principle in this sort of division is that each sub-class shares some common features with the base class from which it is derived, but also has its own particular features.
Truck
Inheritance
Vehiclebrake()
start_engine()Car
brake()start_engine()open_door()pull_trailer()
Super Class
sub-classes orderived classes
• A sub-class also shares common methods with its super-class but can add its own methods or overwrite the methods of its super-class.
brake()start_engine()open_door()open_trunk()
Inheritance
Terminology: • Car is a sub-class (or derived class) of Vehicle • Car inherits from Vehicle• Car is a specialization of Vehicle• Vehicle is a super-class (or base class) of Car• Vehicle is a generalization of Car
Reusability
• Reusability means that a class that has been designed, created and debugged once can be distributed to other programmers for use in their own programs.
• Similar to the idea of a library of functions in a procedural language.
29
Basic Terminology:Polymorphism
• Polymorphism means “having many forms”. It allows different objects to respond to the same message in different ways, the response specific to the type of the object.
• E.g. the method displayDetails() of the Person class should give different results when send to a Student object (e.g. the enrolment number).
Basic Program Structure - Class
• A class definition begins with the keyword class.
• The body of the class is contained within a set of braces, { } ; (notice the semi-colon).
class class_name{
….….….};
Class body (data member + methods)
Any valid identifier
Classes in C++
• Within the body, the keywords private: and public: specify the access level of the members of the class.– the default is private.
• Usually, the data members of a class are declared in the private: section of the class and the member functions are in public: section.
Classes in C++
class class_name{ private:
………
public:………
};
Public members or methods
private members or methods
Classes in C++• Member access specifiers– public: • can be accessed outside the class directly.– The public stuff is the interface.
– private:• Accessible only to member functions of class• Private members and methods are for internal use only.
Class Example
• This class example shows how we can encapsulate (gather) a circle information into one package (class)
class Circle{ private:
double radius; public:
void setRadius(double r);double getDiameter();
double getArea();double getCircumference();
};
No need for others classes to access and retrieve its value directly. Theclass methods are responsible forthat only.
They are accessible from outsidethe class, and they can access themember (radius)
Creating an object of a Class
• Declaring a variable of a class type creates an object. You can have many variables of the same type (class).– Creating Instance
• Once an object of a certain class is created, a new memory location is created for it to store its data members and code
• You can create many objects from a class type.– circle smallcircle; – circle bigcircle;
36
The two steps of Object Oriented Programming
• Making Classes: Creating, extending or reusing abstract data types.
• Making Objects interact: Creating objects from abstract data types and defining their relationships.