Introduction of C++ Variable: V
Introduction of C++ Variable: V
Introduction of C++ Variable: V
VARIABLE:
Variable is a quantity which changes during a execution of the program.
Declaration does three things:
It gives name to memory location.
It specifies the type of data.
It allocates memory space.
Rules for naming variable:
The first character in a variable must be an alphabet A,B,C, Underscore -, dollar
$. Commas or blankspace are not allowed within a variable name.
Variable names are case sensitive.
Declaration of variable:
All the variable to be defined anywhere in the space.
The general format is
Data type variable _ name;
Example:
int count;
Float(x,y);
Double p;
Intiliazation of variable:
The general format is
Data type variable _name=value or expression;
Example:
int A=10;
char c=”m”;
string “MAN”;
Dynamic intilization:
The variable can be intiliazed at run time using expression at a place of declaration .
This is referred to as dynamic intilization.
Example:
int m=10;
OPERATORS:
An operator is a symbol which helps the user to command the complier to do
Certain mathematical or logical manipulations .Operators are used in c language program
to operate on data and variable.
TYPES OF DATA:
Arithmetic operators.
Relational operators.
Logical operators.
Assignment operators.
Increment and decrement operators.
Bitwise operators.
Conditional operators.
Special operators.
ARITHMETIC OPERATORS:
Arithmetic operators are used to perform arithmetic calculations.
C provides basic arithmetic operators.
/ a/b division.
Equal to
Equal to
== Equal to a==b
LOGICAL OPERATORS:
Logical operators is used to find out thr relationship between two or more
Relationship expression.
Operators meaning
|| logical OR
! logical NOT
The logical operators && and || are used when we want to form compound
Conditions by combining two or more relation.
ASSIGNMENT OPERATORS:
Assignment operators are used to assign value of an expression is a
Variable . The assignment operators evaluate an expression on the right of the
Expression and substitutes it to the value or variable on the left of the expression.
SYNTAX:
V op=exp;
Where,
V=variable
Exp=expression
Op=binary operators
= is known as the assignment operators.
V op =exp; is equal to v=v op(exp);
That is,
X+=y+1; is equal to x=x+(y+1)
BITWISE OPERATORS:
Bitwise operators are used to perform bit by bit operations.
A Bitwise operators operates on each bit of data.
Those operators are used for testing complementing or shifting
Bits to the right on left.
Operators meaning
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
>> Bitwise right shift
<< Bitwise left shift
CONDITIONAL OPERATORS:
The conditional operators are also called binary operators.
They are used to construct expressions .
Conditional operators has three operands.
SYNTAX:
Condition?expression1;expression2;
Example:
int x=10,y=15;
int y=(x>y)?x;y;
COMMA OPERATORS:
The comma operators can be used to link related expression together.
A comma linked list of expression are evaluated test of right and value
Of the right most exoression is the value of the combined expression.
MANIPULATORS:
Manipulators are operators are used to format the data display.
The most commonly used manipulators are endl and setw.
The endl operators used in an output statement.
It has the same effect using the new line characters (“backslash”).
The setw manipulators also used in an output.
The form of output can specify a common field width for all the number
And force them to be printer right justified.
Example:
Cout<<setw(&)<<sum<<endl;
Program:
#include<iostream.h>
#include<iomanip.h>
int main()
{
int basic=950,allowance=95,total=1045;cout<<setw(10)<<”basic”<<
Setw(10)<<basic<<endl<<setw(10)<<”allowance”<<setw(10)<<
Allowance<<endl<<setw(10)<<”total”<<setw(10)<<total<<endl;
Return0;
}
CONTROL STRUCTURES:
The combination of control structure are
Sequence structure(straight line)
Selection structure(branching)
Loop structure(iteration and repeatition).
The if statement:
The if statement is implemented into tow forms
Simple if statement
If else statement.
Simple if statement:
This statement is only used for ture condition.
Syntax:
If(expression)
{
action1;
}
action2:
When this statement is executed, evaluates the value of the test condition.
If the value is true then the statement block and the next statement gets
Executed sequentically.
If the value is false the statement block is skipped and execution starts
the next statement.
If else statment:
The if else statement is used to execute one group of statement.
If the test condition is true and other group of statement if the test
Condition is false.
Syntax:
If(test condition)
{
Statement1;
}
Else
{
Statement2;
}
Statement3;
SWITCH STATEMENT:
This is a multiple branching statement were based on a condition,
the control is transferred to one of the many possible points.
Syntax:
Switch(exp)
{
Case1;
Statement1;
Break;
Case2;
Statement2;
Break;
Case3;
Statement n;
Break;
}
DO WHILE STATEMENT:
The do while condition is in an exist control loop.
Based on a condition a control is transferred to a particular point in a
Program.
Syntax:
While(condition is true)
{
action 1;
}
action2;
FUNCTION PROTYPING:
Function protyping is one of the major improvement added to c++
Function.
The protype describes a the function interface to the complier by
giving details such as the number and type of argument and type of return type.
In function protype a template this always used when declaring
and defining a function.
Syntax:
Type function _name(argument _list)_
Example:
Double volume (doubler int h)
In general we can either include or exclude the variable names
in the argument list of protype.
The variable names in the argument just act as place holders
and therefore if names are used,they do not have the match the names
used in the function call or function definition.
In function definition names required because the arguments,
must be referred inside the function.
UNIT-2
CONSTUCTOR:
A constructor is a ‘special’ member function whose task is to
Initialize the object of its class.
It is special because its name is the same as the class name.
The constructor is invoked whenever an object of its associated
Class is created.
It is called constructor because it constructs the values of data
Members of the class.