36
+ Lecture 1 – Introduction to C++ Classes (Part 2) TTTK1924 – Program Design and Problem Solving

Lecture1 classes2

Embed Size (px)

Citation preview

Page 1: Lecture1 classes2

+

Lecture 1 – Introduction to C++ Classes (Part 2)TTTK1924 – Program Design and Problem Solving

Page 2: Lecture1 classes2

+Part 2 Structured Data Types

Page 3: Lecture1 classes2

+Types of Data

Types of Data

Simple Data Type Structured Data Type Address Data Type

int

float

double

boolean struct

array class pointer

(…. and others …)

Page 4: Lecture1 classes2

+Using Simple Data Types

Declare variables Eg.

int x, y;

Assign values Eg.

x = 5;

Manipulate values using arithmetic operations Eg.

y = x * x;

Page 5: Lecture1 classes2

+Structured Data Types A collection of data (possibly of different types)

An array can only contain data of the same type, while a struct and a class may have different types of data

Declaration of an array must contain number of elements in the collection Eg.

int num[5] ; declares and array of 5 integers called num

A struct or class need to be defined before being used. Eg.

struct time {

int hour;

int minute;

int second;

};

struct time startTime; declares startTime as a variable of type struct time

Defines struct time

Page 6: Lecture1 classes2

+

Arrays Revisited

Page 7: Lecture1 classes2

+Arrays – Example 4

#include <iostream>

using namespace std;

int main (int argc, const char * argv[]){ int item[5]; int sum; int counter; for (counter=0; counter<5; counter++) { cin >> item[counter]; sum = sum + item[counter]; } cout << "The sum of the numbers is: " << sum << endl; return 0;}

Declaration of an array

Loop to access every item in the array

Page 8: Lecture1 classes2

+Arrays – Example 4

#include <iostream>

using namespace std;

int main (int argc, const char * argv[]){ int item[5]; int sum; int counter; for (counter=0; counter<5; counter++) { cin >> item[counter]; sum = sum + item[counter]; } cout << "The sum of the numbers is: " << sum << endl; return 0;}

item

0 1 2 3 4

sum counter

Page 9: Lecture1 classes2

+Arrays – Example 4

#include <iostream>

using namespace std;

int main (int argc, const char * argv[]){ int item[5]; int sum; int counter; for (counter=0; counter<5; counter++) { cin >> item[counter]; sum = sum + item[counter]; } cout << "The sum of the numbers is: " << sum << endl; return 0;}

12

item

0 1 2 3 4

sum

12

counter

0

12

Page 10: Lecture1 classes2

+Arrays – Example 4

#include <iostream>

using namespace std;

int main (int argc, const char * argv[]){ int item[5]; int sum; int counter; for (counter=0; counter<5; counter++) { cin >> item[counter]; sum = sum + item[counter]; } cout << "The sum of the numbers is: " << sum << endl; return 0;}

12

76

item

0 1 2 3 4

sum

88

counter

1

12 76

Page 11: Lecture1 classes2

+Arrays – Example 4

… and so on until….

Page 12: Lecture1 classes2

+Arrays – Example 4

#include <iostream>

using namespace std;

int main (int argc, const char * argv[]){ int item[5]; int sum; int counter; for (counter=0; counter<5; counter++) { cin >> item[counter]; sum = sum + item[counter]; } cout << "The sum of the numbers is: " << sum << endl; return 0;}

12

76 34

52

89

item

0 1 2 3 4

sum

263

counter

4

12 76 34 52 89

Page 13: Lecture1 classes2

+Arrays – Example 4

#include <iostream>

using namespace std;

int main (int argc, const char * argv[]){ int item[5]; int sum; int counter; for (counter=0; counter<5; counter++) { cin >> item[counter]; sum = sum + item[counter]; } cout << "The sum of the numbers is: " << sum << endl; return 0;}

12

76 34

52

89

item

0 1 2 3 4

sum

263

counter

5

12 76 34 52 89The sum of numbers is: 263

Page 14: Lecture1 classes2

+Array and Function – Example 5

:int seqSearch(const int list[], int listLength, int searchItem){ int loc; bool found = false; for (loc=0; loc<listLength; loc++) if (list[loc] == searchItem) { found = true; break; } if (found) return loc; else return -1;}

#include <iostream>

using namespace std;const int arraySize = 10;

int seqSearch(const int list[], int listLength, int searchItem);

int main (int argc, const char * argv[]){ int intList[arraySize]; int number; int index; cout << "Enter " << arraySize << " integers: "; for (index=0; index < arraySize; index++) cin >> intList[index]; cout << endl; cout << "Enter number to be searched: "; cin >> number; cout << endl; index = seqSearch(intList, arraySize, number); if (index != -1) cout << "Number " << number << " is found at position " << index << endl; else cout << "Number " << number << " is not in the list." << endl; return 0;}:

function prototype

function call

Formal parameter

Page 15: Lecture1 classes2

+Array and Function – Example 5#include <iostream>

using namespace std;const int arraySize = 10;

int seqSearch(const int list[], int listLength, int searchItem);

int main (int argc, const char * argv[]){ int intList[arraySize]; int number; int index; cout << "Enter " << arraySize << " integers: "; for (index=0; index < arraySize; index++) cin >> intList[index]; cout << endl; cout << "Enter number to be searched: "; cin >> number; cout << endl; index = seqSearch(intList, arraySize, number); if (index != -1) cout << "Number " << number << " is found at position " << index << endl; else cout << "Number " << number << " is not in the list." << endl; return 0;}:

intList

arraySize

10

0 1 2 3 4 5 6 7 8 9

number

index

Page 16: Lecture1 classes2

+Array and Function – Example 5#include <iostream>

using namespace std;const int arraySize = 10;

int seqSearch(const int list[], int listLength, int searchItem);

int main (int argc, const char * argv[]){ int intList[arraySize]; int number; int index; cout << "Enter " << arraySize << " integers: "; for (index=0; index < arraySize; index++) cin >> intList[index]; cout << endl; cout << "Enter number to be searched: "; cin >> number; cout << endl; index = seqSearch(intList, arraySize, number); if (index != -1) cout << "Number " << number << " is found at position " << index << endl; else cout << "Number " << number << " is not in the list." << endl; return 0;}:

intList

arraySize

10

0 1 2 3 4 5 6 7 8 9

Enter 10 integers:

number

index

Page 17: Lecture1 classes2

+Array and Function – Example 5#include <iostream>

using namespace std;const int arraySize = 10;

int seqSearch(const int list[], int listLength, int searchItem);

int main (int argc, const char * argv[]){ int intList[arraySize]; int number; int index; cout << "Enter " << arraySize << " integers: "; for (index=0; index < arraySize; index++) cin >> intList[index]; cout << endl; cout << "Enter number to be searched: "; cin >> number; cout << endl; index = seqSearch(intList, arraySize, number); if (index != -1) cout << "Number " << number << " is found at position " << index << endl; else cout << "Number " << number << " is not in the list." << endl; return 0;}:

2 56 34

25

73

46

89

10

5 16

intList

arraySize

10

0 1 2 3 4 5 6 7 8 9

Enter 10 integers: 2 56 34 25 73 46 89 10 5 16

number

index

Page 18: Lecture1 classes2

+Array and Function – Example 5#include <iostream>

using namespace std;const int arraySize = 10;

int seqSearch(const int list[], int listLength, int searchItem);

int main (int argc, const char * argv[]){ int intList[arraySize]; int number; int index; cout << "Enter " << arraySize << " integers: "; for (index=0; index < arraySize; index++) cin >> intList[index]; cout << endl; cout << "Enter number to be searched: "; cin >> number; cout << endl; index = seqSearch(intList, arraySize, number); if (index != -1) cout << "Number " << number << " is found at position " << index << endl; else cout << "Number " << number << " is not in the list." << endl; return 0;}:

2 56 34

25

73

46

89

10

5 16

intList

arraySize

10

0 1 2 3 4 5 6 7 8 9

Enter 10 integers: 2 56 34 25 73 46 89 10 5 16Enter number to be searched: 25

number

25

index

Page 19: Lecture1 classes2

+Array and Function – Example 5#include <iostream>

using namespace std;const int arraySize = 10;

int seqSearch(const int list[], int listLength, int searchItem);

int main (int argc, const char * argv[]){ int intList[arraySize]; int number; int index; cout << "Enter " << arraySize << " integers: "; for (index=0; index < arraySize; index++) cin >> intList[index]; cout << endl; cout << "Enter number to be searched: "; cin >> number; cout << endl; index = seqSearch(intList, arraySize, number); if (index != -1) cout << "Number " << number << " is found at position " << index << endl; else cout << "Number " << number << " is not in the list." << endl; return 0;}:

2 56 34

25

73

46

89

10

5 16

intList

arraySize

10

0 1 2 3 4 5 6 7 8 9

Enter 10 integers: 2 56 34 25 73 46 89 10 5 16Enter number to be searched: 25

number

25

:int seqSearch(const int list[], int listLength, int searchItem){ int loc; bool found = false; for (loc=0; loc<listLength; loc++) if (list[loc] == searchItem) { found = true; break; } if (found) return loc; else return -1;}

index

Page 20: Lecture1 classes2

+Array and Function – Example 5

2 56 34

25

73

46

89

10

5 16

intList

arraySize

10

0 1 2 3 4 5 6 7 8 9

Enter 10 integers: 2 56 34 25 73 46 89 10 5 16Enter number to be searched: 25

number

25

:int seqSearch(const int list[], int listLength, int searchItem){ int loc; bool found = false; for (loc=0; loc<listLength; loc++) if (list[loc] == searchItem) { found = true; break; } if (found) return loc; else return -1;}

list

listLength

10

searchItem

25

index

Page 21: Lecture1 classes2

+Array and Function – Example 5

2 56 34

25

73

46

89

10

5 16

intList

arraySize

10

0 1 2 3 4 5 6 7 8 9

Enter 10 integers: 2 56 34 25 73 46 89 10 5 16Enter number to be searched: 25

number

25

:int seqSearch(const int list[], int listLength, int searchItem){ int loc; bool found = false; for (loc=0; loc<listLength; loc++) if (list[loc] == searchItem) { found = true; break; } if (found) return loc; else return -1;}

list

listLength

10

searchItem

25

loc found

false

index

Page 22: Lecture1 classes2

+Array and Function – Example 5

2 56 34

25

73

46

89

10

5 16

intList

arraySize

10

0 1 2 3 4 5 6 7 8 9

Enter 10 integers: 2 56 34 25 73 46 89 10 5 16Enter number to be searched: 25

number

25

:int seqSearch(const int list[], int listLength, int searchItem){ int loc; bool found = false; for (loc=0; loc<listLength; loc++) if (list[loc] == searchItem) { found = true; break; } if (found) return loc; else return -1;}

list

listLength

10

searchItem

25

loc

0

found

false

index

Page 23: Lecture1 classes2

+Array and Function – Example 5

2 56 34

25

73

46

89

10

5 16

intList

arraySize

10

0 1 2 3 4 5 6 7 8 9

Enter 10 integers: 2 56 34 25 73 46 89 10 5 16Enter number to be searched: 25

number

25

:int seqSearch(const int list[], int listLength, int searchItem){ int loc; bool found = false; for (loc=0; loc<listLength; loc++) if (list[loc] == searchItem) { found = true; break; } if (found) return loc; else return -1;}

list

listLength

10

searchItem

25

loc

3

found

true

index

Page 24: Lecture1 classes2

+Array and Function – Example 5

:int seqSearch(const int list[], int listLength, int searchItem){ int loc; bool found = false; for (loc=0; loc<listLength; loc++) if (list[loc] == searchItem) { found = true; break; } if (found) return loc; else return -1;}

list

listLength

10

searchItem

25

loc

3

found

true

#include <iostream>

using namespace std;const int arraySize = 10;

int seqSearch(const int list[], int listLength, int searchItem);

int main (int argc, const char * argv[]){ int intList[arraySize]; int number; int index; cout << "Enter " << arraySize << " integers: "; for (index=0; index < arraySize; index++) cin >> intList[index]; cout << endl; cout << "Enter number to be searched: "; cin >> number; cout << endl; index = seqSearch(intList, arraySize, number); if (index != -1) cout << "Number " << number << " is found at position " << index << endl; else cout << "Number " << number << " is not in the list." << endl; return 0;}:

Page 25: Lecture1 classes2

+Array and Function – Example 5#include <iostream>

using namespace std;const int arraySize = 10;

int seqSearch(const int list[], int listLength, int searchItem);

int main (int argc, const char * argv[]){ int intList[arraySize]; int number; int index; cout << "Enter " << arraySize << " integers: "; for (index=0; index < arraySize; index++) cin >> intList[index]; cout << endl; cout << "Enter number to be searched: "; cin >> number; cout << endl; index = seqSearch(intList, arraySize, number); if (index != -1) cout << "Number " << number << " is found at position " << index << endl; else cout << "Number " << number << " is not in the list." << endl; return 0;}:

2 56 34

25

73

46

89

10

5 16

intList

arraySize

10

0 1 2 3 4 5 6 7 8 9

Enter 10 integers: 2 56 34 25 73 46 89 10 5 16Enter number to be searched: 25Number 25 is found at position 3

number

25

index

3

Page 26: Lecture1 classes2

+ Structs Revisited

Page 27: Lecture1 classes2

+Using structs1. Define a struct type according to the following syntax:

struct <structName> {

<declaration of data items>

};

Eg.

struct timeType {

int hour;

int minute;

int second;

};

2. Declare a variable of the struct type

Eg. timeType startTime;

3. Access or modify the data with the dot (.) operator

Eg. startTime.hour = 6;

Page 28: Lecture1 classes2

+Struct – Example 6

#include <iostream>#include <cstring>

using namespace std;

struct studentType { string firstName; string lastName; float marks; char grade; };

:

:int main (int argc, const char * argv[]){ studentType student1; student1.firstName = "John"; student1.lastName = "Doe"; student1.marks = 73.5; student1.grade = 'B'; cout << "Test Result " << endl; cout << "Name : " << student1.firstName << " " <<

student1.lastName << endl; cout << "Grade : " << student1.grade << "(" << student1.marks

<< ")" << endl; return 0;}

Definition of studentType

Declaration of variable of studentType

Page 29: Lecture1 classes2

+Struct – Example 6

:int main (int argc, const char * argv[]){ studentType student1; student1.firstName = "John"; student1.lastName = "Doe"; student1.marks = 73.5; student1.grade = 'B'; cout << "Test Result " << endl; cout << "Name : " << student1.firstName << " " <<

student1.lastName << endl; cout << "Grade : " << student1.grade << "(" << student1.marks

<< ")" << endl; return 0;}

first

student1

firstName

lastName

marks

grade

Page 30: Lecture1 classes2

+Struct – Example 6

:int main (int argc, const char * argv[]){ studentType student1; student1.firstName = "John"; student1.lastName = "Doe"; student1.marks = 73.5; student1.grade = 'B'; cout << "Test Result " << endl; cout << "Name : " << student1.firstName << " " <<

student1.lastName << endl; cout << "Grade : " << student1.grade << "(" << student1.marks

<< ")" << endl; return 0;}

first

JohnDoe

student1

firstName

lastName

marks

grade

Page 31: Lecture1 classes2

+Struct – Example 6

:int main (int argc, const char * argv[]){ studentType student1; student1.firstName = "John"; student1.lastName = "Doe"; student1.marks = 73.5; student1.grade = 'B'; cout << "Test Result " << endl; cout << "Name : " << student1.firstName << " " <<

student1.lastName << endl; cout << "Grade : " << student1.grade << "(" << student1.marks

<< ")" << endl; return 0;}

first

JohnDoe

73.5

student1

firstName

lastName

marks

grade

Page 32: Lecture1 classes2

+Struct – Example 6

:int main (int argc, const char * argv[]){ studentType student1; student1.firstName = "John"; student1.lastName = "Doe"; student1.marks = 73.5; student1.grade = 'B'; cout << "Test Result " << endl; cout << "Name : " << student1.firstName << " " <<

student1.lastName << endl; cout << "Grade : " << student1.grade << "(" << student1.marks

<< ")" << endl; return 0;}

first

JohnDoe

73.5B

student1

firstName

lastName

marks

grade

Page 33: Lecture1 classes2

+Struct – Example 6

:int main (int argc, const char * argv[]){ studentType student1; student1.firstName = "John"; student1.lastName = "Doe"; student1.marks = 73.5; student1.grade = 'B'; cout << "Test Result " << endl; cout << "Name : " << student1.firstName << " " <<

student1.lastName << endl; cout << "Grade : " << student1.grade << "(" << student1.marks

<< ")" << endl; return 0;}

first

JohnDoe

73.5B

student1

firstName

lastName

marks

grade

Test Result

Page 34: Lecture1 classes2

+Struct – Example 6

:int main (int argc, const char * argv[]){ studentType student1; student1.firstName = "John"; student1.lastName = "Doe"; student1.marks = 73.5; student1.grade = 'B'; cout << "Test Result " << endl; cout << "Name : " << student1.firstName << " " <<

student1.lastName << endl; cout << "Grade : " << student1.grade << "(" << student1.marks

<< ")" << endl; return 0;}

first

JohnDoe

73.5B

student1

firstName

lastName

marks

grade

Test ResultName : John Doe

Page 35: Lecture1 classes2

+Struct – Example 6

:int main (int argc, const char * argv[]){ studentType student1; student1.firstName = "John"; student1.lastName = "Doe"; student1.marks = 73.5; student1.grade = 'B'; cout << "Test Result " << endl; cout << "Name : " << student1.firstName << " " <<

student1.lastName << endl; cout << "Grade : " << student1.grade << "(" << student1.marks

<< ")" << endl; return 0;}

first

JohnDoe

73.5B

student1

firstName

lastName

marks

grade

Test ResultName : John DoeGrade : B(73.5)

Page 36: Lecture1 classes2

+References:

D.S. Malik (2012). C++ Programming: Program Design Including Data Structures (5th ed), Thomson Course Technology. Chapter 9 – Arrays and Strings Chapter 11 – Records (structs)