SlideShare a Scribd company logo
1 of 17
Introduction to C++
• 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?)
Outline
Intro to C++
Object-Oriented Programming
Changes in C++
comments
variable declaration location
initialization
pointer changes
tagged structure type
enum types
bool type
Object-Oriented Programming
• 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)
Object-Oriented Idea
• 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 of Objects in C++
• 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
Instances of Classes in C++
• 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
Comments in C++
• 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
Variable Declarations
• 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);
Counter Variables in a For Loop
• 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)
Initializing Global Variables
• 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() {
...
Initializing Array Elements
• 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 };
void*
• 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
NULL in C++
• 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)
...
Tags and struct
• 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;
enum in C++
• 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++
bool
• 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
bool operations
• 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; )

More Related Content

Similar to C++Chapter01(2).PPT

(2) cpp imperative programming
(2) cpp imperative programming(2) cpp imperative programming
(2) cpp imperative programmingNico Ludwig
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#ANURAG SINGH
 
Learning C++ - Introduction to c++ programming 1
Learning C++ - Introduction to c++ programming 1Learning C++ - Introduction to c++ programming 1
Learning C++ - Introduction to c++ programming 1Ali Aminian
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.pptDevliNeeraj
 
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit DubeyMCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubeykiranrajat
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 

Similar to C++Chapter01(2).PPT (20)

lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
Unsafe
UnsafeUnsafe
Unsafe
 
C++ Presentation
C++ PresentationC++ Presentation
C++ Presentation
 
(2) cpp imperative programming
(2) cpp imperative programming(2) cpp imperative programming
(2) cpp imperative programming
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
 
Learning C++ - Introduction to c++ programming 1
Learning C++ - Introduction to c++ programming 1Learning C++ - Introduction to c++ programming 1
Learning C++ - Introduction to c++ programming 1
 
c++ ppt.ppt
c++ ppt.pptc++ ppt.ppt
c++ ppt.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
lecture02-cpp.ppt
lecture02-cpp.pptlecture02-cpp.ppt
lecture02-cpp.ppt
 
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit DubeyMCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
MCA 101-Programming in C with Data Structure UNIT I by Prof. Rohit Dubey
 
C language
C languageC language
C language
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
#Code2 create c++ for beginners
#Code2 create  c++ for beginners #Code2 create  c++ for beginners
#Code2 create c++ for beginners
 
OOP
OOPOOP
OOP
 
Basic of c &c++
Basic of c &c++Basic of c &c++
Basic of c &c++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Lecture1
Lecture1Lecture1
Lecture1
 

Recently uploaded

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
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
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 

Recently uploaded (20)

mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
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
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

C++Chapter01(2).PPT

  • 1. Introduction to C++ • 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?)
  • 2. Outline Intro to C++ Object-Oriented Programming Changes in C++ comments variable declaration location initialization pointer changes tagged structure type enum types bool type
  • 3. Object-Oriented Programming • 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)
  • 4. Object-Oriented Idea • 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
  • 5. Classes of Objects in C++ • 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
  • 6. Instances of Classes in C++ • 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
  • 7. Comments in C++ • 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
  • 8. Variable Declarations • 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);
  • 9. Counter Variables in a For Loop • 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)
  • 10. Initializing Global Variables • 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() { ...
  • 11. Initializing Array Elements • 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 };
  • 12. void* • 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
  • 13. NULL in C++ • 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) ...
  • 14. Tags and struct • 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;
  • 15. enum in C++ • 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++
  • 16. bool • 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
  • 17. bool operations • 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; )