Lecture Intro
Lecture Intro
Programming
Introduction
Dr Khuram Shazhad
[email protected]
Office: A-308
Outline
• Course Objectives
• Learning Outcomes
• Course Outline
• Labs
• Grading
• Books
Course Objectives
Theory - 75%
40% OHTS - 02
10% Quizzes - 06
10% Assignments - 03
40% Final Exam
Labs - 25 %
70% Lab Tasks
30% Project
Books
Thursday: 11 am – 01pm
(Any other day – By appointment only)
Intro. to Objects
At the end of the day…
Computers just manipulate 0’s and 1’s
Assembly languages
Binary code
There are always trade-offs
• High-level languages
– Simpler to write & understand
– More library support, data structures
• Low-level languages
– Closer to hardware
– More efficient
Programming Programming
provides paradigm is a
solution(program) to mental view of the
a real-world
problem and its
problem using
computational solution
models
Procedural Programming
f()
f()
Procedural Programming
f() x
f()
x
Procedural Programming
Procedural Programming
SPAGHETTI CODE
OOP Approach
• The fundamental idea behind object-oriented
language is to combine into a single unit both
– data and
– the functions that operate on that data.
• Such a unit is called an object
Object Oriented Programming (OOP)
f() x
f()
x
OOP Approach
Object Oriented Programming (OOP)
Paradigm
• “Object Oriented programming is an approach
of problem solving where all computations
are carried out using objects”
Object?
Object?
Object
Attributes & Behaviors in OOP
Attributes Behavior
age Walk()
Name Run()
address Teach(
)
Class
• Close your eyes for a minute and think about what do
you understand when you hear the word “chair”
• However, if you look around the room, there are several chairs are
in the room, and each chair is an object. Each of these objects is an
example of that thing called the Chair (class)
The things that are tangible and exist are the Objects.
Objects may belong to a classification.
What is an Object in OOP ?
• It has a door
COLOR,
Attribute(Variables) SURFACE,
DIMENSION
Court
DO COURT BOOKING(),
Functionality(Functions) COURT CLEANING
Student Management
System
Roll number
Attributes Name
Marks
Student
Curricular
performan
ce
Behavior:
E
xtra curricular
performance
Basic Structure C and C++
Class
• A class is a programmer-defined data type. It consists
of data members and functions which operate on that
data.
– Data Members define Object attributes
– Member Functions define Object Behavior
};
C++ Class – Example
class Time {
private: Private:and Public:
are member-access
int hour; // 0 - 23 specifiers.
int minute; // 0 - 59
Time();
setTime, printMilitary,
void setTime( int, int, int ); and printStandard are
member functions.
void printMilitary(); Time is the constructor.
void printStandard();
};
C++ Class – Body
• Every piece of code inside the braces is
called
‘class body’
• The class body thus contains class members
C++ Class – Body
• Every piece of code inside the braces is
called
‘class body’
• The class body thus contains class members
C++ Class – Body
• Every piece of code inside the braces is
called
‘class body’
• The class body thus contains class members
C++ Class – Body
• Every piece of code inside the braces is
called
‘class body’
• The class body thus contains class members
Class
Class Student
{
– Attributes
• Roll number
• Name
• Marks
– Behavior:
• Curricular performance
• Extra curricular performance
};
57
C++ Class – Body
C++ Class – Class
•
Members
A class’s declaration declares the class’s
members,
which may consist of
• The data members
• The constructor & destructor functions
• Specialized member functions
• Other member functions
• Include the functionality that can be invoked on the object
of
this class (or type)
C++ Class – Data
• Members
A data member can be of any valid C++ data
type. For example
– Ordinary variable – primitive type
– An instance variable of another class –
programmer defined type
– A pointer or reference variable
C++ Access Modifiers
• Three types of Access Modifiers:
private:
public:
protected
:
• The scope of an access modifier continues until
another access modifier is mentioned explicitly
• There is a colon after every access modifier
C++ Access Modifiers
class Base {
public:
//
public
mem
bers
go
here
prote
cted:
public:
• Public members can be accessed by
member functions as well as by functions that
declare the objects of that class.
• Normally, for classes, those member functions are
made public which must interact with the outside
world.
private:
• Private members can only be accessed by member
functions.
– This means they can only be accessed in the member
function body.
• Remember the ‘members’ mean both variables
and functions.
• By default all class members are private, unless
declared otherwise.
public: OR private:
• It is not compulsory for all members functions to be
public and all data members to be private.
• Some member functions can also be private.
• Similarly, data members can also be public.
– Violation of Encapsulation principle