Basics For C++
What kind of language computer understands?
Basically computer is a machine that understands only one language which is in the form of 0 or 1 this o & 1 signifies electrical signals.
- 0 means on
- 1 means off
Basically there are two types of languages:
- Low level Language.
- Machine Language.
- Assembly Language.
- High Level Language.
- C
- C++
- JAVA
So C++ is a high level language which is converted into machine language with the help of compiler. Compiler converts the high level language into code consisting of 0 and 1 in this way program run.
Basics Concepts For C++
Syntax: The rules for writing a language is known as syntax. It means that rules to use special symbols like , . : ; “ ’ { } [ ] ( ) etc.
Tokens: The smallest individual unit in C++ program is known as C++ program.
These are of six types:
- Keywords.
- Identifiers.
- Operators.
- Strings.
- Constants.
- Special Operators.
For Example: include, int, float etc.
Identifiers: Identifiers are the names given to the variables, constants, functions and arrays.
Operators: A symbol that tells computer to do some mathematical or logical calculation or computation is known as an Operator.
For Example: +, -, *, /,< etc.
String: The sequence of characters which are treated as a single data item ia known as string.
For Example:
Cout<<“Welcome to C++”;
Will output the string
Welcome to C++
Constants: These are those values which do not change during the execution of the program.
These are of two types:
- Primary Constants.
- Integer Constant
- Real Constant
- Character Constant
- Secondary Constants.
- Array
- Pointer
- Structure
- Union
- Enum.
Data Types: The type of data in memory location or a variable hold is determined by data type.
Data Types are of four types:
- Primary/Fundamental/Build-in Data types.
- Derived Data types
- User-Defined Data types
- Empty Data types
- Character.
- Int
- Float
- Double
Polymorphism
It is the ability to take more than one form for example ‘ * ’ can be used to multiply as well as to declare pointer variable.
Types Of Polymorphism:
- Compile Time Polymorphism.
- Function Overloading.
- Operator Overloading.
- Run Time Polymorphism.
Compile Time Polymorphism:
This is also known as early binding or static binding or static linking. In this type the code to a (function) call is known at compile time itself to the compiler. So the compiler is able to select appropriate function to a particular call at the compile time itself.
Types Of Compile Time Polymorphism:
- Function Overloading
- Operator Overloading
Function Overloading:
It is the technique to use the same function name in a program to a family of function that can perform a variety of tasks.
Using this concept we can create a family of functions who have same function name but with different argument list. Each function performs depending upon the argument list.
The correct function to invoke is determined checking the number and type of arguments and the sequence of the arguments but not by the return type of the function.
When function call is made , the compiler invokes particular functions using the following steps to match a correct function:
- Compiler tries to find a correct match.
- If exact match fails, the compiler uses integral promotions such as Char to Int and Float to Double.
- When either of them fails, then compiler uses built-in conversions to actual argument.
- If all the above fails, then compiler will try the user-defined conversions in combination with integralpromotion and build-in conversion to find an exact match.
- If these steps also fails, then computer generate an error.
Operator Overloading:
The mechanism of giving special (additional) meaning to an operator without changing its original behavior is known as operator overloading in c++.
We can overload all operators except the following :
- Class member access operator (. , .*)
- Scope resolution operator ( :: )
- Size operator (size)
- Conditional operator ( ?: )
The additional meaning to an operator is givien with the help of special function known as operator function. This function describes a new task to be performed by the operator.
The general syntax of operator overloading is:
Return type classname :: operator op (argument list)
{
Function // task defined
}