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

Lecture 14 Encapsulation

The document explains access specifiers in programming, specifically public, private, and protected variables. It illustrates the concept with examples of classes, such as Circle and Employee, demonstrating how private members are accessed through public functions. Additionally, it outlines a task for students to implement a Student class for managing student information and displaying a marksheet.

Uploaded by

Saleem Bhatti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture 14 Encapsulation

The document explains access specifiers in programming, specifically public, private, and protected variables. It illustrates the concept with examples of classes, such as Circle and Employee, demonstrating how private members are accessed through public functions. Additionally, it outlines a task for students to implement a Student class for managing student information and displaying a marksheet.

Uploaded by

Saleem Bhatti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Access Specifiers

Illustrate the concept of public, private and protected variables


Private:-Only visible inside a class, not accessible directly. Information hiding
Public:-Visible to the world, directly accessible.
Now how to access private member?
We use public functions to modify private members values. (Security)e.g.
GetModelNumber,
SetModelNumber.
Functions are public and data is private.
Example
class Circle
{
private:
intnRadius;
POINT ptCenter;
public:
voidSetRadius(int rad);
voidSetCenter(POINT pt);
voidSetCenter(int x, int y);
voidGetArea();
};
void Circle::SetRadius(int rad){
if(rad > 0)
nRadius = rad;
else
cout<<“Radius cannot be -ve ”;
}
void Circle:: SetCenter(POINT pt) { ptCenter = pt;}
void Circle:: SetCenter(int x, int y) {pt.x-cord = x; pt.y-cord = y;}
int Circle:: GetArea()
{
return PI*nRadius*nRadius;
}

Example 2: Students should implement this class himself


Example 2 Employee
Attributes:
- Name string
- Age int
- Salary double
- EmpCodeint
Behaviors:
+SetEmployCode(int c)
+ intGetEmployCode()

+SetAge(int age)
+ intGetAge()

+Get/SetName
+Get/SetSalary

Example 3.
Write a program in C++ to display marksheet of the student. Definea class Student that
contains data members to store student information like name,Branch,semester, marks in 6
different subjects,etc. Declare some member functions to get this information from the
keyboard, to calculate result and to display all gathered information on to the computer
screen in proper format.

You might also like