0% found this document useful (0 votes)
29 views

Object Oriented Programming: Ms. Saima Majeed Department of Creative Technologies Air University Islamabad

The document provides an outline for an Object Oriented Programming course. It discusses key OOP concepts like classes, objects, encapsulation, inheritance and polymorphism. It lists the reference book and OOP concepts that will be covered. It also outlines the evaluation criteria of midterm, final and internal assessments. The document then contrasts structured programming with OOP and lists some key features of OOP like objects, classes, real-world modeling, reusability, information hiding and polymorphism.

Uploaded by

ali anwar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Object Oriented Programming: Ms. Saima Majeed Department of Creative Technologies Air University Islamabad

The document provides an outline for an Object Oriented Programming course. It discusses key OOP concepts like classes, objects, encapsulation, inheritance and polymorphism. It lists the reference book and OOP concepts that will be covered. It also outlines the evaluation criteria of midterm, final and internal assessments. The document then contrasts structured programming with OOP and lists some key features of OOP like objects, classes, real-world modeling, reusability, information hiding and polymorphism.

Uploaded by

ali anwar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

DEPARTMENT OF

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 1 FUNCTION 2 FUNCTION 3

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

• Objects have both data and methods


•Objects of the same class have the same data
elements and methods
•Objects send and receive messages to invoke
actions
Features of
OOP
• Objects:
• OOP provide the facility of programming based on objects.
• Object is an entity that consists of data and functions.
• Classes:
• Classes are designed for creating objects
• OOP provides the facility to design classes for creating different objects
• All properties and function of an object are specified in classes.
• Real-world Modeling:
• OOP is based on real-world modeling.
• Object have data and function.
• Data represents properties and function represent working of objects.
• Reusability:
• Inheritance is a technique that allows a programmer to use the code of existing program to create
new programs.
Features of
OOP
• Information Hiding:
• OOP allows the programmer to hide important data from the user.
• It is performed by encapsulation
• Polymorphism:
• Polymorphism is an ability of an object of different types to respond to
function of the same name.
Todays
Lecture
• Structure
• Structure declaration
• Structure Variables
• Accessing Structure Members
• Initializing a Structure
• Structure with Arrays
• Nested Structure
• Structure with Functions
• Structures with Pointers
What is
Structure
• Example of Student: class, name, address, date of birth, GPA etc.
• The information should be grouped and handled as a group .
• This is what the concept of structure is.
• Structure we introduce new datatype.

• A structure is a collection of variables under a single name. These


variables can be of different types, and each has a name that is used
to select it from the structure
Structure
s
• Structure: C++ construct that allows multiple variables to be grouped together
• Structure Declaration Format:
• Void main()
• {
Struct student
{
int makrs;
char name;
Float average;
};
Student s1;
Cin>>s1.marks;
Cin>>s1.name;
Cout<<s1.marks;
Cout<<s1.name;
Example struct
Declaration
struct Student
{
structure tag
int studentID;
string name;
short year;
double gpa; structure members
};

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:

if (s1 >= s2) // won’t work!

• Instead, compare member variables:


if (s1.gpa >= s2.gpa) // better

7-20
Initializing a
Structure
Cannot initialize members in the structure declaration,
because no memory has been allocated yet

struct Student // Illegal


{ // initialization int
studentID = 1145;
string name = "Alex";
short year = 1;
float gpa = 2.95;
};

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

{12, 6, 3} // initialization list


// with 3 values

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

You might also like