SlideShare une entreprise Scribd logo
1  sur  13
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

Contenu connexe

Tendances

Programming in c++
Programming in c++Programming in c++
Programming in c++Baljit Saini
 
Need of object oriented programming
Need of object oriented programmingNeed of object oriented programming
Need of object oriented programmingAmar Jukuntla
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programmingnirajmandaliya
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented ProgrammingIqra khalil
 
Concepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming LanguagesConcepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming Languagesppd1961
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsRanel Padon
 
C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]Rome468
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented TechnologiesTushar B Kute
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented TechnologiesUmesh Nikam
 
SKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSkillwise Group
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops conceptsNilesh Dalvi
 
Oop in c++ lecture 1
Oop in c++  lecture 1Oop in c++  lecture 1
Oop in c++ lecture 1zk75977
 
Course outline for c programming
Course outline for c  programming Course outline for c  programming
Course outline for c programming Rokonuzzaman Rony
 
Introduction to oop
Introduction to oop Introduction to oop
Introduction to oop Kumar
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesDurgesh Singh
 
Object Oriented Programming Lecture Notes
Object Oriented Programming Lecture NotesObject Oriented Programming Lecture Notes
Object Oriented Programming Lecture NotesFellowBuddy.com
 
Reviewing OOP Design patterns
Reviewing OOP Design patternsReviewing OOP Design patterns
Reviewing OOP Design patternsOlivier Bacs
 
Introduction to oop
Introduction to oopIntroduction to oop
Introduction to oopcolleges
 

Tendances (20)

oop Lecture 11
oop Lecture 11oop Lecture 11
oop Lecture 11
 
Programming in c++
Programming in c++Programming in c++
Programming in c++
 
Need of object oriented programming
Need of object oriented programmingNeed of object oriented programming
Need of object oriented programming
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
 
Object oriented concepts
Object oriented conceptsObject oriented concepts
Object oriented concepts
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Concepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming LanguagesConcepts In Object Oriented Programming Languages
Concepts In Object Oriented Programming Languages
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and Objects
 
C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented Technologies
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented Technologies
 
SKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPT
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
 
Oop in c++ lecture 1
Oop in c++  lecture 1Oop in c++  lecture 1
Oop in c++ lecture 1
 
Course outline for c programming
Course outline for c  programming Course outline for c  programming
Course outline for c programming
 
Introduction to oop
Introduction to oop Introduction to oop
Introduction to oop
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modules
 
Object Oriented Programming Lecture Notes
Object Oriented Programming Lecture NotesObject Oriented Programming Lecture Notes
Object Oriented Programming Lecture Notes
 
Reviewing OOP Design patterns
Reviewing OOP Design patternsReviewing OOP Design patterns
Reviewing OOP Design patterns
 
Introduction to oop
Introduction to oopIntroduction to oop
Introduction to oop
 

Similaire à Object Oriented Programming lecture 1

Similaire à Object Oriented Programming lecture 1 (20)

Spring
SpringSpring
Spring
 
Oop java
Oop javaOop java
Oop java
 
oop Lecture 16
oop Lecture 16oop Lecture 16
oop Lecture 16
 
ECMAScript 2015
ECMAScript 2015ECMAScript 2015
ECMAScript 2015
 
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
 
SRAVANByCPP
SRAVANByCPPSRAVANByCPP
SRAVANByCPP
 
iOS Application Development
iOS Application DevelopmentiOS Application Development
iOS Application Development
 
Nitin Mishra 0301EC201039 Internship PPT.pptx
Nitin Mishra 0301EC201039 Internship PPT.pptxNitin Mishra 0301EC201039 Internship PPT.pptx
Nitin Mishra 0301EC201039 Internship PPT.pptx
 
c++ UNIT II.pptx
c++ UNIT II.pptxc++ UNIT II.pptx
c++ UNIT II.pptx
 
Object Oriented Programming using C++ Part I
Object Oriented Programming using C++ Part IObject Oriented Programming using C++ Part I
Object Oriented Programming using C++ Part I
 
Objective c slide I
Objective c slide IObjective c slide I
Objective c slide I
 
Sling Models Using Sightly and JSP by Deepak Khetawat
Sling Models Using Sightly and JSP by Deepak KhetawatSling Models Using Sightly and JSP by Deepak Khetawat
Sling Models Using Sightly and JSP by Deepak Khetawat
 
55j7
55j755j7
55j7
 
C# for beginners
C# for beginnersC# for beginners
C# for beginners
 
Introduction to Ecmascript - ES6
Introduction to Ecmascript - ES6Introduction to Ecmascript - ES6
Introduction to Ecmascript - ES6
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
 
C_and_C++_notes.pdf
C_and_C++_notes.pdfC_and_C++_notes.pdf
C_and_C++_notes.pdf
 
Iphone course 1
Iphone course 1Iphone course 1
Iphone course 1
 
201005 accelerometer and core Location
201005 accelerometer and core Location201005 accelerometer and core Location
201005 accelerometer and core Location
 
Presentation c++
Presentation c++Presentation c++
Presentation c++
 

Dernier

Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfOverkill Security
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 

Dernier (20)

Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 

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 // 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. 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 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
  • 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 • 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. 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 • 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. 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

Notes de l'éditeur

  1. 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.