KEMBAR78
Lecture1 classes3 | PPTX
+




    Lecture 1 – Introduction to
    C++ Classes (Part 3)
    TTTK1924 – Program Design and Problem
    Solving
+
    Part 3
    Classes
+
    Using Classes
    1.   Define a class according to the following syntax:
             class <classIdentifier> {
             private:
                        <declaration of member variables>
             public:
                        <definition of member functions>
             };

    2.   Declare a variable of the class – object variable
             <classIdentifier> <object-variable>;


    3.   Access or modify the data by calling member functions with the dot (.)
         operator
             <object-variable>.<member-function>;
             or
             <variable> = <object-variable>.<member-function>;
+
    Class Definition

     Includes
        declaration of member variables(may contain different types
         of data)
        definition of member functions – used to manipulate data
         items

     Member  variables are declared private to the class
     – can only be accessed or modified by its own class

     Member  functions are declared public ( can be used
     by other than its own class)
+
    Class Definition - Example
    class timeType {                                    :
                                                       Name of class
    private:                                           string getZone() {
       int hour;
                                                      (class identifier)
                                                          return zone;
       int minute;                                         }
       int second;
       string zone;                                        void printTime() const {
                                                             if (hour < 10)
    public:                                                      cout << "0";
      void setTime(int h, int m, int s, string z) {          cout << hour << ":";
        hour = h;                                            if (minute < 10)
        minute = m;                                              cout << "0";
        second = s;                                          cout << minute << ":";
        zone = z;                                            if (second < 10)
      }                                                          cout << "0";
                                                             cout << second << ":";
      int getHour() {                                        cout << zone << endl;
         return hour;                                      }
      }
                                                           timeType(){
      int getMinute() {                                      hour = 0;
         return minute;                                      minute = 0;
      }                                                      second = 0;
                                                             zone = "Kuala Lumpur";
      int getSecond() {                                    }
         return second;                               };
      }
      :
+
    Class Definition - Example
    class timeType {                                       :
    private:                                               string getZone() {
       int hour;                                              return zone;
       int minute;                                         }
       int second;
       string zone;                                    void printTime() const {
                                                         if (hour < 10)
    public:                                           Declaration"0"; member
                                                             cout <<
                                                                      of              variables
      void setTime(int h, int m, int s, string z) {      cout << hour << ":";
        hour = h;                                        if (minute < 10)
        minute = m;                                          cout << "0";
        second = s;                                      cout << minute << ":";
        zone = z;                                        if (second < 10)
      }                                                      cout << "0";
                                                         cout << second << ":";
      int getHour() {                                    cout << zone << endl;
         return hour;                                  }
      }
                                                           timeType(){
      int getMinute() {                                      hour = 0;
         return minute;                                      minute = 0;
      }                                                      second = 0;
                                                             zone = "Kuala Lumpur";
      int getSecond() {                                    }
         return second;                               };
      }
      :
+
    Class Definition - Example
    class timeType {                                       :
                          Definition of                    string getZone() {
    private:
       int hour;        member functions                      return zone;
       int minute;                                         }
       int second;
       string zone;                                        void printTime() const {
                                                             if (hour < 10)
    public:                                                      cout << "0";
      void setTime(int h, int m, int s, string z) {          cout << hour << ":";
        hour = h;                                            if (minute < 10)
        minute = m;                                              cout << "0";
        second = s;                                          cout << minute << ":";
        zone = z;                                            if (second < 10)
      }                                                          cout << "0";
                                                             cout << second << ":";
      int getHour() {                                        cout << zone << endl;
         return hour;                                      }
      }
                                                           timeType(){
      int getMinute() {                                      hour = 0;
         return minute;                                      minute = 0;
      }                                                      second = 0;
                                                             zone = "Kuala Lumpur";
      int getSecond() {                                    }
         return second;                               };
      }
      :
+
    Using Classes

       Defined classes can be used by
           The main program, or
           Other classes

       Only public member functions can be called or used by the
        main program or other classes
+
    Using Classes - Example

    int main (int argc, const char * argv[])
    {
      timeType currentTime;
      currentTime.printTime();
      currentTime.setTime(5, 13, 0, "Singapore");
      currentTime.printTime();
      cout << "The time in " << currentTime.getZone() << " now is "
         << currentTime.getHour() << ":" << currentTime.getMinute()
         << endl;
      return 0;
    }
+
    Using Classes - Example

    int main (int argc, const char * argv[])
    {
      timeType currentTime;                                           Declaration of
      currentTime.printTime();                                        object variable
      currentTime.setTime(5, 13, 0, "Singapore");
      currentTime.printTime();
      cout << "The time in " << currentTime.getZone() << " now is "
         << currentTime.getHour() << ":" << currentTime.getMinute()
         << endl;
      return 0;
    }
+
    Using Classes - Example

    int main (int argc, const char * argv[])
    {
      timeType currentTime;
      currentTime.printTime();
      currentTime.setTime(5, 13, 0, "Singapore");
      currentTime.printTime();
      cout << "The time in " << currentTime.getZone() << " now is "
         << currentTime.getHour() << ":" << currentTime.getMinute()
         << endl;
      return 0;
    }



                          Accessing member variables using
                                  member functions
+
    Using Classes - Example

    int main (int argc, const char * argv[])
    {
      timeType currentTime;
      currentTime.printTime();
      currentTime.setTime(5, 13, 0, "Singapore");
      currentTime.printTime();
      cout << "The time in " << currentTime.getZone() << " now is "
         << currentTime.getHour() << ":" << currentTime.getMinute()
         << endl;
      return 0;
    }




                                      Modifying member variables
                                       using member functions
+
    Using Classes – Example 7

    int main (int argc, const char * argv[])
    {
      timeType currentTime;
      currentTime.printTime();
      currentTime.setTime(5, 13, 0, "Singapore");
      currentTime.printTime();                                :
                                                                timeType(){
      cout << "The time in " << currentTime.getZone() << " now is "
                                                                  hour = 0;
         << currentTime.getHour() << ":" << currentTime.getMinute() = 0;
                                                                  minute
         << endl;                                                 second = 0;
      return 0;                                                   zone = "Kuala Lumpur";
    }                                                           }

              currentTime                                      };

              hour           0
               minute        0
                             first
               second        0
               zone         Kuala Lumpur
+
    Using Classes – Example 7

    int main (int argc, const char * argv[])
    {
      timeType currentTime;
      currentTime.printTime();
      currentTime.setTime(5, 13, 0, "Singapore");
      currentTime.printTime();                                :
      cout << "The time in " << currentTime.getZone() << " now is " printTime() const {
                                                                void
                                                                  if (hour < 10)
         << currentTime.getHour() << ":" << currentTime.getMinute() << "0";
                                                                     cout
         << endl;                                                 cout << hour << ":";
      return 0;                                                   if (minute < 10)
    }                                                                cout << "0";
                                                                         cout << minute << ":";
              currentTime                                                if (second < 10)
                                                                            cout << "0";
                                                                         cout << second << ":";
               hour          0
                                                                         cout << zone << endl;
               minute        0                                       }
                             first
               second        0                                       :
               zone         Kuala Lumpur                        };
+
    Using Classes - Example 7
    :
         void printTime() const {
           if (hour < 10)
              cout << "0";
           cout << hour << ":";
           if (minute < 10)
              cout << "0";
           cout << minute << ":";
           if (second < 10)
              cout << "0";
           cout << second << ":";
           cout << zone << endl;
         }
         :
    };

                   currentTime

                                                   00:
                   hour              0
                    minute           0
                                     first
                    second           0
                    zone            Kuala Lumpur
+
    Using Classes - Example 7
    :
         void printTime() const {
           if (hour < 10)
              cout << "0";
           cout << hour << ":";
           if (minute < 10)
              cout << "0";
           cout << minute << ":";
           if (second < 10)
              cout << "0";
           cout << second << ":";
           cout << zone << endl;
         }
         :
    };

                   currentTime

                                                   00:00:
                   hour              0
                    minute           0
                                     first
                    second           0
                    zone            Kuala Lumpur
+
    Using Classes - Example 7
    :
         void printTime() const {
           if (hour < 10)
              cout << "0";
           cout << hour << ":";
           if (minute < 10)
              cout << "0";
           cout << minute << ":";
           if (second < 10)
              cout << "0";
           cout << second << ":";
           cout << zone << endl;
         }
         :
    };

                   currentTime

                                                   00:00:00:
                   hour              0
                    minute           0
                                     first
                    second           0
                    zone            Kuala Lumpur
+
    Using Classes - Example 7
    :
         void printTime() const {
           if (hour < 10)
              cout << "0";
           cout << hour << ":";
           if (minute < 10)
              cout << "0";
           cout << minute << ":";
           if (second < 10)
              cout << "0";
           cout << second << ":";
           cout << zone << endl;
         }
         :
    };

                   currentTime

                                                   00:00:00:Kuala Lumpur
                   hour              0
                    minute           0
                                     first
                    second           0
                    zone            Kuala Lumpur
+
    Using Classes - Example 7

    int main (int argc, const char * argv[])
    {
      timeType currentTime;
      currentTime.printTime();                              :
      currentTime.setTime(5, 13, 0, "Singapore");              void setTime(int h, int m, int s, string z)
      currentTime.printTime();                                 {
      cout << "The time in " << currentTime.getZone() << " now is " = h;
                                                                 hour
         << currentTime.getHour() << ":" << currentTime.getMinute() = m;
                                                                 minute
         << endl;                                                second = s;
      return 0;                                                  zone = z;
    }                                                          }
               currentTime                                   :
                                                            };
                                                                    00:00:00:Kuala Lumpur
               hour           0
               minute         0
                              first
               second         0
               zone          Kuala Lumpur
+
    Using Classes - Example 7

    :
         void setTime(int h, int m, int s, string z)
         {                                             h           m          s
           hour = h;
           minute = m;                                 5           13         0

           second = s;
           zone = z;                                   z
         }
                                                       Singapore
     :
    };


                  currentTime

                                                              00:00:00:Kuala Lumpur
                  hour            0
                  minute          0
                                  first
                  second          0
                  zone           Kuala Lumpur
+
    Using Classes - Example 7

    :
         void setTime(int h, int m, int s, string z)
         {                                             h           m          s
           hour = h;
           minute = m;                                 5           13         0

           second = s;
           zone = z;                                   z
         }
                                                       Singapore
     :
    };


                  currentTime

                                                              00:00:00:Kuala Lumpur
                  hour            5
                  minute          0
                                  first
                  second          0
                  zone           Kuala Lumpur
+
    Using Classes - Example 7

    :
         void setTime(int h, int m, int s, string z)
         {                                             h           m          s
           hour = h;
           minute = m;                                 5           13         0

           second = s;
           zone = z;                                   z
         }
                                                       Singapore
     :
    };


                  currentTime

                                                              00:00:00:Kuala Lumpur
                  hour             5
                  minute          13
                                  first
                  second           0
                  zone           Kuala Lumpur
+
    Using Classes - Example 7

    :
         void setTime(int h, int m, int s, string z)
         {                                             h           m          s
           hour = h;
           minute = m;                                 5           13         0

           second = s;
           zone = z;                                   z
         }
                                                       Singapore
     :
    };


                  currentTime

                                                              00:00:00:Kuala Lumpur
                  hour             5
                  minute          13
                                  first
                  second           0
                  zone           Kuala Lumpur
+
    Using Classes - Example 7

    :
         void setTime(int h, int m, int s, string z)
         {                                             h           m          s
           hour = h;
           minute = m;                                 5           13         0

           second = s;
           zone = z;                                   z
         }
                                                       Singapore
     :
    };


                  currentTime

                                                              00:00:00:Kuala Lumpur
                  hour             5
                  minute          13
                                  first
                  second           0
                  zone             Singapore
+
    Using Classes - Example 7

    int main (int argc, const char * argv[])
    {
      timeType currentTime;
      currentTime.printTime();
      currentTime.setTime(5, 13, 0, "Singapore");
      currentTime.printTime();                                :
      cout << "The time in " << currentTime.getZone() << " now is " printTime() const {
                                                                void
                                                                  if (hour < 10)
         << currentTime.getHour() << ":" << currentTime.getMinute() << "0";
                                                                     cout
         << endl;                                                 cout << hour << ":";
      return 0;                                                   if (minute < 10)
    }                                                                cout << "0";
                                                                         cout << minute << ":";
              currentTime                                                if (second < 10)
                                                                            cout << "0";
                                                                         cout << second << ":";
               hour           5
                                                                         cout << zone << endl;
               minute        13                                      }
                             first
               second         0                                      :
               zone           Singapore                         };
+
    Using Classes - Example 7

    int main (int argc, const char * argv[])
    {
      timeType currentTime;
      currentTime.printTime();
      currentTime.setTime(5, 13, 0, "Singapore");
      currentTime.printTime();
      cout << "The time in " << currentTime.getZone() << " now is "
         << currentTime.getHour() << ":" << currentTime.getMinute()
         << endl;
      return 0;
    }
              currentTime

                                                               00:00:00:Kuala Lumpur
              hour           5                                 05:13:00:Singapore
              minute        13
                            first
              second         0
              zone           Singapore
+
    Using Classes - Example 7

    int main (int argc, const char * argv[])
    {
      timeType currentTime;
      currentTime.printTime();
      currentTime.setTime(5, 13, 0, "Singapore");
      currentTime.printTime();
      cout << "The time in " << currentTime.getZone() << " now is "
         << currentTime.getHour() << ":" << currentTime.getMinute()
         << endl;
      return 0;
    }
              currentTime

                                                               00:00:00:Kuala Lumpur
              hour           5                                 05:13:00:Singapore
              minute        13                                 The time in
                            first
              second         0
              zone           Singapore
+
    Using Classes - Example 7

    int main (int argc, const char * argv[])
    {                                                           :
      timeType currentTime;                                      string getZone() const {
      currentTime.printTime();                                     return zone;
                                                                }
      currentTime.setTime(5, 13, 0, "Singapore");
                                                                :
      currentTime.printTime();
      cout << "The time in " << currentTime.getZone() << " now is "
         << currentTime.getHour() << ":" << currentTime.getMinute()
         << endl;
      return 0;
    }
              currentTime

                                                                     00:00:00:Kuala Lumpur
               hour            5                                     05:13:00:Singapore
               minute         13                                     The time in Singapore
                              first
               second          0
               zone            Singapore
+
    Using Classes - Example 7

    int main (int argc, const char * argv[])
    {
      timeType currentTime;
      currentTime.printTime();
      currentTime.setTime(5, 13, 0, "Singapore");
      currentTime.printTime();
      cout << "The time in " << currentTime.getZone() << " now is "
         << currentTime.getHour() << ":" << currentTime.getMinute()
         << endl;
      return 0;
    }
              currentTime

                                                               00:00:00:Kuala Lumpur
              hour           5                                 05:13:00:Singapore
              minute        13                                 The time in Singapore now is
                            first
              second         0
              zone           Singapore
+
    Using Classes - Example 7

    int main (int argc, const char * argv[])
    {                                                           :
      timeType currentTime;                                      int getHour() const {
      currentTime.printTime();                                     return hour;
                                                                }
      currentTime.setTime(5, 13, 0, "Singapore");
                                                                :
      currentTime.printTime();
      cout << "The time in " << currentTime.getZone() << " now is "
         << currentTime.getHour() << ":" << currentTime.getMinute()
         << endl;
      return 0;
    }
              currentTime

                                                                    00:00:00:Kuala Lumpur
               hour            5                                    05:13:00:Singapore
               minute         13                                    The time in Singapore now is 5
                              first
               second          0
               zone            Singapore
+
    Using Classes - Example 7

    int main (int argc, const char * argv[])
    {
      timeType currentTime;
      currentTime.printTime();
      currentTime.setTime(5, 13, 0, "Singapore");
      currentTime.printTime();
      cout << "The time in " << currentTime.getZone() << " now is "
         << currentTime.getHour() << ":" << currentTime.getMinute()
         << endl;
      return 0;
    }
              currentTime

                                                               00:00:00:Kuala Lumpur
              hour           5                                 05:13:00:Singapore
              minute        13                                 The time in Singapore now is 5:
                            first
              second         0
              zone           Singapore
+
    Using Classes - Example 7

    int main (int argc, const char * argv[])
    {                                                           :
      timeType currentTime;                                      int getMinute() const {
      currentTime.printTime();                                     return minute;
                                                                }
      currentTime.setTime(5, 13, 0, "Singapore");
      currentTime.printTime();                                  :
      cout << "The time in " << currentTime.getZone() << " now is "
         << currentTime.getHour() << ":" << currentTime.getMinute()
         << endl;
      return 0;
    }
              currentTime

                                                                    00:00:00:Kuala Lumpur
               hour            5                                    05:13:00:Singapore
               minute         13                                    The time in Singapore now is 5:13
                              first
               second          0
               zone            Singapore
+
    Class Definition Variations
       A class in C++ is more commonly defined using the following
        syntax:
              class <classIdentifier> {
              public:
                         <member functions prototype>
              private:
                         <declaration of member variables>
              };
              :
              <member functions implementation>

       In this case, we need to use the class name and scope resolution
        operator (::) in member functions implementation
+
    Class Definition Variations
                                                               :
        Eg.                                                   void timeType::setTime(int h, int m, int s, string z) {
                                                                hour = h;
        class timeType {                                        minute = m;
        public:                                                 second = s;
          void setTime(int h, int m, int s, string z);          zone = z;
                                                           }
          int getHour();
          int getMinute();
                                                           int timeType::getHour() {
          int getSecond();                                   return hour;
          string getZone();                                }
          void printTime() const;
          timeType();                                     int timeType::getMinute() {
                                                             return minute;
        private:                                          }
           int hour;                                      :
           int minute;                                    timeType:: timeType(){
                                                         Member functions
           int second;                                          hour = 0;
                                                             declaration
           string zone;                                         minute = 0;
        };                                                      second = 0;
         :                                                      zone = "Kuala Lumpur";
                                                           }
+
    Class Definition Variations
                            Member functions
                                                             :
        Eg.                 implementation
                                                             void timeType::setTime(int h, int m, int s, string z) {
                                                              hour = h;
        class timeType {                                      minute = m;
        public:                                               second = s;
          void setTime(int h, int m, int s, string z);        zone = z;
                                                         }
          int getHour();
          int getMinute();
                                                         int timeType::getHour() {
          int getSecond();                                 return hour;
          string getZone();                              }
          void printTime() const;
          timeType();                                    int timeType::getMinute() {
                                                            return minute;
        private:                                         }
           int hour;                                     :
           int minute;                                   timeType:: timeType(){
           int second;                                         hour = 0;
           string zone;                                        minute = 0;
        };                                                     second = 0;
         :                                                     zone = "Kuala Lumpur";
                                                          }
                             Definition ends here
+
    Class Definition Variations
       Eg.            Name of the class


         :
        void timeType::setTime(int h, int m, int s, string z) {
         hour = h;
         minute = m;
         second = s;
         zone = z;
    }
                                  Scope resolution operator
    :
+
    Class Definition Variations
       The order of declarations can also be changed, like in the
        following syntax:
              class <classIdentifier> {
              private:
                         <declaration of member variables>


              public:
                         <member functions prototype>
              };
              :
              <member functions implementation>
+
    Class Definition Variations
                                                                   :
        Eg.                                                       void timeType::setTime(int h, int m, int s, string z) {
                                                                    hour = h;
        class timeType {                                            minute = m;
        private:                                                    second = s;
          int hour;                                                 zone = z;
                                                               }
          int minute;                                     private member variables
          int second;                                          aretimeType::getHour() {
                                                               int declared first
          string zone;                                             return hour;
                                                               }
        public:
           void setTime(int h, int m, int s, string z);        int timeType::getMinute() {
           int getHour();                                         return minute;
           int getMinute();                                    }
           int getSecond();                                    :
           string getZone();                                   timeType:: timeType(){
           void printTime() const;                                   hour = 0;
           timeType();                                               minute = 0;
        };                                                           second = 0;
         :                                                           zone = "Kuala Lumpur";
                                                                }
+
    Class Definition Variations
       The default specifier for a class member is private, therefore, if
        private members are placed first, the keyword private can be
        omitted.

       The following syntax is the same as the previous one:
              class <classIdentifier> {
                        <declaration of member variables>
              public:
                        <member functions prototype>
              };
              :
              <member functions implementation>
+
    Class Definition Variations
                                                                    :
        Eg.                                                     void timeType::setTime(int h, int m, int s, string z) {
                                                                  hour = h;
        class timeType {                                          minute = m;
          int hour;                                               second = s;
                                                    private keyword is omitted
          int minute;                                             zone = z;
                                                               }
          int second;
          string zone;
                                                                  int timeType::getHour() {
                                                                    return hour;
        public:                                                   }
           void setTime(int h, int m, int s, string z);
           int getHour();                                         int timeType::getMinute() {
           int getMinute();                                          return minute;
           int getSecond();                                       }
           string getZone();                                      :
           void printTime() const;                                timeType:: timeType(){
           timeType();                                                  hour = 0;
        };                                                              minute = 0;
         :                                                              second = 0;
                                                                        zone = "Kuala Lumpur";
                                                                   }
+
    Class Definition Variations

       Member variables and member functions can also be declared
        as protected

       For further explanation, please refer the class inheritance topic
+ Accessor, Mutator and Constant Functions

     Accessor functions – member functions that are used to (and only) access
      (do not modify) the value/s of member variables

     Eg.
             getHour(), getMinute(), getSecond(), getZone(), printTime()

     Mutator functions – member functions that are used to modify the value/s
      of member variables

     Eg.
             setTime()

     Constant functions – accessor functions that have the keyword const in its
      heading – as a safeguard not to modify member variables

     Eg.
             void printTime() const

     A class definition must have accessor and mutator functions to let other
      classes use them
+ Accessor, Mutator and Constant Functions


   class timeType {
   private:                                           Mutator function
   :

   public:
      void setTime(int h, int m, int s, string z);
      int getHour();                                 Accessor functions
      int getMinute();
      int getSecond();
      string getZone();
      void printTime() const;                        Constant function
      timeType();
   };
    :
+
    Reference Parameters
       Let’s say we want to define a member function that calculates the time
        difference in hours between two cities.

       We can do this by passing an object variable as a parameter of a
        function as follows:
                  int timeType::timeDifference(timeType otherTime){
                        if (hour > otherTime.hour)
                               return (hour - otherTime.hour);
                        else if (hour < otherTime.hour)
                               return (otherTime.hour - hour);
                        else
                               return 0;
                 }

       Therefore, we can call the function like this:

    cout << "The time difference between " << time1.getZone() << " and " << time2.getZone() << " is "
         << time1.timeDifference(time2) << " hours " << endl;
+
    Reference Parameters
       However, this is not a good programming practice, as we it
        requires memory space to copy the value of the actual
        parameters

       Assuming we have the following statements :

             timeType time1, time2;
             time2.setTime(5, 13, 0, "Paris");



       We will have memory space allocated as follows:
               time1                             time2

                hour          0                  hour      5
                minute        0                  minute   13
                              first                       first
                second        0                  second    0
                zone         Kuala Lumpur        zone         Paris
+
    Reference Parameters
       When we call the timeDifference() function, the values of
        member variables of time2 are copied into otherTime

       So, additional memory space is allocated for otherTime :

            otherTime

             hour        5
             minute     13
                        first
             second      0
             zone           Paris



       This can be improved by using reference parameters.
+
    Reference Parameters

       The following member function uses a reference parameter:

               int timeType::timeDifference (const timeType& otherTime){
                     if (hour > otherTime.hour)
                           return (hour - otherTime.hour);
                     else if (hour < otherTime.hour)
                           return (otherTime.hour - hour);
                     else
                           return 0;
              }


       Similarly, we can call the function:
    cout << "The time difference between " << time1.getZone() << " and " << time2.getZone()
    << " is " << time1.timeDifference(time2) << " hours " << endl;
+
    Reference Parameters
       otherTime refers to the memory allocated for time2:

                                       time2
           otherTime
                                       hour        5
                                       minute     13
                                                  first
                                       second      0
                                       zone           Paris


       No additional memory needed

       Can the values of the member variables be modified during the
        function call?

       No, because of the keyword const in the function header
+
    Constructors
     A constructor is a special member function that is
     called during the declaration of an object variable

     A constructor is used to initialise the member
     variables of a class

     Notice   that it has neither a return type nor void

     A class
            can have more that one constructors, and
     which constructor is called depends on the number
     and type of the actual parameters (if any)when it is
     called

     A constructorwithout any parameters is called the
     default constructor
+
    Constructors
       Let’s say we have the following constructors in a class:
             timeType::timeType(){
               hour = 0;
               minute = 0;
               second = 0;                                         Default constructor
               zone = "Kuala Lumpur";
             }

             timeType::timeType(int h, int m, int s, string z) {
               hour = h;
               minute = m;
               second = s;
               zone = z;
             }



       And we have the following statements in the main program:
             :
             timeType time1;
             timeType time2(5, 13, 0, "Paris");
             time1.printTime();
             time2.printTime();
             :
+
    Constructors
     :                                    timeType::timeType(){
     timeType time1;                        hour = 0;
     timeType time2(5, 13, 0, "Paris");     minute = 0;
     time1.printTime();                     second = 0;
     time2.printTime();                     zone = "Kuala Lumpur";
     :                                    }

                                          timeType::timeType(int h, int m, int s, string z) {
                                            hour = h;
                                            minute = m;
                                            second = s;
                                            zone = z;
                                          }

    time1

    hour           0
    minute         0
                   first
    second         0
    zone          Kuala Lumpur
+
    Constructors
     :                                    timeType::timeType(){
     timeType time1;                        hour = 0;
     timeType time2(5, 13, 0, "Paris");     minute = 0;
     time1.printTime();                     second = 0;
     time2.printTime();                     zone = "Kuala Lumpur";
     :                                    }

                                          timeType::timeType(int h, int m, int s, string z) {
                                            hour = h;
                                            minute = m;
                                            second = s;
                                            zone = z;
                                          }

    time1                                 time2

    hour           0                      hour               5
    minute         0                       minute           13
                   first                                    first
    second         0                       second            0
    zone          Kuala Lumpur             zone                 Paris
+
    Constructor With Default Values

       We can also simplify the previous definition using a constructor with
        default values:

         class timeType2 {
         public:                                                        Default values
           void setTime(int h, int m, int s, string z);
           int getHour();
           int getMinute();
           int getSecond();
           string getZone();
           void getTime(int& h, int& m, int& s, string z) const;
           void printTime() const;
           int timeDifference(const timeType2& otherTime);
           timeType2(int = 0, int = 0, int = 0, string = "Kuala Lumpur");

         private:
            int hour;
            int minute;
            int second;
            string zone;
         };
+
    Constructor With Default Values
       And use only one constructor implementation:
             timeType::timeType(int h, int m, int s, string z) {
                hour = h;
                minute = m;
                second = s;
                zone = z;
             }



       Assuming we have the following statements in the main
        program:
               :
               timeType time1;
               timeType time2(5, 13, 0, "Paris");
               :
+
    Constructor With Default Values


       We get the same result as before:

                 :
                 timeType time1;
                 timeType time2(5, 13, 0, "Paris");
                 :


        time1

        hour         0
        minute       0
                     first
        second       0
         zone       Kuala Lumpur
+
    Constructor With Default Values


       We get the same result as before:

                 :
                 timeType time1;
                 timeType time2(5, 13, 0, "Paris");
                 :


        time1                                 time2

        hour         0                        hour       5
        minute       0                         minute   13
                     first                              first
        second       0                         second    0
         zone       Kuala Lumpur               zone         Paris
+
    Destructors

     A class   can only have one destructor

     Justlike a constructor, it has no type nor return
      value, and marked with tilda (~)

     Called automatically when the program has finished
      using an object

     Ifno destructor is declared, the compiler will
      implicitly insert one
+
    Destructors - Example
    class timeType {                        Destructor declaration
    public:
       void setTime(int h, int m, int
    s, string z);
       int getHour();                                      Destructor
       int getMinute();                                  implementation
       int getSecond();
       string getZone();                :
       void printTime() const;
       timeType();                      timeType:: timeType(){
       ~timeType();                         hour = 0;
                                            minute = 0;
    private:                                second = 0;
       int hour;                            zone = "Kuala Lumpur";
       int minute;                       }
       int second;
       string zone;                     timeType::~timeType() {
    };                                  }
     :
+
    Array of Class Objects (Variables)

       Just like other types of data, we can declare an array of object
        variables

       Eg.

               timeType2 time[5];
               time[1].setTime(1, 0, 0, "Tokyo");
               time[2].setTime(5, 0, 0, "Paris");
               time[3].setTime(11, 0, 0, "New York");
               time[4].setTime(3, 0, 0, "Sydney");
               for (int i=0; i<5; i++)
                     time[i].printTime();
+
    Array of Class Objects (Variables)
     timeType2 time[5];
     time[1].setTime(1, 0, 0, "Tokyo");
     time[2].setTime(5, 0, 0, "Paris");
     time[3].setTime(11, 0, 0, "New York");
     time[4].setTime(3, 0, 0, "Sydney");
     for (int i=0; i<5; i++)
          time[i].printTime();


      time                               time[0]
                                            time[1]
                                         hour time[2] 0
                                            hour time[3] 0
                                         minute       0
                                            minute    first
                                         secondhour time[4] 0
                                                      0 0
                                             second hour first0 0
                                          zone minute     0
                                                      Kuala Lumpur
                                                       hour   first 0
                                             zone minute      0 0
                                                 second Kuala Lumpur
                                                             first
                                               zone minute   0 0
                                                  second Kuala Lumpur
                                                                 first
                                                                 0
                                                     second Kuala Lumpur
                                                  zone
                                                     zone       Kuala Lumpur
+
    Array of Class Objects (Variables)
     timeType2 time[5];
     time[1].setTime(1, 0, 0, "Tokyo");
     time[2].setTime(5, 0, 0, "Paris");
     time[3].setTime(11, 0, 0, "New York");
     time[4].setTime(3, 0, 0, "Sydney");
     for (int i=0; i<5; i++)
          time[i].printTime();


      time
                                      time[1]

                                      hour      1
                                       minute   0
                                                first
                                       second   0
                                       zone         Tokyo
+
    Array of Class Objects (Variables)
     timeType2 time[5];
     time[1].setTime(1, 0, 0, "Tokyo");
     time[2].setTime(5, 0, 0, "Paris");
     time[3].setTime(11, 0, 0, "New York");
     time[4].setTime(3, 0, 0, "Sydney");
     for (int i=0; i<5; i++)
          time[i].printTime();


      time

                                      time[2]

                                      hour      5
                                       minute   0
                                                first
                                       second   0
                                       zone         Paris
+
    Array of Class Objects (Variables)
     timeType2 time[5];
     time[1].setTime(1, 0, 0, "Tokyo");
     time[2].setTime(5, 0, 0, "Paris");
     time[3].setTime(11, 0, 0, "New York");
     time[4].setTime(3, 0, 0, "Sydney");
     for (int i=0; i<5; i++)
          time[i].printTime();


      time



                                      time[3]

                                      hour      11
                                       minute    0
                                                first
                                       second    0
                                       zone       New York
+
    Array of Class Objects (Variables)
     timeType2 time[5];
     time[1].setTime(1, 0, 0, "Tokyo");
     time[2].setTime(5, 0, 0, "Paris");
     time[3].setTime(11, 0, 0, "New York");
     time[4].setTime(3, 0, 0, "Sydney");
     for (int i=0; i<5; i++)
          time[i].printTime();


      time




                                      time[4]

                                      hour      3
                                       minute   0
                                                first
                                       second   0
                                       zone        Sydney
+
    Array of Class Objects (Variables)

     timeType2 time[5];
     time[1].setTime(1, 0, 0, "Tokyo");
     time[2].setTime(5, 0, 0, "Paris");
     time[3].setTime(11, 0, 0, "New York");
     time[4].setTime(3, 0, 0, "Sydney");
     for (int i=0; i<5; i++)
          time[i].printTime();




                               00:00:00:Kuala Lumpur
                               01:00:00:Tokyo
                               05:00:00:Paris
                               11:00:00:New York
                               03:00:00:Sydney
+
    References:

       D.S. Malik (2012). C++ Programming: Program Design
        Including Data Structures (5th ed), Thomson Course
        Technology.
           Chapter 12 – Classes and Data Abstraction

Lecture1 classes3

  • 1.
    + Lecture 1 – Introduction to C++ Classes (Part 3) TTTK1924 – Program Design and Problem Solving
  • 2.
    + Part 3 Classes
  • 3.
    + Using Classes 1. Define a class according to the following syntax: class <classIdentifier> { private: <declaration of member variables> public: <definition of member functions> }; 2. Declare a variable of the class – object variable <classIdentifier> <object-variable>; 3. Access or modify the data by calling member functions with the dot (.) operator <object-variable>.<member-function>; or <variable> = <object-variable>.<member-function>;
  • 4.
    + Class Definition  Includes  declaration of member variables(may contain different types of data)  definition of member functions – used to manipulate data items  Member variables are declared private to the class – can only be accessed or modified by its own class  Member functions are declared public ( can be used by other than its own class)
  • 5.
    + Class Definition - Example class timeType { : Name of class private: string getZone() { int hour; (class identifier) return zone; int minute; } int second; string zone; void printTime() const { if (hour < 10) public: cout << "0"; void setTime(int h, int m, int s, string z) { cout << hour << ":"; hour = h; if (minute < 10) minute = m; cout << "0"; second = s; cout << minute << ":"; zone = z; if (second < 10) } cout << "0"; cout << second << ":"; int getHour() { cout << zone << endl; return hour; } } timeType(){ int getMinute() { hour = 0; return minute; minute = 0; } second = 0; zone = "Kuala Lumpur"; int getSecond() { } return second; }; } :
  • 6.
    + Class Definition - Example class timeType { : private: string getZone() { int hour; return zone; int minute; } int second; string zone; void printTime() const { if (hour < 10) public: Declaration"0"; member cout << of variables void setTime(int h, int m, int s, string z) { cout << hour << ":"; hour = h; if (minute < 10) minute = m; cout << "0"; second = s; cout << minute << ":"; zone = z; if (second < 10) } cout << "0"; cout << second << ":"; int getHour() { cout << zone << endl; return hour; } } timeType(){ int getMinute() { hour = 0; return minute; minute = 0; } second = 0; zone = "Kuala Lumpur"; int getSecond() { } return second; }; } :
  • 7.
    + Class Definition - Example class timeType { : Definition of string getZone() { private: int hour; member functions return zone; int minute; } int second; string zone; void printTime() const { if (hour < 10) public: cout << "0"; void setTime(int h, int m, int s, string z) { cout << hour << ":"; hour = h; if (minute < 10) minute = m; cout << "0"; second = s; cout << minute << ":"; zone = z; if (second < 10) } cout << "0"; cout << second << ":"; int getHour() { cout << zone << endl; return hour; } } timeType(){ int getMinute() { hour = 0; return minute; minute = 0; } second = 0; zone = "Kuala Lumpur"; int getSecond() { } return second; }; } :
  • 8.
    + Using Classes  Defined classes can be used by  The main program, or  Other classes  Only public member functions can be called or used by the main program or other classes
  • 9.
    + Using Classes - Example int main (int argc, const char * argv[]) { timeType currentTime; currentTime.printTime(); currentTime.setTime(5, 13, 0, "Singapore"); currentTime.printTime(); cout << "The time in " << currentTime.getZone() << " now is " << currentTime.getHour() << ":" << currentTime.getMinute() << endl; return 0; }
  • 10.
    + Using Classes - Example int main (int argc, const char * argv[]) { timeType currentTime; Declaration of currentTime.printTime(); object variable currentTime.setTime(5, 13, 0, "Singapore"); currentTime.printTime(); cout << "The time in " << currentTime.getZone() << " now is " << currentTime.getHour() << ":" << currentTime.getMinute() << endl; return 0; }
  • 11.
    + Using Classes - Example int main (int argc, const char * argv[]) { timeType currentTime; currentTime.printTime(); currentTime.setTime(5, 13, 0, "Singapore"); currentTime.printTime(); cout << "The time in " << currentTime.getZone() << " now is " << currentTime.getHour() << ":" << currentTime.getMinute() << endl; return 0; } Accessing member variables using member functions
  • 12.
    + Using Classes - Example int main (int argc, const char * argv[]) { timeType currentTime; currentTime.printTime(); currentTime.setTime(5, 13, 0, "Singapore"); currentTime.printTime(); cout << "The time in " << currentTime.getZone() << " now is " << currentTime.getHour() << ":" << currentTime.getMinute() << endl; return 0; } Modifying member variables using member functions
  • 13.
    + Using Classes – Example 7 int main (int argc, const char * argv[]) { timeType currentTime; currentTime.printTime(); currentTime.setTime(5, 13, 0, "Singapore"); currentTime.printTime(); : timeType(){ cout << "The time in " << currentTime.getZone() << " now is " hour = 0; << currentTime.getHour() << ":" << currentTime.getMinute() = 0; minute << endl; second = 0; return 0; zone = "Kuala Lumpur"; } } currentTime }; hour 0 minute 0 first second 0 zone Kuala Lumpur
  • 14.
    + Using Classes – Example 7 int main (int argc, const char * argv[]) { timeType currentTime; currentTime.printTime(); currentTime.setTime(5, 13, 0, "Singapore"); currentTime.printTime(); : cout << "The time in " << currentTime.getZone() << " now is " printTime() const { void if (hour < 10) << currentTime.getHour() << ":" << currentTime.getMinute() << "0"; cout << endl; cout << hour << ":"; return 0; if (minute < 10) } cout << "0"; cout << minute << ":"; currentTime if (second < 10) cout << "0"; cout << second << ":"; hour 0 cout << zone << endl; minute 0 } first second 0 : zone Kuala Lumpur };
  • 15.
    + Using Classes - Example 7 : void printTime() const { if (hour < 10) cout << "0"; cout << hour << ":"; if (minute < 10) cout << "0"; cout << minute << ":"; if (second < 10) cout << "0"; cout << second << ":"; cout << zone << endl; } : }; currentTime 00: hour 0 minute 0 first second 0 zone Kuala Lumpur
  • 16.
    + Using Classes - Example 7 : void printTime() const { if (hour < 10) cout << "0"; cout << hour << ":"; if (minute < 10) cout << "0"; cout << minute << ":"; if (second < 10) cout << "0"; cout << second << ":"; cout << zone << endl; } : }; currentTime 00:00: hour 0 minute 0 first second 0 zone Kuala Lumpur
  • 17.
    + Using Classes - Example 7 : void printTime() const { if (hour < 10) cout << "0"; cout << hour << ":"; if (minute < 10) cout << "0"; cout << minute << ":"; if (second < 10) cout << "0"; cout << second << ":"; cout << zone << endl; } : }; currentTime 00:00:00: hour 0 minute 0 first second 0 zone Kuala Lumpur
  • 18.
    + Using Classes - Example 7 : void printTime() const { if (hour < 10) cout << "0"; cout << hour << ":"; if (minute < 10) cout << "0"; cout << minute << ":"; if (second < 10) cout << "0"; cout << second << ":"; cout << zone << endl; } : }; currentTime 00:00:00:Kuala Lumpur hour 0 minute 0 first second 0 zone Kuala Lumpur
  • 19.
    + Using Classes - Example 7 int main (int argc, const char * argv[]) { timeType currentTime; currentTime.printTime(); : currentTime.setTime(5, 13, 0, "Singapore"); void setTime(int h, int m, int s, string z) currentTime.printTime(); { cout << "The time in " << currentTime.getZone() << " now is " = h; hour << currentTime.getHour() << ":" << currentTime.getMinute() = m; minute << endl; second = s; return 0; zone = z; } } currentTime : }; 00:00:00:Kuala Lumpur hour 0 minute 0 first second 0 zone Kuala Lumpur
  • 20.
    + Using Classes - Example 7 : void setTime(int h, int m, int s, string z) { h m s hour = h; minute = m; 5 13 0 second = s; zone = z; z } Singapore : }; currentTime 00:00:00:Kuala Lumpur hour 0 minute 0 first second 0 zone Kuala Lumpur
  • 21.
    + Using Classes - Example 7 : void setTime(int h, int m, int s, string z) { h m s hour = h; minute = m; 5 13 0 second = s; zone = z; z } Singapore : }; currentTime 00:00:00:Kuala Lumpur hour 5 minute 0 first second 0 zone Kuala Lumpur
  • 22.
    + Using Classes - Example 7 : void setTime(int h, int m, int s, string z) { h m s hour = h; minute = m; 5 13 0 second = s; zone = z; z } Singapore : }; currentTime 00:00:00:Kuala Lumpur hour 5 minute 13 first second 0 zone Kuala Lumpur
  • 23.
    + Using Classes - Example 7 : void setTime(int h, int m, int s, string z) { h m s hour = h; minute = m; 5 13 0 second = s; zone = z; z } Singapore : }; currentTime 00:00:00:Kuala Lumpur hour 5 minute 13 first second 0 zone Kuala Lumpur
  • 24.
    + Using Classes - Example 7 : void setTime(int h, int m, int s, string z) { h m s hour = h; minute = m; 5 13 0 second = s; zone = z; z } Singapore : }; currentTime 00:00:00:Kuala Lumpur hour 5 minute 13 first second 0 zone Singapore
  • 25.
    + Using Classes - Example 7 int main (int argc, const char * argv[]) { timeType currentTime; currentTime.printTime(); currentTime.setTime(5, 13, 0, "Singapore"); currentTime.printTime(); : cout << "The time in " << currentTime.getZone() << " now is " printTime() const { void if (hour < 10) << currentTime.getHour() << ":" << currentTime.getMinute() << "0"; cout << endl; cout << hour << ":"; return 0; if (minute < 10) } cout << "0"; cout << minute << ":"; currentTime if (second < 10) cout << "0"; cout << second << ":"; hour 5 cout << zone << endl; minute 13 } first second 0 : zone Singapore };
  • 26.
    + Using Classes - Example 7 int main (int argc, const char * argv[]) { timeType currentTime; currentTime.printTime(); currentTime.setTime(5, 13, 0, "Singapore"); currentTime.printTime(); cout << "The time in " << currentTime.getZone() << " now is " << currentTime.getHour() << ":" << currentTime.getMinute() << endl; return 0; } currentTime 00:00:00:Kuala Lumpur hour 5 05:13:00:Singapore minute 13 first second 0 zone Singapore
  • 27.
    + Using Classes - Example 7 int main (int argc, const char * argv[]) { timeType currentTime; currentTime.printTime(); currentTime.setTime(5, 13, 0, "Singapore"); currentTime.printTime(); cout << "The time in " << currentTime.getZone() << " now is " << currentTime.getHour() << ":" << currentTime.getMinute() << endl; return 0; } currentTime 00:00:00:Kuala Lumpur hour 5 05:13:00:Singapore minute 13 The time in first second 0 zone Singapore
  • 28.
    + Using Classes - Example 7 int main (int argc, const char * argv[]) { : timeType currentTime; string getZone() const { currentTime.printTime(); return zone; } currentTime.setTime(5, 13, 0, "Singapore"); : currentTime.printTime(); cout << "The time in " << currentTime.getZone() << " now is " << currentTime.getHour() << ":" << currentTime.getMinute() << endl; return 0; } currentTime 00:00:00:Kuala Lumpur hour 5 05:13:00:Singapore minute 13 The time in Singapore first second 0 zone Singapore
  • 29.
    + Using Classes - Example 7 int main (int argc, const char * argv[]) { timeType currentTime; currentTime.printTime(); currentTime.setTime(5, 13, 0, "Singapore"); currentTime.printTime(); cout << "The time in " << currentTime.getZone() << " now is " << currentTime.getHour() << ":" << currentTime.getMinute() << endl; return 0; } currentTime 00:00:00:Kuala Lumpur hour 5 05:13:00:Singapore minute 13 The time in Singapore now is first second 0 zone Singapore
  • 30.
    + Using Classes - Example 7 int main (int argc, const char * argv[]) { : timeType currentTime; int getHour() const { currentTime.printTime(); return hour; } currentTime.setTime(5, 13, 0, "Singapore"); : currentTime.printTime(); cout << "The time in " << currentTime.getZone() << " now is " << currentTime.getHour() << ":" << currentTime.getMinute() << endl; return 0; } currentTime 00:00:00:Kuala Lumpur hour 5 05:13:00:Singapore minute 13 The time in Singapore now is 5 first second 0 zone Singapore
  • 31.
    + Using Classes - Example 7 int main (int argc, const char * argv[]) { timeType currentTime; currentTime.printTime(); currentTime.setTime(5, 13, 0, "Singapore"); currentTime.printTime(); cout << "The time in " << currentTime.getZone() << " now is " << currentTime.getHour() << ":" << currentTime.getMinute() << endl; return 0; } currentTime 00:00:00:Kuala Lumpur hour 5 05:13:00:Singapore minute 13 The time in Singapore now is 5: first second 0 zone Singapore
  • 32.
    + Using Classes - Example 7 int main (int argc, const char * argv[]) { : timeType currentTime; int getMinute() const { currentTime.printTime(); return minute; } currentTime.setTime(5, 13, 0, "Singapore"); currentTime.printTime(); : cout << "The time in " << currentTime.getZone() << " now is " << currentTime.getHour() << ":" << currentTime.getMinute() << endl; return 0; } currentTime 00:00:00:Kuala Lumpur hour 5 05:13:00:Singapore minute 13 The time in Singapore now is 5:13 first second 0 zone Singapore
  • 33.
    + Class Definition Variations  A class in C++ is more commonly defined using the following syntax: class <classIdentifier> { public: <member functions prototype> private: <declaration of member variables> }; : <member functions implementation>  In this case, we need to use the class name and scope resolution operator (::) in member functions implementation
  • 34.
    + Class Definition Variations :  Eg. void timeType::setTime(int h, int m, int s, string z) { hour = h; class timeType { minute = m; public: second = s; void setTime(int h, int m, int s, string z); zone = z; } int getHour(); int getMinute(); int timeType::getHour() { int getSecond(); return hour; string getZone(); } void printTime() const; timeType(); int timeType::getMinute() { return minute; private: } int hour; : int minute; timeType:: timeType(){ Member functions int second; hour = 0; declaration string zone; minute = 0; }; second = 0; : zone = "Kuala Lumpur"; }
  • 35.
    + Class Definition Variations Member functions :  Eg. implementation void timeType::setTime(int h, int m, int s, string z) { hour = h; class timeType { minute = m; public: second = s; void setTime(int h, int m, int s, string z); zone = z; } int getHour(); int getMinute(); int timeType::getHour() { int getSecond(); return hour; string getZone(); } void printTime() const; timeType(); int timeType::getMinute() { return minute; private: } int hour; : int minute; timeType:: timeType(){ int second; hour = 0; string zone; minute = 0; }; second = 0; : zone = "Kuala Lumpur"; } Definition ends here
  • 36.
    + Class Definition Variations  Eg. Name of the class : void timeType::setTime(int h, int m, int s, string z) { hour = h; minute = m; second = s; zone = z; } Scope resolution operator :
  • 37.
    + Class Definition Variations  The order of declarations can also be changed, like in the following syntax: class <classIdentifier> { private: <declaration of member variables> public: <member functions prototype> }; : <member functions implementation>
  • 38.
    + Class Definition Variations :  Eg. void timeType::setTime(int h, int m, int s, string z) { hour = h; class timeType { minute = m; private: second = s; int hour; zone = z; } int minute; private member variables int second; aretimeType::getHour() { int declared first string zone; return hour; } public: void setTime(int h, int m, int s, string z); int timeType::getMinute() { int getHour(); return minute; int getMinute(); } int getSecond(); : string getZone(); timeType:: timeType(){ void printTime() const; hour = 0; timeType(); minute = 0; }; second = 0; : zone = "Kuala Lumpur"; }
  • 39.
    + Class Definition Variations  The default specifier for a class member is private, therefore, if private members are placed first, the keyword private can be omitted.  The following syntax is the same as the previous one: class <classIdentifier> { <declaration of member variables> public: <member functions prototype> }; : <member functions implementation>
  • 40.
    + Class Definition Variations :  Eg. void timeType::setTime(int h, int m, int s, string z) { hour = h; class timeType { minute = m; int hour; second = s; private keyword is omitted int minute; zone = z; } int second; string zone; int timeType::getHour() { return hour; public: } void setTime(int h, int m, int s, string z); int getHour(); int timeType::getMinute() { int getMinute(); return minute; int getSecond(); } string getZone(); : void printTime() const; timeType:: timeType(){ timeType(); hour = 0; }; minute = 0; : second = 0; zone = "Kuala Lumpur"; }
  • 41.
    + Class Definition Variations  Member variables and member functions can also be declared as protected  For further explanation, please refer the class inheritance topic
  • 42.
    + Accessor, Mutatorand Constant Functions  Accessor functions – member functions that are used to (and only) access (do not modify) the value/s of member variables  Eg. getHour(), getMinute(), getSecond(), getZone(), printTime()  Mutator functions – member functions that are used to modify the value/s of member variables  Eg. setTime()  Constant functions – accessor functions that have the keyword const in its heading – as a safeguard not to modify member variables  Eg. void printTime() const  A class definition must have accessor and mutator functions to let other classes use them
  • 43.
    + Accessor, Mutatorand Constant Functions class timeType { private: Mutator function : public: void setTime(int h, int m, int s, string z); int getHour(); Accessor functions int getMinute(); int getSecond(); string getZone(); void printTime() const; Constant function timeType(); }; :
  • 44.
    + Reference Parameters  Let’s say we want to define a member function that calculates the time difference in hours between two cities.  We can do this by passing an object variable as a parameter of a function as follows: int timeType::timeDifference(timeType otherTime){ if (hour > otherTime.hour) return (hour - otherTime.hour); else if (hour < otherTime.hour) return (otherTime.hour - hour); else return 0; }  Therefore, we can call the function like this: cout << "The time difference between " << time1.getZone() << " and " << time2.getZone() << " is " << time1.timeDifference(time2) << " hours " << endl;
  • 45.
    + Reference Parameters  However, this is not a good programming practice, as we it requires memory space to copy the value of the actual parameters  Assuming we have the following statements : timeType time1, time2; time2.setTime(5, 13, 0, "Paris");  We will have memory space allocated as follows: time1 time2 hour 0 hour 5 minute 0 minute 13 first first second 0 second 0 zone Kuala Lumpur zone Paris
  • 46.
    + Reference Parameters  When we call the timeDifference() function, the values of member variables of time2 are copied into otherTime  So, additional memory space is allocated for otherTime : otherTime hour 5 minute 13 first second 0 zone Paris  This can be improved by using reference parameters.
  • 47.
    + Reference Parameters  The following member function uses a reference parameter: int timeType::timeDifference (const timeType& otherTime){ if (hour > otherTime.hour) return (hour - otherTime.hour); else if (hour < otherTime.hour) return (otherTime.hour - hour); else return 0; }  Similarly, we can call the function: cout << "The time difference between " << time1.getZone() << " and " << time2.getZone() << " is " << time1.timeDifference(time2) << " hours " << endl;
  • 48.
    + Reference Parameters  otherTime refers to the memory allocated for time2: time2 otherTime hour 5 minute 13 first second 0 zone Paris  No additional memory needed  Can the values of the member variables be modified during the function call?  No, because of the keyword const in the function header
  • 49.
    + Constructors  A constructor is a special member function that is called during the declaration of an object variable  A constructor is used to initialise the member variables of a class  Notice that it has neither a return type nor void  A class can have more that one constructors, and which constructor is called depends on the number and type of the actual parameters (if any)when it is called  A constructorwithout any parameters is called the default constructor
  • 50.
    + Constructors  Let’s say we have the following constructors in a class: timeType::timeType(){ hour = 0; minute = 0; second = 0; Default constructor zone = "Kuala Lumpur"; } timeType::timeType(int h, int m, int s, string z) { hour = h; minute = m; second = s; zone = z; }  And we have the following statements in the main program: : timeType time1; timeType time2(5, 13, 0, "Paris"); time1.printTime(); time2.printTime(); :
  • 51.
    + Constructors : timeType::timeType(){ timeType time1; hour = 0; timeType time2(5, 13, 0, "Paris"); minute = 0; time1.printTime(); second = 0; time2.printTime(); zone = "Kuala Lumpur"; : } timeType::timeType(int h, int m, int s, string z) { hour = h; minute = m; second = s; zone = z; } time1 hour 0 minute 0 first second 0 zone Kuala Lumpur
  • 52.
    + Constructors : timeType::timeType(){ timeType time1; hour = 0; timeType time2(5, 13, 0, "Paris"); minute = 0; time1.printTime(); second = 0; time2.printTime(); zone = "Kuala Lumpur"; : } timeType::timeType(int h, int m, int s, string z) { hour = h; minute = m; second = s; zone = z; } time1 time2 hour 0 hour 5 minute 0 minute 13 first first second 0 second 0 zone Kuala Lumpur zone Paris
  • 53.
    + Constructor With Default Values  We can also simplify the previous definition using a constructor with default values: class timeType2 { public: Default values void setTime(int h, int m, int s, string z); int getHour(); int getMinute(); int getSecond(); string getZone(); void getTime(int& h, int& m, int& s, string z) const; void printTime() const; int timeDifference(const timeType2& otherTime); timeType2(int = 0, int = 0, int = 0, string = "Kuala Lumpur"); private: int hour; int minute; int second; string zone; };
  • 54.
    + Constructor With Default Values  And use only one constructor implementation: timeType::timeType(int h, int m, int s, string z) { hour = h; minute = m; second = s; zone = z; }  Assuming we have the following statements in the main program: : timeType time1; timeType time2(5, 13, 0, "Paris"); :
  • 55.
    + Constructor With Default Values  We get the same result as before: : timeType time1; timeType time2(5, 13, 0, "Paris"); : time1 hour 0 minute 0 first second 0 zone Kuala Lumpur
  • 56.
    + Constructor With Default Values  We get the same result as before: : timeType time1; timeType time2(5, 13, 0, "Paris"); : time1 time2 hour 0 hour 5 minute 0 minute 13 first first second 0 second 0 zone Kuala Lumpur zone Paris
  • 57.
    + Destructors  A class can only have one destructor  Justlike a constructor, it has no type nor return value, and marked with tilda (~)  Called automatically when the program has finished using an object  Ifno destructor is declared, the compiler will implicitly insert one
  • 58.
    + Destructors - Example class timeType { Destructor declaration public: void setTime(int h, int m, int s, string z); int getHour(); Destructor int getMinute(); implementation int getSecond(); string getZone(); : void printTime() const; timeType(); timeType:: timeType(){ ~timeType(); hour = 0; minute = 0; private: second = 0; int hour; zone = "Kuala Lumpur"; int minute; } int second; string zone; timeType::~timeType() { }; } :
  • 59.
    + Array of Class Objects (Variables)  Just like other types of data, we can declare an array of object variables  Eg. timeType2 time[5]; time[1].setTime(1, 0, 0, "Tokyo"); time[2].setTime(5, 0, 0, "Paris"); time[3].setTime(11, 0, 0, "New York"); time[4].setTime(3, 0, 0, "Sydney"); for (int i=0; i<5; i++) time[i].printTime();
  • 60.
    + Array of Class Objects (Variables) timeType2 time[5]; time[1].setTime(1, 0, 0, "Tokyo"); time[2].setTime(5, 0, 0, "Paris"); time[3].setTime(11, 0, 0, "New York"); time[4].setTime(3, 0, 0, "Sydney"); for (int i=0; i<5; i++) time[i].printTime(); time time[0] time[1] hour time[2] 0 hour time[3] 0 minute 0 minute first secondhour time[4] 0 0 0 second hour first0 0 zone minute 0 Kuala Lumpur hour first 0 zone minute 0 0 second Kuala Lumpur first zone minute 0 0 second Kuala Lumpur first 0 second Kuala Lumpur zone zone Kuala Lumpur
  • 61.
    + Array of Class Objects (Variables) timeType2 time[5]; time[1].setTime(1, 0, 0, "Tokyo"); time[2].setTime(5, 0, 0, "Paris"); time[3].setTime(11, 0, 0, "New York"); time[4].setTime(3, 0, 0, "Sydney"); for (int i=0; i<5; i++) time[i].printTime(); time time[1] hour 1 minute 0 first second 0 zone Tokyo
  • 62.
    + Array of Class Objects (Variables) timeType2 time[5]; time[1].setTime(1, 0, 0, "Tokyo"); time[2].setTime(5, 0, 0, "Paris"); time[3].setTime(11, 0, 0, "New York"); time[4].setTime(3, 0, 0, "Sydney"); for (int i=0; i<5; i++) time[i].printTime(); time time[2] hour 5 minute 0 first second 0 zone Paris
  • 63.
    + Array of Class Objects (Variables) timeType2 time[5]; time[1].setTime(1, 0, 0, "Tokyo"); time[2].setTime(5, 0, 0, "Paris"); time[3].setTime(11, 0, 0, "New York"); time[4].setTime(3, 0, 0, "Sydney"); for (int i=0; i<5; i++) time[i].printTime(); time time[3] hour 11 minute 0 first second 0 zone New York
  • 64.
    + Array of Class Objects (Variables) timeType2 time[5]; time[1].setTime(1, 0, 0, "Tokyo"); time[2].setTime(5, 0, 0, "Paris"); time[3].setTime(11, 0, 0, "New York"); time[4].setTime(3, 0, 0, "Sydney"); for (int i=0; i<5; i++) time[i].printTime(); time time[4] hour 3 minute 0 first second 0 zone Sydney
  • 65.
    + Array of Class Objects (Variables) timeType2 time[5]; time[1].setTime(1, 0, 0, "Tokyo"); time[2].setTime(5, 0, 0, "Paris"); time[3].setTime(11, 0, 0, "New York"); time[4].setTime(3, 0, 0, "Sydney"); for (int i=0; i<5; i++) time[i].printTime(); 00:00:00:Kuala Lumpur 01:00:00:Tokyo 05:00:00:Paris 11:00:00:New York 03:00:00:Sydney
  • 66.
    + References:  D.S. Malik (2012). C++ Programming: Program Design Including Data Structures (5th ed), Thomson Course Technology.  Chapter 12 – Classes and Data Abstraction