You are on page 1of 45

1

Object Oriented
Programming

LECTURE # 1
Course / Lab Marking 2

 Assignment
 Lab Tasks
 Open Lab
 Semester Project
 Mid Term
 Final Exam
Policies 3

 Join class/ lecture in time.


 Exams will be closed book.
 Never cheat.
 “Better fail NOW or else will fail somewhere LATER in life”
Adapted from What is Plagiarism PowerPoint
http://mciu.org/~spjvweb/plagiarism.ppt
 Plagiarism will also have strict penalties.
 AllSubmissions will be on LMS. Submissions through any other
means will not be accepted.
Important 4

 Attendance of Lab will be monitored separately and should meet NUST


Attendance i.e. ( > = 75% ) criteria to avoid XF.
COURSE LEARNING OUTCOMES 5

Course Learning Outcome (CLOs) Learning


PLOs Level
 

CLO 1 Explain the principles of the object oriented programming paradigm


specifically including abstraction, encapsulation, inheritance and
2 C2
polymorphism

CLO 2 Use an object oriented programming language, and associated class libraries, to
develop object oriented programs. 3 C3

CLO 3 Implement, test, and debug programs using object oriented principles in
conjuncture with an integrated development environment. 4 P2

CLO 4 Create appropriate diagrams and textual descriptions to communicate the static
structure and dynamic behaviour of an object oriented solution. 3 C4
Books 6

 Text book
 Deitel and Deitel, “C++ How to Program,” 7th Edition, ISBN-10: 0-13-611726-0, Prentice Hall,
2009

 Reference Books
 Robert Lafore, “Object Oriented Programming with C++,” 4th Edition, ISBN-10: 0-672-32308-
7, Sams, 2002
 Goran Svenk, “Object-Oriented Programming Using C++ for Engineering and Technology,” 3rd
Edition, 2003
OFFICE HOURS 7

Office # 01, Faculty Cabin, First Floor, Department of Computer & Software
Engineering,

Day Timings
Tuesday 1230 -1430 hrs
Wednesday 1230 – 1430 hrs
Thursday 1230 – 1430 hrs
8
9

https://medium.com/@chris_38599/programming-is-magic-bd61c089729e
Computer Program 10
Programming Paradigms 11

 Different approaches to build solutions for specific problems


#include<iostream>
Using namespace std; 12
Int g;
Float add(float d, float e)
{ 5 7
d=d+2; // 7
return d+e;
}
Void main()
{
float a,b;
cin>>a; cin>>b; //5 7
Cout<<add(a,b); // 5 7
Cout<<a;
}
#include<iostream>
Using namespace std; 13
Float add( float c, float d )
{
return c+d;
}
Void main()
{
Float a,b;
cin>>a;
cin>>b;
add(a,b);

}
Programming Paradigms 14

 Procedural
 Object Oriented
Procedural Programming 15

 Using function
 Function & program is divided into modules
 Every module has its own data and function which can be called by other
modules.
Procedural Programming Language 16
Example 17

void main() void getInput(float &rad)


{ { cin>>rad;}
float rad1, rad2, rad3;
getInput(rad1); float calculateCircum(float rad)
getInput(rad2); { return 2*PI*rad;}
getInput(rad3);
cout<<calculateCircum(rad1); float getDia(float rad)
cout<<calculateCircum(rad2); { return rad*2;}
cout<<calculateCircum(rad3);
}
Class Task 18

Write a program in C++ with following


functionalities
⁻ Take 2 complex numbers from user
⁻ Add 2 complex numbers
⁻ Subtract 2 complex numbers
⁻ Display a complex number in a + bi format
⁻ Compute Magnitude of a complex Number
How can you save complex number in form a +
bi?
Add (float r1,float i1,float r2, float i2) Void main()
{ {
float r3,i3; float a,b,c,d; 19
r3=r1+r2; input(a,b); //
i3=i1+i2; output(a,b); //
Output(r3,i3); input(c,d); // 5 6
} Add(a,b,c,d);
Sub(a,b,c,d);
Sub (float r1,float i1,float r2, float i2)
{
float r3,i3; }
r3=r1-r2;
i3=i1-i2;
Output(r3,i3); Void output(float r, float i)
} {
cout<<r<<“+”<<i<<“i”<<endl;
Void input( float &r, float& i) }
{
cout<<“Enter real part”;
Cin>>r; //7
Cout<<“Enter imag part”;
cin>>i; //9
}
Struct complexNumber
20
{
floar r,i;

// functions
};

Void main()
{
complexNumber a;
}
What is Object Oriented Programming 21

Object-oriented programming(OOP) refers to a type of computer programming


(software design) in which programmers define the data type to represent an
entity, and also the types of operations (functions) that can be applied to the entity.
In this way, the entity becomes an object that includes both data and functions.
In addition, programmers can create relationships between one object and another.
For example, objects can inherit characteristics from other objects.
What is Object Oriented Programming 22
Data types 23

Basic, built-in, pre-defined types: char, int, double, …

Variables + operations on them


int a, b,c;
c=a+b;
c=a mod b;

programmer-defined types: classes

Variables  objects
Types  classes
Design a programmer defined datatype 24

 Structures
 Classes
Structures and Classes 25

 To define a data type having more than one pre-defined data types

 A Structure or a class serves only as a plan, or a template, or sketch- of a


number of similar things (i.e. objects)

 A Structure or a class is a mechanism for creating programmer-defined data


types.
Syntax 26

struct name { class name {


// attributes // attributes
}; };
Objects 27

 A structure and an object of that structure has the same relationship


as a data type and a variable

 All objects with the same characteristics constitute one structure.


Object Instantiation 28

Once you create a struct type, you can declare one or more objects of that class
type. For example:
Struct circle{ class circle {
// struct body // class body
}; };
void main() void main()
{ {
circle c; circle c;
} }
Objects ?
 An object is an instance of a structure

Objects
Attributes

Professor Smith

Professor Mellon

Professor Jones
Contd. 30
31
Contd.
 Informally, an object represents an entity, either physical, conceptual, or
software.

― Physical entity
Truck
Chemical
Process
― Conceptual entity
Linked List

― Software entity
Review 32

 Two Programming Paradigms


PROCEDURAL vs. OBJECT- 33

ORIENTED LANGUAGES
 Procedural Language
 Views a program as a series of steps to be carried out
 E.g. C, FORTRAN, Pascal
 Object Oriented Language
 Views a program as a group of objects that have certain properties and can
perform certain functions
 E.g. C++, Java, C#
PROCEDURAL VS. OBJECT- 34
ORIENTED LANGUAGES
 Problems with Procedural Languages
 Cannot cope with very large project sizes
 Expensive software errors
 Causes of Problems
 Unrestricted Access to Data
 Global data is allowed
 Access to data by multiple functions means many connections between functions
 Programs become difficult to understand, modify and maintain
PROCEDURAL VS. OBJECT- 35
ORIENTED LANGUAGES

 Poor Modeling of Real World Things


 Real world things are integral collections of data and functions
 e.g. a car: has data (make, model etc.) and functions (acceleration)
 Procedural languages do not tie up data with functions
GOAL OF OOP 36

 Clearer, more reliable, more easily maintained programs


 More effective way of coping with program complexity
OBJECT-ORIENTED LANGUAGES 37

 C++
 Most widely used
 Largest programmer base
 Java
 Lacks certain features, e.g. multiple inheritance, pointers, templates
 Less powerful than C++ (but more safe, of course)
 C#
 Emerging
 Will be covered later in PLE course
THE OO APPROACH 38

 The fundamental idea is to combine into a single unit both data and functions
that operate on the data.
 Such a unit is called an “Object”.
 An object’s functions are called “member functions” or “methods” in C++
 And its data is called “ data members”.
THE OO APPROACH 39

 An object’s data is typically accessed through its member functions, i.e. it is


hidden from accidental alteration
 Data and its function are said to be encapsulated into a single entity (i.e. the
object)
 Data encapsulation and data hiding are key elements of object-oriented
languages
THE OO APPROACH 40

 If you want to modify data in an object, you know exactly what


functions interact with it (i.e. the member functions of the object).
 This simplifies writing, debugging, and maintaining the programs
 An OO program consists of a number of objects which communicate
with each other through member functions
Analogy of a Company 41

 If sales guy needs to know how much has he sold, he does not go into
the finance department and start going through the files.
 He asks (letter) from finance section to provide him the details
 This allows the data integrity/privacy
CHARACTERISTICS OF OO 42

LANGUAGES
 Designing new data types (Classes)
 Instantiating the data types (Objects)
 Inheritance
 Reusability
 Polymorphism and overloading
 Data Abstraction
 Encapsulation
Object-Oriented Programming 43

 Advantages:
 Reusable
 OOP: well designed module could be used on future projects.
 Procedural Programming: repeatedly reinvent the wheel.
 Maintenance
 OOP: modular, easy to maintain, interchanged as units without disassembly of the
modules.
 Procedural Programming: not well modularized, change the structure, start from
scratch.
Basic Component of OOP 44

 Programmer defined datatypes


 Structures
 Classes
Class Task 45
1. Create a datatype Rectangle, where a Rectangle is defined by Length & Width. Your
Program should be capable to compute:
 Area (LxW)
 Perimeter 2(L+W)
 compare two rectangles with respect to the area and return true if two rectangles are equal, False
otherwise
2. Create a data type Student, where properties associated with each student are his
Name, Registration Number, Father Name, Degree and Department. One can view
the details of any student and can also overwrite the details.
3. Make a datatype “Cube”. Choose appropriate data members. Your program should
be able to calculate:
 Surface Area of cube (formula area= 6a2)
 Volume of cube (volume = a3)

You might also like