What is a Reference variable? Explain with example
Explain Symbolic Constants with example.
Short note on Declaration & Dynamic initialization of variables
1. Prepared By: Asst. Prof. Sejal Jadav
Unit-1
Principles of Object Oriented Programming Tokens,
expressions & Control Statements
(14 MARKS)
B.C.A & B.Sc.(IT) – 3
CS-13 C++ and Object Oriented
Programming
Prepared By: Asst. Prof. Sejal Jadav
2. Prepared By: Asst. Prof. Sejal Jadav
Explain Symbolic Constants with example.
• # define <variable_nm> <constant_value>
• Above syntax is used in C. using # define directive,
constants have no type information.
• Instead, in C++ using ‘const’ qualifier, the constants
can be defined with their data types.
3. Prepared By: Asst. Prof. Sejal Jadav
• Using the qualifier ‘const’
Syntax
• const <datatype> <variable_name> = <constant_value>;
Example
• const float pi = 3.14;
• Even though the datatype part is optional.
• If datatype is not specified the default datatype is
integer.
4. Prepared By: Asst. Prof. Sejal Jadav
Example
• const int size = 10;
• above statement is identical to const size = 10;
• C++ requires a const to be initialized immediately
when it is declared otherwise it will generate an
error.
• const int size; //error
• size = 10; //not valid
5. Prepared By: Asst. Prof. Sejal Jadav
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
const int size=10;
char name[size];
cout<<endl<<"enter your name: ";
cin>>name;
cout<<name;
//size=15;// error cannot modify a const object
}
6. Prepared By: Asst. Prof. Sejal Jadav
Defining a set of integer constants using ‘enum’ keyword.
• An enumerated data type is another user defined type
which provides a way for attaching names to
numbers.
• They are named integers.
• Can be defined much like structures.
7. Prepared By: Asst. Prof. Sejal Jadav
Syntax
enum [<type_tag>]
{
<constant_name> = [<value>]
…..
} [variable_list…];
• Here type tag and variable list is also optional.
8. Prepared By: Asst. Prof. Sejal Jadav
Example
enum color
{
red,
blue,
green=10,
yellow,
black=50,}e1,e2;
9. Prepared By: Asst. Prof. Sejal Jadav
• Here type tag and variable list is also optional.
• The enum keyword automatically enumerates a list of
words by assigning them values 0,1,2 and so on.
• We can also change the values by assigning words
with constant values.
• Provides an alternative way for creating symbolic
constants.
10. Prepared By: Asst. Prof. Sejal Jadav
enum color
{
red,
blue,
green=10,
yellow,
black=50,}
Here values are as under
red = 0
blue = 1
green = 10
yellow = 11
black = 50
11. Prepared By: Asst. Prof. Sejal Jadav
• when we assign green=10 then the subsequent word
yellow is automatically assigns with the value 11.
• Note that the subsequent initialized enumerators are
larger by one than their predecessors.
• Let’s see examples…….
12. Prepared By: Asst. Prof. Sejal Jadav
Short note on Declaration & Dynamic
initialization of variables
• Variable are used in C++, where we need storage for
any value, which will change in program.
• Variable can be declared in multiple ways each with
different memory requirements and functioning.
• Variable is the name of memory location allocated by
the compiler depending upon the datatype of the
variable.
14. Prepared By: Asst. Prof. Sejal Jadav
Declaration and Initialization
• Variable must be declared before they are used.
Usually it is preferred to declare them at the starting
of the program, but in C++ they can be declared in the
middle of program too, but must be done before using
them.
15. Prepared By: Asst. Prof. Sejal Jadav
For example:
• int i; // declared but not initialised
• char c;
• int i, j, k; // Multiple declaration
16. Prepared By: Asst. Prof. Sejal Jadav
• Initialization means assigning value to an already declared
variable,
int i; // declaration
i = 10; // initialization
• Initialization and declaration can be done in one single step
also,
int i=10; //initialization and declaration in same step
int i=10, j=11;
17. Prepared By: Asst. Prof. Sejal Jadav
• If a variable is declared and not initialized by default it
will hold a garbage value.
• Also, if a variable is once declared and if try to declare it
again, we will get a compile time error.
int i,j;
i=10;
j=20;
int j=i+j; //compile time error, cannot redeclare a
variable in same scope
19. Prepared By: Asst. Prof. Sejal Jadav
What is Reference variable? Explain with example
• C++ introduced new kind of variable known as
reference variable.
• A reference variable provides an alias [alternative
name] for existing variable.
• Syntax
• data_type & refrence_name = variable_name
20. Prepared By: Asst. Prof. Sejal Jadav
Example:
float total = 100;
float &sum = total;
• Here sum is a reference to variable total.
• So sum and total can be used interchangeably to represent
that variable data.
• And therefore
cout << total; //100
cout << sum; //100
• Both the statements are identical.
21. Prepared By: Asst. Prof. Sejal Jadav
Consider following statements
total = total + 10;
cout << sum; //110
sum = 0;
cout << total; //0
22. Prepared By: Asst. Prof. Sejal Jadav
• Reference means Address.
• Reference variable is an internal pointer.
• A reference variable must be initialized at the time of
declaration.
23. Prepared By: Asst. Prof. Sejal Jadav
• C++ assigns additional meaning to & symbol. Here &
symbol is not the address operator but it is reference
operator
• It can be initialized with already declared variables
only.
• Reference variable cannot be updated.
24. Prepared By: Asst. Prof. Sejal Jadav
• Look at the following statement
char &nuln = ‘n’
• Unlike pointer, this will create reference to the
unknown location where the new line constant n is
stored.
• Let’s see example…………..