Professional Documents
Culture Documents
Oop Con
Oop Con
If you want others to read or modify the value of a private member, you
can provide public getter and setter methods.
#include <iostream>
class Employee {
int salary;
public:
salary = s;
}
int getSalary() // Getter{
return salary;
};
int main() {
Employee myObj;
myObj.setSalary(50000);
return 0;
Example explained
The salary attribute is private, which have restricted access.
The public setSalary() method takes a parameter (s) and assigns it to the salary
attribute (salary = s).
The public getSalary() method returns the value of the private salary attribute.
Inside main(), we create an object of the Employee class. Now we can use the
setSalary() method to set the value of the private attribute to 50000. Then we call
the getSalary() method on the object to return the value.
Abstraction means displaying only essential information and hiding the details.
Data abstraction refers to providing only essential information about the data to
the outside world, hiding the background details or implementation
Constructor
A constructor is a member function of a class which initializes objects of a class. In
C++, Constructor is automatically called when object(instance of class) create. It is
special member function of the class.
class MyClass { // The class
public: // Access specifier
MyClass() { // Constructor
cout << "Hello World!";
}
};
int main() {
MyClass myObj; // Create an object of MyClass (this will call the constructor)
return 0;
}
______________________________________
#include <iostream>
using namespace std;
class construct {
public:
int a, b;
// Default Constructor
construct()
{
a = 10;
b = 20;
}
};
int main()
{
// Default constructor called automatically
// when the object is created
construct c;
cout << "a: " << c.a << endl
<< "b: " << c.b;
return 1;
}