Data Structures using OOP C++ Lecture 1



Similar documents
Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Comp151. Definitions & Declarations

Passing 1D arrays to functions.

Answers to Review Questions Chapter 7

COSC 181 Foundations of Computer Programming. Class 6

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

C++ DATA STRUCTURES. Defining a Structure: Accessing Structure Members:

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

An Incomplete C++ Primer. University of Wyoming MA 5310

Appendix K Introduction to Microsoft Visual C++ 6.0

Multichoice Quetions 1. Atributes a. are listed in the second part of the class box b. its time is preceded by a colon. c. its default value is

Embedded SQL. Unit 5.1. Dr Gordon Russell, Napier University

Moving from C++ to VBA

5 Arrays and Pointers

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

Member Functions of the istream Class

Compiler Construction

1. The First Visual C++ Program

COMPUTER SCIENCE 1999 (Delhi Board)

Chapter One Introduction to Programming

Matrix Multiplication

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Chapter 13 Storage classes

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Basics of I/O Streams and File I/O

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

13 Classes & Objects with Constructors/Destructors

Sequential Program Execution

Ch 7-1. Object-Oriented Programming and Classes

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

C++ Outline. cout << "Enter two integers: "; int x, y; cin >> x >> y; cout << "The sum is: " << x + y << \n ;

VB.NET Programming Fundamentals

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

The University of Alabama in Huntsville Electrical and Computer Engineering CPE Test #4 November 20, True or False (2 points each)

C++ INTERVIEW QUESTIONS

1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright /6/11 12:33 PM!

EP241 Computer Programming

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 3: Input/Output

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Subtopics - Functions Function Declaration Function Arguments Return Statements and values. By Hardeep Singh

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

C++FA 5.1 PRACTICE MID-TERM EXAM

Laboratory Assignments of OBJECT ORIENTED METHODOLOGY & PROGRAMMING (USING C++) [IT 553]

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

Chapter 9 Text Files User Defined Data Types User Defined Header Files

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Syllabus OBJECT ORIENTED PROGRAMMING C++

Solving Problems Recursively

Object Oriented Software Design II

(Eng. Hayam Reda Seireg) Sheet Java

Cohort: BCA/07B/PT - BCA/06/PT - BCNS/06/FT - BCNS/05/FT - BIS/06/FT - BIS/05/FT - BSE/05/FT - BSE/04/PT-BSE/06/FT

Course notes Standard C++ programming

The C Programming Language course syllabus associate level

Learning Computer Programming using e-learning as a tool. A Thesis. Submitted to the Department of Computer Science and Engineering.

C++ Input/Output: Streams

7.7 Case Study: Calculating Depreciation

One Dimension Array: Declaring a fixed-array, if array-name is the name of an array

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

arrays C Programming Language - Arrays

Object Oriented Software Design

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

Introduction to C++ Introduction to C++ Week 7 Dr Alex Martin 2013 Slide 1

CISC 181 Project 3 Designing Classes for Bank Accounts

Common Beginner C++ Programming Mistakes

While Loop. 6. Iteration

Lecture 22: C Programming 4 Embedded Systems

Sources: On the Web: Slides will be available on:

Formatting Numbers with C++ Output Streams

C Strings and Pointers

Introduction to Data Structures

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Illustration 1: Diagram of program function and data flow

Chapter 5 Functions. Introducing Functions

Project 2: Bejeweled

F ahrenheit = 9 Celsius + 32

The While Loop. Objectives. Textbook. WHILE Loops

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

Lecture 2 Notes: Flow of Control

Schedule. Structures and Classes in C++ Outline. Goals for This Topic. Another Example of a Structure. What is a Structure? Classes May 12-17, 2005

Note: Syntactically, a ; is needed at the end of a struct definition.

Computer Programming C++ Classes and Objects 15 th Lecture

Arrays in Java. Working with Arrays

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

In this Chapter you ll learn:

6. Control Structures

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Basics of C++ and object orientation in OpenFOAM

Introduction to Java

Object Oriented Software Design II

BCS2B02: OOP Concepts and Data Structures Using C++

Informatica e Sistemi in Tempo Reale

Transcription:

References: 1. E Balagurusamy, Object Oriented Programming with C++, 4 th edition, McGraw-Hill 2008. 2. Robert Lafore, Object-Oriented Programming in C++, 4 th 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 // 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 Asst. Lec. Zainab Mahmood Fadhil Page 1

// 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 twodimensional (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]; Asst. Lec. Zainab Mahmood Fadhil Page 2

For example: a[3] = 60; // assign 60 to the fourth element cin >> mark[3]; // read the value of the 4 th 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: Asst. Lec. Zainab Mahmood Fadhil Page 3

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 ; Asst. Lec. Zainab Mahmood Fadhil Page 4

Example: Write a C++ program that adds two initialized 3 4 matrices A and B and then stores the result in a matrix C. 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 Asst. Lec. Zainab Mahmood Fadhil Page 5

// 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; Asst. Lec. Zainab Mahmood Fadhil Page 6

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. using namespace std struct Rectangle int length; int width; ; int area1, area2; Rectangle rect1 = 20, 10; Rectangle rect2 = 15, 7 ; area1 = rect1.length * rect1.width; Asst. Lec. Zainab Mahmood Fadhil Page 7

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 > struct Time int hour; int minute; int second; ; 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. Asst. Lec. Zainab Mahmood Fadhil Page 8

The Address-of Operator & We can find the address occupied by a variable by using the address-of operator &. Example: int var1 = 11; int var2 = 22; int var3 = 33; cout << &var1 << endl << &var2 << endl << &var3 << endl; Asst. Lec. Zainab Mahmood Fadhil Page 9

Output: Note: 0x8fd4fff4 0x8fd4fff2 0x8fd4fff0 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: 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 Asst. Lec. Zainab Mahmood Fadhil Page 10

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. Asst. Lec. Zainab Mahmood Fadhil Page 11

Accessing the content of pointer The content of pointer is the value of the variable pointed to by that pointer. Example: int var1 = 11; int var2 = 22; int *ptr; ptr = &var1; cout<< *ptr <<endl; ptr = &var2; cout<< *ptr <<endl; Example: using namespce std; 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. Asst. Lec. Zainab Mahmood Fadhil Page 12

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: 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; Asst. Lec. Zainab Mahmood Fadhil Page 13

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: 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; Asst. Lec. Zainab Mahmood Fadhil Page 14