KEMBAR78
Object Oriented Programming lecture 1 | PPT
CSC 103 - Object Oriented
     Programming

        Spring, 2011
      Lecture 1, Background


          14th Feb, 2011


    Instructor: M. Anwar-ul-Haq
First Program
// my first program in C++

#include <iostream>
using namespace std;

int main ()
{
   cout << “Hello World";
   return 0;
}
               OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad
                                                                                  2
Background

• // comments
• Lines beginning with a hash sign (#) are
  directives for the preprocessor.
• #include <iostream> tells the preprocessor
  to include the iostream standard file.
• using namespace std; Elements of the
  standard C++ library are declared within
  what is called a namespace, the
  namespace with the name std.
            OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad
                                                                               3
Background

• int main () The main function is the point
  by where all C++ programs start their
  execution, independently of its location
  within the source code.
• For that same reason, it is essential that
  all C++ programs have a main function.



             OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad
                                                                                4
Background

• cout << "Hello World!";
• cout is the name of the standard output
  stream in C++, and the meaning of the
  entire statement is to insert a sequence of
  characters (in this case the Hello
  World sequence of characters) into the
  standard output stream (cout, which
  usually corresponds to the screen).

             OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad
                                                                                5
Background

• cout is declared in the iostream standard
  file within the std namespace, so that's
  why we needed to include that specific file
  and to declare that we were going to use
  this specific namespace earlier in our
  code.



             OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad
                                                                                6
Background

• This operator >> applied to an input
  stream (cin) is known as extraction
  operator.
• The << operator applied to an output
  stream (cout) is known as insertion
  operator.



            OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad
                                                                               7
C++ Structures
• A collection of one or more variables,
  typically of different types, grouped
  together under a single name for
  convenient handling

• Known as struct in C and C++



             OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad
                                                                                8
C++ Structures
• Structures
   – Aggregate data types built using elements of other
     types
       struct Time{                  Structure tag

             int hour;
                                                  Structure members
             int minute;
             int second;
         };
   – Members of the same structure must have unique
     names
   – Two different structures may contain members of the
     same name
   – Each structure definition must end with a semicolon
                   OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad
                                                                                      9
C++ Structures
• struct
  – Creates a new data type that is used to
    declare variables
  – Structure variables are declared like variables
    of other types




              OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad
                                                                                10
C++ Structures
• Member access operators:
  – Dot operator (.) for structures and objects
  – Arrow operator (->) for pointers
  – Print member hour of timeObject:
       cout << timeObject.hour;




              OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad
                                                                                11
Examples
 struct point {                struct {                           struct point {
    int x;                        int x;                             int x;
    int y;                        int y;                             int y;
 };                            } p1, p2;                          } p1, p2;

 struct point p1, p2;          p1 and p2 both                     same as the other
                               have the defined                   two versions, but
 p1 and p2 are both            structure, containing              united into one set
 points, containing an         an x and a y, but                  of code, p1 and p2
 x and a y value               do not have a tag                  have the tag point


For the first and last sets of code, point is a defined tag and can be used
later (to define more points, or to declare a type of parameter, etc) but in
the middle code, there is no tag, so there is no way to reference more
examples of this structure
                         OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad
                                                                                           12
#include <iostream>


                                       Example
#include <string>
//using std::string;
using namespace std;

struct employee{

                int id_number;
                int age;
                string name;
                float salary;
};

int main()
{
  employee emp;

     emp.id_number=1;
     emp.age = 25;
     emp.name="adsdfsd";
     emp.salary =60000;

     cout<<"Employee id:"<<emp.id_number<<endl;
     cout<<"Employee Age:"<<emp.age<<endl;
     cout<<"Employee Name:"<<emp.name<<endl;
     cout<<"Employee Salary:"<<emp.salary<<endl;

    return 0;
}                                OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad
                                                                                                   13

Object Oriented Programming lecture 1

  • 1.
    CSC 103 -Object Oriented Programming Spring, 2011 Lecture 1, Background 14th Feb, 2011 Instructor: M. Anwar-ul-Haq
  • 2.
    First Program // myfirst program in C++ #include <iostream> using namespace std; int main () { cout << “Hello World"; return 0; } OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad 2
  • 3.
    Background • // comments •Lines beginning with a hash sign (#) are directives for the preprocessor. • #include <iostream> tells the preprocessor to include the iostream standard file. • using namespace std; Elements of the standard C++ library are declared within what is called a namespace, the namespace with the name std. OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad 3
  • 4.
    Background • int main() The main function is the point by where all C++ programs start their execution, independently of its location within the source code. • For that same reason, it is essential that all C++ programs have a main function. OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad 4
  • 5.
    Background • cout <<"Hello World!"; • cout is the name of the standard output stream in C++, and the meaning of the entire statement is to insert a sequence of characters (in this case the Hello World sequence of characters) into the standard output stream (cout, which usually corresponds to the screen). OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad 5
  • 6.
    Background • cout isdeclared in the iostream standard file within the std namespace, so that's why we needed to include that specific file and to declare that we were going to use this specific namespace earlier in our code. OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad 6
  • 7.
    Background • This operator>> applied to an input stream (cin) is known as extraction operator. • The << operator applied to an output stream (cout) is known as insertion operator. OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad 7
  • 8.
    C++ Structures • Acollection of one or more variables, typically of different types, grouped together under a single name for convenient handling • Known as struct in C and C++ OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad 8
  • 9.
    C++ Structures • Structures – Aggregate data types built using elements of other types struct Time{ Structure tag int hour; Structure members int minute; int second; }; – Members of the same structure must have unique names – Two different structures may contain members of the same name – Each structure definition must end with a semicolon OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad 9
  • 10.
    C++ Structures • struct – Creates a new data type that is used to declare variables – Structure variables are declared like variables of other types OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad 10
  • 11.
    C++ Structures • Memberaccess operators: – Dot operator (.) for structures and objects – Arrow operator (->) for pointers – Print member hour of timeObject: cout << timeObject.hour; OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad 11
  • 12.
    Examples struct point{ struct { struct point { int x; int x; int x; int y; int y; int y; }; } p1, p2; } p1, p2; struct point p1, p2; p1 and p2 both same as the other have the defined two versions, but p1 and p2 are both structure, containing united into one set points, containing an an x and a y, but of code, p1 and p2 x and a y value do not have a tag have the tag point For the first and last sets of code, point is a defined tag and can be used later (to define more points, or to declare a type of parameter, etc) but in the middle code, there is no tag, so there is no way to reference more examples of this structure OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad 12
  • 13.
    #include <iostream> Example #include <string> //using std::string; using namespace std; struct employee{ int id_number; int age; string name; float salary; }; int main() { employee emp; emp.id_number=1; emp.age = 25; emp.name="adsdfsd"; emp.salary =60000; cout<<"Employee id:"<<emp.id_number<<endl; cout<<"Employee Age:"<<emp.age<<endl; cout<<"Employee Name:"<<emp.name<<endl; cout<<"Employee Salary:"<<emp.salary<<endl; return 0; } OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad 13

Editor's Notes

  • #13 The first approach above is the typical way to use struct, although you can also use the third approach as a shortcut to define the struct and then declare variables. You should never use the middle approach.