SlideShare a Scribd company logo
1 of 19
Submitted By:- Albert Sihota
Submitted to:- Prof Jagdeep Singh   Branch:- CSE N1
                                    Roll No.:- 115302
   Readings: 1.1-1.3, 1.9-1.13, 1.16-1.18, 1.21-1.22
   C++
       Bjarne Stroustrup (Bell Labs, 1979)
       started as extension to C (macros and variables)
       added new useful, features
       nowadays a language of its own
       C++ (the next thing after C, though wouldn’t ++C be
        more appropriate?)
Intro to C++
  Object-Oriented Programming
  Changes in C++
    comments
    variable declaration location
    initialization
    pointer changes
    tagged structure type
    enum types
    bool type
   First-class objects - atomic types in C
       int, float, char
       have:
         values
         sets of operations that can be applied to them
       how represented irrelevant to how they are
        manipulated
   Other objects - structures in C
       cannot be printed
       do not have operations associated with them (at
        least, not directly)
   Make all objects, whether C-defined or user-
    defined, first-class objects
   For C++ structures (called classes) allow:
       functions to be associated with the class
       only allow certain functions to access the internals of
        the class
       allow the user to re-define existing functions (for
        example, input and output) to work on class
   Classes
       similar to structures in C (in fact, you can can still
        use the struct definition)
       have fields corresponding to fields of a structure in C
        (similar to variables)
       have fields corresponding to functions in C
        (functions that can be applied to that structure)
       some fields are accessible by everyone, some not
        (data hiding)
       some fields shared by the entire class
   A class in C++ is like a type in C
   Variables created of a particular class are
    instances of that class
   Variables have values for fields of the class
   Class example: Student
       has name, id, gpa, etc. fields that store values
       has functions, changeGPA, addCredits, that can be
        applied to instances of that class
   Instance examples: John Doe, Jane Doe
       each with their own values for the fields of the class
   Can use C form of comments /* A Comment
    */
   Can also use // form:
       when // encountered, remainder of line ignored
       works only on that line
   Examples:
    void main() {
      int I; // Variable used in loops
      char C; // No comment comment
   In C++, variable declarations are not restricted
    to the beginnings of blocks (before any code)
       you may interleave declarations/statements as
        needed
       it is still good style to have declarations first
   Example
    void main() {
      int I = 5;
      printf(“Please enter J: “);
      int J; // Not declared at the start
      scanf(“%d”,&J);
   You can declare the variable(s) used in a for
    loop in the initialization section of the for loop
       good when counter used in for loop only exists in for
        loop (variable is throw-away)
   Example
    for (int I = 0; I < 5; I++)
      printf(“%dn”,I);
   Variable exists only during for loop (goes away
    when loop ends)
   Not restricted to using constant literal values in
    initializing global variables, can use any
    evaluable expression
   Example:
    int rows = 5;
    int cols = 6;
    int size = rows * cols;

    void main() {
    ...
   When giving a list of initial array values in C+
    +, you can use expressions that have to be
    evaluated
   Values calculated at run-time before
    initialization done
   Example:
    void main() {
      int n1, n2, n3;
      int *nptr[] = { &n1, &n2, &n3 };
   In C it is legal to cast other pointers to and from
    a void *
   In C++ this is an error, to cast you should use
    an explicit casting command
   Example:
    int N;
    int *P = &N;
    void *Q = P;          // illegal in C++
    void *R = (void *) P; // ok
   C++ does not use the value NULL, instead
    NULL is always 0 in C++, so we simply use 0
   Example:
    int *P = 0; // equivalent to
                // setting P to NULL
   Can check for a 0 pointer as if true/false:
    if (!P) // P is 0 (NULL)
      ...
    else // P is not 0 (non-NULL)
      ...
   When using struct command in C++ (and for
    other tagged types), can create type using tag
    format and not use tag in variable declaration:
    struct MyType {
       int A;
       float B;
    };
    MyType V;
   Enumerated types not directly represented as
    integers in C++
       certain operations that are legal in C do not work in
        C++
   Example:
    void main() {
      enum Color { red, blue, green };
      Color c = red;
      c = blue;
      c = 1; // Error in C++
      ++c; // Error in C++
   C has no explicit type for true/false values
   C++ introduces type bool (later versions of C+
    +)
       also adds two new bool literal constants true (1) and
        false (0)
   Other integral types (int, char, etc.) are
    implicitly converted to bool when appropriate
       non-zero values are converted to true
       zero values are converted to false
   Operators requiring bool value(s) and
    producing a bool value:
    && (And), || (Or), ! (Not)
   Relational operators (==, !=, <, >, <=, >=)
    produce bool values
   Some statements expect expressions that
    produce bool values:
     if (boolean_expression)
      while (boolean_expression)
      do … while (boolean_expression)
      for ( ; boolean_expression; )
C++ Introduction

More Related Content

What's hot

Csc1100 lecture06 ch06_pt2
Csc1100 lecture06 ch06_pt2Csc1100 lecture06 ch06_pt2
Csc1100 lecture06 ch06_pt2IIUM
 
Csc1100 lecture04 ch04
Csc1100 lecture04 ch04Csc1100 lecture04 ch04
Csc1100 lecture04 ch04IIUM
 
Reduce course notes class xii
Reduce course notes class xiiReduce course notes class xii
Reduce course notes class xiiSyed Zaid Irshad
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 FeaturesJan Rüegg
 
basics of C and c++ by eteaching
basics of C and c++ by eteachingbasics of C and c++ by eteaching
basics of C and c++ by eteachingeteaching
 
C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)sachindane
 
VTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in CVTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in CSyed Mustafa
 
Hands-on Introduction to the C Programming Language
Hands-on Introduction to the C Programming LanguageHands-on Introduction to the C Programming Language
Hands-on Introduction to the C Programming LanguageVincenzo De Florio
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c languageRai University
 
answer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcdanswer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcdSyed Mustafa
 

What's hot (20)

Csc1100 lecture06 ch06_pt2
Csc1100 lecture06 ch06_pt2Csc1100 lecture06 ch06_pt2
Csc1100 lecture06 ch06_pt2
 
Csc1100 lecture04 ch04
Csc1100 lecture04 ch04Csc1100 lecture04 ch04
Csc1100 lecture04 ch04
 
Reduce course notes class xii
Reduce course notes class xiiReduce course notes class xii
Reduce course notes class xii
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 Features
 
basics of C and c++ by eteaching
basics of C and c++ by eteachingbasics of C and c++ by eteaching
basics of C and c++ by eteaching
 
CP Handout#8
CP Handout#8CP Handout#8
CP Handout#8
 
Assignment10
Assignment10Assignment10
Assignment10
 
C Language (All Concept)
C Language (All Concept)C Language (All Concept)
C Language (All Concept)
 
VTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in CVTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in C
 
Basic of c &c++
Basic of c &c++Basic of c &c++
Basic of c &c++
 
Assignment8
Assignment8Assignment8
Assignment8
 
Ppt of c vs c#
Ppt of c vs c#Ppt of c vs c#
Ppt of c vs c#
 
Chaptr 1
Chaptr 1Chaptr 1
Chaptr 1
 
C language basics
C language basicsC language basics
C language basics
 
Hands-on Introduction to the C Programming Language
Hands-on Introduction to the C Programming LanguageHands-on Introduction to the C Programming Language
Hands-on Introduction to the C Programming Language
 
Oops
OopsOops
Oops
 
Basic concept of c++
Basic concept of c++Basic concept of c++
Basic concept of c++
 
Assignment6
Assignment6Assignment6
Assignment6
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
 
answer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcdanswer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcd
 

Viewers also liked

Mobile Networking
Mobile NetworkingMobile Networking
Mobile Networkingparmsidhu
 
open source technology
open source technologyopen source technology
open source technologyparmsidhu
 
Mobile Networking
Mobile NetworkingMobile Networking
Mobile Networkingparmsidhu
 
Biometric Sensors
Biometric SensorsBiometric Sensors
Biometric Sensorsparmsidhu
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerLuminary Labs
 

Viewers also liked (8)

Mobile Networking
Mobile NetworkingMobile Networking
Mobile Networking
 
open source technology
open source technologyopen source technology
open source technology
 
Parm
ParmParm
Parm
 
Parm
ParmParm
Parm
 
Mobile Networking
Mobile NetworkingMobile Networking
Mobile Networking
 
Biometric Sensors
Biometric SensorsBiometric Sensors
Biometric Sensors
 
(2012) Evolution of the Human Biometric Sensor Interaction model
(2012) Evolution of the Human Biometric Sensor Interaction model(2012) Evolution of the Human Biometric Sensor Interaction model
(2012) Evolution of the Human Biometric Sensor Interaction model
 
Hype vs. Reality: The AI Explainer
Hype vs. Reality: The AI ExplainerHype vs. Reality: The AI Explainer
Hype vs. Reality: The AI Explainer
 

Similar to C++ Introduction

Similar to C++ Introduction (20)

C++Chapter01(2).PPT
C++Chapter01(2).PPTC++Chapter01(2).PPT
C++Chapter01(2).PPT
 
C++Chapter01.PPT
C++Chapter01.PPTC++Chapter01.PPT
C++Chapter01.PPT
 
Introduction to C++ programming language
Introduction to C++ programming languageIntroduction to C++ programming language
Introduction to C++ programming language
 
2 expressions (ppt-2) in C++
2 expressions (ppt-2) in C++2 expressions (ppt-2) in C++
2 expressions (ppt-2) in C++
 
C –FAQ:
C –FAQ:C –FAQ:
C –FAQ:
 
Notes(1).pptx
Notes(1).pptxNotes(1).pptx
Notes(1).pptx
 
Object Oriented Programming with C++
Object Oriented Programming with C++Object Oriented Programming with C++
Object Oriented Programming with C++
 
C programming tutorial for Beginner
C programming tutorial for BeginnerC programming tutorial for Beginner
C programming tutorial for Beginner
 
C programming notes
C programming notesC programming notes
C programming notes
 
C programming notes.pdf
C programming notes.pdfC programming notes.pdf
C programming notes.pdf
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
Csharp4 basics
Csharp4 basicsCsharp4 basics
Csharp4 basics
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
 
Structured Languages
Structured LanguagesStructured Languages
Structured Languages
 
Swift, swiftly
Swift, swiftlySwift, swiftly
Swift, swiftly
 
Apa style-1 (1)
Apa style-1 (1)Apa style-1 (1)
Apa style-1 (1)
 
Interview Questions For C Language
Interview Questions For C Language Interview Questions For C Language
Interview Questions For C Language
 

Recently uploaded

ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 

Recently uploaded (20)

ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 

C++ Introduction

  • 1. Submitted By:- Albert Sihota Submitted to:- Prof Jagdeep Singh Branch:- CSE N1 Roll No.:- 115302
  • 2. Readings: 1.1-1.3, 1.9-1.13, 1.16-1.18, 1.21-1.22  C++  Bjarne Stroustrup (Bell Labs, 1979)  started as extension to C (macros and variables)  added new useful, features  nowadays a language of its own  C++ (the next thing after C, though wouldn’t ++C be more appropriate?)
  • 3. Intro to C++ Object-Oriented Programming Changes in C++ comments variable declaration location initialization pointer changes tagged structure type enum types bool type
  • 4. First-class objects - atomic types in C  int, float, char  have:  values  sets of operations that can be applied to them  how represented irrelevant to how they are manipulated  Other objects - structures in C  cannot be printed  do not have operations associated with them (at least, not directly)
  • 5. Make all objects, whether C-defined or user- defined, first-class objects  For C++ structures (called classes) allow:  functions to be associated with the class  only allow certain functions to access the internals of the class  allow the user to re-define existing functions (for example, input and output) to work on class
  • 6. Classes  similar to structures in C (in fact, you can can still use the struct definition)  have fields corresponding to fields of a structure in C (similar to variables)  have fields corresponding to functions in C (functions that can be applied to that structure)  some fields are accessible by everyone, some not (data hiding)  some fields shared by the entire class
  • 7. A class in C++ is like a type in C  Variables created of a particular class are instances of that class  Variables have values for fields of the class  Class example: Student  has name, id, gpa, etc. fields that store values  has functions, changeGPA, addCredits, that can be applied to instances of that class  Instance examples: John Doe, Jane Doe  each with their own values for the fields of the class
  • 8. Can use C form of comments /* A Comment */  Can also use // form:  when // encountered, remainder of line ignored  works only on that line  Examples: void main() { int I; // Variable used in loops char C; // No comment comment
  • 9. In C++, variable declarations are not restricted to the beginnings of blocks (before any code)  you may interleave declarations/statements as needed  it is still good style to have declarations first  Example void main() { int I = 5; printf(“Please enter J: “); int J; // Not declared at the start scanf(“%d”,&J);
  • 10. You can declare the variable(s) used in a for loop in the initialization section of the for loop  good when counter used in for loop only exists in for loop (variable is throw-away)  Example for (int I = 0; I < 5; I++) printf(“%dn”,I);  Variable exists only during for loop (goes away when loop ends)
  • 11. Not restricted to using constant literal values in initializing global variables, can use any evaluable expression  Example: int rows = 5; int cols = 6; int size = rows * cols; void main() { ...
  • 12. When giving a list of initial array values in C+ +, you can use expressions that have to be evaluated  Values calculated at run-time before initialization done  Example: void main() { int n1, n2, n3; int *nptr[] = { &n1, &n2, &n3 };
  • 13. In C it is legal to cast other pointers to and from a void *  In C++ this is an error, to cast you should use an explicit casting command  Example: int N; int *P = &N; void *Q = P; // illegal in C++ void *R = (void *) P; // ok
  • 14. C++ does not use the value NULL, instead NULL is always 0 in C++, so we simply use 0  Example: int *P = 0; // equivalent to // setting P to NULL  Can check for a 0 pointer as if true/false: if (!P) // P is 0 (NULL) ... else // P is not 0 (non-NULL) ...
  • 15. When using struct command in C++ (and for other tagged types), can create type using tag format and not use tag in variable declaration: struct MyType { int A; float B; }; MyType V;
  • 16. Enumerated types not directly represented as integers in C++  certain operations that are legal in C do not work in C++  Example: void main() { enum Color { red, blue, green }; Color c = red; c = blue; c = 1; // Error in C++ ++c; // Error in C++
  • 17. C has no explicit type for true/false values  C++ introduces type bool (later versions of C+ +)  also adds two new bool literal constants true (1) and false (0)  Other integral types (int, char, etc.) are implicitly converted to bool when appropriate  non-zero values are converted to true  zero values are converted to false
  • 18. Operators requiring bool value(s) and producing a bool value: && (And), || (Or), ! (Not)  Relational operators (==, !=, <, >, <=, >=) produce bool values  Some statements expect expressions that produce bool values: if (boolean_expression) while (boolean_expression) do … while (boolean_expression) for ( ; boolean_expression; )