Introduction
The C++ programming language was developed at AT&T Bell Laboratories in earlier 1980s by Bjarne Stroustrup. He found 'C' lacking for simulations and decided to extend the language by adding features from his favourite language, Simula 67. It was one of the earliest OOPs languages. The name C++ was coined by Rick Mascitti where "++" is the increment operator.
The latest version of C++ is C++11 or formally C++0x.
The major reason behind the success is that it supports object-oriented technology, the latest software was most near to the real world.
Fundamental Data Type
- These are the data types that are not composed of any other data type.
- There are 5 Fundamental Data Types
- Char
- Int
- Float
- Double
- Void
- Ex: int a=10;
Derived Data Type
- These are the data types that are composed of fundamental data types.
- These are
- Array
- Function
- Pointer
- Class
- Structure
- Constant etc.
- Ex: int a[10];
Purpose Of Header Files
Header files provide function prototype, the definitions of the library function, declaration of data type and constants with the library functions.
Main Integer
- Short
- Int
- Long
Each comes in both signed and unsigned versions.
Constant
The keyword const can be added to the declaration of an object to make the object a constant rather than a variable.
Constant can't be altered during the program run.
The General Form:
const type name = value;
const int a = 5;
Reference
A reference variable provides an alias for a previously defined variable.
The General Form:
type &reference-variable = variable-name;
Example
int a = 5;
int &b = a;
cout << a << "\n" << b;
Output
5
5
Variable
It represents the named storage location during the program run.
The General Form:
type name;
int a = 5;
Union
The union is used to combine two or more different variables.
Error
ERROR | CORRECTED |
cout<<"="a; | cout<<"="<<a; or cout<<"=a"; |
m=5, n=12; o=15 | m=5, n=12, o=15; |
cout<<"x";<<x; | cout<<"x"<<x; |
cin>>y;>>j; | cin>>y>>j; |
cin>>"\n">>y; | cin>>y; |
cout>>"\n"abc"; | cout<<"\n abc"; |
a=b+c | a=b+c; |
break=x*y; | break1=x*y; or a =x*y; |
Different Way to initialize
- By Separate Assignment Statement. Ex: int age; age=3;
- At the time of Declaration. Ex: int age=3;
- Dynamic Initialization. Ex: float avg=sum/count;
How Numbers Used in Char?
char a = 75;
It treats 75 as ASCII value and converts it into relative character.
What if const variable is incremented?
const int a=10;
a++;
cout<<a;
Output
10
Arithmetic Operator
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Remainder (%)
Unary Arithmetic Operator
It has only one operand
They are:
- ++
- --
- &
- *
- +
- - etc.
Ex:
short x = 987;
x = -x;
Binary Arithmetic Operator
It has only two operand
They are:
- >>
- ==
- <<
- +
- - etc.
Ex:
int x = 7, y = 7;
float z;
z = x + y;
Increment Operator |
Decrement Operator |
++, adds 1 to its operand | --, subtract 1 from its operand |
Prefix: It performs the increment operation before using the value of the operant. | Prefix: It performs the decrement operation before using the value of the operant. |
Postfix: First use the value before incrementing the operant value. | Postfix: First use the value before decrementing the operant value. |
Reasons for Following Invalid expression
Invalid Expression |
Reason |
asm = 1 || val < 21 | In this expression, Keywords can't be used and Single "=" operator is used for comparison which is invalid. |
age > 7 && <90 | In this expression, Variable name is not mentioned after the "&&" operator. which is invalid. |
a > 50 ||&& v < 50 | In this expression, both || and && operators are written together which is invalid. |
res !> 20 || !x > 20 | In this expression, !> is invalid and !x should written in parenthesis (!x > 20`) |
Casting
It is the process of converting data from one type to another.
- Type Casting Ex: int a=1, b=2; float c = (float)a/b;
- Automatic Type Casting Ex: int a=2; char c = a;
0 comments:
Post a Comment