1 mr. muhammad hanif lecturer information technology mbbs campus dadu university of sindh

18
1 Object Oriented Programming Paradigm Lesson 01 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

Upload: augustus-horn

Post on 03-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

1

Object Oriented

Programming Paradigm

Lesson 01

Mr. Muhammad Hanif

Lecturer Information Technology

MBBS Campus Dadu University of SIndh

Page 2: 1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

2

Arrays and StringsArray Fundamentals

Two Dimensional Arrays

Function Declaration with Array Argument

Arrays of Structures

Arrays As Class Member Data

Arrays of Objects

C-Strings

The Standard C++ string Class

Page 3: 1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

3

Arrays and StringsArray Fundamentals

Definitions of array

Use of Arrays

Arrays Elements

Initialization of Arrays

Programs:

Basic Array with for loop

Calculate no. of days of year

Calculate average sales

Average of marks

Copying array

Array of characters

Page 4: 1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

Arrays FundamentalsDefinition: An array is an aggregate data type that lets you

access multiple variables of same type through a single name by use of an index.

OR

• An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.

4

Page 5: 1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

Arrays Fundamentals Use of Arrays:

When multiple, similar type of variables are used to store data.

Example 1: We can store 5 values of type int in an array without having to declare 5 different variables, each one with a different identifier. Instead of that, using an array we can store 5 different values of the same type, int for example, with a unique identifier.

Example 2: To store the record of students in class we need 50 variables with different names, by using array we could manage data by just one name.

5

Page 6: 1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

6

Arrays FundamentalsArrays Elements:

The items in an array are called elements

(in contrast to the items in a structure, called members).

As we noted, all the elements in an array are of the same type; only the values vary. Figure 7.2 shows the elements of the array age.

Arrays are LIFO (Last-in First-out) structure.

It means elements included in Last will be manipulated First.

Page 7: 1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

7

Arrays FundamentalsDefinition of Arrays

Definition of arrays includes three parts:

1. Datatype of array: Type of data elements included in array.

2. Name of array: Any name relevant to data elements.

3. Size of array: It defines how many data elements are included in array.

It is antonym of structure.

Structure is collection of different types of data members.

Page 8: 1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

8

Arrays Fundamentals

Figure7.1: Definition of Array(Datatype, Name & Size)

Figure 7.2: Arrays Elements

Page 9: 1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

Arrays Fundamentals (Index)The elements of the array are manipulated using the

index.

The index of array starts from zero and is one less than array's size.

Index of array is also called subscript.

9

C[0]

C[1]

C[2]

C[3]

Index

C[5] C[6] C[7]

C[8]

C[9]

...

35

59

24

... ...

... ... ...

Memory Name

The indexing of the array starts from zero, not from one.

So in the above example, the index of the array C will be from C[0] to C[9].

Page 10: 1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

Arrays (Declaration)Every array has a data type i.e. name and size.

The rules of variable naming convention apply to array names.

The size of the array tells how many elements are there in the array.

The arrays occupy the memory depending upon their size and have contiguous area of memory.

Declaration:

Syntax: data_type array_name [size] ;

Example: int ages [10];

The array occupies the contiguous area, in this case it will occupy forty bytes (one int = 4 bytes) 10

Page 11: 1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

Arrays (Declaration)Arrays may be declared with simple variables in a single line.

int i, age [10];

int height [10], length [10] ;

To access array, we can’t use the whole array at a time. We access arrays element by element.

An index (subscript) may be used to access the first element of the array.

In this case, to access first element we write like age[0]. To access the 5th element, we will write age[4] and so on.

11

Page 12: 1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

Arrays FundamentalsInitialization of Arrays

It includes definition and starting values of each element of array.

Note: Don't use the default initialization of arrays. Compiler may assign some value to each declared array.

12

Figure 7.3: Syntax of Initialization of Arrays

Page 13: 1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

Arrays Fundamentals (Initialization…)

13

int i, age [10];

for ( i = 0; i < 10 ; i++ )

{ age[i] = 0; }

With the help of this simple loop, we have initialized all the elements of array age to zero.

int age [10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

With the help of this simple loop, we have initialized all the elements of array age to zero.

int age [10] = { 0};

Same could be performed by using following shortcut.

int age [ ] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

The compiler detect the initialization list which consists of ten 0’s. Therefore, it creates an

array of 10 integers and initializes all the elements with zero.

Page 14: 1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

14

Arrays Fundamentals (Program: Basic Array with for loop)

#include <iostream.h>

int main()

{

int age[4]; //array ‘age’ of 4 integers

for(int j=0; j<4; j++) //get 4 ages

{

cout << “Enter an age: “;

cin >> age[j]; //access array element

}

for(j=0; j<4; j++) //display 4 ages

cout << “You entered “ << age[j] << endl;

}

Write a program using array, which get ages of four persons and return back all ages in same sequence.

Page 15: 1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

15

Arrays Fundamentals (Program: Calculate no. of days of year)

#include <iostream.h>

int main()

{

int month, day, total_days;

int days_per_month[12] = { 31, 28, 31, 30, 31, 30,31, 31, 30, 31, 30, 31 };

cout << “\nEnter month (1 to 12): “; //get date

cin >> month;

cout << “Enter day (1 to 31): “; //insert date of month

cin >> day; //date of month saved in day variable

total_days = day; //separate days

for(int j=0; j<month-1; j++) //add days each month

total_days += days_per_month[j];

cout << “Total days from start of year is: “ << total_days<< endl;

}

Write a program which calculates the number of days from the beginning of the year to a date specified by the user.

Page 16: 1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

16

Arrays Fundamentals (Program: Calculate average sales)

//Sales.cpp

#include <iostream.h>

int main()

{

const int SIZE = 6; //size of array

double sales[SIZE]; //array of 6 variables

cout << "Enter sales for 6 days\n";

for(int j=0; j<SIZE; j++) //put figures in array

cin >> sales[j];

double total = 0;

for(j=0; j<SIZE; j++) //read figures from array

total += sales[j]; //to find total

double average = total / SIZE; // find average

cout << "Average = " << average << endl;

}

Write a program which calculates the number of average sales of six days.

Page 17: 1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

17

Arrays Fundamentals (Program: average of marks)

#include <iostream.h>

int main()

{

int sum=0;

int marks[3];

for(int a=0;a<3;a++)

{

cout<<"please insert marks of subject "<<endl;

cin>>marks[a];

cout<<endl;

sum=sum+marks[a];

}

cout<<"The average is: "<<sum/(sizeof(marks)/2)<<endl;

}

Write a program using array, which gets marks of 4 subjects and provide sum and average of marks. Also use sizeof() function.

Page 18: 1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh

18

Arrays Fundamentals (Program: Copying arrays)#include <iostream.h>int main(){int ages[4]={5,10,15,20};int same_age[4];same_age[0]=ages[0]; same_age[1]=ages[1]; same_age[2]=ages[2];same_age[3]=ages[3];for(int k=0;k<4;k++)cout<<"Output of new array: "<<same_age[k]<<endl;//orfor(int i=0;i<4;i++){same_age[i]=ages[i];cout<<"Output of new array: \t"<<same_age[i]<<endl;}}

Write a program which copy values from one array to another array.