You are on page 1of 15

INTRODUCTION OF C++

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.

Operators Example Meaning


+ a+b Addition or
Unary plus
- a-b subtraction or
unary minus.
 a*b multiplication.

/ a/b division.

% a%b modulo division


(remainder).
RELATIONAL OPERATORS:
Relational operator are used to find out the relationship between the
Operand.

Operators Operations Example

< less than a<b

> greater than a>b

<= less than or a<=b

Equal to

>= greater than or a>=b

Equal to

== Equal to a==b

!= not equal to a!=b

LOGICAL OPERATORS:
Logical operators is used to find out thr relationship between two or more
Relationship expression.
Operators meaning

&& logical AND

|| 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)

INCREMENT AND DECREMENT OPERATORS:


C++ has very useful operators.
They are increment (++)and decremented(--)operators.
The increment operators(++)adds to the operators value contained in the variable
Increment has two types:
Pre increment(variable++).
Post increment(variable ++)
Decrement has two types:
Pre decrement(variable--)
Post decrement(variable--).

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.

Size of the operators:


This operators accepts one parcumeter, which can be either a type or
a variable itself and returns the size in bytes of that type or objects.
Syntax:
a=size of char;
This will assign the value 1 to a, because char is an one-byte long type.
The value returned by size of is a constant, so it is always determine
Before program execution.

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;

FOR LOOP STATEMENT:


The for is an entrolled loop and is used when an action is to be repeated for a
Three determine number of time.
Syntax:
For(initialize;condition;increment/decrement)
{
Action1;
}
MAIN FUNCTION:
C++ does not specific any written type of main()function which
is the starting point of the execution of the program.
The definition of main function
Main ()function
{
Main pro
}
In c++ the main function returns the value of type int to the
Operator system.
In c++ therefore explicity defines main function as matching
One of the following protype.
int main()
int main(int arg,char*arg v[];
The function that have a return value should use the return
Statement for termination.
The main function in c++is therefore defined as follows:
int main()
{
Return0;
}

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.

Long volume(long l,intb,int h);


{
Return(l*b*h)
}

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.

A constructor is declared and defined follow as


// class with a constructor
Class integer
{
int m,n;
public:
integer(void);
};
Integer::integer(void)
{
m=0;n=0;
}
When a class contains a constructor like the one defined above
it is guaranted that an object created by the class will be initialized automatically .
For example:
The declaration
Integer int1;
PARAMETERIZED CONSTRUCTORS:
The constructor integer( ),initialize the data members of all
Object to zero.
The initialize the various data elements of different objects
With different values when they are created to achieve this objective by
Passing arugments to the constructor f unction when the object are created.
The constructor that can take arguments are called
Parameterized constructors.
The constructor integer( ) may be modified to take argument
as shown below
class integer
{
int m,n;e
public:
integer(int x,int y);
};
Integer::integer(int x,int y);
{
m=x;n=y;
}
When a constructor has been parameterized the object declaration
Statement such as integer int 1;
CLASS WITH CONSTRUCTOR
#include <iostream.h>
Using namespace std;
Class integer
{
int m,n;
public:
integer(int ,int);
void display(void)
{
cout<<”m=”<<m<<”\n”;
cout<<”n=”<<n<<”\n”;
};
integer::integer(int x,int y)
{
m=x;n=y;
}
int main()
{
Integer int(10,100);
integer int 2=integer(25,75);
cout<<”\n OBJECT 1”<<”\n”;
int 1 .display( );
cout<<”\n OBJECT 2”<<\n”;
int 2.display( );
return 0;
}
Multiple constructor in a class:
Two kinds of constructor They are
Integer( );
Integer (int,int);
The constructor itself supplies the data values and no values
are passed the appropriate values from main( )
class integer
{
int m,n;
public:
integer( ){m=0;n=0;}
integer(int a ,int b;)
{ m=i.m; n=i.n;}
};
This declares three constructors for an integer object.
The first constructor receives no arguments,the second receives
one integer object as an argument.

CONSTRUCTOR WITH DEFAULT ARUGMENTS


It is a possible to define constructor with default aruguments.
For example, the constructor complex( ) can be declared asa follow.
Complex(float real,float imag=0);
COPY CONSTRUCTOR:
A copy constructor is used to declare and initialize it to the
Value of I1. Another form of this statement is
Integer I2=I1;
Will not invoke the copy constructor reference to an object
of same class as itself as an argument.
Program with copy constructor:
#include<iostream.h>
Using namespace std;
Class code;
{
int id;
public:
code( ){ }
code(int a ){id=A;}
code (code &x)
{
id=x.id;
}
Void display (void)
{
Cout<<id;
}
};
int main( )
{
code A(100);
code B(A);
code C=A;
code D;
D=A;
Cout<<”\n id of A:”;A.display( );
Cout<<”\n id of B:”;B.display( );
Cout<<”\n id of C:”;c.display( );
Cout<<”n id of D:”;D.display( )
return 0;
}
DYNAMIC CONSTUCTOR:
The constructor can also be used to allocate memory while
creating object

You might also like