data structures using oop c++ lecture 1 2013n/2nd-ds...the data items in a structure are called the...
TRANSCRIPT
Data Structures using OOP C++ Lecture 1
Asst. Lec. Zainab Mahmood Fadhil Page 1
References:
1. E Balagurusamy ,”Object Oriented Programming with C++”, 4th
edition,
McGraw-Hill 2008.
2. Robert Lafore, “Object-Oriented Programming in C++”, 4th
edition, 2002, SAMS publishing.
3. Robert L. Kruse and Alexander J. Ryba, “Data Structures and Program
Design in C++”, Prentice-Hall 2000.
C++ Review
A Simple C++ Program
// welcome.cpp
//
// This program prints
// a welcome message
#include <iostream>
using namespace std;
void main()
{
// print welcome message
cout<<“Welcome to C++ programming“;
}
C++ Program Structure
Comments
Libraries
main function
Statements that define memory locations (constants and
variables)
Statements that specify actions to be taken (i.e. flow of controls)
Operational expressions involving data representations
Representations of data of different types (e.g. structure)
Other Functions and procedures
Data Structures using OOP C++ Lecture 1
Asst. Lec. Zainab Mahmood Fadhil Page 2
// Comments
.....
.....
// Comments
Links to Libraries
...... Lib 1
......
...... Lib M
Links to Libraries
Main Program
{
input & initialisation
Expressions, Statements
function calls
output & termination
}
Arrays
An array is a group of consecutive, adjacent memory locations (i.e.
elements) that all have the same data type. Arrays may have from one to
several dimensions. We will study the one-dimensional (1D) and two-
dimensional (2D) arrays.
1D Array
Definition:
data type arrayName[ Size ];
The Size must be an integer constant greater than zero.
For example:
int a[10];
char name[20];
float temperature[6];
Data Structures using OOP C++ Lecture 1
Asst. Lec. Zainab Mahmood Fadhil Page 3
For example:
a[3] = 60; // assign 60 to the fourth element
cin >> mark[3]; // read the value of the 4th mark
for(int i=0; i<10; i++)
cin >> a[i]; // input values to the array
for(int j=0; j<10; j++)
cout << a[j]; // print values of the array
Array initialization:
C++ allows the initialization of arrays at the time of their declaration. For
example:
int a[5] = { 8 , 5 , 13 , 2 , 9};
int a[ ] = { 8 , 5 , 13 , 2 , 9};
2D Array (Matrix)
Two-dimensional arrays consist of values arranged in rows and columns.
Definition:
data type arrayName[ RowSize ][ColumnSize];
For example:
Data Structures using OOP C++ Lecture 1
Asst. Lec. Zainab Mahmood Fadhil Page 4
int a[3][4];
float b[10][20];
Accessing 2D array elements:
arrayName[RowIndex] [ColumnIndex]
For example:
a[3][4] = 60;
cin >> mark[3][1];
for(int i=0; i<10; i++)
for(int j=0; j<10; j++)
cin >> a[i][j]; // input values to the 2D array
for(int m=0; m<10; m++)
{
for(int n=0; n<10; n++)
cout<< a[m][n]<<”\t”; //print values of 2D array
cout<<endl;
}
2D Array initialization:
int b[2][2] = { {1 , 2} , {3 , 4} };
int a[3][4] = { {1 , 2 , 3 , 4} , {5 , 6 , 7 , 8} ,
{3 , 4 , 1 , 2} };
Data Structures using OOP C++ Lecture 1
Asst. Lec. Zainab Mahmood Fadhil Page 5
Example: Write a C++ program that adds two initialized 3×4 matrices A
and B and then stores the result in a matrix C. #include <iostream>
using namespace std;
void main()
{
int A[3][4] = { {1, 4, 3, 2},
{5, 6, 7, 8},
{9, 10, 11, 12} };
int B[3][4] = { {3, 4, 3, 1},
{8, 7, 5, 6},
{12, 9, 11, 8} };
int C[3][4];
for (int i=0; i<3; i++)
{
for (int j=0; j<4; j++)
{
C[i][j] = A[i][j] + B[i][j];
cout << C[i][j] << "\t";
}
cout << endl;
}
}
Structures
A structure is a collection of simple variables that have same or different
data types: int, float, and so on. This is unlike the array in which all the
variables must be the same type. The data items in a structure are called
the members of the structure.
In order to use a structure in our C++ programs, we need the following:
1) Defining the structure.
2) Defining a structure variable(s).
3) Accessing and manipulating the members of the structure.
Defining the structure
The general form of a structure definition is:
struct structure_name
Data Structures using OOP C++ Lecture 1
Asst. Lec. Zainab Mahmood Fadhil Page 6
{
// Structure members
type variable 1;
type variable 2;
.
.
type variable n;
};
Notes:
The structure definition serves only as a blueprint for the creation
of structure variables.
It does not set aside any space in memory or even name any
variables.
A structure definition is merely a specification for how structure
variables will look when they are defined.
Examples: struct Rectangle
{
float length;
float width;
};
Defining the structure variable
The general form of defining a structure variable is:
structure_name structure_var1 , structure_var2 , … ;
Defining a structure variable reserves space in memory. This memory
space is equal to the total size of all the members of structure.
Examples:
Rectangle rec;
Time time1 , time2;
Data Structures using OOP C++ Lecture 1
Asst. Lec. Zainab Mahmood Fadhil Page 7
Person ali , ahmed;
There is another way to define the structure variable in the structure
definition:
struct Rectangle
{
float length;
float width;
} rec;
Accessing and manipulating structure members
Once a structure variable has been defined, its members can be accessed
using the dot operator :
structure_var.member
Examples: rec.length = 10;
time2.seconds++;
cin >> ali.name;
cout << ahmed.age;
Initializing Structure Members
Example: Write a C++ program that computes the shaded area in the
figure below using the structure Rectangle. #include <iostream>
using namespace std
struct Rectangle
{
int length;
int width;
};
void main()
{
int area1 , area2;
Rectangle rect1 = {20 , 10};
Rectangle rect2 = {15 , 7 };
area1 = rect1.length * rect1.width;
Data Structures using OOP C++ Lecture 1
Asst. Lec. Zainab Mahmood Fadhil Page 8
area2 = rect2.length * rect2.width;
cout << "The shaded area is "
<< area1 - area2 << endl;
}
Structure Variables Assignment
A structure variable can be assigned to another only when they are of the
same structure type.
Example: #include <iostream >
using namespace std;
struct Time
{
int hour;
int minute;
int second;
};
void main()
{
Time time1 = { 10 , 15 , 20 };
Time time2;
time2 = time1; // assign first structure variable
// to the second
cout << "The second time is "
<< time2.hour <<":"<< time2.minute << ":"
<< time2.second << endl;
}
Pointers
Every byte in the computer’s memory has an address. Each program,
when it is loaded into memory, occupies a certain range of these
addresses. That means that every variable and every function in your
program starts at a particular address.
Data Structures using OOP C++ Lecture 1
Asst. Lec. Zainab Mahmood Fadhil Page 9
The Address-of Operator &
We can find the address occupied by a variable by using the address-of
operator &.
Example: #include <iostream>
using namespace std; void main()
{
int var1 = 11;
int var2 = 22;
int var3 = 33;
cout << &var1 << endl
<< &var2 << endl
<< &var3 << endl;
}
Data Structures using OOP C++ Lecture 1
Asst. Lec. Zainab Mahmood Fadhil Page 10
Output:
0x8fd4fff4
0x8fd4fff2
0x8fd4fff0
Note:
Remember that the address of a variable is not at all the same as its
content. The contents of the three variables are 11, 22, and 33.
Pointer
A pointer is a variable that holds an address value. Hence we can use
pointers to find out where things are in memory. The data type of pointer
is not the same as the variable whose address is being stored; a pointer to
int is not type int.
Example: #include <iostream>
using namespace std;
void main()
{
int var1 = 11;
int var2 = 22;
cout << &var1 << endl
<< &var2 << endl;
int *ptr; // define a pointer to integers
ptr = &var1; // pointer points to var1
cout << ptr << endl; // print pointer value
ptr = &var2; // pointer points to var2
cout << ptr << endl; // print pointer value
}
Data Structures using OOP C++ Lecture 1
Asst. Lec. Zainab Mahmood Fadhil Page 11
Another examples: char *cptr; // pointer to char
float *fptr; // pointer to float
Distance *distptr; // pointer to Distance structure
To define more than one pointer of the same type: int *ptr1, *ptr2, *ptr3;
Important Note:
Pointers must have a value. Before a pointer is used, a specific address
must be placed in it. Otherwise it will point to an address we don’t want it
to point to (into our program or operating system). This can cause system
crash since the compiler gives no warning.
Data Structures using OOP C++ Lecture 1
Asst. Lec. Zainab Mahmood Fadhil Page 12
Accessing the content of pointer
The content of pointer is the value of the variable pointed to by that
pointer.
Example: #include <iostream>
using namespace std;
void main()
{
int var1 = 11;
int var2 = 22;
int *ptr;
ptr = &var1;
cout<< *ptr <<endl;
ptr = &var2;
cout<< *ptr <<endl;
}
Example: #include <iostream>
using namespce std;
void main()
{
int var1, var2;
int* ptr;
ptr = &var1;
*ptr = 37; // same as var1=37
var2 = *ptr; // same as var2=var1
cout << var2 << endl;
}
The advantage of using pointers becomes evident when we cannot access
a variable directly, as we will see later.
Data Structures using OOP C++ Lecture 1
Asst. Lec. Zainab Mahmood Fadhil Page 13
Uses of pointers
Pointers are an important feature of C++. They are used for:
Accessing array elements
Passing arguments to a function when the function needs to modify
the original argument
Passing arrays and strings to functions
Obtaining memory from the system
Creating data structures such as linked lists
Pointers and Arrays
Example: #include <iostream>
using namespace std;
void main()
{
int a[5] = { 31, 54, 77, 52, 93 };
for(int i=0; i<5; i++)
cout << a[i] << endl;
cout<<endl;
// Or use pointer
for(i=0; i<5; i++)
cout<< *(a+i) << endl;
}
Data Structures using OOP C++ Lecture 1
Asst. Lec. Zainab Mahmood Fadhil Page 14
Note:
The name of an array is a pointer constant (i.e. address).
Since we cannot increment or decrement an address, we can increment or
decrement a pointer that holds that address.
Example: #include <iostream>
using namespace std;
void main()
{
int a[5] = { 31, 54, 77, 52, 93 };
int *ptr;
ptr = a; //points to a
for(int j=0; j<5; j++)
cout << *(ptr++) << endl;
}