KEY POINTS:
Introduction to C++
-----------------------------------------------------------------------------------------------------------------------------
C++ is the successor of C language & developed by Bjarne Stroustrup at Bell Laboratories,
New Jersey in 1979.
Tokens- smallest individual unit. Following are the tokens
-> Keyword-Reserve word that can‘t be used as identifier
-> Identifiers-Names given to any variable, function, class, union etc.
-> Literals-Value of specific data type
-> Variable- memory block of certain size where value can be stored and changed.
-> Constant- memory block where value can be stored once but can‘t changed later on
Operator – performs some action on data
--------------------------------------------------------------------------------------------------------------------------------
1. Arithmetic(+,-,*,/,%)
2. Relational/comparison (<,>,<=,>=,==,!=).
3. Logical(AND(&&),OR(||),NOT(!).
4. Conditional (? :)
5. Increment/Decrement Operators( ++/--)
Precedence of operators:
-------------------------------------------------------------------------------------------------------------------------------
1. ++(post increment),--(post decrement)
2. ++(pre increment),--(pre decrement),sizeof !(not),-(unary),+unary plus)
3. *(multiply), / (divide), %(modulus)
4. +(add),-(subtract)
5. <(less than),<=(less than or equal),>(greater than), >=(greater than or equal to)
6. ==(equal),!=(not equal)
7. && (logical AND)
8. ||(logical OR)
9. ?:(conditional expression)
10. =(simple assignment) and other assignment operators(arithmetic assignment
operator)
11. , Comma operator
---------------------------------------------------------------------------------------------------------------------------------
Data type- A specifier to create memory block of some specific size and type. For example –int,float,double,char etc.
cout – It is an object of ostream_withassign class defined in iostream.h header file and used to
display value on monitor.
cin – It is an object of istream_withassign class defined in iostream.h header file and used to read
value from keyboard for specific variable.
comment- Used for better understanding of program statements and escaped by the compiler to
compile . e.g. – single line (//) and multi line(/*....*/)
---------------------------------------------------------------------------------------------------------------------------------
Control structures :
Conditional Control Structures:
-------------------------------------------------------------
1. if(expression)
{
Statements;
}
2. if(expression)
{
Statements;
}
else
{
Statements;
}
3. switch(integral expression)
{
case :(const expr1)
statement1;
break;
case :(const expr2)
statements2;
break;
default:
statements;
Looping control structures:
-----------------------------------------------------
4. while(expression)
{
statements;
}
5. do
{
statements;
}while(expression);
6. for(initialisation; condition; increment/decrement)
{
statements;
}
Note: any non-zero value of an expression is treated as true and exactly 0 (i.e. all bits contain
0) is treated as false.
Nested loop -loop within loop.
exit()- defined in process.h and used to leave from the program.
break- exit from the current loop.
continue- to skip the remaining statements of the current loop and passes control to the next loop
control statement.
goto- control is unconditionally transferred to the location of local label specified by <identifier>.
For example
A1:
cout<<"test";
goto A1;
Some Standard C++ libraries
iostream.h
Defines stream classes for input/output streams
stdio.h
Standard input and output
ctype.h
Character tests
string.h
String operations
math.h
Mathematical functions such as sin() and cos()
stdlib.h
Utility functions such as malloc() and rand()
Some functions
isalpha(c)-check whether the argument is alphabetic or not.
islower(c)- check whether the argument is lowecase or not.
isupper(c) - check whether the argument is upercase or not.
isdigit(c)- check whether the argument is digit or not.
isalnum(c)- check whether the argument is alphanumeric or not.
tolower()-converts argument in lowercase if its argument is a letter.
toupper(c)- converts argument in uppercase if its argument is a letter.
strcat()- concatenates two string.
strcmp-compare two string.
pow(x,y)-return x raised to power y.
sqrt(x)-return square root of x.
random(num)-return a random number between 0 and (num-1)
randomize- initializes the random number generator with a random value.
Array- Collection of element of same type that are referred by a common name.
One Dimension array
An array is a continuous memory location holding similar type of data in single row or single
column
Two dimensional array
A two dimensional array is a continuous memory location holding similar type of data in of
both row sand columns (like a matrix structure).
Function -Self-contained block of code that does some specific task and may return a value.
Function prototypes-Function declaration that specifies the function name, return type and
parameter list of the function.
syntax: return_type function_name(type var1,type var2,....,type varn );
Passing value to function-
Passing by value
Passing by address/reference
Function overloading
Processing of two or more functions having same name but different list of parameters
Function recursion
Function that call itself either directly or indirectly.
Local variables
Declared inside the function.
Global variables
Declared outside all braces { } in a program
Actual Parameters
Variables associated with function name during function call statement.
Formal Parameters
Variables which contains copy of actual parameters inside the function definition.
Structure-Collection of logically related different data types (Primitive and Derived) referenced under
one name.
Nested structure
A Structure definition within another structure.
A structure containing object of another structure.
typedef
Used to define new data type name
#define Directives
Use to define a constant number or macro or to replace an instruction.
Inline Function
Inline functions are functions where the call is made to inline functions, the actual code then
gets placed in the calling program.
What happens when an inline function is written?
The inline function takes the format as a normal function but when it is compiled it is compiled
as inline code. The function is placed separately as inline function, thus adding readability to
the source program. When the program is compiled, the code present in function body is
replaced in the place of function call.
General Format of inline Function:
The general format of inline function is as follows:
inline datatype function_name(arguments)
inline int MyClass( )
Example:
#include <iostream.h>
int MyClass(int);
void main( )
{int x;
cout <<"\n Enter the Input Value:";
cin>>x;
cout<<"\n The Output is: " << MyClass(x);
}
inline int MyClass(int x1)
{return 5*x1;}
The output of the above program is:
Enter the Input Value: 10
The Output is: 50
The output would be the same even when the inline function is written solely as a function.
The concept, however, is different. When the program is compiled, the code present in the
inline function MyClass ( ) is replaced in the place of function call in the calling program. The
concept of inline function is used in this example because the function is a small line of code.
The above example, when compiled, would have the structure as follows:
#include <iostream.h>
int MyClass(int);
void main( )
{int x;
cout << "\n Enter the Input Value: "; cin>>x;
//The MyClass(x) gets replaced with code return 5*x1;
cout<<"\n The Output is: " << MyClass(x);
}
#include <iostream.h>
int MyClass(int);
void main( )
{int x;
cout <<"\n Enter the Input Value:";
cin>>x;
//Call is made to the function MyClass
cout<<"\n The Output is:" << MyClass(x);
}
int MyClass(int x1)
{return 5*x1;}
Introduction to C++
-----------------------------------------------------------------------------------------------------------------------------
C++ is the successor of C language & developed by Bjarne Stroustrup at Bell Laboratories,
New Jersey in 1979.
Tokens- smallest individual unit. Following are the tokens
-> Keyword-Reserve word that can‘t be used as identifier
-> Identifiers-Names given to any variable, function, class, union etc.
-> Literals-Value of specific data type
-> Variable- memory block of certain size where value can be stored and changed.
-> Constant- memory block where value can be stored once but can‘t changed later on
Operator – performs some action on data
--------------------------------------------------------------------------------------------------------------------------------
1. Arithmetic(+,-,*,/,%)
2. Relational/comparison (<,>,<=,>=,==,!=).
3. Logical(AND(&&),OR(||),NOT(!).
4. Conditional (? :)
5. Increment/Decrement Operators( ++/--)
Precedence of operators:
-------------------------------------------------------------------------------------------------------------------------------
1. ++(post increment),--(post decrement)
2. ++(pre increment),--(pre decrement),sizeof !(not),-(unary),+unary plus)
3. *(multiply), / (divide), %(modulus)
4. +(add),-(subtract)
5. <(less than),<=(less than or equal),>(greater than), >=(greater than or equal to)
6. ==(equal),!=(not equal)
7. && (logical AND)
8. ||(logical OR)
9. ?:(conditional expression)
10. =(simple assignment) and other assignment operators(arithmetic assignment
operator)
11. , Comma operator
---------------------------------------------------------------------------------------------------------------------------------
Data type- A specifier to create memory block of some specific size and type. For example –int,float,double,char etc.
cout – It is an object of ostream_withassign class defined in iostream.h header file and used to
display value on monitor.
cin – It is an object of istream_withassign class defined in iostream.h header file and used to read
value from keyboard for specific variable.
comment- Used for better understanding of program statements and escaped by the compiler to
compile . e.g. – single line (//) and multi line(/*....*/)
---------------------------------------------------------------------------------------------------------------------------------
Control structures :
Conditional Control Structures:
-------------------------------------------------------------
1. if(expression)
{
Statements;
}
2. if(expression)
{
Statements;
}
else
{
Statements;
}
3. switch(integral expression)
{
case :(const expr1)
statement1;
break;
case :(const expr2)
statements2;
break;
default:
statements;
Looping control structures:
-----------------------------------------------------
4. while(expression)
{
statements;
}
5. do
{
statements;
}while(expression);
6. for(initialisation; condition; increment/decrement)
{
statements;
}
Note: any non-zero value of an expression is treated as true and exactly 0 (i.e. all bits contain
0) is treated as false.
Nested loop -loop within loop.
exit()- defined in process.h and used to leave from the program.
break- exit from the current loop.
continue- to skip the remaining statements of the current loop and passes control to the next loop
control statement.
goto- control is unconditionally transferred to the location of local label specified by <identifier>.
For example
A1:
cout<<"test";
goto A1;
Some Standard C++ libraries
iostream.h
Defines stream classes for input/output streams
stdio.h
Standard input and output
ctype.h
Character tests
string.h
String operations
math.h
Mathematical functions such as sin() and cos()
stdlib.h
Utility functions such as malloc() and rand()
Some functions
isalpha(c)-check whether the argument is alphabetic or not.
islower(c)- check whether the argument is lowecase or not.
isupper(c) - check whether the argument is upercase or not.
isdigit(c)- check whether the argument is digit or not.
isalnum(c)- check whether the argument is alphanumeric or not.
tolower()-converts argument in lowercase if its argument is a letter.
toupper(c)- converts argument in uppercase if its argument is a letter.
strcat()- concatenates two string.
strcmp-compare two string.
pow(x,y)-return x raised to power y.
sqrt(x)-return square root of x.
random(num)-return a random number between 0 and (num-1)
randomize- initializes the random number generator with a random value.
Array- Collection of element of same type that are referred by a common name.
One Dimension array
An array is a continuous memory location holding similar type of data in single row or single
column
Two dimensional array
A two dimensional array is a continuous memory location holding similar type of data in of
both row sand columns (like a matrix structure).
Function -Self-contained block of code that does some specific task and may return a value.
Function prototypes-Function declaration that specifies the function name, return type and
parameter list of the function.
syntax: return_type function_name(type var1,type var2,....,type varn );
Passing value to function-
Passing by value
Passing by address/reference
Function overloading
Processing of two or more functions having same name but different list of parameters
Function recursion
Function that call itself either directly or indirectly.
Local variables
Declared inside the function.
Global variables
Declared outside all braces { } in a program
Actual Parameters
Variables associated with function name during function call statement.
Formal Parameters
Variables which contains copy of actual parameters inside the function definition.
Structure-Collection of logically related different data types (Primitive and Derived) referenced under
one name.
Nested structure
A Structure definition within another structure.
A structure containing object of another structure.
typedef
Used to define new data type name
#define Directives
Use to define a constant number or macro or to replace an instruction.
Inline Function
Inline functions are functions where the call is made to inline functions, the actual code then
gets placed in the calling program.
What happens when an inline function is written?
The inline function takes the format as a normal function but when it is compiled it is compiled
as inline code. The function is placed separately as inline function, thus adding readability to
the source program. When the program is compiled, the code present in function body is
replaced in the place of function call.
General Format of inline Function:
The general format of inline function is as follows:
inline datatype function_name(arguments)
inline int MyClass( )
Example:
#include <iostream.h>
int MyClass(int);
void main( )
{int x;
cout <<"\n Enter the Input Value:";
cin>>x;
cout<<"\n The Output is: " << MyClass(x);
}
inline int MyClass(int x1)
{return 5*x1;}
The output of the above program is:
Enter the Input Value: 10
The Output is: 50
The output would be the same even when the inline function is written solely as a function.
The concept, however, is different. When the program is compiled, the code present in the
inline function MyClass ( ) is replaced in the place of function call in the calling program. The
concept of inline function is used in this example because the function is a small line of code.
The above example, when compiled, would have the structure as follows:
#include <iostream.h>
int MyClass(int);
void main( )
{int x;
cout << "\n Enter the Input Value: "; cin>>x;
//The MyClass(x) gets replaced with code return 5*x1;
cout<<"\n The Output is: " << MyClass(x);
}
#include <iostream.h>
int MyClass(int);
void main( )
{int x;
cout <<"\n Enter the Input Value:";
cin>>x;
//Call is made to the function MyClass
cout<<"\n The Output is:" << MyClass(x);
}
int MyClass(int x1)
{return 5*x1;}
No comments:
Post a Comment