Instance variable in C++
In C++, what are instance variables?
The word "instance variable" is not a standard or widely used term in C++. However, it appears that you are referring to what are commonly known as "member variables" or "data members."
Variables declared within a class represent the attributes or properties of objects produced from that class. These variables are also known as instance-specific variables because each object (instance) of the class has its own collection of them. Here's an illustration:
class MyClass {
public:
int age; // Member variable representing age
std::string name; // Member variable representing name
};
Age and name are member variables in the MyClass class described above. When you construct MyClass class objects, each object will have its own age and name variables. You can access and edit these member variables individually for each object.
MyClass obj1; // Creating an object of MyClass
obj1.age = 25;
obj1.name = "John";
MyClass obj2; // Creating another object of MyClass
obj2.age = 30;
obj2.name = "Alice";
How to access instance variable?
In C++, you must use the dot (.) operator followed by the name of the variable and the name of the object to which you want to access the variable. The syntax is as follows:
objectName.variableName
Here's how to retrieve an instance variable step by step:
1.Make a Class: To begin, create a class that contains the instance variable you want to use. Within the class, the instance variable should be declared.
class MyClass {
public:
int myVariable; // Instance variable
};
2.Make an Object: Create an object of the class. This object will have an instance variable of its own.
MyClass obj; // Creating an object of the class
3.Access the Instance Variable: In order to access the instance variable, use the dot operator followed by the variable name and the object name.
int value = obj.myVariable; // Accessing the instance variable
In the preceding code, we access the obj object's myVariable instance variable and store its value in the value variable.
Here's a whole example:
Program 1
#include <iostream>
class MyClass {
public:
int myVariable;
};
int main() {
MyClass obj;
obj.myVariable = 42;
std::cout << "Value of myVariable: " << obj.myVariable << std::endl;
return 0;
}
Output:
![Instance variable in C++](https://static.tutorialandexample.com/cpp/instance-variable-in-cpp1.png)
In this example, we construct an object obj of the MyClass class and use obj.myVariable to retrieve the myVariable instance variable.
Within class methods or functions in C++, you can access and edit the values of instance variables (also known as member variables or data members). Here's how to go about it:
Program 2
#include <iostream>
#include <string>
class MyClass {
public:
int age;
std::string name;
MyClass(int a, const std::string& n) : age(a), name(n) {
}
void displayInfo() {
std::cout << "Name: " << name << ", Age: " << age << std::endl;
}
void updateInfo(int newAge, const std::string& newName) {
age = newAge;
name = newName;
}
};
int main() {
MyClass obj(25, "John");
obj.displayInfo();
obj.updateInfo(30, "Alice");
obj.displayInfo();
return 0;
}
Output
![Instance variable in C++](https://static.tutorialandexample.com/cpp/instance-variable-in-cpp2.png)
In the above example:
- We've created the class MyClass, which has the member variables age and name.
- When an object is formed, we use a constructor to initialise these member values.
- The displayInfo member function is used to display the instance variables' current values.
- The updateInfo member function is used to change the instance variables' values.
Within class methods, you can access and edit instance variables by using the dot (.) operator followed by the variable name. In the main function, we create a class object and then call the updateInfo function to change the age and name variables.
Advantages
1. Object State: They keep track of the state or properties of certain objects.
2. Encapsulation: They can be enclosed in order to regulate access and ensure data integrity.
3. Modularity: They encourage modularity by clustering similar data within objects.
4. Object-Oriented Concepts: They support essential object-oriented programming principles.
5. Reusability: By establishing classes with reusable properties, they enable code reuse.
6. Data Hiding: They conceal implementation details, which improves code maintainability.
7. Maintenance and Debugging: They make code more maintainable and debuggable.
8. Scalability: They allow many instances of a class to be created as a programme grows.
Disadvantages
1. Issues with data integrity if not adequately wrapped.
2. Memory overhead associated with storing variables in each object.
3. Controlling access may result in unforeseen consequences.
4. Growing complexity as the number of variables increases.
5. Careful setup is required to avoid unexpected behaviour.
6. Issues with inheritance and class hierarchies.
7. There is a minor performance hit while accessing variables.
8. Tight coupling between code areas that directly access variables.