SlideShare a Scribd company logo
1 of 39
+




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

       Functions Revisited
           Void Functions
           Void Function with Reference Parameters
           Value-returning Functions

       Structured Data Types
           Arrays Revisited
               Array and Function
           Structs Revisited
+
    Subtopics (cont’d)

       Classes
           Class Definition
           Using Classes
           Class Definition Variations
           Accessor, Mutator and Constant Functions
           Reference Parameters
           Constructors
           Destructors
           Array of Class Objects
+
    Part 1
    Functions Revisited
+ Void Functions – Example 1
 #include <iostream>
 using namespace std;
                                                  function prototype
 void printStars(int blanks, int starsInline);

 int main (int argc, const char * argv[])            :
 {                                                   void printStars(int blanks, int starsInLine) {
   int noOfLines;                                      int count;
   int counter;
   int noOfBlanks;                                     for (count=1; count <= blanks; count++)
                                                         cout << ' ';
   cout << "Enter the number of star lines to be       for (count=1; count <=starsInLine; count++)
 printed: ";                                             cout << "* ";
   cin >> noOfLines;                                   cout << endl;
   cout << endl;                                      }
   noOfBlanks = noOfLines;
   for (counter=1; counter <=noOfLines;
 counter++) {
      printStars(noOfBlanks, counter);          function call
      noOfBlanks--;
   }                                                                     function definition
   return 0;
 }
 :
+ Void Functions
 #include <iostream>
 using namespace std;
                                                   formal parameters (value)
 void printStars(int blanks, int starsInline);

 int main (int argc, const char * argv[])           :
 {                                                  void printStars(int blanks, int starsInLine) {
   int noOfLines;                                     int count;
   int counter;
   int noOfBlanks;                                      for (count=1; count <= blanks; count++)
                                                          cout << ' ';
   cout << "Enter the number of star lines to be        for (count=1; count <=starsInLine; count++)
 printed: ";                                              cout << "* ";
   cin >> noOfLines;                                    cout << endl;
   cout << endl;                                    }
   noOfBlanks = noOfLines;
   for (counter=1; counter <=noOfLines;
 counter++) {
      printStars(noOfBlanks, counter);                  actual parameters
      noOfBlanks--;
   }
   return 0;
 }
 :
+
    Void Functions– Example 1
    #include <iostream>
    using namespace std;

    void printStars(int blanks, int starsInline);

    int main (int argc, const char * argv[])
    {                                                               noOfLines   noOfBlanks
      int noOfLines;
      int counter;
      int noOfBlanks;

        cout << "Enter the number of star lines to be printed: ";
        cin >> noOfLines;                                            counter
        cout << endl;
        noOfBlanks = noOfLines;
        for (counter=1; counter <=noOfLines; counter++) {
          printStars(noOfBlanks, counter);
          noOfBlanks--;
        }
        return 0;
    }
    :
+
    Void Functions– Example 1
    #include <iostream>
    using namespace std;

    void printStars(int blanks, int starsInline);

    int main (int argc, const char * argv[])
    {                                                               noOfLines   noOfBlanks
      int noOfLines;
      int counter;                                                     10
      int noOfBlanks;

        cout << "Enter the number of star lines to be printed: ";
        cin >> noOfLines;                                            counter
        cout << endl;
        noOfBlanks = noOfLines;
        for (counter=1; counter <=noOfLines; counter++) {
          printStars(noOfBlanks, counter);
          noOfBlanks--;
        }
        return 0;
    }
    :
+
    Void Functions – Example 1
    #include <iostream>
    using namespace std;

    void printStars(int blanks, int starsInline);

    int main (int argc, const char * argv[])
    {                                                               noOfLines   noOfBlanks
      int noOfLines;
      int counter;                                                     10          10
      int noOfBlanks;

        cout << "Enter the number of star lines to be printed: ";
        cin >> noOfLines;                                            counter
        cout << endl;
        noOfBlanks = noOfLines;
        for (counter=1; counter <=noOfLines; counter++) {
          printStars(noOfBlanks, counter);
          noOfBlanks--;
        }
        return 0;
    }
    :
+
    Void Functions – Example 1
    #include <iostream>
    using namespace std;

    void printStars(int blanks, int starsInline);

    int main (int argc, const char * argv[])
    {                                                               noOfLines   noOfBlanks
      int noOfLines;
      int counter;                                                     10          10
      int noOfBlanks;

        cout << "Enter the number of star lines to be printed: ";
        cin >> noOfLines;                                            counter
        cout << endl;
        noOfBlanks = noOfLines;
                                                                       1
        for (counter=1; counter <=noOfLines; counter++) {
          printStars(noOfBlanks, counter);
          noOfBlanks--;
        }
        return 0;
    }
    :
+
    Void Functions – Example 1


     :
     void printStars(int blanks, int starsInLine) {
       int count;                                      blanks   starsInLine
                                                         10        1
         for (count=1; count <= blanks; count++)
           cout << ' ';
         for (count=1; count <=starsInLine; count++)
           cout << "* ";                               count
         cout << endl;
     }
+
    Void Functions – Example 1


     :
     void printStars(int blanks, int starsInLine) {
       int count;                                      blanks   starsInLine
                                                         10        1
         for (count=1; count <= blanks; count++)
           cout << ' ';
         for (count=1; count <=starsInLine; count++)
           cout << "* ";                                count
         cout << endl;
     }                                                   11



        Output:
+
    Void Functions – Example 1


     :
     void printStars(int blanks, int starsInLine) {
       int count;                                      blanks   starsInLine
                                                         10        1
         for (count=1; count <= blanks; count++)
           cout << ' ';
         for (count=1; count <=starsInLine; count++)
           cout << "* ";                                count
         cout << endl;
     }                                                   2



        Output:
                            *
+
    Void Functions – Example 1
    #include <iostream>
    using namespace std;

    void printStars(int blanks, int starsInline);

    int main (int argc, const char * argv[])
    {                                                               noOfLines   noOfBlanks
      int noOfLines;
      int counter;                                                     10          9
      int noOfBlanks;

        cout << "Enter the number of star lines to be printed: ";
        cin >> noOfLines;                                            counter
        cout << endl;
        noOfBlanks = noOfLines;
                                                                       1
        for (counter=1; counter <=noOfLines; counter++) {
          printStars(noOfBlanks, counter);
          noOfBlanks--;
        }
        return 0;
    }
    :
+
    Void Functions – Example 1
    #include <iostream>
    using namespace std;

    void printStars(int blanks, int starsInline);

    int main (int argc, const char * argv[])
    {                                                               noOfLines   noOfBlanks
      int noOfLines;
      int counter;                                                     10          9
      int noOfBlanks;

        cout << "Enter the number of star lines to be printed: ";
        cin >> noOfLines;                                            counter
        cout << endl;
        noOfBlanks = noOfLines;
                                                                       2
        for (counter=1; counter <=noOfLines; counter++) {
          printStars(noOfBlanks, counter);
          noOfBlanks--;
        }
        return 0;
    }
    :
+
    Void Functions – Example 1


     :
     void printStars(int blanks, int starsInLine) {
       int count;                                      blanks   starsInLine
                                                         9         2
         for (count=1; count <= blanks; count++)
           cout << ' ';
         for (count=1; count <=starsInLine; count++)
           cout << "* ";                               count
         cout << endl;
     }
+
    Void Functions – Example 1


     :
     void printStars(int blanks, int starsInLine) {
       int count;                                      blanks   starsInLine
                                                         9         2
         for (count=1; count <= blanks; count++)
           cout << ' ';
         for (count=1; count <=starsInLine; count++)
           cout << "* ";                                count
         cout << endl;
     }                                                   10



        Output:
                               *
+
    Void Functions – Example 1


     :
     void printStars(int blanks, int starsInLine) {
       int count;                                      blanks   starsInLine
                                                         10        1
         for (count=1; count <= blanks; count++)
           cout << ' ';
         for (count=1; count <=starsInLine; count++)
           cout << "* ";                                count
         cout << endl;
     }                                                   3



        Output:
                              *
                             **
+
    Void Functions – Example 1




            … and so on until….
+
    Void Functions – Example 1
     printStars(0, 10);


     :
     void printStars(int blanks, int starsInLine) {
       int count;                                      blanks   starsInLine
                                                         0         10
         for (count=1; count <= blanks; count++)
           cout << ' ';
         for (count=1; count <=starsInLine; count++)
           cout << "* ";                                count
         cout << endl;
     }                                                   11
+
    Void Functions – Example 1

       Final Output:
                            *
                            **
                           ***
                           ****
                          *****
                          ******
                         *******
                         ********
                        *********
                        **********
+ Void Functions – Reference Parameters
  (Example 2)
  #include <iostream>                                       value parameter
  using namespace std;

  void getScore(int& score);
  void printGrade(int cScore);                        :
                                                      void printGrade(int cScore) {
  int main (int argc, const char * argv[])              cout << "Your grade for the course is ";
  {                                                     if (cScore >= 85)
     int courseScore;                                       cout << "A." << endl;
     cout << "This program determines the grade for     else if (cScore >= 65)
  a given score." << endl;                                  cout << "B." << endl;
     getScore(courseScore);                             else if (cScore >= 55)
     cout << "Score is " << courseScore << endl;            cout << "C." << endl;
     printGrade(courseScore);                           else if (cScore >= 45)
     return 0;                                              cout << "D." << endl;
  }                                                     else
                                                            cout << "F." << endl;
  void getScore(int& score) {                         }
    cout << "Enter score: ";
    cin >> score;
  }
  :                              reference parameter
+ Void Functions – Reference Parameters
  (Example 2)
  #include <iostream>
  using namespace std;

  void getScore(int& score);
  void printGrade(int cScore);                        This program determines the grade for a given score

  int main (int argc, const char * argv[])
  {
     int courseScore;
     cout << "This program determines the grade for
  a given score." << endl;
     getScore(courseScore);
                                                      courseScore
     cout << "Score is " << courseScore << endl;
     printGrade(courseScore);
     return 0;
  }

  void getScore(int& score) {
    cout << "Enter score: ";
    cin >> score;
  }
  :
+ Void Functions – Reference Parameters
  (Example 2)
  #include <iostream>
  using namespace std;

  void getScore(int& score);
  void printGrade(int cScore);                        This program determines the grade for a given score

  int main (int argc, const char * argv[])
  {
     int courseScore;
     cout << "This program determines the grade for
  a given score." << endl;
     getScore(courseScore);
                                                      courseScore
     cout << "Score is " << courseScore << endl;
     printGrade(courseScore);
     return 0;
  }

  void getScore(int& score) {
    cout << "Enter score: ";
    cin >> score;
  }
  :                                                        score
+ Void Functions – Reference Parameters
  (Example 2)
  #include <iostream>
  using namespace std;

  void getScore(int& score);
  void printGrade(int cScore);                        This program determines the grade for a given score
                                                      Enter score:
  int main (int argc, const char * argv[])
  {
     int courseScore;
     cout << "This program determines the grade for
  a given score." << endl;
     getScore(courseScore);
                                                      courseScore
     cout << "Score is " << courseScore << endl;
     printGrade(courseScore);
     return 0;
  }

  void getScore(int& score) {
    cout << "Enter score: ";
    cin >> score;
  }
  :                                                        score
+ Void Functions – Reference Parameters
  (Example 2)
  #include <iostream>
  using namespace std;

  void getScore(int& score);
  void printGrade(int cScore);                        This program determines the grade for a given score
                                                      Enter score: 70
  int main (int argc, const char * argv[])
  {
     int courseScore;
     cout << "This program determines the grade for
  a given score." << endl;
     getScore(courseScore);
                                                      courseScore
     cout << "Score is " << courseScore << endl;
     printGrade(courseScore);                                 70
     return 0;
  }

  void getScore(int& score) {
    cout << "Enter score: ";
    cin >> score;
  }
  :                                                        score
+ Void Functions – Reference Parameters
  (Example 2)
  #include <iostream>
  using namespace std;

  void getScore(int& score);
  void printGrade(int cScore);                        This program determines the grade for a given score
                                                      Enter score: 70
  int main (int argc, const char * argv[])            Score is 70
  {
     int courseScore;
     cout << "This program determines the grade for
  a given score." << endl;
     getScore(courseScore);
                                                      courseScore
     cout << "Score is " << courseScore << endl;
     printGrade(courseScore);                                 70
     return 0;
  }

  void getScore(int& score) {
    cout << "Enter score: ";
    cin >> score;
  }
  :
+ Void Functions – Reference Parameters
  (Example 2)
  #include <iostream>
  using namespace std;

  void getScore(int& score);
  void printGrade(int cScore);                        This program determines the grade for a given score
                                                      Enter score: 70
  int main (int argc, const char * argv[])            Score is 70
  {
     int courseScore;
     cout << "This program determines the grade for
  a given score." << endl;
     getScore(courseScore);
                                                      courseScore
     cout << "Score is " << courseScore << endl;
     printGrade(courseScore);                                 70
     return 0;
  }

  void getScore(int& score) {
    cout << "Enter score: ";
    cin >> score;
  }
  :
+ Void Functions – Reference Parameters
  (Example 2)




  :                                            This program determines the grade for a given score
  void printGrade(int cScore) {                Enter score: 70
    cout << "Your grade for the course is ";   Score is 70
    if (cScore >= 85)
        cout << "A." << endl;
    else if (cScore >= 65)
        cout << "B." << endl;
    else if (cScore >= 55)                         cScore
        cout << "C." << endl;
    else if (cScore >= 45)                             70
        cout << "D." << endl;
    else
        cout << "F." << endl;
  }
+ Void Functions – Reference Parameters
  (Example 2)




  :                                            This program determines the grade for a given score
  void printGrade(int cScore) {                Enter score: 70
    cout << "Your grade for the course is ";   Score is 70
    if (cScore >= 85)                          Your grade for the course is
        cout << "A." << endl;
    else if (cScore >= 65)
        cout << "B." << endl;
    else if (cScore >= 55)                         cScore
        cout << "C." << endl;
    else if (cScore >= 45)                             70
        cout << "D." << endl;
    else
        cout << "F." << endl;
  }
+ Void Functions – Reference Parameters
  (Example 2)




  :                                            This program determines the grade for a given score
  void printGrade(int cScore) {                Enter score: 70
    cout << "Your grade for the course is ";   Score is 70
    if (cScore >= 85)                          Your grade for the course is B.
        cout << "A." << endl;
    else if (cScore >= 65)
        cout << "B." << endl;
    else if (cScore >= 55)                         cScore
        cout << "C." << endl;
    else if (cScore >= 45)                             70
        cout << "D." << endl;
    else
        cout << "F." << endl;
  }
+ Value-Returning Functions – Example 3
  #include <iostream>                                 function returns an int
  using namespace std;

  int getScore();
  void printGrade(int cScore);                          :
                                                        void printGrade(int cScore) {
  int main (int argc, const char * argv[])                 cout << "Your grade for the course is ";
  {                                                        if (cScore >= 85)
     int courseScore;                                          cout << "A." << endl;
     cout << "This program determines the grade for        else if (cScore >= 65)
  a given score." << endl;                                captures "B." << endl; returned
                                                               cout << the value
     courseScore = getScore();                             else if (cScore >= 55)
     cout << "Score is " << courseScore << endl;               cout << "C." << endl;
     printGrade(courseScore);                              else if (cScore >= 45)
     return 0;                                                 cout << "D." << endl;
  }                                                        else
                                                               cout << "F." << endl;
  int getScore() {                                      }
     int score;                                         statement to return value
     cout << "Enter score: ";
     cin >> score;
     return score;
  }
  :
+ Value-Returning Functions – Example 3
  #include <iostream>
  using namespace std;

  int getScore();
  void printGrade(int cScore);                        This program determines the grade for a given score

  int main (int argc, const char * argv[])
  {
     int courseScore;
     cout << "This program determines the grade for
  a given score." << endl;
     courseScore = getScore();
                                                      courseScore
     cout << "Score is " << courseScore << endl;
     printGrade(courseScore);
     return 0;
  }

  int getScore() {
     int score;
     cout << "Enter score: ";
     cin >> score;
     return score;
  }
  :
+ Value-Returning Functions – Example 3
  #include <iostream>
  using namespace std;

  int getScore();
  void printGrade(int cScore);                        This program determines the grade for a given score

  int main (int argc, const char * argv[])
  {
     int courseScore;
     cout << "This program determines the grade for
  a given score." << endl;
     courseScore = getScore();
                                                      courseScore
     cout << "Score is " << courseScore << endl;
     printGrade(courseScore);
     return 0;
  }

  int getScore() {
     int score;
     cout << "Enter score: ";
     cin >> score;
     return score;                                         score
  }
  :
+ Value-Returning Functions – Example 3
  #include <iostream>
  using namespace std;

  int getScore();
  void printGrade(int cScore);                        This program determines the grade for a given score
                                                      Enter score: 67
  int main (int argc, const char * argv[])
  {
     int courseScore;
     cout << "This program determines the grade for
  a given score." << endl;
     courseScore = getScore();
                                                      courseScore
     cout << "Score is " << courseScore << endl;
     printGrade(courseScore);
     return 0;
  }

  int getScore() {
     int score;
     cout << "Enter score: ";
                                                            67
     cin >> score;
     return score;                                         score
  }
  :
+ Value-Returning Functions – Example 3
  #include <iostream>
  using namespace std;

  int getScore();
  void printGrade(int cScore);                        This program determines the grade for a given score
                                                      Enter score: 67
  int main (int argc, const char * argv[])
  {
     int courseScore;
     cout << "This program determines the grade for
  a given score." << endl;
     courseScore = getScore();
                                                      courseScore
     cout << "Score is " << courseScore << endl;
     printGrade(courseScore);                                67
     return 0;
  }

  int getScore() {
     int score;
     cout << "Enter score: ";
                                                            67
     cin >> score;
     return score;                                         score
  }
  :
+ Value-Returning Functions – Example 3
  #include <iostream>
  using namespace std;

  int getScore();
  void printGrade(int cScore);                        This program determines the grade for a given score
                                                      Enter score: 67
  int main (int argc, const char * argv[])            Score is 67
  {
     int courseScore;
     cout << "This program determines the grade for
  a given score." << endl;
     courseScore = getScore();
                                                      courseScore
     cout << "Score is " << courseScore << endl;
     printGrade(courseScore);                                67
     return 0;
  }

  int getScore() {
     int score;
     cout << "Enter score: ";
     cin >> score;
     return score;
  }
  :
+ Value-Returning Functions – Example 3
  #include <iostream>
  using namespace std;

  int getScore();
  void printGrade(int cScore);                        This program determines the grade for a given score
                                                      Enter score: 67
  int main (int argc, const char * argv[])            Score is 67
  {                                                   Your grade for the course is B.
     int courseScore;
     cout << "This program determines the grade for
  a given score." << endl;
     courseScore = getScore();
                                                      courseScore
     cout << "Score is " << courseScore << endl;
     printGrade(courseScore);                               67
     return 0;
  }

  int getScore() {
     int score;
     cout << "Enter score: ";
     cin >> score;
     return score;
  }
  :
+
    References:

       D.S. Malik (2012). C++ Programming: Program Design
        Including Data Structures (5th ed), Thomson Course
        Technology.
           Chapter 6 – User-Defined Functions I
           Chapter 7 – User-Defined Functions II

More Related Content

What's hot

The Ring programming language version 1.5.1 book - Part 77 of 180
The Ring programming language version 1.5.1 book - Part 77 of 180The Ring programming language version 1.5.1 book - Part 77 of 180
The Ring programming language version 1.5.1 book - Part 77 of 180Mahmoud Samir Fayed
 
The Ring programming language version 1.5.4 book - Part 77 of 185
The Ring programming language version 1.5.4 book - Part 77 of 185The Ring programming language version 1.5.4 book - Part 77 of 185
The Ring programming language version 1.5.4 book - Part 77 of 185Mahmoud Samir Fayed
 
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java script
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java scriptCodiLime Tech Talk - Grzegorz Rozdzialik: What the java script
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java scriptCodiLime
 
The Ring programming language version 1.5.3 book - Part 87 of 184
The Ring programming language version 1.5.3 book - Part 87 of 184The Ring programming language version 1.5.3 book - Part 87 of 184
The Ring programming language version 1.5.3 book - Part 87 of 184Mahmoud Samir Fayed
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2zindadili
 
Extend R with Rcpp!!!
Extend R with Rcpp!!!Extend R with Rcpp!!!
Extend R with Rcpp!!!mickey24
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with PythonHan Lee
 
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays 2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays kinan keshkeh
 
The Ring programming language version 1.5.4 book - Part 81 of 185
The Ring programming language version 1.5.4 book - Part 81 of 185The Ring programming language version 1.5.4 book - Part 81 of 185
The Ring programming language version 1.5.4 book - Part 81 of 185Mahmoud Samir Fayed
 
Computer Vision using Ruby and libJIT - RubyConf 2009
Computer Vision using Ruby and libJIT - RubyConf 2009Computer Vision using Ruby and libJIT - RubyConf 2009
Computer Vision using Ruby and libJIT - RubyConf 2009Jan Wedekind
 
Javascript Uncommon Programming
Javascript Uncommon ProgrammingJavascript Uncommon Programming
Javascript Uncommon Programmingjeffz
 
Дмитрий Верескун «Синтаксический сахар C#»
Дмитрий Верескун «Синтаксический сахар C#»Дмитрий Верескун «Синтаксический сахар C#»
Дмитрий Верескун «Синтаксический сахар C#»SpbDotNet Community
 
Why learn new programming languages
Why learn new programming languagesWhy learn new programming languages
Why learn new programming languagesJonas Follesø
 
Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)hhliu
 

What's hot (20)

The Ring programming language version 1.5.1 book - Part 77 of 180
The Ring programming language version 1.5.1 book - Part 77 of 180The Ring programming language version 1.5.1 book - Part 77 of 180
The Ring programming language version 1.5.1 book - Part 77 of 180
 
Oop1
Oop1Oop1
Oop1
 
The Ring programming language version 1.5.4 book - Part 77 of 185
The Ring programming language version 1.5.4 book - Part 77 of 185The Ring programming language version 1.5.4 book - Part 77 of 185
The Ring programming language version 1.5.4 book - Part 77 of 185
 
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java script
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java scriptCodiLime Tech Talk - Grzegorz Rozdzialik: What the java script
CodiLime Tech Talk - Grzegorz Rozdzialik: What the java script
 
The Ring programming language version 1.5.3 book - Part 87 of 184
The Ring programming language version 1.5.3 book - Part 87 of 184The Ring programming language version 1.5.3 book - Part 87 of 184
The Ring programming language version 1.5.3 book - Part 87 of 184
 
Operator overloading2
Operator overloading2Operator overloading2
Operator overloading2
 
Pointers
PointersPointers
Pointers
 
P1
P1P1
P1
 
Extend R with Rcpp!!!
Extend R with Rcpp!!!Extend R with Rcpp!!!
Extend R with Rcpp!!!
 
Imugi: Compiler made with Python
Imugi: Compiler made with PythonImugi: Compiler made with Python
Imugi: Compiler made with Python
 
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays 2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
 
C++ examples &revisions
C++ examples &revisionsC++ examples &revisions
C++ examples &revisions
 
The Ring programming language version 1.5.4 book - Part 81 of 185
The Ring programming language version 1.5.4 book - Part 81 of 185The Ring programming language version 1.5.4 book - Part 81 of 185
The Ring programming language version 1.5.4 book - Part 81 of 185
 
Computer Vision using Ruby and libJIT - RubyConf 2009
Computer Vision using Ruby and libJIT - RubyConf 2009Computer Vision using Ruby and libJIT - RubyConf 2009
Computer Vision using Ruby and libJIT - RubyConf 2009
 
Javascript Uncommon Programming
Javascript Uncommon ProgrammingJavascript Uncommon Programming
Javascript Uncommon Programming
 
C++ practical
C++ practicalC++ practical
C++ practical
 
Дмитрий Верескун «Синтаксический сахар C#»
Дмитрий Верескун «Синтаксический сахар C#»Дмитрий Верескун «Синтаксический сахар C#»
Дмитрий Верескун «Синтаксический сахар C#»
 
C sharp 8
C sharp 8C sharp 8
C sharp 8
 
Why learn new programming languages
Why learn new programming languagesWhy learn new programming languages
Why learn new programming languages
 
Chapter 7 functions (c)
Chapter 7 functions (c)Chapter 7 functions (c)
Chapter 7 functions (c)
 

Viewers also liked

Viewers also liked (14)

Aae oop xp_05
Aae oop xp_05Aae oop xp_05
Aae oop xp_05
 
C++ programming (Array)
C++ programming (Array)C++ programming (Array)
C++ programming (Array)
 
C++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLESC++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLES
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Array in c++
Array in c++Array in c++
Array in c++
 
Arrays Class presentation
Arrays Class presentationArrays Class presentation
Arrays Class presentation
 
Lecture17 arrays.ppt
Lecture17 arrays.pptLecture17 arrays.ppt
Lecture17 arrays.ppt
 
Pointer in c++ part1
Pointer in c++ part1Pointer in c++ part1
Pointer in c++ part1
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
 
Array in C
Array in CArray in C
Array in C
 
Arrays
ArraysArrays
Arrays
 
Array in c language
Array in c languageArray in c language
Array in c language
 

Similar to Lecture1 classes1

CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - ReferenceMohammed Sikander
 
Nesting of for loops using C++
Nesting of for loops using C++Nesting of for loops using C++
Nesting of for loops using C++prashant_sainii
 
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
 
2 BytesC++ course_2014_c4_ arrays
2 BytesC++ course_2014_c4_ arrays2 BytesC++ course_2014_c4_ arrays
2 BytesC++ course_2014_c4_ arrayskinan keshkeh
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101premrings
 
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
 
An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL乐群 陈
 
Developer Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duoDeveloper Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duoThe Software House
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++Neeru Mittal
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_papervandna123
 

Similar to Lecture1 classes1 (20)

CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
 
Nesting of for loops using C++
Nesting of for loops using C++Nesting of for loops using C++
Nesting of for loops using C++
 
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
 
Managing console
Managing consoleManaging console
Managing console
 
2 BytesC++ course_2014_c4_ arrays
2 BytesC++ course_2014_c4_ arrays2 BytesC++ course_2014_c4_ arrays
2 BytesC++ course_2014_c4_ arrays
 
C program
C programC program
C program
 
Lecture1 classes2
Lecture1 classes2Lecture1 classes2
Lecture1 classes2
 
54602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee0108310154602399 c-examples-51-to-108-programe-ee01083101
54602399 c-examples-51-to-108-programe-ee01083101
 
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
 
Pointers+(2)
Pointers+(2)Pointers+(2)
Pointers+(2)
 
Arrays
ArraysArrays
Arrays
 
An Introduction to Part of C++ STL
An Introduction to Part of C++ STLAn Introduction to Part of C++ STL
An Introduction to Part of C++ STL
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
Developer Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duoDeveloper Experience i TypeScript. Najbardziej ikoniczne duo
Developer Experience i TypeScript. Najbardziej ikoniczne duo
 
C programming
C programmingC programming
C programming
 
Lecture05
Lecture05Lecture05
Lecture05
 
Library functions in c++
Library functions in c++Library functions in c++
Library functions in c++
 
oodp elab.pdf
oodp elab.pdfoodp elab.pdf
oodp elab.pdf
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_paper
 
Cpp programs
Cpp programsCpp programs
Cpp programs
 

Lecture1 classes1

  • 1. + Lecture 1 – Introduction to C++ Classes (Part 1) TTTK1924 – Program Design and Problem Solving
  • 2. + Subtopics  Functions Revisited  Void Functions  Void Function with Reference Parameters  Value-returning Functions  Structured Data Types  Arrays Revisited  Array and Function  Structs Revisited
  • 3. + Subtopics (cont’d)  Classes  Class Definition  Using Classes  Class Definition Variations  Accessor, Mutator and Constant Functions  Reference Parameters  Constructors  Destructors  Array of Class Objects
  • 4. + Part 1 Functions Revisited
  • 5. + Void Functions – Example 1 #include <iostream> using namespace std; function prototype void printStars(int blanks, int starsInline); int main (int argc, const char * argv[]) : { void printStars(int blanks, int starsInLine) { int noOfLines; int count; int counter; int noOfBlanks; for (count=1; count <= blanks; count++) cout << ' '; cout << "Enter the number of star lines to be for (count=1; count <=starsInLine; count++) printed: "; cout << "* "; cin >> noOfLines; cout << endl; cout << endl; } noOfBlanks = noOfLines; for (counter=1; counter <=noOfLines; counter++) { printStars(noOfBlanks, counter); function call noOfBlanks--; } function definition return 0; } :
  • 6. + Void Functions #include <iostream> using namespace std; formal parameters (value) void printStars(int blanks, int starsInline); int main (int argc, const char * argv[]) : { void printStars(int blanks, int starsInLine) { int noOfLines; int count; int counter; int noOfBlanks; for (count=1; count <= blanks; count++) cout << ' '; cout << "Enter the number of star lines to be for (count=1; count <=starsInLine; count++) printed: "; cout << "* "; cin >> noOfLines; cout << endl; cout << endl; } noOfBlanks = noOfLines; for (counter=1; counter <=noOfLines; counter++) { printStars(noOfBlanks, counter); actual parameters noOfBlanks--; } return 0; } :
  • 7. + Void Functions– Example 1 #include <iostream> using namespace std; void printStars(int blanks, int starsInline); int main (int argc, const char * argv[]) { noOfLines noOfBlanks int noOfLines; int counter; int noOfBlanks; cout << "Enter the number of star lines to be printed: "; cin >> noOfLines; counter cout << endl; noOfBlanks = noOfLines; for (counter=1; counter <=noOfLines; counter++) { printStars(noOfBlanks, counter); noOfBlanks--; } return 0; } :
  • 8. + Void Functions– Example 1 #include <iostream> using namespace std; void printStars(int blanks, int starsInline); int main (int argc, const char * argv[]) { noOfLines noOfBlanks int noOfLines; int counter; 10 int noOfBlanks; cout << "Enter the number of star lines to be printed: "; cin >> noOfLines; counter cout << endl; noOfBlanks = noOfLines; for (counter=1; counter <=noOfLines; counter++) { printStars(noOfBlanks, counter); noOfBlanks--; } return 0; } :
  • 9. + Void Functions – Example 1 #include <iostream> using namespace std; void printStars(int blanks, int starsInline); int main (int argc, const char * argv[]) { noOfLines noOfBlanks int noOfLines; int counter; 10 10 int noOfBlanks; cout << "Enter the number of star lines to be printed: "; cin >> noOfLines; counter cout << endl; noOfBlanks = noOfLines; for (counter=1; counter <=noOfLines; counter++) { printStars(noOfBlanks, counter); noOfBlanks--; } return 0; } :
  • 10. + Void Functions – Example 1 #include <iostream> using namespace std; void printStars(int blanks, int starsInline); int main (int argc, const char * argv[]) { noOfLines noOfBlanks int noOfLines; int counter; 10 10 int noOfBlanks; cout << "Enter the number of star lines to be printed: "; cin >> noOfLines; counter cout << endl; noOfBlanks = noOfLines; 1 for (counter=1; counter <=noOfLines; counter++) { printStars(noOfBlanks, counter); noOfBlanks--; } return 0; } :
  • 11. + Void Functions – Example 1 : void printStars(int blanks, int starsInLine) { int count; blanks starsInLine 10 1 for (count=1; count <= blanks; count++) cout << ' '; for (count=1; count <=starsInLine; count++) cout << "* "; count cout << endl; }
  • 12. + Void Functions – Example 1 : void printStars(int blanks, int starsInLine) { int count; blanks starsInLine 10 1 for (count=1; count <= blanks; count++) cout << ' '; for (count=1; count <=starsInLine; count++) cout << "* "; count cout << endl; } 11  Output:
  • 13. + Void Functions – Example 1 : void printStars(int blanks, int starsInLine) { int count; blanks starsInLine 10 1 for (count=1; count <= blanks; count++) cout << ' '; for (count=1; count <=starsInLine; count++) cout << "* "; count cout << endl; } 2  Output: *
  • 14. + Void Functions – Example 1 #include <iostream> using namespace std; void printStars(int blanks, int starsInline); int main (int argc, const char * argv[]) { noOfLines noOfBlanks int noOfLines; int counter; 10 9 int noOfBlanks; cout << "Enter the number of star lines to be printed: "; cin >> noOfLines; counter cout << endl; noOfBlanks = noOfLines; 1 for (counter=1; counter <=noOfLines; counter++) { printStars(noOfBlanks, counter); noOfBlanks--; } return 0; } :
  • 15. + Void Functions – Example 1 #include <iostream> using namespace std; void printStars(int blanks, int starsInline); int main (int argc, const char * argv[]) { noOfLines noOfBlanks int noOfLines; int counter; 10 9 int noOfBlanks; cout << "Enter the number of star lines to be printed: "; cin >> noOfLines; counter cout << endl; noOfBlanks = noOfLines; 2 for (counter=1; counter <=noOfLines; counter++) { printStars(noOfBlanks, counter); noOfBlanks--; } return 0; } :
  • 16. + Void Functions – Example 1 : void printStars(int blanks, int starsInLine) { int count; blanks starsInLine 9 2 for (count=1; count <= blanks; count++) cout << ' '; for (count=1; count <=starsInLine; count++) cout << "* "; count cout << endl; }
  • 17. + Void Functions – Example 1 : void printStars(int blanks, int starsInLine) { int count; blanks starsInLine 9 2 for (count=1; count <= blanks; count++) cout << ' '; for (count=1; count <=starsInLine; count++) cout << "* "; count cout << endl; } 10  Output: *
  • 18. + Void Functions – Example 1 : void printStars(int blanks, int starsInLine) { int count; blanks starsInLine 10 1 for (count=1; count <= blanks; count++) cout << ' '; for (count=1; count <=starsInLine; count++) cout << "* "; count cout << endl; } 3  Output: * **
  • 19. + Void Functions – Example 1 … and so on until….
  • 20. + Void Functions – Example 1 printStars(0, 10); : void printStars(int blanks, int starsInLine) { int count; blanks starsInLine 0 10 for (count=1; count <= blanks; count++) cout << ' '; for (count=1; count <=starsInLine; count++) cout << "* "; count cout << endl; } 11
  • 21. + Void Functions – Example 1  Final Output: * ** *** **** ***** ****** ******* ******** ********* **********
  • 22. + Void Functions – Reference Parameters (Example 2) #include <iostream> value parameter using namespace std; void getScore(int& score); void printGrade(int cScore); : void printGrade(int cScore) { int main (int argc, const char * argv[]) cout << "Your grade for the course is "; { if (cScore >= 85) int courseScore; cout << "A." << endl; cout << "This program determines the grade for else if (cScore >= 65) a given score." << endl; cout << "B." << endl; getScore(courseScore); else if (cScore >= 55) cout << "Score is " << courseScore << endl; cout << "C." << endl; printGrade(courseScore); else if (cScore >= 45) return 0; cout << "D." << endl; } else cout << "F." << endl; void getScore(int& score) { } cout << "Enter score: "; cin >> score; } : reference parameter
  • 23. + Void Functions – Reference Parameters (Example 2) #include <iostream> using namespace std; void getScore(int& score); void printGrade(int cScore); This program determines the grade for a given score int main (int argc, const char * argv[]) { int courseScore; cout << "This program determines the grade for a given score." << endl; getScore(courseScore); courseScore cout << "Score is " << courseScore << endl; printGrade(courseScore); return 0; } void getScore(int& score) { cout << "Enter score: "; cin >> score; } :
  • 24. + Void Functions – Reference Parameters (Example 2) #include <iostream> using namespace std; void getScore(int& score); void printGrade(int cScore); This program determines the grade for a given score int main (int argc, const char * argv[]) { int courseScore; cout << "This program determines the grade for a given score." << endl; getScore(courseScore); courseScore cout << "Score is " << courseScore << endl; printGrade(courseScore); return 0; } void getScore(int& score) { cout << "Enter score: "; cin >> score; } : score
  • 25. + Void Functions – Reference Parameters (Example 2) #include <iostream> using namespace std; void getScore(int& score); void printGrade(int cScore); This program determines the grade for a given score Enter score: int main (int argc, const char * argv[]) { int courseScore; cout << "This program determines the grade for a given score." << endl; getScore(courseScore); courseScore cout << "Score is " << courseScore << endl; printGrade(courseScore); return 0; } void getScore(int& score) { cout << "Enter score: "; cin >> score; } : score
  • 26. + Void Functions – Reference Parameters (Example 2) #include <iostream> using namespace std; void getScore(int& score); void printGrade(int cScore); This program determines the grade for a given score Enter score: 70 int main (int argc, const char * argv[]) { int courseScore; cout << "This program determines the grade for a given score." << endl; getScore(courseScore); courseScore cout << "Score is " << courseScore << endl; printGrade(courseScore); 70 return 0; } void getScore(int& score) { cout << "Enter score: "; cin >> score; } : score
  • 27. + Void Functions – Reference Parameters (Example 2) #include <iostream> using namespace std; void getScore(int& score); void printGrade(int cScore); This program determines the grade for a given score Enter score: 70 int main (int argc, const char * argv[]) Score is 70 { int courseScore; cout << "This program determines the grade for a given score." << endl; getScore(courseScore); courseScore cout << "Score is " << courseScore << endl; printGrade(courseScore); 70 return 0; } void getScore(int& score) { cout << "Enter score: "; cin >> score; } :
  • 28. + Void Functions – Reference Parameters (Example 2) #include <iostream> using namespace std; void getScore(int& score); void printGrade(int cScore); This program determines the grade for a given score Enter score: 70 int main (int argc, const char * argv[]) Score is 70 { int courseScore; cout << "This program determines the grade for a given score." << endl; getScore(courseScore); courseScore cout << "Score is " << courseScore << endl; printGrade(courseScore); 70 return 0; } void getScore(int& score) { cout << "Enter score: "; cin >> score; } :
  • 29. + Void Functions – Reference Parameters (Example 2) : This program determines the grade for a given score void printGrade(int cScore) { Enter score: 70 cout << "Your grade for the course is "; Score is 70 if (cScore >= 85) cout << "A." << endl; else if (cScore >= 65) cout << "B." << endl; else if (cScore >= 55) cScore cout << "C." << endl; else if (cScore >= 45) 70 cout << "D." << endl; else cout << "F." << endl; }
  • 30. + Void Functions – Reference Parameters (Example 2) : This program determines the grade for a given score void printGrade(int cScore) { Enter score: 70 cout << "Your grade for the course is "; Score is 70 if (cScore >= 85) Your grade for the course is cout << "A." << endl; else if (cScore >= 65) cout << "B." << endl; else if (cScore >= 55) cScore cout << "C." << endl; else if (cScore >= 45) 70 cout << "D." << endl; else cout << "F." << endl; }
  • 31. + Void Functions – Reference Parameters (Example 2) : This program determines the grade for a given score void printGrade(int cScore) { Enter score: 70 cout << "Your grade for the course is "; Score is 70 if (cScore >= 85) Your grade for the course is B. cout << "A." << endl; else if (cScore >= 65) cout << "B." << endl; else if (cScore >= 55) cScore cout << "C." << endl; else if (cScore >= 45) 70 cout << "D." << endl; else cout << "F." << endl; }
  • 32. + Value-Returning Functions – Example 3 #include <iostream> function returns an int using namespace std; int getScore(); void printGrade(int cScore); : void printGrade(int cScore) { int main (int argc, const char * argv[]) cout << "Your grade for the course is "; { if (cScore >= 85) int courseScore; cout << "A." << endl; cout << "This program determines the grade for else if (cScore >= 65) a given score." << endl; captures "B." << endl; returned cout << the value courseScore = getScore(); else if (cScore >= 55) cout << "Score is " << courseScore << endl; cout << "C." << endl; printGrade(courseScore); else if (cScore >= 45) return 0; cout << "D." << endl; } else cout << "F." << endl; int getScore() { } int score; statement to return value cout << "Enter score: "; cin >> score; return score; } :
  • 33. + Value-Returning Functions – Example 3 #include <iostream> using namespace std; int getScore(); void printGrade(int cScore); This program determines the grade for a given score int main (int argc, const char * argv[]) { int courseScore; cout << "This program determines the grade for a given score." << endl; courseScore = getScore(); courseScore cout << "Score is " << courseScore << endl; printGrade(courseScore); return 0; } int getScore() { int score; cout << "Enter score: "; cin >> score; return score; } :
  • 34. + Value-Returning Functions – Example 3 #include <iostream> using namespace std; int getScore(); void printGrade(int cScore); This program determines the grade for a given score int main (int argc, const char * argv[]) { int courseScore; cout << "This program determines the grade for a given score." << endl; courseScore = getScore(); courseScore cout << "Score is " << courseScore << endl; printGrade(courseScore); return 0; } int getScore() { int score; cout << "Enter score: "; cin >> score; return score; score } :
  • 35. + Value-Returning Functions – Example 3 #include <iostream> using namespace std; int getScore(); void printGrade(int cScore); This program determines the grade for a given score Enter score: 67 int main (int argc, const char * argv[]) { int courseScore; cout << "This program determines the grade for a given score." << endl; courseScore = getScore(); courseScore cout << "Score is " << courseScore << endl; printGrade(courseScore); return 0; } int getScore() { int score; cout << "Enter score: "; 67 cin >> score; return score; score } :
  • 36. + Value-Returning Functions – Example 3 #include <iostream> using namespace std; int getScore(); void printGrade(int cScore); This program determines the grade for a given score Enter score: 67 int main (int argc, const char * argv[]) { int courseScore; cout << "This program determines the grade for a given score." << endl; courseScore = getScore(); courseScore cout << "Score is " << courseScore << endl; printGrade(courseScore); 67 return 0; } int getScore() { int score; cout << "Enter score: "; 67 cin >> score; return score; score } :
  • 37. + Value-Returning Functions – Example 3 #include <iostream> using namespace std; int getScore(); void printGrade(int cScore); This program determines the grade for a given score Enter score: 67 int main (int argc, const char * argv[]) Score is 67 { int courseScore; cout << "This program determines the grade for a given score." << endl; courseScore = getScore(); courseScore cout << "Score is " << courseScore << endl; printGrade(courseScore); 67 return 0; } int getScore() { int score; cout << "Enter score: "; cin >> score; return score; } :
  • 38. + Value-Returning Functions – Example 3 #include <iostream> using namespace std; int getScore(); void printGrade(int cScore); This program determines the grade for a given score Enter score: 67 int main (int argc, const char * argv[]) Score is 67 { Your grade for the course is B. int courseScore; cout << "This program determines the grade for a given score." << endl; courseScore = getScore(); courseScore cout << "Score is " << courseScore << endl; printGrade(courseScore); 67 return 0; } int getScore() { int score; cout << "Enter score: "; cin >> score; return score; } :
  • 39. + References:  D.S. Malik (2012). C++ Programming: Program Design Including Data Structures (5th ed), Thomson Course Technology.  Chapter 6 – User-Defined Functions I  Chapter 7 – User-Defined Functions II