SlideShare a Scribd company logo
1 of 13
Download to read offline
Reserved Words – I
1. Const :
Constant, it is something fixed and can't be changed later. In
programming languages we used the word const to make the variables constant,
so that it's value can't be changed later. In C++, we use this word with
variables, pointers, functions and its return types, classes members and
objects.
➢ With variables :
If the variables are declared using const keyword as prefix, they are
called as Constant variables.
For example :
const int x ;
• The constant variables are initialised at the time of delcaration only.
const int x = 1;
• we can also rewrite const int x = 1; as
int const x = 1;
Look at this:
#include <iostream>
using namespace std;
int main()
{
Also See more posts : www.comsciguide.blogspot.com
const int x; // declaration
x = 1; // initialization
cout<<x;
return 0;
}
The above program produces an error, as the const variable x is not
initilaised at the time of declaration. Declaration and Initialization can't be done
seperately.
int main()
{
const int x = 1;
x++;
return 0;
}
It results in compilation error as the const variables values can't be
changed later.
➢ With Pointers :
In this section, there are two types.
1. Pointers to const
2. const pointers
➔Pointers to const :
Also See more posts : www.comsciguide.blogspot.com
Generally we called this as Pointers to constant variables. Here,
pointers point to constant data types like int, float, char etc. We can change
pointer to point to any other integer variable, but we cannot change the value of
object(entity) pointed using pointer. Pointer is stored in read-write area (stack
in present case). Object pointed may be in read only or read write area.
For example :
int x = 10; ( or ) int x = 10;
const int *p = &x; int const *p = &x;
Here x is a const integer and p is a pointer variable pointed to const
int x.
 Ofcoure u can change x directly but, If u perform to change the value of x
using p (pointer), it gives an error.
Let's take with an example :
#include<iostream>
using namespace std;
int main()
{
int x = 10;
int const *p = &x;
x=11;
x++;
cout<<x;
Also See more posts : www.comsciguide.blogspot.com
return 0;
}
Output :
12
But
#include<iostream>
using namespace std;
int main()
{
int x = 10;
int const *p = &x;
(*p)++;
cout<<x;
return 0;
}
It gives an error as the pointer is assigned to read only location.
 We can change pointer to point to any other integer variable
int x = 10;
int y = 11;
int const *p = &x;
*p = &y; // pointer is assigned to another variable
 The non-const pointers can't point to const data types.
Example :
const int x=1;
Also See more posts : www.comsciguide.blogspot.com
int *p = &x; // non-const pointer can't
assigned to const int
It results in compilation error.
➔const pointers to variables:
If pointers are declared const as prefix, they are called as const
pointers. We can't change the pointer to point to another variable, but
can change the value that it points to.
For example:
int x = 10;
int * const a = &x;
Here, a is the pointer which is const that points to int x. Now we
can't change the pointer, but can change the value that it points to.
Take a look with an example :
#include<iostream>
using namespace std;
int main()
{
int x = 10;
int y = 11;
int *const p = &x;
Also See more posts : www.comsciguide.blogspot.com
(*p) = 12; // valid
*p = & y; // error
return 0;
}
It results in error as the pointer points to, can't be changed. But we
can change the value that it points to.
Note :
“const with pointers” - In a nutshell
char c;
char *const cp = &c;
cp is a pointer to a char. The const means that cp is not to be
modified, although whatever it points to can be--the pointer is constant, not the
thing that it points to. The other way is
const char *cp;
which means that now cp is an ordinary, modifiable pointer, but the thing that it
points to must not be modified. So, depending on what you choose to do, both
the pointer and the thing it points to, may be modifiable.
➔Const pointers to const variables :
Here, We can't change pointer to point to any other integer variable
and also, the value of object (entity) it pointed.
Also See more posts : www.comsciguide.blogspot.com
Lets see with an example:
#include<iostream>
using namespace std;
int main()
{
int x = 10;
int y = 11;
const int *const p = &x;
(*p) = 12; // error
p = &y; // error
return 0;
}
➢ With Functions arguments and its Return type:
The const keyword in functions arguments is similiar to that of
const with variables
Example :
void fun (const int x) {}
By doing this, u can't modify the x value inside the function.
 int const f() is equivilent to const int f(), which means that
return type is const.
void fun(const int x) // example for fun arg
Also See more posts : www.comsciguide.blogspot.com
{
x++; // error
}
const int fun() // example for return type
{
return 1; //any constant value
}
 If a function has a non-const parameter, then we cannot make a const
argument call i.e. we can't pass the const argument at the function call.
Example :
#include<iostream>
using namespace std;
void fun(int *p) // non-const ar
{
cout<< *p;
}
int main()
{
const int x=10; // const parameter
fun(&x); // error
return 0;
}
Here, we are passing the const argument to the non – const
parameters. So it results in error.
But a, function with a const parameter, can make a const argument
call as well as non- const argument at the function call.
Also See more posts : www.comsciguide.blogspot.com
Example :
#include<iostream>
using namespace std;
void fun(const int *p) // non-const parameter
{
cout<< *p;
}
int main()
{
const int x = 10;
fun(&x); // const agrument
int y = 11;
fun(&y); // non const argument
return 0;
}
4. With classes and its members :
• const class data members
These are data variables in class, which are made const. They
are not initialized during declaration. ButTheir initialization occur in the
constructor.
For Example:
#include<iostream>
using namespace std;
class cls
{
int const x;
Also See more posts : www.comsciguide.blogspot.com
public:
cls () : x(1) // constructor
{ }
void disp()
{
cout<< x;
}
};
int main()
{
cls a;
a.disp();
return 0; output : 1
}
Notice the syntax : x(1) after the constructor. This tells C++ to
initialize the variable y to have value 1. More generally, you can use this syntax
to initialize data members of the class.
• const member funcions & objects :
1. The member functions of class are declared constant as follows :
return_type fun_name () const ;
This makes the function itself as constant.
2. The objects of class can be declared constant as follows :
const class_name obj_name ;
Also See more posts : www.comsciguide.blogspot.com
Making a member function const means that it cannot change any
member variable inside the function. It also means that the function can be
called via a const object of the class.
Look at the example below :
#include<iostream>
using namespace std;
class A
{
public:
void Const_No() {} // nonconst member function
void Const_Yes() const {} // const member function
};
int main()
{
A obj_nonconst; // nonconst object
obj_nonconst.Const_No(); // works fine
obj_nonconst.Const_Yes(); // works fine
const A obj_const ; // const object
obj_const.Const_Yes(); // const object can call const
function
obj_const.Const_No(); // ERROR-- const object cannot call
nonconst function
}
A const member function can be used with both objects (const and
Also See more posts : www.comsciguide.blogspot.com
non-const) while, A non - const function can be used only with non – const
objects, but not wtih const objects.
Try it urself :
#include<iostream>
using namespace std;
class cls
{
public:
int x;
cls(int i) // constructor initialization
{
x = i;
}
void f() const // constant function
{
x++;
}
void g() // non-constant function
{
x++;
}
};
int main()
{
const cls a(20); // constant object call
cls b(30); // non-constant object call
a.f();
a.g();
b.f();
Also See more posts : www.comsciguide.blogspot.com
b.g();
cout<<a.x;
cout<<b.x;
return 0;
}
Also See more posts : www.comsciguide.blogspot.com

More Related Content

What's hot

pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cppgourav kottawar
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphismlalithambiga kamaraj
 
pointer, virtual function and polymorphism
pointer, virtual function and polymorphismpointer, virtual function and polymorphism
pointer, virtual function and polymorphismramya marichamy
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpprajshreemuthiah
 
Lec 38.39 - pointers
Lec 38.39 -  pointersLec 38.39 -  pointers
Lec 38.39 - pointersPrincess Sam
 
Let's make a contract: The art of designing a Java API | DevNation Tech Talk
Let's make a contract: The art of designing a Java API | DevNation Tech TalkLet's make a contract: The art of designing a Java API | DevNation Tech Talk
Let's make a contract: The art of designing a Java API | DevNation Tech TalkRed Hat Developers
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)SURBHI SAROHA
 
Practical TypeScript
Practical TypeScriptPractical TypeScript
Practical TypeScriptldaws
 
Lecture 2 keyword of C Programming Language
Lecture 2 keyword of C Programming LanguageLecture 2 keyword of C Programming Language
Lecture 2 keyword of C Programming LanguageSURAJ KUMAR
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)SURBHI SAROHA
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2rohassanie
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)Ritika Sharma
 

What's hot (20)

pointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpppointers, virtual functions and polymorphisms in c++ || in cpp
pointers, virtual functions and polymorphisms in c++ || in cpp
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
C++ functions
C++ functionsC++ functions
C++ functions
 
16 virtual function
16 virtual function16 virtual function
16 virtual function
 
pointer, virtual function and polymorphism
pointer, virtual function and polymorphismpointer, virtual function and polymorphism
pointer, virtual function and polymorphism
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
 
Lec 38.39 - pointers
Lec 38.39 -  pointersLec 38.39 -  pointers
Lec 38.39 - pointers
 
Let's make a contract: The art of designing a Java API | DevNation Tech Talk
Let's make a contract: The art of designing a Java API | DevNation Tech TalkLet's make a contract: The art of designing a Java API | DevNation Tech Talk
Let's make a contract: The art of designing a Java API | DevNation Tech Talk
 
Vbscript
VbscriptVbscript
Vbscript
 
Lec 37 - pointers
Lec 37 -  pointersLec 37 -  pointers
Lec 37 - pointers
 
C programming(part 3)
C programming(part 3)C programming(part 3)
C programming(part 3)
 
Practical TypeScript
Practical TypeScriptPractical TypeScript
Practical TypeScript
 
Lecture 14 - Scope Rules
Lecture 14 - Scope RulesLecture 14 - Scope Rules
Lecture 14 - Scope Rules
 
Lecture 2 keyword of C Programming Language
Lecture 2 keyword of C Programming LanguageLecture 2 keyword of C Programming Language
Lecture 2 keyword of C Programming Language
 
Programming in C (part 2)
Programming in C (part 2)Programming in C (part 2)
Programming in C (part 2)
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdfPOINTERS IN C MRS.SOWMYA JYOTHI.pdf
POINTERS IN C MRS.SOWMYA JYOTHI.pdf
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
Function overloading(C++)
Function overloading(C++)Function overloading(C++)
Function overloading(C++)
 

Similar to CONST-- once initialised, no one can change it

2.Types_Variables_Functions.pdf
2.Types_Variables_Functions.pdf2.Types_Variables_Functions.pdf
2.Types_Variables_Functions.pdfTrnThBnhDng
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operatorSAFFI Ud Din Ahmad
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptxhara69
 
Presentation 5th
Presentation 5thPresentation 5th
Presentation 5thConnex
 
C++ Pointers with Examples.docx
C++ Pointers with Examples.docxC++ Pointers with Examples.docx
C++ Pointers with Examples.docxJoeyDelaCruz22
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloadingankush_kumar
 
overloading in C++
overloading in C++overloading in C++
overloading in C++Prof Ansari
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2Warui Maina
 
C questions
C questionsC questions
C questionsparm112
 
Switch case and looping kim
Switch case and looping kimSwitch case and looping kim
Switch case and looping kimkimberly_Bm10203
 
C++ Function
C++ FunctionC++ Function
C++ FunctionHajar
 
Beginner C++ easy slide and simple definition with questions
Beginner C++ easy slide and simple definition with questions Beginner C++ easy slide and simple definition with questions
Beginner C++ easy slide and simple definition with questions khawajasharif
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and loopingaprilyyy
 

Similar to CONST-- once initialised, no one can change it (20)

2.Types_Variables_Functions.pdf
2.Types_Variables_Functions.pdf2.Types_Variables_Functions.pdf
2.Types_Variables_Functions.pdf
 
18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator18 dec pointers and scope resolution operator
18 dec pointers and scope resolution operator
 
1. DSA - Introduction.pptx
1. DSA - Introduction.pptx1. DSA - Introduction.pptx
1. DSA - Introduction.pptx
 
Presentation 5th
Presentation 5thPresentation 5th
Presentation 5th
 
C++ Pointers with Examples.docx
C++ Pointers with Examples.docxC++ Pointers with Examples.docx
C++ Pointers with Examples.docx
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 
overloading in C++
overloading in C++overloading in C++
overloading in C++
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
C questions
C questionsC questions
C questions
 
Switch case and looping kim
Switch case and looping kimSwitch case and looping kim
Switch case and looping kim
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Beginner C++ easy slide and simple definition with questions
Beginner C++ easy slide and simple definition with questions Beginner C++ easy slide and simple definition with questions
Beginner C++ easy slide and simple definition with questions
 
Switch case and looping jam
Switch case and looping jamSwitch case and looping jam
Switch case and looping jam
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
Switch case and looping
Switch case and loopingSwitch case and looping
Switch case and looping
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 

More from Ajay Chimmani

24 standard interview puzzles - Secret mail puzzle
24 standard interview puzzles - Secret mail puzzle24 standard interview puzzles - Secret mail puzzle
24 standard interview puzzles - Secret mail puzzleAjay Chimmani
 
24 standard interview puzzles - The pot of beans
24 standard interview puzzles - The pot of beans24 standard interview puzzles - The pot of beans
24 standard interview puzzles - The pot of beansAjay Chimmani
 
24 standard interview puzzles - How strog is an egg
24 standard interview puzzles - How strog is an egg24 standard interview puzzles - How strog is an egg
24 standard interview puzzles - How strog is an eggAjay Chimmani
 
24 standard interview puzzles - 4 men in hats
24 standard interview puzzles - 4 men in hats24 standard interview puzzles - 4 men in hats
24 standard interview puzzles - 4 men in hatsAjay Chimmani
 
Aptitude Training - TIME AND DISTANCE 3
Aptitude Training - TIME AND DISTANCE 3Aptitude Training - TIME AND DISTANCE 3
Aptitude Training - TIME AND DISTANCE 3Ajay Chimmani
 
Aptitude Training - PIPES AND CISTERN
Aptitude Training - PIPES AND CISTERNAptitude Training - PIPES AND CISTERN
Aptitude Training - PIPES AND CISTERNAjay Chimmani
 
Aptitude Training - PROFIT AND LOSS
Aptitude Training - PROFIT AND LOSSAptitude Training - PROFIT AND LOSS
Aptitude Training - PROFIT AND LOSSAjay Chimmani
 
Aptitude Training - SOLID GEOMETRY 1
Aptitude Training - SOLID GEOMETRY 1Aptitude Training - SOLID GEOMETRY 1
Aptitude Training - SOLID GEOMETRY 1Ajay Chimmani
 
Aptitude Training - SIMPLE AND COMPOUND INTEREST
Aptitude Training - SIMPLE AND COMPOUND INTERESTAptitude Training - SIMPLE AND COMPOUND INTEREST
Aptitude Training - SIMPLE AND COMPOUND INTERESTAjay Chimmani
 
Aptitude Training - TIME AND DISTANCE 4
Aptitude Training - TIME AND DISTANCE 4Aptitude Training - TIME AND DISTANCE 4
Aptitude Training - TIME AND DISTANCE 4Ajay Chimmani
 
Aptitude Training - TIME AND DISTANCE 1
Aptitude Training - TIME AND DISTANCE 1Aptitude Training - TIME AND DISTANCE 1
Aptitude Training - TIME AND DISTANCE 1Ajay Chimmani
 
Aptitude Training - PROBLEMS ON CUBES
Aptitude Training - PROBLEMS ON CUBESAptitude Training - PROBLEMS ON CUBES
Aptitude Training - PROBLEMS ON CUBESAjay Chimmani
 
Aptitude Training - RATIO AND PROPORTION 1
Aptitude Training - RATIO AND PROPORTION 1Aptitude Training - RATIO AND PROPORTION 1
Aptitude Training - RATIO AND PROPORTION 1Ajay Chimmani
 
Aptitude Training - PROBABILITY
Aptitude Training - PROBABILITYAptitude Training - PROBABILITY
Aptitude Training - PROBABILITYAjay Chimmani
 
Aptitude Training - RATIO AND PROPORTION 4
Aptitude Training - RATIO AND PROPORTION 4Aptitude Training - RATIO AND PROPORTION 4
Aptitude Training - RATIO AND PROPORTION 4Ajay Chimmani
 
Aptitude Training - NUMBERS
Aptitude Training - NUMBERSAptitude Training - NUMBERS
Aptitude Training - NUMBERSAjay Chimmani
 
Aptitude Training - RATIO AND PROPORTION 2
Aptitude Training - RATIO AND PROPORTION 2Aptitude Training - RATIO AND PROPORTION 2
Aptitude Training - RATIO AND PROPORTION 2Ajay Chimmani
 
Aptitude Training - PERMUTATIONS AND COMBINATIONS 2
Aptitude Training - PERMUTATIONS AND COMBINATIONS 2Aptitude Training - PERMUTATIONS AND COMBINATIONS 2
Aptitude Training - PERMUTATIONS AND COMBINATIONS 2Ajay Chimmani
 
Aptitude Training - PERCENTAGE 2
Aptitude Training - PERCENTAGE 2Aptitude Training - PERCENTAGE 2
Aptitude Training - PERCENTAGE 2Ajay Chimmani
 
Aptitude Training - PERCENTAGE 1
Aptitude Training - PERCENTAGE 1Aptitude Training - PERCENTAGE 1
Aptitude Training - PERCENTAGE 1Ajay Chimmani
 

More from Ajay Chimmani (20)

24 standard interview puzzles - Secret mail puzzle
24 standard interview puzzles - Secret mail puzzle24 standard interview puzzles - Secret mail puzzle
24 standard interview puzzles - Secret mail puzzle
 
24 standard interview puzzles - The pot of beans
24 standard interview puzzles - The pot of beans24 standard interview puzzles - The pot of beans
24 standard interview puzzles - The pot of beans
 
24 standard interview puzzles - How strog is an egg
24 standard interview puzzles - How strog is an egg24 standard interview puzzles - How strog is an egg
24 standard interview puzzles - How strog is an egg
 
24 standard interview puzzles - 4 men in hats
24 standard interview puzzles - 4 men in hats24 standard interview puzzles - 4 men in hats
24 standard interview puzzles - 4 men in hats
 
Aptitude Training - TIME AND DISTANCE 3
Aptitude Training - TIME AND DISTANCE 3Aptitude Training - TIME AND DISTANCE 3
Aptitude Training - TIME AND DISTANCE 3
 
Aptitude Training - PIPES AND CISTERN
Aptitude Training - PIPES AND CISTERNAptitude Training - PIPES AND CISTERN
Aptitude Training - PIPES AND CISTERN
 
Aptitude Training - PROFIT AND LOSS
Aptitude Training - PROFIT AND LOSSAptitude Training - PROFIT AND LOSS
Aptitude Training - PROFIT AND LOSS
 
Aptitude Training - SOLID GEOMETRY 1
Aptitude Training - SOLID GEOMETRY 1Aptitude Training - SOLID GEOMETRY 1
Aptitude Training - SOLID GEOMETRY 1
 
Aptitude Training - SIMPLE AND COMPOUND INTEREST
Aptitude Training - SIMPLE AND COMPOUND INTERESTAptitude Training - SIMPLE AND COMPOUND INTEREST
Aptitude Training - SIMPLE AND COMPOUND INTEREST
 
Aptitude Training - TIME AND DISTANCE 4
Aptitude Training - TIME AND DISTANCE 4Aptitude Training - TIME AND DISTANCE 4
Aptitude Training - TIME AND DISTANCE 4
 
Aptitude Training - TIME AND DISTANCE 1
Aptitude Training - TIME AND DISTANCE 1Aptitude Training - TIME AND DISTANCE 1
Aptitude Training - TIME AND DISTANCE 1
 
Aptitude Training - PROBLEMS ON CUBES
Aptitude Training - PROBLEMS ON CUBESAptitude Training - PROBLEMS ON CUBES
Aptitude Training - PROBLEMS ON CUBES
 
Aptitude Training - RATIO AND PROPORTION 1
Aptitude Training - RATIO AND PROPORTION 1Aptitude Training - RATIO AND PROPORTION 1
Aptitude Training - RATIO AND PROPORTION 1
 
Aptitude Training - PROBABILITY
Aptitude Training - PROBABILITYAptitude Training - PROBABILITY
Aptitude Training - PROBABILITY
 
Aptitude Training - RATIO AND PROPORTION 4
Aptitude Training - RATIO AND PROPORTION 4Aptitude Training - RATIO AND PROPORTION 4
Aptitude Training - RATIO AND PROPORTION 4
 
Aptitude Training - NUMBERS
Aptitude Training - NUMBERSAptitude Training - NUMBERS
Aptitude Training - NUMBERS
 
Aptitude Training - RATIO AND PROPORTION 2
Aptitude Training - RATIO AND PROPORTION 2Aptitude Training - RATIO AND PROPORTION 2
Aptitude Training - RATIO AND PROPORTION 2
 
Aptitude Training - PERMUTATIONS AND COMBINATIONS 2
Aptitude Training - PERMUTATIONS AND COMBINATIONS 2Aptitude Training - PERMUTATIONS AND COMBINATIONS 2
Aptitude Training - PERMUTATIONS AND COMBINATIONS 2
 
Aptitude Training - PERCENTAGE 2
Aptitude Training - PERCENTAGE 2Aptitude Training - PERCENTAGE 2
Aptitude Training - PERCENTAGE 2
 
Aptitude Training - PERCENTAGE 1
Aptitude Training - PERCENTAGE 1Aptitude Training - PERCENTAGE 1
Aptitude Training - PERCENTAGE 1
 

Recently uploaded

Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 

Recently uploaded (20)

Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 

CONST-- once initialised, no one can change it

  • 1. Reserved Words – I 1. Const : Constant, it is something fixed and can't be changed later. In programming languages we used the word const to make the variables constant, so that it's value can't be changed later. In C++, we use this word with variables, pointers, functions and its return types, classes members and objects. ➢ With variables : If the variables are declared using const keyword as prefix, they are called as Constant variables. For example : const int x ; • The constant variables are initialised at the time of delcaration only. const int x = 1; • we can also rewrite const int x = 1; as int const x = 1; Look at this: #include <iostream> using namespace std; int main() { Also See more posts : www.comsciguide.blogspot.com
  • 2. const int x; // declaration x = 1; // initialization cout<<x; return 0; } The above program produces an error, as the const variable x is not initilaised at the time of declaration. Declaration and Initialization can't be done seperately. int main() { const int x = 1; x++; return 0; } It results in compilation error as the const variables values can't be changed later. ➢ With Pointers : In this section, there are two types. 1. Pointers to const 2. const pointers ➔Pointers to const : Also See more posts : www.comsciguide.blogspot.com
  • 3. Generally we called this as Pointers to constant variables. Here, pointers point to constant data types like int, float, char etc. We can change pointer to point to any other integer variable, but we cannot change the value of object(entity) pointed using pointer. Pointer is stored in read-write area (stack in present case). Object pointed may be in read only or read write area. For example : int x = 10; ( or ) int x = 10; const int *p = &x; int const *p = &x; Here x is a const integer and p is a pointer variable pointed to const int x.  Ofcoure u can change x directly but, If u perform to change the value of x using p (pointer), it gives an error. Let's take with an example : #include<iostream> using namespace std; int main() { int x = 10; int const *p = &x; x=11; x++; cout<<x; Also See more posts : www.comsciguide.blogspot.com
  • 4. return 0; } Output : 12 But #include<iostream> using namespace std; int main() { int x = 10; int const *p = &x; (*p)++; cout<<x; return 0; } It gives an error as the pointer is assigned to read only location.  We can change pointer to point to any other integer variable int x = 10; int y = 11; int const *p = &x; *p = &y; // pointer is assigned to another variable  The non-const pointers can't point to const data types. Example : const int x=1; Also See more posts : www.comsciguide.blogspot.com
  • 5. int *p = &x; // non-const pointer can't assigned to const int It results in compilation error. ➔const pointers to variables: If pointers are declared const as prefix, they are called as const pointers. We can't change the pointer to point to another variable, but can change the value that it points to. For example: int x = 10; int * const a = &x; Here, a is the pointer which is const that points to int x. Now we can't change the pointer, but can change the value that it points to. Take a look with an example : #include<iostream> using namespace std; int main() { int x = 10; int y = 11; int *const p = &x; Also See more posts : www.comsciguide.blogspot.com
  • 6. (*p) = 12; // valid *p = & y; // error return 0; } It results in error as the pointer points to, can't be changed. But we can change the value that it points to. Note : “const with pointers” - In a nutshell char c; char *const cp = &c; cp is a pointer to a char. The const means that cp is not to be modified, although whatever it points to can be--the pointer is constant, not the thing that it points to. The other way is const char *cp; which means that now cp is an ordinary, modifiable pointer, but the thing that it points to must not be modified. So, depending on what you choose to do, both the pointer and the thing it points to, may be modifiable. ➔Const pointers to const variables : Here, We can't change pointer to point to any other integer variable and also, the value of object (entity) it pointed. Also See more posts : www.comsciguide.blogspot.com
  • 7. Lets see with an example: #include<iostream> using namespace std; int main() { int x = 10; int y = 11; const int *const p = &x; (*p) = 12; // error p = &y; // error return 0; } ➢ With Functions arguments and its Return type: The const keyword in functions arguments is similiar to that of const with variables Example : void fun (const int x) {} By doing this, u can't modify the x value inside the function.  int const f() is equivilent to const int f(), which means that return type is const. void fun(const int x) // example for fun arg Also See more posts : www.comsciguide.blogspot.com
  • 8. { x++; // error } const int fun() // example for return type { return 1; //any constant value }  If a function has a non-const parameter, then we cannot make a const argument call i.e. we can't pass the const argument at the function call. Example : #include<iostream> using namespace std; void fun(int *p) // non-const ar { cout<< *p; } int main() { const int x=10; // const parameter fun(&x); // error return 0; } Here, we are passing the const argument to the non – const parameters. So it results in error. But a, function with a const parameter, can make a const argument call as well as non- const argument at the function call. Also See more posts : www.comsciguide.blogspot.com
  • 9. Example : #include<iostream> using namespace std; void fun(const int *p) // non-const parameter { cout<< *p; } int main() { const int x = 10; fun(&x); // const agrument int y = 11; fun(&y); // non const argument return 0; } 4. With classes and its members : • const class data members These are data variables in class, which are made const. They are not initialized during declaration. ButTheir initialization occur in the constructor. For Example: #include<iostream> using namespace std; class cls { int const x; Also See more posts : www.comsciguide.blogspot.com
  • 10. public: cls () : x(1) // constructor { } void disp() { cout<< x; } }; int main() { cls a; a.disp(); return 0; output : 1 } Notice the syntax : x(1) after the constructor. This tells C++ to initialize the variable y to have value 1. More generally, you can use this syntax to initialize data members of the class. • const member funcions & objects : 1. The member functions of class are declared constant as follows : return_type fun_name () const ; This makes the function itself as constant. 2. The objects of class can be declared constant as follows : const class_name obj_name ; Also See more posts : www.comsciguide.blogspot.com
  • 11. Making a member function const means that it cannot change any member variable inside the function. It also means that the function can be called via a const object of the class. Look at the example below : #include<iostream> using namespace std; class A { public: void Const_No() {} // nonconst member function void Const_Yes() const {} // const member function }; int main() { A obj_nonconst; // nonconst object obj_nonconst.Const_No(); // works fine obj_nonconst.Const_Yes(); // works fine const A obj_const ; // const object obj_const.Const_Yes(); // const object can call const function obj_const.Const_No(); // ERROR-- const object cannot call nonconst function } A const member function can be used with both objects (const and Also See more posts : www.comsciguide.blogspot.com
  • 12. non-const) while, A non - const function can be used only with non – const objects, but not wtih const objects. Try it urself : #include<iostream> using namespace std; class cls { public: int x; cls(int i) // constructor initialization { x = i; } void f() const // constant function { x++; } void g() // non-constant function { x++; } }; int main() { const cls a(20); // constant object call cls b(30); // non-constant object call a.f(); a.g(); b.f(); Also See more posts : www.comsciguide.blogspot.com
  • 13. b.g(); cout<<a.x; cout<<b.x; return 0; } Also See more posts : www.comsciguide.blogspot.com