Table of contents
Hey friends, welcome to brand new Blog # 7 of the oop series in c++. In this blog, we will learn about access specifiers, constructors, destructors, and a little more. Let’s start the fun.
Access Specifier:
Access specifiers define how we can access class members (function) and attributes(variables). Access specifier and access modifier are both the same. Access specifiers implement the concept of Encapsulation (a process of wrapping code and data together into a single unit).
There are three types of access specifiers used in C++.
Public: The variables and member functions declared as public can be accessed by other classes and functions too.
Example:
#include <iostream>
using namespace std;
class Student {
int roll_num;
float CGPA;
public:
void set_data(){
roll_num = 0;
CGPA = 0.0;
}
};
int main(){
Student s1;
cout<< s1.set_data()<<endl;
return 0;
}
Private: The class members declared as private can only be accessed by the functions inside the class. They are not allowed to be accessed by any object or any function outside the class. Only the class’s member functions are allowed to access the private members of a class.
#include <iostream>
using namespace std;
class Student {
private:
int roll_num;
float CGPA;
public:
void set_data( int r){
roll_num = r;
}
};
int main(){
Student s1;
s1.roll_num = 23; // throw error
return 0;
}
Explanation:
This code will throw an error because we are trying to access private data outside the class. Private members are only accessed by accessor functions(member function of class).
#include <iostream>
using namespace std;
class Student {
int roll_num;
float CGPA;
public:
void set_data(){
roll_num = 0;
CGPA = 0.0;
}
};
int main(){
Student s1;
cout<< s1.set_data(23)<<endl;
return 0;
}
Protected: Members declared as protected cannot be accessed outside the class but can be accessed by derived class (subclass) of that class. We will learn more about inheritance.
Constructor:
A constructor is a special member function that is used to initialize an object of a class. To create a constructor, use the same name as the class, followed by parentheses ().
It is called special for several reasons.
- It has the same name as the class.
- Do not have a return type.
- Declared under public access specifier.
- A constructor is automatically called when an object is created.
class Student {
Student() { // Constructor
cout << "I am a new student here!";
}
};
int main() {
Student s1; // Create an object of Student (this will call the constructor)
return 0;
}
Output:
I am a new student here!
Destructor:
A destructor is used to free the space allotted during dynamic allocation. It is the same name as a class but with a tilde (~).
class Student {
Student() { // Constructor
cout << "I am a constructor!";
}
~Student() { // Constructor
cout << "I am a destructor”;
}
};
int main() {
Student s1; // Create an object of Student (this will call the constructor)
return 0;
}
Output:
I am a constructor.
I am a destructor.
So, friends, this is all about access specifiers, and a little introduction to constructors and destructors. ✌In upcoming blogs, we will explore constructors in more detail. 😊 Till then keep learning and keep exploring!🎉