CSE 2001 - Object Oriented Programming
with C++
Unit I: Variables
VIT BHOPAL UNIVERSITY
1
C++ using OOP
Variables in C++
• A variable is a name given to a memory location. It is the basic
unit of storage in a program.
• The value stored in a variable can be changed during program
execution.
• A variable is only a name given to a memory location, all the
operations done on the variable effects that memory location.
• In C++, all the variables must be declared before use.
• The variable declaration refers to the part where a variable is
first declared or introduced before its first use.
• A variable definition is a part where the variable is assigned a
memory location and a value. Most of the times, variable
declaration and definition are done together.
• In addition, all variables have a scope, which defines their
visibility, and a lifetime. 2
C++ using OOP
Types of Variables in C++
• There are Four types of variables based on the scope of
variables in C++:
– Local Variables
– Global Variables
– Instance Variables
– Static Variables
• Local Variables: A variable defined within a block or method
or constructor is called local variable.
• The scope of these variables exists only within the block in
which the variable is declared. i.e. we can access these
variable only within that block.
• Initialisation of Local Variable is Mandatory.
• A local variable cannot be defined with "static" keyword. 3
Local Varibale Example
#include <iostream>
using namespace std;
void test();
int main()
{ // local variable to main()
int var = 5;
test();
// illegal: var1 not declared inside main()
var1 = 9;
}
void test()
{
// local variable to test()
int var1;
var1 = 6;
// illegal: var not declared inside test()
cout << var;
}
4
C++ using OOP
Global Variables:
• The variables that are declared outside a function are called
global variables.
• The global variable is visible inside all the functions that are
defined after its declaration. That means the global variables can
be accessed by all the functions that are created after the global
variable declaration.
• The functions that have created before the global variable
declaration can't access it.
5
C++ using OOP
#include <iostream>
using namespace std;
int a = 10;
// Global variable
int main()
{
int b = 20;
// Local variable
void show();
cout << "Inside the main!!!" << endl;
cout << "a = " << a << endl;
show();
return 0;
}
void show()
{
int a = 30;
// Local variable with same name as global
cout << "Inside the show!!" << endl;
cout << "a = " << a << endl;
// refer to local variable
cout << "a = " << : :a << endl;
// refer to global variable
} 6
C++ using OOP
• Instance Variables: Instance variables are non-static variables
and are declared in a class outside any method, constructor or
block.
• As instance variables are declared in a class, these variables
are created when an object of the class is created and
destroyed when the object is destroyed.
• Unlike local variables, we may use access specifiers for
instance variables.
• If we do not specify any access specifiers then the default
access specifier will be used.
• Initialisation of Instance Variable is not Mandatory.
• Instance Variable can be accessed only by creating objects.
7
Instance Varibale Example
#include<iostream>
using namespace std;
class Student
{
public:
int rollno;
string name;
float age;
void setData(int r, string n, float a)
{
rollno=r;
name=n;
age=a;
}
void getData()
{
cout<<"Roll no="<<rollno<<"\nS_name="<<name<<"\nage="<<age;
}
};
int main()
{
int rollnum;
string name;
float age;
Student s;
cout<<"Enter the Roll number, name, and age:";
cin>>rollnum>>name>>age;
[Link](rollnum,name,age);
[Link]();
return 0;
}
8
C++ using OOP
• Static Variables: Static variables are also known as Class
variables.
• These variables are declared similarly as instance variables,
the difference is that static variables are declared using
the static keyword within a class outside any method
constructor or block.
• Unlike instance variables, we can only have one copy of a
static variable per class irrespective of how many objects we
create.
• Static variables are created at the start of program execution
and destroyed automatically when execution ends.
• Initialization of Static Variable is not Mandatory.
• Its default value is 0
9
Static Variable/member
class Counter
{
private:
//static data member as count
static int count;
public:
//default constructor
Counter()
{ count++; }
void Print()
{ cout<<"\nTotal objects are: "<<count; }
};
//count initialization with 0
int Counter :: count = 0;
int main()
{
Counter OB1;
[Link]();
Counter OB2;
[Link]();
Counter OB3;
[Link]();
return 0; }
10
Static Variable/member
11
Static Member Function
• Like a static member variable we can also have static
member function. A member function declared with
static keyword is called as static member function.
static member function has following characteristics:
[Link] function can have access to only other static
members/functions declared in same class
[Link] member function can be called using class
name(instead of object) as
Class_name:: function_name;
12
Static Member Function
class Counter
{
private:
//static data member as count
static int count;
public:
//default constructor
Counter()
{ count++; }
//static member function
static void Print()
{ cout<<"\nTotal objects are: "<<count; }
};
//count initialization with 0
int Counter :: count = 0;
int main()
{
Counter OB1;
[Link]();
Counter OB2;
[Link]();
Counter OB3;
[Link]();
return 0; } 13