SlideShare a Scribd company logo
1 of 55
Introduction to Object Technology,
Introduction to C++ Programming
Lecture No 2
COMSATS Institute of Information & Technology
ď‚—Reference Books:
ď‚—Object Oriented Programming in C++, Robert Lafore
ď‚—C++ How to Program, Deitel & Deitel
ď‚—Object oriented programming, Ashok N. Kamthane

2

COMSATS Institute of Information Technology
Object Oriented Programming
High-level Languages
Common programming languages include …

C C++ Java Pascal Visual Basic FORTRAN
COBOL Lisp Scheme Ada
These high – level languages
ď‚—Resemble human languages
ď‚—Are designed to be easy to read and write
ď‚—Use more complicated instructions than
the CPU can follow
ď‚—Must be translated to zeros and ones for the CPU
to execute a program
3

COMSATS Institute of Information Technology
Object Oriented Programming
Low-level Languages
ď‚—An assembly language command such as

ADD X Y Z
might mean add the values found at x and y
in memory, and store the result in location z.
ď‚—Assembly language must be translated to

machine language (zeros and ones)
0110 1001 1010 1011
ď‚—The CPU can follow machine language
4

COMSATS Institute of Information Technology
Object Oriented Programming
5

COMSATS Institute of Information Technology
Object Oriented Programming
Compilers
ď‚—Translate high-level language to

machine language
ď‚—Source code
ď‚— the original program in a high level language

ď‚—Object code
ď‚— the translated version in machine language

6

COMSATS Institute of Information Technology
Object Oriented Programming
Linkers
ď‚—Some programs we use are already compiled
ď‚—Their object code is available for us to use
ď‚—For example: Input and output routines

ď‚—A Linker combines
ď‚—The object code for the programs we write

and
ď‚—The object code for the pre-compiled routines
into
The machine language program the CPU can run

7

COMSATS Institute of Information Technology
Object Oriented Programming
Programming
and Problem Solving
ď‚—Algorithm
ď‚—A sequence of precise instructions which

leads to a solution
ď‚—Program
ď‚—An algorithm expressed in a language the computer

can understand

8

COMSATS Institute of Information Technology
Object Oriented Programming
Program Design
ď‚—Programming is a creative process
ď‚—No complete set of rules for creating a program

ď‚—Program Design Process
ď‚—Problem Solving Phase
ď‚— Result is an algorithm that solves the problem

ď‚—Implementation Phase
ď‚— Result is the algorithm translated into a programming

language

9

COMSATS Institute of Information Technology
Object Oriented Programming
Problem Solving Phase
ď‚—Be certain the task is completely specified
ď‚—What is the input?
ď‚—What information is in the output?
ď‚—How is the output organized?

ď‚—Develop the algorithm before implementation
ď‚—Experience shows this saves time in getting your

program to run.
ď‚—Test the algorithm for correctness

10

COMSATS Institute of Information Technology
Object Oriented Programming
Implementation Phase
ď‚—Translate the algorithm into a programming

language

ď‚—Easier as you gain experience with the language

ď‚—Compile the source code
ď‚—Locates errors in using the programming language
ď‚—Run the program on sample data
ď‚—Verify correctness of results
ď‚—Results may require modification of

the algorithm and program

11

COMSATS Institute of Information Technology
Object Oriented Programming
12

COMSATS Institute of Information Technology
Object Oriented Programming
Procedural Languages
•

•
•
•
•

Examples of procedural languages: C, Pascal, Fortran
A program in a procedural language is basically
a list of instructions
As programs become larger they are usually broken down
into smaller units, such as functions, procedures, subroutines
Functions can be grouped together into modules according
to their functionality, objectives and tasks.
Structured programming is a programming paradigm that
to a large extent relies on the idea of dividing a program
into functions and modules.

13

COMSATS Institute of Information Technology
Object Oriented Programming
Problems with Structured Programming
• Functions have unrestricted access to global data
Function A:

Function B:

Function C:

local data

local data

local data

global data X

global data Y

• 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
e.g. : it is difficult to tell which functions access the data
14

COMSATS Institute of Information Technology
Object Oriented Programming

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
• attributes
• people: name, date of birth, eye color, job title
• cars: horse power, number of doors, color
• behaviours
• people: ask a person to bring you a beer
• cars: apply the brakes, turn on the engine
• attributes and behaviors alone are sufficient to realistically
COMSATS Institute of Information Technology
15
Object Oriented Programming
model real world objects but a unified view is needed
Object Oriented
Programming
ď‚—Abbreviated OOP
ď‚—Used for many modern programs
ď‚—Program is viewed as interacting objects
ď‚—Each object contains algorithms to describe its behavior
ď‚—Program design phase involves designing objects and

their algorithms

16

COMSATS Institute of Information Technology
Object Oriented Programming
OOP Characteristics
ď‚—Encapsulation
ď‚—Information hiding
ď‚—Objects contain their own data and algorithms
ď‚—Inheritance
ď‚—Writing reusable code
ď‚—Objects can inherit characteristics from other objects
ď‚—Polymorphism
ď‚—A single name can have multiple meanings depending
on its context
17

COMSATS Institute of Information Technology
Object Oriented Programming
Object Oriented Approach
Object
functions

data

• Encapsulation: integrate data and functions into one object
• Data hiding : data is hidden to the outside world and can
only be accessed via the functions
• In C++ functions are called membership functions in other
languages they are called methods
• Data items are called attributes or instance variables
18

COMSATS Institute of Information Technology
Object Oriented Programming
Object Oriented Approach
• separation: objects interact with each other only via their membership functions
• separation helps to maintain the integrity of the entire
program

Object A
functions

Object C
functions

data
19

COMSATS Institute of Information Technology
Object Oriented Programming

data

Object B
functions

data
Abstraction
• An abstraction is a named collection of attributes and
behavior relevant to model a given entity for some
particular purpose
real-world

abstraction

attributes

software

{data, data,…}

entity

behavior
20

COMSATS Institute of Information Technology
Object Oriented Programming

{ method, method…}
Separation
• independent specification of a visible interface and
a hidden implementation
• interface is some kind of contract between the object
and the user of this object or module
• separation is not restricted to object-oriented programming
for example header files in standard C can be regarded
as interfaces

visible

hidden
21

COMSATS Institute of Information Technology
Object Oriented Programming

interface

Implementation
Structure of an Object
Implementation

Interface

Object
method

method

code

code
data

method
method
22

code
code

COMSATS Institute of Information Technology
Object Oriented Programming
Examples of Objects

23

• physcial objects
• vehicles in a traffic-flow simulation
• electrical components in a circuit-design program
• elements of a computer user environment
• menus
• graphic objects
• data-storage constructs
• arrays
• linked lists
• human entities
• employees
• students
• collections of data
• an inventory
• an address book
• user defined data types
COMSATS Institute of Information Technology
• time
Object Oriented Programming
• complex numbers
Example of a Class in C++
class someobject
//declares a class
{
private:
int somedata;
//class data
public:
void setdata(int d) //membership function to set data
{ somedata=d; }
int getdata()
//membership function to get data
{ return somedata; }
}
24

COMSATS Institute of Information Technology
Object Oriented Programming
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 member or instance of a class
• A class can be considered as a more complex
data structure than an ordinary built-in data type
• Standard C already knows the struct command for
user defined data types:
struct complex
{
double re;
double im;
};
complex x;

25

COMSATS Institute of Information Technology
Object Oriented Programming
Instantiation of Objects
person
data: name, p_nummer, address, date of birth
methods: getage(), changeaddress(newaddress)

Class

person
data: Lena Brat, 761203-7111, Stureplan 4, female
person
data: Erik Olsson, 780605-4789, Hamngatan 3, male
person
data: Lars Backe, 671110-A562, Mälartorget 19, male
26

COMSATS Institute of Information Technology
Object Oriented Programming
Relationships among Objects
• Attribute:

27

One object uses as another object as an attribute,
namely as member data, for example a Person
contains an attribute Name. This type of relationship is also called a weak association or has-a
relationship. Example: A Person has a Name
• Association:
One object uses another to help it carry out a task.
Classes that collaborate are usually related through
associations. This type of relationship is also
called a uses relationship.
Example: The object Driver invokes
COMSATS Institute of Information Technology
the method Brake
Object Oriented Programming of the object BrakingSystem.
Relationships among Objects
• Aggregation:
Aggregation means that one object contains other
objects. Aggregation is also called part-of relationship.
Example: The class Adressbook contains many People
Objects.
• Composition:

Composition is building objects from parts. It is a stronger
type of aggregation in which the parts are necessary to
the whole, namely they are permanently bound to the
object and do not exist outside the object.
A class Processor contains a CPU, Memory and I/O-Ports.
28

COMSATS Institute of Information Technology
Object Oriented Programming
Relationships among Objects
• Generalization

Generalization is a relationship defined at the class level
not the object level, which means that all objects of
this class must obey the relationship. This is type of
relationship is also called a is-a-kind-of relationship.
Generalization in object oriented languages is realized
by means of inheritance.
Example: A car is a kind of vehicle.

29

COMSATS Institute of Information Technology
Object Oriented Programming
Inheritance
• In our daily lives we use the concept of classes divided
into subclasses, for example vehicles are divided into cars,
trucks, buses and motor cycles.
• 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.

base class
Vehicle
wheels
engine
Car

30

wheels
sub-classes or
engine
COMSATS Institute of Information Technologyderived classes
trunk
trunk
Object Oriented Programming

Truck
wheels
engine
trailer
trailer
Inheritance
• 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.

base class
Vehicle
brake()
start_engine()
Car
brake()
start_engine()
open_door()
open_door()
31

COMSATS Institute of Information Technology
Object Oriented Programming

Truck
sub-classes or
derived classes

brake()
start_engine()
open_door()
pull_trailer()
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
• In C++ an object of a sub-class is substitutable
for an object of the super-class, in other words
an object of class Car can be used whenever
an object of class Vehicle is required.
32

COMSATS Institute of Information Technology
Object Oriented Programming
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.
• The concept of inheritance provides an important extension
to the idea of reusability, as it allows a programmer to take
an existing class without modifying it and adding additional
features and functionality. This is done by inheriting a new
sub-class from the exisiting base class.

33

COMSATS Institute of Information Technology
Object Oriented Programming
Polymorphism & Overloading
• Polymorphism : using functions and operators in different
ways, depending on what they are operating on.
• Polymorphism allows it to manipulate objects without
knowing their exact type but only their common property.
for example, the classes Triangle and Circle both
have their own (polymorphic) version of the method Draw,
but a graphic routine that draws graphical elements does
not have to know which object it manipulates.
• Overloading: an existing operator, such as + or = is given
the capability to operate on a new data type, for example
define the operator + for the class Complex such that
it realizes the addition of two complex numbers.
34

COMSATS Institute of Information Technology
Object Oriented Programming
Polymorphism
• polymorphism means ”having many shapes”
• in C++ it refers to a situation in which an object
could have any of several types
• a polymorphic variable can refer to objects of
different classes, for example a graphic object can
be either a circle or a triangle
• a polymorphic function or operator can take arguments of
different types
• example:
int max(int a, int b);
double max(double a, double b);
35

COMSATS Institute of Information Technology
Object Oriented Programming
C++ and C

36

• C++ is derived from the language C
• C++ is a superset of C, that means almost every
correct statement in C is also correct in C++
• The most important elements added to C are concerned
with classes, objects and object-oriented programming
• New features of C++
• improved approach to input/output
• standard template library (STL)
container classes for vectors, lists, maps, etc.
• reference type replaces pointers
• const variables replaces #define statements
• string data type replaces C-style strings char[]
• new comment style augments C-style comments /* */
COMSATS Institute of Information Technology
Object Oriented Programming
Software Life Cycle
1.
2.
3.
4.
5.

37

Analysis and specification of the task
(problem definition)
Design of the software
(object and algorithm design)
Implementation (coding)
Maintenance and evolution of the system
Obsolescence

COMSATS Institute of Information Technology
Object Oriented Programming
C++ History
ď‚—C developed by Dennis Ritchie at AT&T

Bell Labs in the 1970s.

ď‚—Used to maintain UNIX systems
ď‚—Many commercial applications written in c

ď‚—C++ developed by Bjarne Stroustrup at AT&T

Bell Labs in the 1980s.

ď‚—Overcame several shortcomings of C
ď‚—Incorporated object oriented programming
ď‚—C remains a subset of C++

38

COMSATS Institute of Information Technology
Object Oriented Programming
C++ Standard Library
ď‚—C++ programs
ď‚—Built from pieces called classes and functions

ď‚—C++ standard library
ď‚—Rich collections of existing classes and functions

“Building block approach” to creating programs
“Software reuse”

39

COMSATS Institute of Information Technology
Object Oriented Programming
Basics of a Typical C++ Environment
ď‚—C++ systems
ď‚—Program-development environment
ď‚—Language
ď‚—C++ Standard Library

40

COMSATS Institute of Information Technology
Object Oriented Programming
Basics of a Typical C++ Environment
Editor

Phases of C++ Programs:
1. Edit
2. Preprocess
3. Compile
4. Link
5. Load
6. Execute

Program is created in
the editor and stored
on disk.

Disk

Preprocessor

Disk

Preprocessor program
processes the code.

Compiler

Disk

Compiler creates
object code and stores
it on disk.

Linker

Disk

Loader

Disk

Primary
Memory

Linker links the object
code with the libraries,
creates a.out and
stores it on disk

Loader puts program
in memory.
.
.
.
.
.
.

Primary
Memory

CPU

41

COMSATS Institute of Information Technology
Object Oriented Programming

.
.
.
.
.
.

CPU takes each
instruction and
executes it, possibly
storing new data
values as the program
executes.
A Sample C++ Program
ď‚—A simple C++ program begins this way

#include <iostream>
using namespace std;
int main()
{
Statements;
}

42

return 0;

COMSATS Institute of Information Technology
Object Oriented Programming
#include <iostream>
using namespace std;
int main()
{
cout << “Hello Worldn”;
return 0;
}

43

COMSATS Institute of Information Technology
Object Oriented Programming
Example: Adding Two Integers
#include <iostream>
using namespace std;
// function main begins program execution
int main()
{
int integer1; // first number to be input by user
int integer2; // second number to be input by user
int sum;

// variable in which sum will be stored

cout << "Enter first integern"; // prompt
cin >> integer1;

// read an integer

cout << "Enter second integern"; // prompt
cin >> integer2;

// read an integer

sum = integer1 + integer2; // assign result to sum
cout << "Sum is " << sum << endl; // print sum
return 0; // indicate that program ended successfully
}

44

COMSATS Institute of Information Technology
Object Oriented Programming
ď‚—Variables
ď‚—Location in memory where value can be stored
ď‚—Common data types
ď‚— int - integer numbers
ď‚— char - characters
ď‚— double - floating point numbers
ď‚—Declare variables with name and data type before use
int integer1;
int integer2;
int sum;
ď‚—Can declare several variables of same type in one declaration
ď‚— Comma-separated list
int integer1, integer2, sum;
45

COMSATS Institute of Information Technology
Object Oriented Programming
ď‚—Variables
ď‚—Variable names
ď‚— Valid identifier
ď‚— Series of characters (letters, digits, underscores)
ď‚— Cannot begin with digit
ď‚— Case sensitive

46

COMSATS Institute of Information Technology
Object Oriented Programming
ď‚—Input stream object
ď‚—>> (stream extraction operator)
ď‚— Used with cin
ď‚— Waits for user to input value, then press Enter (Return) key
ď‚— Stores value in variable to right of operator
ď‚— Converts value to variable data type

ď‚— = (assignment operator)
ď‚— Assigns value to variable
ď‚— Binary operator (two operands)
ď‚— Example:

sum = variable1 + variable2;

47

COMSATS Institute of Information Technology
Object Oriented Programming
ď‚— Comments

ď‚—Document programs
ď‚—Improve program readability
ď‚—Ignored by compiler
ď‚—Single-line comment
ď‚— Begin with //

ď‚— Preprocessor directives

ď‚—Processed by preprocessor before compiling
ď‚—Begin with #

ď‚—Include Directives

#include <iostream>

ď‚—Tells compiler where to find information about items

used in the program
ď‚—iostream is a library containing definitions of cin and cout
48

COMSATS Institute of Information Technology
Object Oriented Programming
ď‚— Standard output stream object

ď‚—cout
“Connected” to screen
ď‚—<<
ď‚— Stream insertion operator
ď‚— Value to right (right operand) inserted into output stream

ď‚— Namespace

ď‚—Tells the compiler to use names in iostream in

a “standard” way
std:: specifies using name that belongs to “namespace” std
ď‚—std:: removed through use of using statements
ď‚— Escape characters

ď‚—
Indicates “special” character output

49

COMSATS Institute of Information Technology
Object Oriented Programming
Running a C++ Program
ď‚—C++ source code is written with a text

editor

ď‚—The compiler on your system converts

source code to object code.

ď‚—The linker combines all the object code

into an executable program.

50

COMSATS Institute of Information Technology
Object Oriented Programming
51

COMSATS Institute of Information Technology
Object Oriented Programming
52

COMSATS Institute of Information Technology
Object Oriented Programming
1.4

Testing and Debugging
ď‚—Bug
ď‚—A mistake in a program

ď‚—Debugging
ď‚—Eliminating mistakes in programs
ď‚—Term used when a moth caused a failed relay

on the Harvard Mark 1 computer. Grace Hopper
and other programmers taped the moth in logbook
stating:
“First actual case of a bug being found.”

53

COMSATS Institute of Information Technology
Object Oriented Programming
Program Errors
ď‚—Syntax errors
ď‚—Violation of the grammar rules of the language
ď‚—Discovered by the compiler
ď‚— Error messages may not always show correct location of

errors

ď‚—Run-time errors
ď‚—Error conditions detected by the computer at run-time
ď‚—Logic errors
Errors in the program’s algorithm
ď‚—Most difficult to diagnose
ď‚—Computer does not recognize an error
54

COMSATS Institute of Information Technology
Object Oriented Programming
Escape Sequence
n

Newline. Position the screen cursor to the
beginning of the next line.

t

Horizontal tab. Move the screen cursor to the next
tab stop.

r

Carriage return. Position the screen cursor to the
beginning of the current line; do not advance to the
next line.

a

Alert. Sound the system bell.



Backslash. Used to print a backslash character.

"

55

Description

Double quote. Used to print a double quote
character.

COMSATS Institute of Information Technology
Object Oriented Programming

More Related Content

What's hot

[OOP - Lec 02] Why do we need OOP
[OOP - Lec 02] Why do we need OOP[OOP - Lec 02] Why do we need OOP
[OOP - Lec 02] Why do we need OOPMuhammad Hammad Waseem
 
Functional modeling
Functional modelingFunctional modeling
Functional modelingPreeti Mishra
 
Software Engineering unit 3
Software Engineering unit 3Software Engineering unit 3
Software Engineering unit 3Abhimanyu Mishra
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programmingSandeep Kumar Singh
 
software engineering
software engineeringsoftware engineering
software engineeringramyavarkala
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programmingAmit Soni (CTFL)
 
CS8494 SOFTWARE ENGINEERING Unit-2
CS8494 SOFTWARE ENGINEERING Unit-2CS8494 SOFTWARE ENGINEERING Unit-2
CS8494 SOFTWARE ENGINEERING Unit-2SIMONTHOMAS S
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingMoutaz Haddara
 
Uml in software engineering
Uml in software engineeringUml in software engineering
Uml in software engineeringMubashir Jutt
 
Oose unit 1 ppt
Oose unit 1 pptOose unit 1 ppt
Oose unit 1 pptDr VISU P
 
Design Concept software engineering
Design Concept software engineeringDesign Concept software engineering
Design Concept software engineeringDarshit Metaliya
 
Object Oriented Analysis and Design
Object Oriented Analysis and DesignObject Oriented Analysis and Design
Object Oriented Analysis and DesignHaitham El-Ghareeb
 
Python unit 3 m.sc cs
Python unit 3 m.sc csPython unit 3 m.sc cs
Python unit 3 m.sc csKALAISELVI P
 
Introduction to object oriented language
Introduction to object oriented languageIntroduction to object oriented language
Introduction to object oriented languagefarhan amjad
 
PROCEDURAL ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMING
PROCEDURAL ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMING PROCEDURAL ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMING
PROCEDURAL ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMING Uttam Singh
 
1 unit (oops)
1 unit (oops)1 unit (oops)
1 unit (oops)Jay Patel
 
Chapter 01 software engineering pressman
Chapter 01  software engineering pressmanChapter 01  software engineering pressman
Chapter 01 software engineering pressmanRohitGoyal183
 
Software Engineering : Requirement Analysis & Specification
Software Engineering : Requirement Analysis & SpecificationSoftware Engineering : Requirement Analysis & Specification
Software Engineering : Requirement Analysis & SpecificationAjit Nayak
 
2 Object Oriented Programming
2 Object Oriented Programming2 Object Oriented Programming
2 Object Oriented ProgrammingPraveen M Jigajinni
 
Object and class relationships
Object and class relationshipsObject and class relationships
Object and class relationshipsPooja mittal
 

What's hot (20)

[OOP - Lec 02] Why do we need OOP
[OOP - Lec 02] Why do we need OOP[OOP - Lec 02] Why do we need OOP
[OOP - Lec 02] Why do we need OOP
 
Functional modeling
Functional modelingFunctional modeling
Functional modeling
 
Software Engineering unit 3
Software Engineering unit 3Software Engineering unit 3
Software Engineering unit 3
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
software engineering
software engineeringsoftware engineering
software engineering
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
CS8494 SOFTWARE ENGINEERING Unit-2
CS8494 SOFTWARE ENGINEERING Unit-2CS8494 SOFTWARE ENGINEERING Unit-2
CS8494 SOFTWARE ENGINEERING Unit-2
 
Introduction to Object Oriented Programming
Introduction to Object Oriented ProgrammingIntroduction to Object Oriented Programming
Introduction to Object Oriented Programming
 
Uml in software engineering
Uml in software engineeringUml in software engineering
Uml in software engineering
 
Oose unit 1 ppt
Oose unit 1 pptOose unit 1 ppt
Oose unit 1 ppt
 
Design Concept software engineering
Design Concept software engineeringDesign Concept software engineering
Design Concept software engineering
 
Object Oriented Analysis and Design
Object Oriented Analysis and DesignObject Oriented Analysis and Design
Object Oriented Analysis and Design
 
Python unit 3 m.sc cs
Python unit 3 m.sc csPython unit 3 m.sc cs
Python unit 3 m.sc cs
 
Introduction to object oriented language
Introduction to object oriented languageIntroduction to object oriented language
Introduction to object oriented language
 
PROCEDURAL ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMING
PROCEDURAL ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMING PROCEDURAL ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMING
PROCEDURAL ORIENTED PROGRAMMING VS OBJECT ORIENTED PROGRAMING
 
1 unit (oops)
1 unit (oops)1 unit (oops)
1 unit (oops)
 
Chapter 01 software engineering pressman
Chapter 01  software engineering pressmanChapter 01  software engineering pressman
Chapter 01 software engineering pressman
 
Software Engineering : Requirement Analysis & Specification
Software Engineering : Requirement Analysis & SpecificationSoftware Engineering : Requirement Analysis & Specification
Software Engineering : Requirement Analysis & Specification
 
2 Object Oriented Programming
2 Object Oriented Programming2 Object Oriented Programming
2 Object Oriented Programming
 
Object and class relationships
Object and class relationshipsObject and class relationships
Object and class relationships
 

Similar to Oop lec 2(introduction to object oriented technology)

OOP-1.pptx
OOP-1.pptxOOP-1.pptx
OOP-1.pptxiansebuabeh
 
A Modern Interface for Data Science on Postgres/Greenplum - Greenplum Summit ...
A Modern Interface for Data Science on Postgres/Greenplum - Greenplum Summit ...A Modern Interface for Data Science on Postgres/Greenplum - Greenplum Summit ...
A Modern Interface for Data Science on Postgres/Greenplum - Greenplum Summit ...VMware Tanzu
 
programacion orientado a abjetos poo
programacion orientado a abjetos pooprogramacion orientado a abjetos poo
programacion orientado a abjetos pooRasec De La Cruz
 
OOP Unit 1 - Foundation of Object- Oriented Programming
OOP Unit 1 - Foundation of Object- Oriented ProgrammingOOP Unit 1 - Foundation of Object- Oriented Programming
OOP Unit 1 - Foundation of Object- Oriented Programmingdkpawar
 
Unit 1 introduction to c++.pptx
Unit 1 introduction to c++.pptxUnit 1 introduction to c++.pptx
Unit 1 introduction to c++.pptxshashiden1
 
Principles of object oriented programing
Principles of object oriented programingPrinciples of object oriented programing
Principles of object oriented programingAhammed Alamin
 
Lecture-4: Introduction to Programming & Databases
Lecture-4: Introduction to Programming & DatabasesLecture-4: Introduction to Programming & Databases
Lecture-4: Introduction to Programming & DatabasesMubashir Ali
 
Need of OOPs and Programming,pop vs oop
Need of OOPs and Programming,pop vs oopNeed of OOPs and Programming,pop vs oop
Need of OOPs and Programming,pop vs oopJanani Selvaraj
 
Introduction to oop
Introduction to oop Introduction to oop
Introduction to oop Kumar
 
OODPunit1.pdf
OODPunit1.pdfOODPunit1.pdf
OODPunit1.pdfKrishMehta47
 

Similar to Oop lec 2(introduction to object oriented technology) (20)

C++
C++C++
C++
 
JAVA PROGRAMMINGD
JAVA PROGRAMMINGDJAVA PROGRAMMINGD
JAVA PROGRAMMINGD
 
OOP-1.pptx
OOP-1.pptxOOP-1.pptx
OOP-1.pptx
 
Part 1
Part 1Part 1
Part 1
 
A Modern Interface for Data Science on Postgres/Greenplum - Greenplum Summit ...
A Modern Interface for Data Science on Postgres/Greenplum - Greenplum Summit ...A Modern Interface for Data Science on Postgres/Greenplum - Greenplum Summit ...
A Modern Interface for Data Science on Postgres/Greenplum - Greenplum Summit ...
 
programacion orientado a abjetos poo
programacion orientado a abjetos pooprogramacion orientado a abjetos poo
programacion orientado a abjetos poo
 
JAVA PROGRAMMING
JAVA PROGRAMMING JAVA PROGRAMMING
JAVA PROGRAMMING
 
Birasa 1
Birasa 1Birasa 1
Birasa 1
 
OOP Unit 1 - Foundation of Object- Oriented Programming
OOP Unit 1 - Foundation of Object- Oriented ProgrammingOOP Unit 1 - Foundation of Object- Oriented Programming
OOP Unit 1 - Foundation of Object- Oriented Programming
 
Unit 1 introduction to c++.pptx
Unit 1 introduction to c++.pptxUnit 1 introduction to c++.pptx
Unit 1 introduction to c++.pptx
 
Principles of object oriented programing
Principles of object oriented programingPrinciples of object oriented programing
Principles of object oriented programing
 
1 puc programming using c++
1 puc programming using c++1 puc programming using c++
1 puc programming using c++
 
Lecture-4: Introduction to Programming & Databases
Lecture-4: Introduction to Programming & DatabasesLecture-4: Introduction to Programming & Databases
Lecture-4: Introduction to Programming & Databases
 
why to do BCA course?
why to do BCA course?why to do BCA course?
why to do BCA course?
 
Need of OOPs and Programming,pop vs oop
Need of OOPs and Programming,pop vs oopNeed of OOPs and Programming,pop vs oop
Need of OOPs and Programming,pop vs oop
 
Introduction to oop
Introduction to oop Introduction to oop
Introduction to oop
 
OODPunit1.pdf
OODPunit1.pdfOODPunit1.pdf
OODPunit1.pdf
 
Oop obj c
Oop obj cOop obj c
Oop obj c
 
OOPS_Unit_1
OOPS_Unit_1OOPS_Unit_1
OOPS_Unit_1
 
Object Oriented Programming using C++ - Part 1
Object Oriented Programming using C++ - Part 1Object Oriented Programming using C++ - Part 1
Object Oriented Programming using C++ - Part 1
 

More from Asfand Hassan

Pak US relation 130411005046-phpapp01
Pak US relation 130411005046-phpapp01Pak US relation 130411005046-phpapp01
Pak US relation 130411005046-phpapp01Asfand Hassan
 
Course outline [csc241 object oriented programming]
Course outline [csc241 object oriented programming]Course outline [csc241 object oriented programming]
Course outline [csc241 object oriented programming]Asfand Hassan
 
Oop lec 3(structures)
Oop lec 3(structures)Oop lec 3(structures)
Oop lec 3(structures)Asfand Hassan
 
Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)Asfand Hassan
 
Course outline [csc241 object oriented programming]
Course outline [csc241 object oriented programming]Course outline [csc241 object oriented programming]
Course outline [csc241 object oriented programming]Asfand Hassan
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Asfand Hassan
 

More from Asfand Hassan (13)

Pak US relation 130411005046-phpapp01
Pak US relation 130411005046-phpapp01Pak US relation 130411005046-phpapp01
Pak US relation 130411005046-phpapp01
 
Chap5java5th
Chap5java5thChap5java5th
Chap5java5th
 
Chap6java5th
Chap6java5thChap6java5th
Chap6java5th
 
Chap4java5th
Chap4java5thChap4java5th
Chap4java5th
 
Chap3java5th
Chap3java5thChap3java5th
Chap3java5th
 
Chap2java5th
Chap2java5thChap2java5th
Chap2java5th
 
Chap1java5th
Chap1java5thChap1java5th
Chap1java5th
 
Course outline [csc241 object oriented programming]
Course outline [csc241 object oriented programming]Course outline [csc241 object oriented programming]
Course outline [csc241 object oriented programming]
 
Oop lec 3(structures)
Oop lec 3(structures)Oop lec 3(structures)
Oop lec 3(structures)
 
Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)Oop lec 4(oop design, style, characteristics)
Oop lec 4(oop design, style, characteristics)
 
Oop lec 1
Oop lec 1Oop lec 1
Oop lec 1
 
Course outline [csc241 object oriented programming]
Course outline [csc241 object oriented programming]Course outline [csc241 object oriented programming]
Course outline [csc241 object oriented programming]
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)
 

Recently uploaded

APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 

Recently uploaded (20)

APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 

Oop lec 2(introduction to object oriented technology)

  • 1. Introduction to Object Technology, Introduction to C++ Programming Lecture No 2 COMSATS Institute of Information & Technology
  • 2. ď‚—Reference Books: ď‚—Object Oriented Programming in C++, Robert Lafore ď‚—C++ How to Program, Deitel & Deitel ď‚—Object oriented programming, Ashok N. Kamthane 2 COMSATS Institute of Information Technology Object Oriented Programming
  • 3. High-level Languages ď‚—Common programming languages include … C C++ Java Pascal Visual Basic FORTRAN COBOL Lisp Scheme Ada ď‚—These high – level languages ď‚—Resemble human languages ď‚—Are designed to be easy to read and write ď‚—Use more complicated instructions than the CPU can follow ď‚—Must be translated to zeros and ones for the CPU to execute a program 3 COMSATS Institute of Information Technology Object Oriented Programming
  • 4. Low-level Languages ď‚—An assembly language command such as ADD X Y Z might mean add the values found at x and y in memory, and store the result in location z. ď‚—Assembly language must be translated to machine language (zeros and ones) 0110 1001 1010 1011 ď‚—The CPU can follow machine language 4 COMSATS Institute of Information Technology Object Oriented Programming
  • 5. 5 COMSATS Institute of Information Technology Object Oriented Programming
  • 6. Compilers ď‚—Translate high-level language to machine language ď‚—Source code ď‚— the original program in a high level language ď‚—Object code ď‚— the translated version in machine language 6 COMSATS Institute of Information Technology Object Oriented Programming
  • 7. Linkers ď‚—Some programs we use are already compiled ď‚—Their object code is available for us to use ď‚—For example: Input and output routines ď‚—A Linker combines ď‚—The object code for the programs we write and ď‚—The object code for the pre-compiled routines into The machine language program the CPU can run 7 COMSATS Institute of Information Technology Object Oriented Programming
  • 8. Programming and Problem Solving ď‚—Algorithm ď‚—A sequence of precise instructions which leads to a solution ď‚—Program ď‚—An algorithm expressed in a language the computer can understand 8 COMSATS Institute of Information Technology Object Oriented Programming
  • 9. Program Design ď‚—Programming is a creative process ď‚—No complete set of rules for creating a program ď‚—Program Design Process ď‚—Problem Solving Phase ď‚— Result is an algorithm that solves the problem ď‚—Implementation Phase ď‚— Result is the algorithm translated into a programming language 9 COMSATS Institute of Information Technology Object Oriented Programming
  • 10. Problem Solving Phase ď‚—Be certain the task is completely specified ď‚—What is the input? ď‚—What information is in the output? ď‚—How is the output organized? ď‚—Develop the algorithm before implementation ď‚—Experience shows this saves time in getting your program to run. ď‚—Test the algorithm for correctness 10 COMSATS Institute of Information Technology Object Oriented Programming
  • 11. Implementation Phase ď‚—Translate the algorithm into a programming language ď‚—Easier as you gain experience with the language ď‚—Compile the source code ď‚—Locates errors in using the programming language ď‚—Run the program on sample data ď‚—Verify correctness of results ď‚—Results may require modification of the algorithm and program 11 COMSATS Institute of Information Technology Object Oriented Programming
  • 12. 12 COMSATS Institute of Information Technology Object Oriented Programming
  • 13. Procedural Languages • • • • • Examples of procedural languages: C, Pascal, Fortran A program in a procedural language is basically a list of instructions As programs become larger they are usually broken down into smaller units, such as functions, procedures, subroutines Functions can be grouped together into modules according to their functionality, objectives and tasks. Structured programming is a programming paradigm that to a large extent relies on the idea of dividing a program into functions and modules. 13 COMSATS Institute of Information Technology Object Oriented Programming
  • 14. Problems with Structured Programming • Functions have unrestricted access to global data Function A: Function B: Function C: local data local data local data global data X global data Y • 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 e.g. : it is difficult to tell which functions access the data 14 COMSATS Institute of Information Technology Object Oriented Programming global data Z
  • 15. 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 • attributes • people: name, date of birth, eye color, job title • cars: horse power, number of doors, color • behaviours • people: ask a person to bring you a beer • cars: apply the brakes, turn on the engine • attributes and behaviors alone are sufficient to realistically COMSATS Institute of Information Technology 15 Object Oriented Programming model real world objects but a unified view is needed
  • 16. Object Oriented Programming ď‚—Abbreviated OOP ď‚—Used for many modern programs ď‚—Program is viewed as interacting objects ď‚—Each object contains algorithms to describe its behavior ď‚—Program design phase involves designing objects and their algorithms 16 COMSATS Institute of Information Technology Object Oriented Programming
  • 17. OOP Characteristics ď‚—Encapsulation ď‚—Information hiding ď‚—Objects contain their own data and algorithms ď‚—Inheritance ď‚—Writing reusable code ď‚—Objects can inherit characteristics from other objects ď‚—Polymorphism ď‚—A single name can have multiple meanings depending on its context 17 COMSATS Institute of Information Technology Object Oriented Programming
  • 18. Object Oriented Approach Object functions data • Encapsulation: integrate data and functions into one object • Data hiding : data is hidden to the outside world and can only be accessed via the functions • In C++ functions are called membership functions in other languages they are called methods • Data items are called attributes or instance variables 18 COMSATS Institute of Information Technology Object Oriented Programming
  • 19. Object Oriented Approach • separation: objects interact with each other only via their membership functions • separation helps to maintain the integrity of the entire program Object A functions Object C functions data 19 COMSATS Institute of Information Technology Object Oriented Programming data Object B functions data
  • 20. Abstraction • An abstraction is a named collection of attributes and behavior relevant to model a given entity for some particular purpose real-world abstraction attributes software {data, data,…} entity behavior 20 COMSATS Institute of Information Technology Object Oriented Programming { method, method…}
  • 21. Separation • independent specification of a visible interface and a hidden implementation • interface is some kind of contract between the object and the user of this object or module • separation is not restricted to object-oriented programming for example header files in standard C can be regarded as interfaces visible hidden 21 COMSATS Institute of Information Technology Object Oriented Programming interface Implementation
  • 22. Structure of an Object Implementation Interface Object method method code code data method method 22 code code COMSATS Institute of Information Technology Object Oriented Programming
  • 23. Examples of Objects 23 • physcial objects • vehicles in a traffic-flow simulation • electrical components in a circuit-design program • elements of a computer user environment • menus • graphic objects • data-storage constructs • arrays • linked lists • human entities • employees • students • collections of data • an inventory • an address book • user defined data types COMSATS Institute of Information Technology • time Object Oriented Programming • complex numbers
  • 24. Example of a Class in C++ class someobject //declares a class { private: int somedata; //class data public: void setdata(int d) //membership function to set data { somedata=d; } int getdata() //membership function to get data { return somedata; } } 24 COMSATS Institute of Information Technology Object Oriented Programming
  • 25. 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 member or instance of a class • A class can be considered as a more complex data structure than an ordinary built-in data type • Standard C already knows the struct command for user defined data types: struct complex { double re; double im; }; complex x; 25 COMSATS Institute of Information Technology Object Oriented Programming
  • 26. Instantiation of Objects person data: name, p_nummer, address, date of birth methods: getage(), changeaddress(newaddress) Class person data: Lena Brat, 761203-7111, Stureplan 4, female person data: Erik Olsson, 780605-4789, Hamngatan 3, male person data: Lars Backe, 671110-A562, Mälartorget 19, male 26 COMSATS Institute of Information Technology Object Oriented Programming
  • 27. Relationships among Objects • Attribute: 27 One object uses as another object as an attribute, namely as member data, for example a Person contains an attribute Name. This type of relationship is also called a weak association or has-a relationship. Example: A Person has a Name • Association: One object uses another to help it carry out a task. Classes that collaborate are usually related through associations. This type of relationship is also called a uses relationship. Example: The object Driver invokes COMSATS Institute of Information Technology the method Brake Object Oriented Programming of the object BrakingSystem.
  • 28. Relationships among Objects • Aggregation: Aggregation means that one object contains other objects. Aggregation is also called part-of relationship. Example: The class Adressbook contains many People Objects. • Composition: Composition is building objects from parts. It is a stronger type of aggregation in which the parts are necessary to the whole, namely they are permanently bound to the object and do not exist outside the object. A class Processor contains a CPU, Memory and I/O-Ports. 28 COMSATS Institute of Information Technology Object Oriented Programming
  • 29. Relationships among Objects • Generalization Generalization is a relationship defined at the class level not the object level, which means that all objects of this class must obey the relationship. This is type of relationship is also called a is-a-kind-of relationship. Generalization in object oriented languages is realized by means of inheritance. Example: A car is a kind of vehicle. 29 COMSATS Institute of Information Technology Object Oriented Programming
  • 30. Inheritance • In our daily lives we use the concept of classes divided into subclasses, for example vehicles are divided into cars, trucks, buses and motor cycles. • 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. base class Vehicle wheels engine Car 30 wheels sub-classes or engine COMSATS Institute of Information Technologyderived classes trunk trunk Object Oriented Programming Truck wheels engine trailer trailer
  • 31. Inheritance • 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. base class Vehicle brake() start_engine() Car brake() start_engine() open_door() open_door() 31 COMSATS Institute of Information Technology Object Oriented Programming Truck sub-classes or derived classes brake() start_engine() open_door() pull_trailer()
  • 32. 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 • In C++ an object of a sub-class is substitutable for an object of the super-class, in other words an object of class Car can be used whenever an object of class Vehicle is required. 32 COMSATS Institute of Information Technology Object Oriented Programming
  • 33. 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. • The concept of inheritance provides an important extension to the idea of reusability, as it allows a programmer to take an existing class without modifying it and adding additional features and functionality. This is done by inheriting a new sub-class from the exisiting base class. 33 COMSATS Institute of Information Technology Object Oriented Programming
  • 34. Polymorphism & Overloading • Polymorphism : using functions and operators in different ways, depending on what they are operating on. • Polymorphism allows it to manipulate objects without knowing their exact type but only their common property. for example, the classes Triangle and Circle both have their own (polymorphic) version of the method Draw, but a graphic routine that draws graphical elements does not have to know which object it manipulates. • Overloading: an existing operator, such as + or = is given the capability to operate on a new data type, for example define the operator + for the class Complex such that it realizes the addition of two complex numbers. 34 COMSATS Institute of Information Technology Object Oriented Programming
  • 35. Polymorphism • polymorphism means ”having many shapes” • in C++ it refers to a situation in which an object could have any of several types • a polymorphic variable can refer to objects of different classes, for example a graphic object can be either a circle or a triangle • a polymorphic function or operator can take arguments of different types • example: int max(int a, int b); double max(double a, double b); 35 COMSATS Institute of Information Technology Object Oriented Programming
  • 36. C++ and C 36 • C++ is derived from the language C • C++ is a superset of C, that means almost every correct statement in C is also correct in C++ • The most important elements added to C are concerned with classes, objects and object-oriented programming • New features of C++ • improved approach to input/output • standard template library (STL) container classes for vectors, lists, maps, etc. • reference type replaces pointers • const variables replaces #define statements • string data type replaces C-style strings char[] • new comment style augments C-style comments /* */ COMSATS Institute of Information Technology Object Oriented Programming
  • 37. Software Life Cycle 1. 2. 3. 4. 5. 37 Analysis and specification of the task (problem definition) Design of the software (object and algorithm design) Implementation (coding) Maintenance and evolution of the system Obsolescence COMSATS Institute of Information Technology Object Oriented Programming
  • 38. C++ History ď‚—C developed by Dennis Ritchie at AT&T Bell Labs in the 1970s. ď‚—Used to maintain UNIX systems ď‚—Many commercial applications written in c ď‚—C++ developed by Bjarne Stroustrup at AT&T Bell Labs in the 1980s. ď‚—Overcame several shortcomings of C ď‚—Incorporated object oriented programming ď‚—C remains a subset of C++ 38 COMSATS Institute of Information Technology Object Oriented Programming
  • 39. C++ Standard Library ď‚—C++ programs ď‚—Built from pieces called classes and functions ď‚—C++ standard library ď‚—Rich collections of existing classes and functions “Building block approach” to creating programs “Software reuse” 39 COMSATS Institute of Information Technology Object Oriented Programming
  • 40. Basics of a Typical C++ Environment ď‚—C++ systems ď‚—Program-development environment ď‚—Language ď‚—C++ Standard Library 40 COMSATS Institute of Information Technology Object Oriented Programming
  • 41. Basics of a Typical C++ Environment Editor Phases of C++ Programs: 1. Edit 2. Preprocess 3. Compile 4. Link 5. Load 6. Execute Program is created in the editor and stored on disk. Disk Preprocessor Disk Preprocessor program processes the code. Compiler Disk Compiler creates object code and stores it on disk. Linker Disk Loader Disk Primary Memory Linker links the object code with the libraries, creates a.out and stores it on disk Loader puts program in memory. . . . . . . Primary Memory CPU 41 COMSATS Institute of Information Technology Object Oriented Programming . . . . . . CPU takes each instruction and executes it, possibly storing new data values as the program executes.
  • 42. A Sample C++ Program ď‚—A simple C++ program begins this way #include <iostream> using namespace std; int main() { Statements; } 42 return 0; COMSATS Institute of Information Technology Object Oriented Programming
  • 43. #include <iostream> using namespace std; int main() { cout << “Hello Worldn”; return 0; } 43 COMSATS Institute of Information Technology Object Oriented Programming
  • 44. Example: Adding Two Integers #include <iostream> using namespace std; // function main begins program execution int main() { int integer1; // first number to be input by user int integer2; // second number to be input by user int sum; // variable in which sum will be stored cout << "Enter first integern"; // prompt cin >> integer1; // read an integer cout << "Enter second integern"; // prompt cin >> integer2; // read an integer sum = integer1 + integer2; // assign result to sum cout << "Sum is " << sum << endl; // print sum return 0; // indicate that program ended successfully } 44 COMSATS Institute of Information Technology Object Oriented Programming
  • 45. ď‚—Variables ď‚—Location in memory where value can be stored ď‚—Common data types ď‚— int - integer numbers ď‚— char - characters ď‚— double - floating point numbers ď‚—Declare variables with name and data type before use int integer1; int integer2; int sum; ď‚—Can declare several variables of same type in one declaration ď‚— Comma-separated list int integer1, integer2, sum; 45 COMSATS Institute of Information Technology Object Oriented Programming
  • 46. ď‚—Variables ď‚—Variable names ď‚— Valid identifier ď‚— Series of characters (letters, digits, underscores) ď‚— Cannot begin with digit ď‚— Case sensitive 46 COMSATS Institute of Information Technology Object Oriented Programming
  • 47. ď‚—Input stream object ď‚—>> (stream extraction operator) ď‚— Used with cin ď‚— Waits for user to input value, then press Enter (Return) key ď‚— Stores value in variable to right of operator ď‚— Converts value to variable data type ď‚— = (assignment operator) ď‚— Assigns value to variable ď‚— Binary operator (two operands) ď‚— Example: sum = variable1 + variable2; 47 COMSATS Institute of Information Technology Object Oriented Programming
  • 48. ď‚— Comments ď‚—Document programs ď‚—Improve program readability ď‚—Ignored by compiler ď‚—Single-line comment ď‚— Begin with // ď‚— Preprocessor directives ď‚—Processed by preprocessor before compiling ď‚—Begin with # ď‚—Include Directives #include <iostream> ď‚—Tells compiler where to find information about items used in the program ď‚—iostream is a library containing definitions of cin and cout 48 COMSATS Institute of Information Technology Object Oriented Programming
  • 49. ď‚— Standard output stream object ď‚—cout “Connected” to screen ď‚—<< ď‚— Stream insertion operator ď‚— Value to right (right operand) inserted into output stream ď‚— Namespace ď‚—Tells the compiler to use names in iostream in a “standard” way ď‚—std:: specifies using name that belongs to “namespace” std ď‚—std:: removed through use of using statements ď‚— Escape characters ď‚— ď‚—Indicates “special” character output 49 COMSATS Institute of Information Technology Object Oriented Programming
  • 50. Running a C++ Program ď‚—C++ source code is written with a text editor ď‚—The compiler on your system converts source code to object code. ď‚—The linker combines all the object code into an executable program. 50 COMSATS Institute of Information Technology Object Oriented Programming
  • 51. 51 COMSATS Institute of Information Technology Object Oriented Programming
  • 52. 52 COMSATS Institute of Information Technology Object Oriented Programming
  • 53. 1.4 Testing and Debugging ď‚—Bug ď‚—A mistake in a program ď‚—Debugging ď‚—Eliminating mistakes in programs ď‚—Term used when a moth caused a failed relay on the Harvard Mark 1 computer. Grace Hopper and other programmers taped the moth in logbook stating: “First actual case of a bug being found.” 53 COMSATS Institute of Information Technology Object Oriented Programming
  • 54. Program Errors ď‚—Syntax errors ď‚—Violation of the grammar rules of the language ď‚—Discovered by the compiler ď‚— Error messages may not always show correct location of errors ď‚—Run-time errors ď‚—Error conditions detected by the computer at run-time ď‚—Logic errors ď‚—Errors in the program’s algorithm ď‚—Most difficult to diagnose ď‚—Computer does not recognize an error 54 COMSATS Institute of Information Technology Object Oriented Programming
  • 55. Escape Sequence n Newline. Position the screen cursor to the beginning of the next line. t Horizontal tab. Move the screen cursor to the next tab stop. r Carriage return. Position the screen cursor to the beginning of the current line; do not advance to the next line. a Alert. Sound the system bell. Backslash. Used to print a backslash character. " 55 Description Double quote. Used to print a double quote character. COMSATS Institute of Information Technology Object Oriented Programming