0% found this document useful (0 votes)
45 views9 pages

An Autonomous Institution: Course Name: Object Oriented Programming

The document discusses variables and constants in C++, defining variables as containers that hold data and constants as variables that are declared with const to make their values unchangeable, explaining that variables can be local or global depending on where they are declared and the rules for naming variables and the different types of variables.

Uploaded by

Karthik Sara M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views9 pages

An Autonomous Institution: Course Name: Object Oriented Programming

The document discusses variables and constants in C++, defining variables as containers that hold data and constants as variables that are declared with const to make their values unchangeable, explaining that variables can be local or global depending on where they are declared and the rules for naming variables and the different types of variables.

Uploaded by

Karthik Sara M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9

SNS COLLEGE OF TECHNOLOGY

Coimbatore-35.
An Autonomous Institution
Accredited by NBA – AICTE and Accredited by NAAC – UGC with ‘A+’ Grade
Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai

COURSE NAME : OBJECT ORIENTED PROGRAMMING

II YEAR/ III SEMESTER

UNIT – I INTRODUCTION

Topic: VARIABLES & CONSTANTS

Mr.M.Karthick
Assistant Professor
Department of Computer Science and Engineering
Variables

• In programming, a variable is a container (storage area) to hold data.


• To indicate the storage area, each variable should be given a unique name (identifier).
• The general syntax for declaring a variable is
data_type variable_name;
Ex: int age;
data_type variable_name = value;
Ex: int age = 14;
  age  variable
data type int
we have assigned an integer value 14 to it.

16CS451 - OOP/Unit-I/Variables & Constants /M.Karthick, AP/CSE


Rules for naming a variable

• A variable name can only have alphabets, numbers and the underscore _.

• A variable name cannot begin with a number.

• Variable names cannot begin with an uppercase character.

• A variable name cannot be a keyword. For example, int is a keyword that is used to denote
integers.

• A variable name can start with an underscore. However, it's not considered a good practice.

16CS451 - OOP/Unit-I/Variables & Constants /M.Karthick, AP/CSE


Types of Variables

Based on the location of variable declaration, variables are classified into two types. They are
as follows.
• Local Variables

• Global Variables

16CS451 - OOP/Unit-I/Variables & Constants /M.Karthick, AP/CSE


Local Variables #include <iostream>
int sum()
• The variables that are declared inside
a function or a block are called local {
variables. The local variable is visible int a = 10, b = 20;
only inside the function or block in
which it is declared. cout<< "Result = " << result;
return a + b;
}
int main()
{
int result;
result = sum();
cout << "Sum = " << result << endl;
cout << a << "+" << b << "=" << sum() << endl;
return 0;
}
16CS451 - OOP/Unit-I/Variables & Constants /M.Karthick, AP/CSE
Global Variables #include <iostream>
int a = 10, b = 20;
• The variables that are declared outside
a function are called global variables. int sum()
The global variable is visible inside {
all the functions that are defined after
its declaration. cout<< "Result = " << result;
return a + b;
}
int main()
{
int result;
result = sum();
cout << "Sum = " << result << endl;
cout << a << "+" << b << "=" << sum() << endl;
return 0;
}
16CS451 - OOP/Unit-I/Variables & Constants /M.Karthick, AP/CSE
C++ Constants

• When we do not want others to override existing variable values, we can use
the const keyword
• This will declare the variable as "constant", which means unchangeable and read-only
Example 1:
const int minutesPerHour = 60;
const float PI = 3.14;
Example 2:
const int myNum = 15;  // myNum will always be 15
myNum = 10; / / error: assignment of read-only variable 'myNum'

16CS451 - OOP/Unit-I/Variables & Constants /M.Karthick, AP/CSE


References
1. Robert Lafore, Object Oriented Programming in-C++, Galgotia Publication, 2009.
2. Deitel & Deitel, “C++ How to program”, Prentice Hall,2005.
3. D.S.Malik, “C++ Programming”, Thomson, 2007.
4. K.R.Venugopal, Rajkumar and T.Ravishankar, “Mastering C++”, Tata McGraw Hill
Publishing Co. Ltd., New Delhi, 2006.
5. E.Balagurusamy, “Object Oriented Programming with C++”, Sixth Edition, McGraw
Hill Education ,2013.

16CS451 - OOP/Unit-I/Variables & Constants /M.Karthick, AP/CSE


16CS451 - OOP/Unit-I/Variables & Constants /M.Karthick, AP/CSE

You might also like