lecture1 classes2
Post on 10-May-2015
286 Views
Preview:
TRANSCRIPT
+
Lecture 1 – Introduction to C++ Classes (Part 2)TTTK1924 – Program Design and Problem Solving
+Part 2 Structured Data Types
+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 …)
+Using Simple Data Types
Declare variables Eg.
int x, y;
Assign values Eg.
x = 5;
Manipulate values using arithmetic operations Eg.
y = x * x;
+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
+
Arrays Revisited
+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
+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
+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
+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
+Arrays – Example 4
… and so on until….
+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
+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
+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
+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
+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
+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
+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
+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
+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
+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
+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
+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
+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;}:
+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
+ Structs Revisited
+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;
+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
+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
+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
+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
+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
+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
+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
+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)
+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)
top related