Constant Operators in C++ Types of Operator: I. II. IV
Constant Operators in C++ Types of Operator: I. II. IV
IIIT, BHUBANESWAR
Dept. Comp. Sc. & Engg.
C++ and Object Oriented Programming
Lecture: 7, 8,9
Example:
CSE/C++/Module-I/Trilochan Rout/Lecture: 7, 8, 9 2
#include <iostream>
Using namespace std;
#define PI 3.14
int main()
{
float radius, area;
cout<<"Enter the radius: ";
cin>>radius
// Notice, the use of PI
area = PI*radius*radius;
Example:
Const int num = 150;
num = 100; //error, constant variable can’t modified.
- Here the value of num always remain equal to 150 and cannot be
changed at run-time.
Example:
#include<iostream>
using namespace std;
int main()
{
const int sunday =0; //const Sunday = 0
const int monday = 1;
int ch;
cout<<” enter the day code (0 or 1)”;
cin>>ch;0
if( ch == sunday)
cout<<”\n It’s a Holiday”;
else
cout<<”\n Nooo!!! It’s a working day”;
return 0;
}
Output:
Enter the day code(0 or 1)
1
Nooo. It’s a working day
Example:
enum color {black, white, blu, red, green};
4.
15.
Example:
variable name
Lecture: 8:
II. Operators in C++:
Operator is an instruction to the compiler or interpreter specified by a single or
double symbol to perform certain operations with constants and variable.
C++ supports all C operators.
An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations. C++ is rich in built-in operators and
provides the following types of operators:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Increment / Decrement Operators
Special Operator
Arithmetic Operators:
There are following arithmetic operators supported by C++ language:
Assume variable A holds 10 and variable B holds 20, then:
a=20
b=2
C=a%b
CSE/C++/Module-I/Trilochan Rout/Lecture: 7, 8, 9 5
Relational Operators:
There are following relational operators supported by C++ language
Assume variable A holds 10 and variable B holds 20, then:
Bitwise Operators:
Bitwise operator works on bits and performs bit-by-bit operation.
The Bitwise operators supported by C++ language are listed in the following
table. Assume variable A holds 60 and variable B holds 13, then:
Special Operator
There are few other operators supported by C++ Language.
Operator Description
sizeof operator returns the size of a variable. For
Sizeof
example, sizeof(a), where a is integer, will return 4.
Conditional operator. If Condition is true ? then it
Condition ? X : Y
returns value X : otherwise value Y
Comma operator causes a sequence of operations to
be performed. The value of the entire comma
,
expression is the value of the last expression of the
comma-separated list.
Lecture: 9:
Additional C++ Operator:
o << → Insertion Operator
o >>→ Extraction Operator
o &→ Referencing Operator
o : : → Scope Resolution Operator
o : :* →Pointer-to-member declaration
o →* →Pointer-to-member Operator
o .* →Pointer-to-member Operator
o new →Memory allocation Operator
o delete → Memory release Operator
o endl → Line feed Operator
o setw → Field width Opeartor
Referencing(&) Operator:
- This operator is used to define referencing variable.
- A reference variable provides an alias(alternative names) for a previously
defined variable.
Syntax:
datatype &newname = original name;
Example:
int qty = 10;
int &qt = qty; → qt is an alternative name for qty
If we display the both variables by using cout statement,
cout<<qty;
cout<<qt;
Both will give the value 10. Changes madeto anyone it will affect to both.
Features:
- A reference variable must be initialized at the time of declaration.
- Once reference variable is declared, it should not refer to any other
variable.
- The reference variable can be created referencing to pointer variable.
o E.g: char *ch = “C++”;
char *&p = h;
CSE/C++/Module-I/Trilochan Rout/Lecture: 7, 8, 9 9
- This operator (::) allows a programmer to access a global variable name even
if it is hidden by a local re-declaration of the same variable.
- It allows to access global version of the variable.
Syntax:
:: variable name
#include<iostream>
Using namespace std;
int z=45;
Int main()
{
Output:
50
50
50
100
55
Example:
#include<iostream>
CSE/C++/Module-I/Trilochan Rout/Lecture: 7, 8, 9 11
int main()
{
Cout<<::k;// 101
int k = 50; //k local to main
{
int x = k;
int k = 75; //local to inner block
cout<<" X = "<<x<<endl;//50
cout<<" K = "<<k<<endl;//75
cout<<" :: K = "<<::k<<endl;//101
}
cout<<" K = "<<k<<endl;//50
cout<<" ::K = "<<::k<<endl;//101
return 0;
Output:
NB:
- It is applicable if the local and global variable having same name and
datatype.
- C++ permits us to access the class members through pointers. For this it
provides a set of three pointer-to-member operators.
I. :: * → to declare a pointer to a member of a class.
II. . * → to access a member using object name and a pointer to that
members.
III. →* : to access a member using a pointer to the object and a pointer to
that member
I. new operator:
- An object can be created using new operator as follows:
Syntax:
pointer_ var_name = new
datatype ;
Int *p;
P=new datatype;
- The new operator allocates sufficient memory to hold a data object of types
<datatype> and returns the starting address of the object to the pointer
variable.
CSE/C++/Module-I/Trilochan Rout/Lecture: 7, 8, 9 13
Example:
int *p;
float *q;
p = new int;
q = new float;
//initialization ways:
*p = 50;
*q = 75.25;
- Or, we can initialize the memory using new operator
Syntax:
pointer_name var_name = new datatype
(value) ;
Example:
int *p = new int(50);
float *q = new float(75.25);
- The new operator can be used to create memory space for user
defined datatype such as arrays, structures, classes, etc.
Syntax:
pointer_name var_name = new datatype
[size] ;
Example:
Example:
delete p;
delete q;
delete [5] p;
delete [ ] p;
NB:
- A data object is created inside a block with new operator, will remain in
existence until it is explicitly destroyed by using delete operator.
- If sufficient memory is not available, then the new operator returns a
NULL pointer.
CSE/C++/Module-I/Trilochan Rout/Lecture: 7, 8, 9 15
Example:
cout<< “a = ”<<150<<endl
<<”b = ”<<20<<endl
<<”c = “<<1025<<endl;
Output:
a
1 5 0
= All are left justified
b
2 0
=
c
1 0 2 5
=
Output:
Example:
void main()
{
int x;
clrscr();
x = 10;
cout<<”\n X = “<<x;
cout<<”\n”;
getch();
}
Output:
X = 10
Some Operators that cannot be overloaded:
o sizeof operator
o . → Member access operator
o .* → ptr-to-member operator
o :: → Scope resolution operator
o ?: → Conditional operator