CSC 103 - Object Oriented     Programming        Spring, 2011      Lecture 1, Background          14th Feb, 2011    Instru...
First Program// my first program in C++#include <iostream>using namespace std;int main (){   cout << “Hello World";   retu...
Background• // comments• Lines beginning with a hash sign (#) are  directives for the preprocessor.• #include <iostream> t...
Background• int main () The main function is the point  by where all C++ programs start their  execution, independently of...
Background• cout << "Hello World!";• cout is the name of the standard output  stream in C++, and the meaning of the  entir...
Background• cout is declared in the iostream standard  file within the std namespace, so thats  why we needed to include t...
Background• This operator >> applied to an input  stream (cin) is known as extraction  operator.• The << operator applied ...
C++ Structures• A collection of one or more variables,  typically of different types, grouped  together under a single nam...
C++ Structures• Structures   – Aggregate data types built using elements of other     types       struct Time{            ...
C++ Structures• struct  – Creates a new data type that is used to    declare variables  – Structure variables are declared...
C++ Structures• Member access operators:  – Dot operator (.) for structures and objects  – Arrow operator (->) for pointer...
Examples struct point {                struct {                           struct point {    int x;                        ...
#include <iostream>                                       Example#include <string>//using std::string;using namespace std;...
Upcoming SlideShare
Loading in …5
×

Object Oriented Programming lecture 1

1,951 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,951
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
30
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • 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.
  • Object Oriented Programming lecture 1

    1. 1. CSC 103 - Object Oriented Programming Spring, 2011 Lecture 1, Background 14th Feb, 2011 Instructor: M. Anwar-ul-Haq
    2. 2. 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
    3. 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. 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. 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. 6. Background• cout is declared in the iostream standard file within the std namespace, so thats 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. 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. 8. 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
    9. 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. 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. 11. 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
    12. 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 pointFor the first and last sets of code, point is a defined tag and can be usedlater (to define more points, or to declare a type of parameter, etc) but inthe middle code, there is no tag, so there is no way to reference moreexamples of this structure OOP, Spring 2011, Engr. Anwar, Foundation University (FUIEMS), Islamabad 12
    13. 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

    ×