Instance Variables in C++ Programming
OOPs in C++
In this lesson, we will understand what is Instance Variable in C++ Programming and how to create them along with some examples.
What are Instance Variables in C++?
Instance Variables are declared inside a class and are used to store values in an object. Each object has its own copy of instance variables that are not shared between other objects.
Example of creating Instance Variables
#include <iostream>
using namespace std;
class data
{
// creating instance variables
public:
char name[30];
int age;
};
int main()
{
// Creating an object x of the class data
data x;
return 0;
}
In the above example, we created two instance variables: name and age inside the class data. After that, we created an object x of the class data inside the main function.
Note: We have declared the instance variable using the keyword public, which means that we can access these variables from outside of its class using the object of the class.
Access and Modify Values of Instance Variables
We can access the value of an instance variable by using the object of the class followed by a dot (.) operator and then writing the name of the instance variable whose value we want to access.
We can modify the value of an instance variable by using the object of the class followed by a dot (.) operator and then writing the name of the instance variable with an equal sign, and then providing the new value to it.
Example
#include <iostream>
#include <string.h>
using namespace std;
class data
{
// creating instance variables
public:
char name[30];
int age;
};
int main()
{
// Creating an object x of the class data
data x;
// Modify the values of instance variables
strcpy(x.name,"Peter");
x.age=15;
// Access the values of instance variables
cout<<"Name: "<<x.name<<endl;
cout<<"Age: "<<x.age;
return 0;
}
Output
Name: Peter Age: 15
Suppose we remove the keyword public while declaring instance variables. In that case, the variables become private variables, meaning they can't be accessed from outside of their class using the object of the class.
Example
#include <iostream>
#include <string.h>
using namespace std;
class data
{
// creating instance variables
char name[30];
int age;
};
int main()
{
// Creating an object x of the class data
data x;
// Modify the values of instance variables
strcpy(x.name,"Peter");
x.age=15;
// Access the values of instance variables
cout<<"Name: "<<x.name<<endl;
cout<<"Age: "<<x.age;
return 0;
}
Running the above program after removing the public keyword will give compilation errors.
error: 'char data::name [30]' is private error: 'int data::age' is private
Note: By default, instance variables are private if not declared with the keyword public.
Initialized Instance Variables using Constructor
We can't initialize instance variables directly at the time of their declaration. To initialize instance variables of a class, we use a method called Constructor.
A Constructor is a unique method whose name is the same as the name of the class inside which it is declared. Inside this method, we initialized the instance variables of the class.
There are two types of constructors and they are:
- Default Constructor
- Parameterized Constructor
Example
#include <iostream>
#include <string.h>
using namespace std;
class data
{
// creating instance variables
public:
char name[30];
int age;
// Default Constructor
data()
{
strcpy(name, "Thomas");
age=18;
}
// Parameterized Constrcutor
data(char nm[], int ag)
{
strcpy(name, nm);
age=ag;
}
};
int main()
{
// Creating an object x and y of the class data
data x;
data y("William", 14);
// Access the values of instance variables of object x
cout<<"Name: "<<x.name<<endl;
cout<<"Age: "<<x.age<<endl;
// Access the values of instance variables of object y
cout<<"Name: "<<y.name<<endl;
cout<<"Age: "<<y.age;
return 0;
}
Output
Name: Thomas Age: 18 Name: William Age: 14
Default Constructor does not take any parameter to initialize the instance variables. Parameterized Constructor takes arguments to initialize the instance variables during object creation in the main function.
In the above example, when declaring the object y, we pass values to its parametrized constructor to initialize the instance variables of the object y.
Scope Resolution Operator
Scope Resolution Operator is written as two colons :: and is used to access the instance variables and instance methods using their class name in different situations. We will discuss all the possible uses of the scope resolution operator with examples in our subsequent lessons.
In this lesson, we will see how to use the scope resolution operator to differentiate between the instance variable and the local variable having the same name.
Example
#include <iostream>
#include <string.h>
using namespace std;
class data
{
// creating instance variables
public:
char name[30];
int age;
// Default Constructor
data()
{
strcpy(name, "Thomas");
age=18;
}
// Parameterized Constrcutor
data(char name[], int age)
{
// :: operator is used to differentiate between instance variable and formal argument
strcpy(data::name, name);
data::age=age;
}
};
int main()
{
// Creating an object x and y of the class data
data x;
data y("William", 14);
// Access the values of instance variables of object x
cout<<"Name: "<<x.name<<endl;
cout<<"Age: "<<x.age<<endl;
// Access the values of instance variables of object y
cout<<"Name: "<<y.name<<endl;
cout<<"Age: "<<y.age;
return 0;
}
Output
Name: Thomas Age: 18 Name: William Age: 14
In the above example, the instance variables' names and the formal arguments' names of the parameterized constructor are the same. We have used the scope resolution operator in this condition to distinguish between variables.