SlideShare a Scribd company logo
2
Most read
4
Most read
9
Most read
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

PPT
C++ Introduction
PPTX
C++ Langauage Training in Ambala ! BATRA COMPUTER CENTRE
PPTX
C_plus_plus
PDF
C++ version 1
PDF
2 expressions (ppt-2) in C++
PPT
lecture02-cpp.ppt
PPTX
Cs1123 11 pointers
PPTX
C++ overview
C++ Introduction
C++ Langauage Training in Ambala ! BATRA COMPUTER CENTRE
C_plus_plus
C++ version 1
2 expressions (ppt-2) in C++
lecture02-cpp.ppt
Cs1123 11 pointers
C++ overview

Similar to C++ PROGRAMMING OBJECT ORIENTED PROGRAMMING (20)

PPTX
Presentation on C++ Programming Language
PPTX
Cs1123 3 c++ overview
PPT
c++ ppt.ppt
PDF
3.-Beginnign-with-C.pdf.Basic c++ Learn and Object oriented programming a to z
PPT
C++ - A powerful and system level language
PPT
lecture02-cpp.ppt
PPT
lecture02-cpp.ppt
PPT
lecture02-cpp.ppt
PPT
lecture02-cpp.ppt
PPTX
lecture NOTES ON OOPS C++ ON CLASS AND OBJECTS
PPSX
Esoft Metro Campus - Programming with C++
PPTX
C++ Presentation
PPTX
C++ language
PPT
lecture5-cpp.pptintroduccionaC++basicoye
PPT
Introduction to Inheritance in C plus plus
PDF
C++ PPT IN NUMERICAL METHOD FOR M.Sc PHYSICS
PPTX
Data Types & Input/Output Streams.pptx — Description
PDF
L6
PPTX
UNIT - 1- Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
PPTX
Programming Language
Presentation on C++ Programming Language
Cs1123 3 c++ overview
c++ ppt.ppt
3.-Beginnign-with-C.pdf.Basic c++ Learn and Object oriented programming a to z
C++ - A powerful and system level language
lecture02-cpp.ppt
lecture02-cpp.ppt
lecture02-cpp.ppt
lecture02-cpp.ppt
lecture NOTES ON OOPS C++ ON CLASS AND OBJECTS
Esoft Metro Campus - Programming with C++
C++ Presentation
C++ language
lecture5-cpp.pptintroduccionaC++basicoye
Introduction to Inheritance in C plus plus
C++ PPT IN NUMERICAL METHOD FOR M.Sc PHYSICS
Data Types & Input/Output Streams.pptx — Description
L6
UNIT - 1- Ood ddnwkjfnewcsdkjnjkfnskfn.pptx
Programming Language
Ad

More from ashutoshgupta1102 (6)

PDF
C++ Programming with examples for B.Tech
PPTX
DC DC Converter.pptx
PPTX
POWER ELECTRONICS
PDF
Firing Circuit
PPTX
Plcc and satelite communcation
PPTX
EMERGENCY RESPONSE PLAN
C++ Programming with examples for B.Tech
DC DC Converter.pptx
POWER ELECTRONICS
Firing Circuit
Plcc and satelite communcation
EMERGENCY RESPONSE PLAN
Ad

Recently uploaded (20)

PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
Safety Seminar civil to be ensured for safe working.
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPT
Project quality management in manufacturing
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
DOCX
573137875-Attendance-Management-System-original
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Sustainable Sites - Green Building Construction
PPT
Mechanical Engineering MATERIALS Selection
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
additive manufacturing of ss316l using mig welding
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Model Code of Practice - Construction Work - 21102022 .pdf
R24 SURVEYING LAB MANUAL for civil enggi
Safety Seminar civil to be ensured for safe working.
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Project quality management in manufacturing
Internet of Things (IOT) - A guide to understanding
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
573137875-Attendance-Management-System-original
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Sustainable Sites - Green Building Construction
Mechanical Engineering MATERIALS Selection
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
additive manufacturing of ss316l using mig welding

C++ PROGRAMMING OBJECT ORIENTED PROGRAMMING

  • 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; )