Upcoming SlideShare
×

# Lecture1 classes2

415 views
330 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
415
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
14
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Lecture1 classes2

1. 1. + Lecture 1 – Introduction to C++ Classes (Part 2) TTTK1924 – Program Design and Problem Solving
2. 2. + Part 2 Structured Data Types
3. 3. + Types of Data Types of DataSimple Data Type Structured Data Type Address Data Type int double array class pointer float boolean struct (…. and others …)
4. 4. + Using Simple Data Types  Declare variables  Eg. int x, y;  Assign values  Eg. x = 5;  Manipulate values using arithmetic operations  Eg. y = x * x;
5. 5. + 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; Defines struct time }; struct time startTime; declares startTime as a variable of type struct time
6. 6. + Arrays Revisited
7. 7. + Arrays – Example 4 #include <iostream> using namespace std; int main (int argc, const char * argv[]) Declaration of an array { int item[5]; int sum; int counter; for (counter=0; counter<5; counter++) { cin >> item[counter]; Loop to access every item sum = sum + item[counter]; in the array } cout << "The sum of the numbers is: " << sum << endl; return 0; }
8. 8. + Arrays – Example 4 #include <iostream> item using namespace std; int main (int argc, const char * argv[]) 0 1 2 3 4 { int item[5]; sum counter 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; }
9. 9. + Arrays – Example 4 #include <iostream> item using namespace std; 1 2 int main (int argc, const char * argv[]) 0 1 2 3 4 { int item[5]; sum counter int sum; int counter; 12 0 for (counter=0; counter<5; counter++) { cin >> item[counter]; sum = sum + item[counter]; 12 } cout << "The sum of the numbers is: " << sum << endl; return 0; }
10. 10. + Arrays – Example 4 #include <iostream> item using namespace std; 1 76 2 int main (int argc, const char * argv[]) 0 1 2 3 4 { int item[5]; sum counter int sum; int counter; 88 1 for (counter=0; counter<5; counter++) { cin >> item[counter]; sum = sum + item[counter]; 12 76 } cout << "The sum of the numbers is: " << sum << endl; return 0; }
11. 11. + Arrays – Example 4 … and so on until….
12. 12. + Arrays – Example 4 #include <iostream> item using namespace std; 1 76 3 52 89 2 4 int main (int argc, const char * argv[]) 0 1 2 3 4 { int item[5]; sum counter int sum; int counter; 263 4 for (counter=0; counter<5; counter++) { cin >> item[counter]; sum = sum + item[counter]; 12 76 34 52 89 } cout << "The sum of the numbers is: " << sum << endl; return 0; }
13. 13. + Arrays – Example 4 #include <iostream> item using namespace std; 1 76 3 52 89 2 4 int main (int argc, const char * argv[]) 0 1 2 3 4 { int item[5]; sum counter int sum; int counter; 263 5 for (counter=0; counter<5; counter++) { cin >> item[counter]; sum = sum + item[counter]; 12 76 34 52 89 } The sum of numbers is: 263 cout << "The sum of the numbers is: " << sum << endl; return 0; }
14. 14. + Array and Function – Example 5 #include <iostream> using namespace std; function prototype const int arraySize = 10; int seqSearch(const int list[], int listLength, int searchItem); : int main (int argc, const char * argv[]) int seqSearch(const int list[], int listLength, int { int intList[arraySize]; searchItem){ int number; int index; int loc; cout << "Enter " << arraySize << " integers: "; bool found = false; for (index=0; index < arraySize; index++) cin >> intList[index]; for (loc=0; loc<listLength; loc++) cout << endl; cout << "Enter number to be searched: "; if (list[loc] == searchItem) { cin >> number; found = true; cout << endl; break; index = seqSearch(intList, arraySize, number); } if (index != -1) if (found) cout << "Number " << number << " is found at position " << return loc; index << endl; else else cout << "Number " << number << " is not in the list." << return -1; endl; } Formal parameter return 0; } function call :
15. 15. + Array and Function – Example 5 #include <iostream> index using namespace std; const int arraySize = 10; int seqSearch(const int list[], int listLength, int searchItem); arraySize number int main (int argc, const char * argv[]) 10 { int intList[arraySize]; int number; intList int index; cout << "Enter " << arraySize << " integers: "; for (index=0; index < arraySize; index++) cin >> intList[index]; 0 1 2 3 4 5 6 7 8 9 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; } :
16. 16. + Array and Function – Example 5 #include <iostream> index using namespace std; const int arraySize = 10; int seqSearch(const int list[], int listLength, int searchItem); arraySize number int main (int argc, const char * argv[]) 10 { int intList[arraySize]; int number; intList int index; cout << "Enter " << arraySize << " integers: "; for (index=0; index < arraySize; index++) cin >> intList[index]; 0 1 2 3 4 5 6 7 8 9 cout << endl; cout << "Enter number to be searched: "; cin >> number; Enter 10 integers: 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; } :
17. 17. + Array and Function – Example 5 #include <iostream> index using namespace std; const int arraySize = 10; int seqSearch(const int list[], int listLength, int searchItem); arraySize number int main (int argc, const char * argv[]) 10 { int intList[arraySize]; int number; intList int index; cout << "Enter " << arraySize << " integers: "; 2 56 3 25 73 46 89 1 5 16 for (index=0; index < arraySize; index++) 4 0 cin >> intList[index]; 0 1 2 3 4 5 6 7 8 9 cout << endl; cout << "Enter number to be searched: "; cin >> number; Enter 10 integers: 2 56 34 25 73 46 89 10 5 16 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; } :
18. 18. + Array and Function – Example 5 #include <iostream> index using namespace std; const int arraySize = 10; int seqSearch(const int list[], int listLength, int searchItem); arraySize number int main (int argc, const char * argv[]) 10 25 { int intList[arraySize]; int number; intList int index; cout << "Enter " << arraySize << " integers: "; 2 56 3 25 73 46 89 1 5 16 for (index=0; index < arraySize; index++) 4 0 cin >> intList[index]; 0 1 2 3 4 5 6 7 8 9 cout << endl; cout << "Enter number to be searched: "; cin >> number; Enter 10 integers: 2 56 34 25 73 46 89 10 5 16 cout << endl; Enter number to be searched: 25 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; } :
19. 19. + Array and Function – Example 5 #include <iostream> index using namespace std; const int arraySize = 10; int seqSearch(const int list[], int listLength, int searchItem); arraySize number int main (int argc, const char * argv[]) 10 25 { int intList[arraySize]; int number; intList int index; cout << "Enter " << arraySize << " integers: "; : 2 56 3 25 73 46 89 1 5 16 for (index=0; index < arraySize; index++) int seqSearch(const int list[], int listLength, int 4 0 cin >> intList[index]; 0 1 searchItem){ 2 3 4 5 6 7 8 9 cout << endl; cout << "Enter number to be searched: "; int loc; cin >> number; Enter 10 integers: 2 56 34 25 73 46 89 10 5 16 bool found = false; cout << endl; Enter number to be searched: 25 index = seqSearch(intList, arraySize, number); for (loc=0; loc<listLength; loc++) if (index != -1) if (list[loc] == searchItem) { cout << "Number " << number << " is found at position " << found = true; index << endl; break; else } cout << "Number " << number << " is not in the list." << if (found) endl; return loc; else return 0; return -1; } } :
20. 20. + Array and Function – Example 5 index listLength searchItem 10 25 arraySize number 10 25 list intList: 2 56 3 25 73 46 89 1 5 16int seqSearch(const int list[], int listLength, int searchItem){ 4 0 0 1 2 3 4 5 6 7 8 9 int loc; bool found = false; Enter 10 integers: 2 56 34 25 73 46 89 10 5 16 Enter number to be searched: 25 for (loc=0; loc<listLength; loc++) if (list[loc] == searchItem) { found = true; break; } if (found) return loc; else return -1;}
21. 21. + Array and Function – Example 5 index listLength searchItem 10 25 arraySize number found 10 25 loc list false intList: 2 56 3 25 73 46 89 1 5 16int seqSearch(const int list[], int listLength, int searchItem){ 4 0 0 1 2 3 4 5 6 7 8 9 int loc; bool found = false; Enter 10 integers: 2 56 34 25 73 46 89 10 5 16 Enter number to be searched: 25 for (loc=0; loc<listLength; loc++) if (list[loc] == searchItem) { found = true; break; } if (found) return loc; else return -1;}
22. 22. + Array and Function – Example 5 index listLength searchItem 10 25 arraySize number found 10 25 loc list 0 false intList: 2 56 3 25 73 46 89 1 5 16int seqSearch(const int list[], int listLength, int searchItem){ 4 0 0 1 2 3 4 5 6 7 8 9 int loc; bool found = false; Enter 10 integers: 2 56 34 25 73 46 89 10 5 16 Enter number to be searched: 25 for (loc=0; loc<listLength; loc++) if (list[loc] == searchItem) { found = true; break; } if (found) return loc; else return -1;}
23. 23. + Array and Function – Example 5 index listLength searchItem 10 25 arraySize number found 10 25 loc list 3 true intList: 2 56 3 25 73 46 89 1 5 16int seqSearch(const int list[], int listLength, int searchItem){ 4 0 0 1 2 3 4 5 6 7 8 9 int loc; bool found = false; Enter 10 integers: 2 56 34 25 73 46 89 10 5 16 Enter number to be searched: 25 for (loc=0; loc<listLength; loc++) if (list[loc] == searchItem) { found = true; break; } if (found) return loc; else return -1;}
24. 24. + Array and Function – Example 5 #include <iostream> using namespace std; listLength searchItem const int arraySize = 10; 10 25 int seqSearch(const int list[], int listLength, int searchItem); int main (int argc, const char * argv[]) loc found list { int intList[arraySize]; 3 true int number; int index; cout << "Enter " << arraySize << " integers: ";: for (index=0; index < arraySize; index++)int seqSearch(const int list[], int listLength, int searchItem){ cin >> intList[index]; cout << endl; int loc; cout << "Enter number to be searched: "; bool found = false; cin >> number; cout << endl; for (loc=0; loc<listLength; loc++) index = seqSearch(intList, arraySize, number); if (list[loc] == searchItem) { if (index != -1) found = true; cout << "Number " << number << " is found at position " << break; index << endl; } else if (found) cout << "Number " << number << " is not in the list." << return loc; endl; else return -1; return 0;} } :
25. 25. + Array and Function – Example 5 #include <iostream> index using namespace std; 3 const int arraySize = 10; int seqSearch(const int list[], int listLength, int searchItem); arraySize number int main (int argc, const char * argv[]) 10 25 { int intList[arraySize]; int number; intList int index; cout << "Enter " << arraySize << " integers: "; 2 56 3 25 73 46 89 1 5 16 for (index=0; index < arraySize; index++) 4 0 cin >> intList[index]; 0 1 2 3 4 5 6 7 8 9 cout << endl; cout << "Enter number to be searched: "; cin >> number; Enter 10 integers: 2 56 34 25 73 46 89 10 5 16 cout << endl; Enter number to be searched: 25 index = seqSearch(intList, arraySize, number); Number 25 is found at position 3 if (index != -1) cout << "Number " << number << " is found at position " << index << endl; else cout << "Number " << number << " is not in the list." << endl; return 0; } :
26. 26. + Structs Revisited
27. 27. + Using structs 1. 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;
28. 28. + Struct – Example 6 Declaration of variable of studentType #include <iostream> : #include <cstring> int main (int argc, const char * argv[]) { using namespace std; studentType student1; student1.firstName = "John"; struct studentType { student1.lastName = "Doe"; string firstName; student1.marks = 73.5; string lastName; student1.grade = B; float marks; cout << "Test Result " << endl; char grade; cout << "Name : " << student1.firstName << " " << }; student1.lastName << endl; cout << "Grade : " << student1.grade << "(" << student1.marks : << ")" << endl; return 0; } Definition of studentType
29. 29. + Struct – Example 6 student1:int main (int argc, const char * argv[]){ firstName studentType student1; lastName student1.firstName = "John"; first marks student1.lastName = "Doe"; student1.marks = 73.5; grade student1.grade = B; cout << "Test Result " << endl; cout << "Name : " << student1.firstName << " " << student1.lastName << endl; cout << "Grade : " << student1.grade << "(" << student1.marks << ")" << endl; return 0;}
30. 30. + Struct – Example 6 student1:int main (int argc, const char * argv[]){ firstName John studentType student1; lastName Doe student1.firstName = "John"; first marks student1.lastName = "Doe"; student1.marks = 73.5; grade student1.grade = B; cout << "Test Result " << endl; cout << "Name : " << student1.firstName << " " << student1.lastName << endl; cout << "Grade : " << student1.grade << "(" << student1.marks << ")" << endl; return 0;}
31. 31. + Struct – Example 6 student1:int main (int argc, const char * argv[]){ firstName John studentType student1; lastName Doe student1.firstName = "John"; first marks 73.5 student1.lastName = "Doe"; student1.marks = 73.5; grade student1.grade = B; cout << "Test Result " << endl; cout << "Name : " << student1.firstName << " " << student1.lastName << endl; cout << "Grade : " << student1.grade << "(" << student1.marks << ")" << endl; return 0;}
32. 32. + Struct – Example 6 student1:int main (int argc, const char * argv[]){ firstName John studentType student1; lastName Doe student1.firstName = "John"; first marks 73.5 student1.lastName = "Doe"; student1.marks = 73.5; grade B student1.grade = B; cout << "Test Result " << endl; cout << "Name : " << student1.firstName << " " << student1.lastName << endl; cout << "Grade : " << student1.grade << "(" << student1.marks << ")" << endl; return 0;}
33. 33. + Struct – Example 6 student1:int main (int argc, const char * argv[]){ firstName John studentType student1; lastName Doe student1.firstName = "John"; first marks 73.5 student1.lastName = "Doe"; student1.marks = 73.5; grade B student1.grade = B; cout << "Test Result " << endl; cout << "Name : " << student1.firstName << " " << student1.lastName << endl; Test Result cout << "Grade : " << student1.grade << "(" << student1.marks << ")" << endl; return 0;}
34. 34. + Struct – Example 6 student1:int main (int argc, const char * argv[]){ firstName John studentType student1; lastName Doe student1.firstName = "John"; first marks 73.5 student1.lastName = "Doe"; student1.marks = 73.5; grade B student1.grade = B; cout << "Test Result " << endl; cout << "Name : " << student1.firstName << " " << student1.lastName << endl; Test Result cout << "Grade : " << student1.grade << "(" << student1.marks Name : John Doe << ")" << endl; return 0;}
35. 35. + Struct – Example 6 student1:int main (int argc, const char * argv[]){ firstName John studentType student1; lastName Doe student1.firstName = "John"; first marks 73.5 student1.lastName = "Doe"; student1.marks = 73.5; grade B student1.grade = B; cout << "Test Result " << endl; cout << "Name : " << student1.firstName << " " << student1.lastName << endl; Test Result cout << "Grade : " << student1.grade << "(" << student1.marks Name : John Doe << ")" << endl; Grade : B(73.5) return 0;}
36. 36. + 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)