0% found this document useful (0 votes)
8 views

Access Specifiers, Encapsulation

Uploaded by

Sadeed Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Access Specifiers, Encapsulation

Uploaded by

Sadeed Ahmed
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Introduction To

Programming
Content
• Access Specifiers
• Encapsulation
Access Specifiers

• Access specifiers define how the members (attributes and methods)


of a class can be accessed.
• In C++, there are three access specifiers:
a. public - members are accessible from outside the class
b. private - members cannot be accessed (or viewed) from outside
the class
c. protected - members cannot be accessed from outside the class,
however, they can be accessed in inherited classes.
Access Specifiers...
We can summarize the different access types according to - who can access them in the following way −

Access public protected private

Same class yes yes yes

Derived classes yes yes no

Outside classes yes no no


Access Specifiers...
class MyClass { class MyClass {
public: // Public access specifier
int a; // Public attribute int a; // Private attribute
private: // Private access specifier
int b; // Private attribute int b; // Private attribute
}; };

int main() { int main() {


MyClass Obj; MyClass Obj;
Obj.a = 10; // Allowed (public) Obj.a = 10; // Not Allowed (private)
Obj.b = 20; // Not allowed (private) Obj.b = 20; // Not allowed (private)
cout<<Obj.a; cout<<Obj.a;
cout<<Obj.b; cout<<Obj.b;
return 0; return 0;
} }
Encapsulation

• The meaning of Encapsulation, is to make sure that "sensitive" data is


hidden from users. To achieve this, you must declare class
variables/attributes as private (cannot be accessed from outside
the class). If you want others to read or modify the value of a private
member, you can provide public get and set methods.
Encapsulation...
class Student { int main() {
private: Student obj1;
float cgpa; Student obj2;
obj1.set(3.55);
public: obj2.set(3.25);
void set(float c) //setter cout << obj1.get()<<endl;
{ cout << obj2.get();
cgpa = c; return 0;
} }
float get() //getter Inside main(), we create 2 objects of the class.
{ Now we can use the set() method to set the
return cgpa; value of the private attribute 3.55, 3.25. Then we
} call the get() method on the object to return
}; the values.
Encapsulation...
class Student {
int main() {
private:
Student obj1;
float cgpa;
Student obj2;
int salary;
obj1.setCgpa(3.55);
public:
obj2.setCgpa(3.25);
void setCgpa(float c) {
obj1.setSalary(30000);
cgpa = c; }
obj2.setSalary(50000);
void setSalary(float s) {
cout << obj1.getCgpa()<<endl;
salary = s; }
cout << obj2.getCgpa()<<endl;
float getCgpa() {
cout << obj1.getSalary()<<endl;
return cgpa; }
cout << obj2.getSalary();
float getSalary() {
return 0;
return salary; }
}
};

You might also like