Object Oriented Programming: Ms. Saima Majeed Department of Creative Technologies Air University Islamabad
Object Oriented Programming: Ms. Saima Majeed Department of Creative Technologies Air University Islamabad
CREATIVE TECHNOLOGIES
Object Oriented
Programming
Ms. Saima Majeed
Department of Creative Technologies
Air University Islamabad
Course
Outline
• Reference Book : Deitel & Deitel – C++ How to Program
• OOPs Concepts:
• Class
• Objects
• Data Abstraction
• Encapsulation
• Inheritance
• Polymorphism
• Dynamic Binding
• Message Passing
Evaluation
Criteria
• Mid: 35%
• Final: 45%
• Internal: 20%
Structured
Programming
• Dividing Problem into smaller sub problems is called
structured design
• Process of implementing a structured design is called
structured programming.
• Also known as top-down design, bottom-up design,
stepwise refinement, and modular programming
Structure Programing
Rules
• Divide and Conquer:
• one should not write very long function
• If function is getting longer than divide into smaller, and well defined task.
• Inside the function single entry single exit rule.
• The rules is very important for readability and useful in managing programs.
• Even if the developer itself tries to use own code if he has followed rules
properly.
• Comment your program well.
Limitations of Structured
Programming
• When we design a functional program, the data is requires to process,
is an entity.
• We take of function rather than the data it is going to process.
• When the problems became complex, we came to know that we cant
leave the data outside.
• Instead of the program driven by function, a program should driven
by data.
• Example: “Word Processor to make word bold” this approach is originated
by
OOP
Object-Oriented
Programming
• In 1980’s a scientist in Bell Labs Strousturp started working in
enhancing C language to overcome the shortcoming of structured
approach.
• This evolution of C language firstly known to be C with Classes,
eventually called C++.
• In OOP first step is to identify object.
• An object combined data and the operations on that data is a single
unit.
• In C++, the mechanism that allows you to combine data and the
operations on that data in a single unit called a class.
Structured Programming
MAIN PROGRAM GLOBAL DATA
FUNCTION 4 FUNCTION 5
• Using function
• Function & program is divided into modules
• Every module has its own data and
which can be called by other function
modules.
OBJECT ORIENTED PROGRAMMING
Object 1 Object 2
Data Data
Function Function
Object 3
Data
Function
Notice the
required
We can also give ;
The variable name here.
Defining Structure
Variables
• struct declaration does not allocate memory or create
variables
• To define variables, use structure tag as type name
• S1 is a knew variable has datatype Student
Student s1;
s1 studentID
name
year
gpa
7-16
Accessing Structure
Members
• Use the dot (.) operator to refer to members of struct
variables
cin >> s1.studentID;
s1.gpa = 3.75;
• Member variables can be used in any manner appropriate for their
data type
7-17
Displaying struct
Members
To display the contents of a struct variable,
you must display each field separately, using the
dot operator
Wrong:
cout << s1; // won’t work!
Correct:
cout << s1.studentID << endl;
cout << s1.name << endl;
cout << s1.year << endl;
cout << s1.gpa;
7-19
Comparing struct
Members
• Similar to displaying a struct, you cannot compare
two struct variables directly:
7-20
Initializing a
Structure
Cannot initialize members in the structure declaration,
because no memory has been allocated yet
7-21
Continue
…
• Structure members are initialized at the time a structure variable is
created
• Can initialize a structure variable’s members with either
• an initialization list
• a constructor
Using an Initialization
List
An initialization list is an ordered set of values,
separated by commas and contained in { },
that provides initial values for a set of data
members
7-23
Partial
Initialization
Can initialize just some members, but
cannot skip over members
Dimensions box1 = {12,6}; //OK
Dimensions box2 = {12,,3};
//illegal
7-24
Array as Member of
Structure
• Write a program that declares a structure to store roll no and marks of
five subjects. It defines a structure variable inputs the values and display
roll no, marks and average marks.
struct student
{
int rollno;
int marks[5];
};
student s1;
double average=0; cout<<“rollnumber:”<<s1.rollno;
cout<<average;
cout<<“enter rollno”;
cin>>s1.rollno;
For(int i=0;i<=4;i++)
{Cout<<“enter marks
for each subject”;
cin>>s1.marks[i];
average=s1.marks[i]
+average;
}
Initializing a structure with Arrays as
Member
Array of
Structure
Initializing Arrays of
Structure
Initializing Nested
Structure
Exampl
e
struct Result
{ int marks;
Int grade;};
struct Record
{
int
rollnumner;
Result ;
} ;
main()
Record rs;
Result r;
cin>>rs.rollnumbe
r; cin>>rs.r.marks;
cin>>rs.r.grade;
Solutio
n
Functions and
Structures
• The structure can also used with function and can accept structure as
parameters.
• A function can also return a structure variable.
• The process of passing structure to function is similar as
passing simple variable by value.
Passing Structure by
Value
Passing Structure by
Reference
• A Structure variable can also be Passed to a function by reference
• The method is same we passed simple variable by reference.
Pointers and
Structures
• Pointer can refer to structure variable in same way as
variables.
simple
• Selection operator -> is used instead of dot operator to access
the structure member.
Exampl
e
Exampl
e