0% found this document useful (0 votes)
25 views7 pages

Lab 4

The document discusses encapsulation in C# programming. It defines encapsulation and describes how it is implemented in C# using access modifiers like public, private, protected, internal and protected internal to control access to class members. It also provides tasks to write programs demonstrating the use of get/set properties, static methods, vehicle class with private fields and accessor/mutator methods, implementation of protected internal, and a student class with properties and methods.

Uploaded by

Saboor Khan
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)
25 views7 pages

Lab 4

The document discusses encapsulation in C# programming. It defines encapsulation and describes how it is implemented in C# using access modifiers like public, private, protected, internal and protected internal to control access to class members. It also provides tasks to write programs demonstrating the use of get/set properties, static methods, vehicle class with private fields and accessor/mutator methods, implementation of protected internal, and a student class with properties and methods.

Uploaded by

Saboor Khan
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/ 7

LAB #04

To study and controlling data members using access specifiers, static class members and ENCAPSULATION USING ACCESSORS AND
MUTATORS

Encapsulation

Encapsulation is defined 'as the process of enclosing one or more items within a physical or logical package'. Encapsulation, in object-oriented
programming methodology, prevents access to implementation details. Encapsulation is used to restrict access to the members of a class so as to
prevent the user of a given class from manipulating objects in ways that are not intended by the designer. While encapsulation hides the internal
implementation of the functionalities of class without affecting the overall functioning of the system, it allows the class to service a request for
functionality and add or modify its internal structure (data or methods) to suit changing requirements.

Encapsulation in C# is implemented with different levels of access to object data that can be specified access modifiers.

Access modifiers
Encapsulation in C# is implemented with different levels of access to object data that can be specified using the following access modifiers:

 Public: Access to all code in the program


 Private: Access to only members of the same class
 Protected: Access to members of same class and its derived classes
 Internal: Access to current assembly
 Protected Internal: Access to current assembly and types derived from containing class

Tasks

Task:

1. Write a program to explain get set properties.


Input:
Output:

2. Write a program to explain method in C#. Create a static function factorial() that accept a number from user and
returns factorial of the number.

Input:

Output:
3. Write a main method for your Vehicle class that creates a few vehicles and prints out their field values. Make the fields in
your Vehicle class private, and add accessor and mutator methods for the fields.

Input:
Output:

4. Give the implementation of “PROTECTED INTERNAL” in C#.


Input:
Output:

5. Create a class called Student that an institute might use to represent a record for students qualifying from the institute. A Student record
should include five pieces of information as either instance variables or auto-implemented properties: a student's id (type string), a student's
name (type string) and three separate variables for scores in three subjects (type decimal). Your class should have a constructor that
initializes the five values. Provide a property with a get and set accessor for any instance variables. For the scores in subjects, if the value
passed to the set accessor is negative, the value of the instance variable should be left unchanged. Also, provide methods named
GetAggregate and GetPercentage that calculate the aggregate marks in the three subjects (sum of three subject marks) and the percentage
(i.e., sum divided by the maximum marks, 50, and then multiplied by 100), and then return the aggregate and percentage as decimal value.
Write a test app named StudentRecordTest that demonstrates class Student’s capabilities.
Input:
Output:

You might also like