CPP loop
By Santosh Tawde
Introduction to C++
C++
By Santosh Tawde
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
Object-Oriented Idea
Classes of Objects in C++
Instances of Classes in C++
Comments in C++
void main() {
int I; // Variable used in loops
char C; // No comment comment
Variable Declarations
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
for (int I = 0; I < 5; I++)
printf(“%d\n”,I);
Initializing Global Variables
int rows = 5;
int cols = 6;
int size = rows * cols;
void main() {
...
Initializing Array Elements
void main() {
int n1, n2, n3;
int *nptr[] = { &n1, &n2, &n3 };
void*
int N;
int *P = &N;
void *Q = P; // illegal in C++
void *R = (void *) P; // ok
NULL in C++
int *P = 0; // equivalent to
// setting P to NULL
if (!P) // P is 0 (NULL)
...
else // P is not 0 (non-NULL)
...
Tags and struct
struct MyType {
int A;
float B;
};
MyType V;
enum in C++
void main() {
enum Color { red, blue, green };
Color c = red;
c = blue;
c = 1; // Error in C++
++c; // Error in C++
bool
bool operations
&& (And), || (Or), ! (Not)
if (boolean_expression)
while (boolean_expression)
do … while (boolean_expression)
for ( ; boolean_expression; )