SlideShare uma empresa Scribd logo
1 de 36
+




    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       double        array              class        pointer


          float   boolean           struct

     (…. 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;         Defines struct time
                  };
                  struct time startTime; declares startTime as a variable of type struct time
+
    Arrays Revisited
+
    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;
    }
+
    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;
    }
+
    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;
    }
+
    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;
    }
+
    Arrays – Example 4




            … and so on until….
+
    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;
    }
+
    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;
    }
+ 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
 :
+ 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;
 }
 :
+ 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;
 }
 :
+ 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;
 }
 :
+ 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;
 }
 :
+ 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;
 }                                                                  }
 :
+ 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   16
int 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;
}
+ 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   16
int 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;
}
+ 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   16
int 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;
}
+ 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   16
int 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;
}
+ 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;
}                                                                  }
                                                                   :
+ 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;
 }
 :
+
    Structs Revisited
+
    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;
+
    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
+
    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;
}
+
    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;
}
+
    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;
}
+
    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;
}
+
    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;
}
+
    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;
}
+
    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;
}
+
    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)

Mais conteúdo relacionado

Mais procurados

Bartosz Milewski, “Re-discovering Monads in C++”
Bartosz Milewski, “Re-discovering Monads in C++”Bartosz Milewski, “Re-discovering Monads in C++”
Bartosz Milewski, “Re-discovering Monads in C++”Platonov Sergey
 
The Ring programming language version 1.7 book - Part 83 of 196
The Ring programming language version 1.7 book - Part 83 of 196The Ring programming language version 1.7 book - Part 83 of 196
The Ring programming language version 1.7 book - Part 83 of 196Mahmoud Samir Fayed
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
Lecture05 operator overloading-and_exception_handling
Lecture05 operator overloading-and_exception_handlingLecture05 operator overloading-and_exception_handling
Lecture05 operator overloading-and_exception_handlingHariz Mustafa
 
Introduction to Objective - C
Introduction to Objective - CIntroduction to Objective - C
Introduction to Objective - CJussi Pohjolainen
 
Lecture08 stacks and-queues_v3
Lecture08 stacks and-queues_v3Lecture08 stacks and-queues_v3
Lecture08 stacks and-queues_v3Hariz Mustafa
 
Python 如何執行
Python 如何執行Python 如何執行
Python 如何執行kao kuo-tung
 
The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184Mahmoud Samir Fayed
 
Functional programming in C++ LambdaNsk
Functional programming in C++ LambdaNskFunctional programming in C++ LambdaNsk
Functional programming in C++ LambdaNskAlexander Granin
 
Lecture02 class -_templatev2
Lecture02 class -_templatev2Lecture02 class -_templatev2
Lecture02 class -_templatev2Hariz Mustafa
 
Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2Ali Aminian
 

Mais procurados (20)

Computer Programming- Lecture 9
Computer Programming- Lecture 9Computer Programming- Lecture 9
Computer Programming- Lecture 9
 
Bartosz Milewski, “Re-discovering Monads in C++”
Bartosz Milewski, “Re-discovering Monads in C++”Bartosz Milewski, “Re-discovering Monads in C++”
Bartosz Milewski, “Re-discovering Monads in C++”
 
Computer Programming- Lecture 5
Computer Programming- Lecture 5 Computer Programming- Lecture 5
Computer Programming- Lecture 5
 
The Ring programming language version 1.7 book - Part 83 of 196
The Ring programming language version 1.7 book - Part 83 of 196The Ring programming language version 1.7 book - Part 83 of 196
The Ring programming language version 1.7 book - Part 83 of 196
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Lecture05 operator overloading-and_exception_handling
Lecture05 operator overloading-and_exception_handlingLecture05 operator overloading-and_exception_handling
Lecture05 operator overloading-and_exception_handling
 
Introduction to Objective - C
Introduction to Objective - CIntroduction to Objective - C
Introduction to Objective - C
 
Lecture08 stacks and-queues_v3
Lecture08 stacks and-queues_v3Lecture08 stacks and-queues_v3
Lecture08 stacks and-queues_v3
 
Blazing Fast Windows 8 Apps using Visual C++
Blazing Fast Windows 8 Apps using Visual C++Blazing Fast Windows 8 Apps using Visual C++
Blazing Fast Windows 8 Apps using Visual C++
 
Idiomatic C++
Idiomatic C++Idiomatic C++
Idiomatic C++
 
Python 如何執行
Python 如何執行Python 如何執行
Python 如何執行
 
The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184
 
Computer Programming- Lecture 8
Computer Programming- Lecture 8Computer Programming- Lecture 8
Computer Programming- Lecture 8
 
Functional programming in C++ LambdaNsk
Functional programming in C++ LambdaNskFunctional programming in C++ LambdaNsk
Functional programming in C++ LambdaNsk
 
Computer Programming- Lecture 6
Computer Programming- Lecture 6Computer Programming- Lecture 6
Computer Programming- Lecture 6
 
Lecture02 class -_templatev2
Lecture02 class -_templatev2Lecture02 class -_templatev2
Lecture02 class -_templatev2
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
 
Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2Learning C++ - Pointers in c++ 2
Learning C++ - Pointers in c++ 2
 
Arrays
ArraysArrays
Arrays
 
Composing method
Composing methodComposing method
Composing method
 

Semelhante a Lecture1 classes2

Array and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxArray and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxJumanneChiyanda
 
maincpp include ListItemh include ltstringgt in.pdf
maincpp include ListItemh include ltstringgt in.pdfmaincpp include ListItemh include ltstringgt in.pdf
maincpp include ListItemh include ltstringgt in.pdfabiwarmaa
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptxMrMaster11
 
Writing native bindings to node.js in C++
Writing native bindings to node.js in C++Writing native bindings to node.js in C++
Writing native bindings to node.js in C++nsm.nikhil
 
Lec 38.39 - pointers
Lec 38.39 -  pointersLec 38.39 -  pointers
Lec 38.39 - pointersPrincess Sam
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingMeghaj Mallick
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfyamew16788
 
Lecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxLecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxarjurakibulhasanrrr7
 
C++ Secure Programming
C++ Secure ProgrammingC++ Secure Programming
C++ Secure ProgrammingMarian Marinov
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - ReferenceMohammed Sikander
 
OOP 2012 - Hint: Dynamic allocation in c++
OOP 2012 - Hint: Dynamic allocation in c++OOP 2012 - Hint: Dynamic allocation in c++
OOP 2012 - Hint: Dynamic allocation in c++Allan Sun
 
삼성 바다 앱개발 실패 노하우 2부
삼성 바다 앱개발 실패 노하우 2부삼성 바다 앱개발 실패 노하우 2부
삼성 바다 앱개발 실패 노하우 2부mosaicnet
 
arrays-120712074248-phpapp01
arrays-120712074248-phpapp01arrays-120712074248-phpapp01
arrays-120712074248-phpapp01Abdul Samee
 

Semelhante a Lecture1 classes2 (20)

Array and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxArray and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptx
 
maincpp include ListItemh include ltstringgt in.pdf
maincpp include ListItemh include ltstringgt in.pdfmaincpp include ListItemh include ltstringgt in.pdf
maincpp include ListItemh include ltstringgt in.pdf
 
Arrays_in_c++.pptx
Arrays_in_c++.pptxArrays_in_c++.pptx
Arrays_in_c++.pptx
 
Writing native bindings to node.js in C++
Writing native bindings to node.js in C++Writing native bindings to node.js in C++
Writing native bindings to node.js in C++
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
C Prog - Array
C Prog - ArrayC Prog - Array
C Prog - Array
 
C Prog - Array
C Prog - ArrayC Prog - Array
C Prog - Array
 
Lec 38.39 - pointers
Lec 38.39 -  pointersLec 38.39 -  pointers
Lec 38.39 - pointers
 
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String ProcessingDynamic Objects,Pointer to function,Array & Pointer,Character String Processing
Dynamic Objects,Pointer to function,Array & Pointer,Character String Processing
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
 
Arrays and strings in c++
Arrays and strings in c++Arrays and strings in c++
Arrays and strings in c++
 
Lecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptxLecture 5Arrays on c++ for Beginner.pptx
Lecture 5Arrays on c++ for Beginner.pptx
 
C++ Secure Programming
C++ Secure ProgrammingC++ Secure Programming
C++ Secure Programming
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
 
Array C programming
Array C programmingArray C programming
Array C programming
 
OOP 2012 - Hint: Dynamic allocation in c++
OOP 2012 - Hint: Dynamic allocation in c++OOP 2012 - Hint: Dynamic allocation in c++
OOP 2012 - Hint: Dynamic allocation in c++
 
삼성 바다 앱개발 실패 노하우 2부
삼성 바다 앱개발 실패 노하우 2부삼성 바다 앱개발 실패 노하우 2부
삼성 바다 앱개발 실패 노하우 2부
 
Lecture 6 - Arrays
Lecture 6 - ArraysLecture 6 - Arrays
Lecture 6 - Arrays
 
arrays-120712074248-phpapp01
arrays-120712074248-phpapp01arrays-120712074248-phpapp01
arrays-120712074248-phpapp01
 

Lecture1 classes2

  • 1. + Lecture 1 – Introduction to C++ Classes (Part 2) TTTK1924 – Program Design and Problem Solving
  • 2. + Part 2 Structured Data Types
  • 3. + Types of Data Types of Data Simple Data Type Structured Data Type Address Data Type int double array class pointer float boolean struct (…. and others …)
  • 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. + 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. + Arrays Revisited
  • 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. + 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. + 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. + 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. + Arrays – Example 4 … and so on until….
  • 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. + 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. + 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. + 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. + 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. + 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. + 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. + 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. + 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 16 int 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. + 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 16 int 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. + 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 16 int 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. + 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 16 int 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. + 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. + 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. + Structs Revisited
  • 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. + 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. + 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. + 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. + 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. + 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. + 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. + 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. + 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. + 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)