Experiment No. 02 - WordPress.com · 2017-03-08 · 2.3 Write a constructor overloading program in...
Transcript of Experiment No. 02 - WordPress.com · 2017-03-08 · 2.3 Write a constructor overloading program in...
Lab 02 – Familiarization of Modifiers, Methods, Class,
Constructor/Destructor, Constructor Overloading, Properties,
ref/out, Boxing/Unboxing
Compiled By: Sana Maqsood CS-321 | Visual Programming 1
Experiment No. 02 Familiarization of C#
1 THEORY
1.1 Access Modifier
Modifiers are used to modify declarations of types and type members. This section introduces the C#
modifiers.
1.1.1 Public
Access is not restricted.
Example 1:
1.1.2 Private
The private keyword is a member access modifier. Private access is the least permissive access level.
Private members are accessible only within the body of the class or the struct in which they are
declared, as in this example:
Lab 02 – Familiarization of Modifiers, Methods, Class,
Constructor/Destructor, Constructor Overloading, Properties,
ref/out, Boxing/Unboxing
Compiled By: Sana Maqsood CS-321 | Visual Programming 2
Example 2:
1.1.3 Protected
Access is limited to the containing class or types derived from the containing class.
Example 4:
Lab 02 – Familiarization of Modifiers, Methods, Class,
Constructor/Destructor, Constructor Overloading, Properties,
ref/out, Boxing/Unboxing
Compiled By: Sana Maqsood CS-321 | Visual Programming 3
1.2 Methods
A method is basically a group of statements. These statements work together to perform a task and
each C# program must at least have one class with a method named main. Now how exactly can
methods be used in a program. For using a method in a C# program, two steps need to be followed.
<Access Specifier> <Return Type> <Method Name> (Parameter List)
{
Method Body
}
////////////////////////////////////////////////
void myMethod()
{
// code here
//
}
///////////////////////////////////////////////
Program myProgram = new Program ();
myProgram.myMethod();
1.3 Classes
A class is a construct that enables you to create your own custom types by grouping together variables
of other types, methods and events. A class is like a blueprint. It defines the data and behavior of a type.
If the class is not declared as static, client code can use it by creating objects or instances which are
assigned to a variable.
A class definition starts with the keyword class followed by the class name; and the class body enclosed
by a pair of curly braces. Following is the general form of a class definition
<access specifier> class class_name
{
// member variables
Lab 02 – Familiarization of Modifiers, Methods, Class,
Constructor/Destructor, Constructor Overloading, Properties,
ref/out, Boxing/Unboxing
Compiled By: Sana Maqsood CS-321 | Visual Programming 4
<access specifier> <data type> variable1; <access specifier> <data type> variable2; ... <access specifier> <data type> variableN;
// member methods
<access specifier> <return type> method1(parameter_list) { // method body } <access specifier> <return type> method2(parameter_list) { // method body } ...
<access specifier> <return type> methodN(parameter_list) { // method body } }
The following example illustrates the concepts discussed so far regarding classes and member functions:
Lab 02 – Familiarization of Modifiers, Methods, Class,
Constructor/Destructor, Constructor Overloading, Properties,
ref/out, Boxing/Unboxing
Compiled By: Sana Maqsood CS-321 | Visual Programming 5
1.4 Member Functions and Encapsulation
A member function of a class is a function that has its definition or its prototype within the class
definition similar to any other variable. It operates on any object of the class of which it is a member,
and has access to all the members of a class for that object.
Member variables are the attributes of an object (from design perspective) and they are kept private to
implement encapsulation. These variables can only be accessed using the public member functions.
Lab 02 – Familiarization of Modifiers, Methods, Class,
Constructor/Destructor, Constructor Overloading, Properties,
ref/out, Boxing/Unboxing
Compiled By: Sana Maqsood CS-321 | Visual Programming 6
Example 2
1.5 C# Constructors A class constructor is a special member function of a class that is executed whenever we create new
objects of that class.
A constructor has exactly the same name as that of class and it does not have any return type. Following
example explains the concept of constructor:
Lab 02 – Familiarization of Modifiers, Methods, Class,
Constructor/Destructor, Constructor Overloading, Properties,
ref/out, Boxing/Unboxing
Compiled By: Sana Maqsood CS-321 | Visual Programming 7
A default constructor does not have any parameter but if you need, a constructor can have parameters.
Such constructors are called parameterized constructors. This technique helps you to assign initial value
to an object at the time of its creation.
1.6 Constructor Overloading
When we use more than one or multiple constructors inside the same class with different parameter list then these constructors are called overloaded and this situation is called constructors overloading. When
Lab 02 – Familiarization of Modifiers, Methods, Class,
Constructor/Destructor, Constructor Overloading, Properties,
ref/out, Boxing/Unboxing
Compiled By: Sana Maqsood CS-321 | Visual Programming 8
we use constructor overloading in C#, the C# implements polymorphism which is one of the principal/feature of an object oriented programming. Overload constructors make a class flexible for creating multiple objects.
In constructors overloading each constructor’s parameter list must be different from one another by their type, number or sequence of parameters.
Overload constructors allow us to create many different forms / shapes of a same type object. These objects will be same in type but totally different from one another’s. Let’s look into the example given below!
Lab 02 – Familiarization of Modifiers, Methods, Class,
Constructor/Destructor, Constructor Overloading, Properties,
ref/out, Boxing/Unboxing
Compiled By: Sana Maqsood CS-321 | Visual Programming 9
1.7 Destructors
A destructor is a special member function of a class that is executed whenever an object of its class goes
out of scope. A destructor has exactly the same name as that of the class with a prefixed tilde (~) and it
can neither return a value nor can it take any parameters. Destructor can be very useful for releasing
memory resources before exiting the program. Destructors cannot be inherited or overloaded.
Following example explains the concept of destructor:
Example 5:
Lab 02 – Familiarization of Modifiers, Methods, Class,
Constructor/Destructor, Constructor Overloading, Properties,
ref/out, Boxing/Unboxing
Compiled By: Sana Maqsood CS-321 | Visual Programming 10
1.8 Properties
A property is a member that provides a flexible mechanism to read, write, or compute the value of a
private field. Properties can be used as if they are public data members, but they are actually special
methods called accessors. This enables data to be accessed easily and still helps promote the safety and
flexibility of methods. The code block for the get accessor is executed when the property is read; the
code block for the set accessor is executed when the property is assigned a new value.
A property without a set accessor is considered read-only. A property without a get accessor is
considered write-only. A property that has both accessors is read-write. Here’s an example:
Lab 02 – Familiarization of Modifiers, Methods, Class,
Constructor/Destructor, Constructor Overloading, Properties,
ref/out, Boxing/Unboxing
Compiled By: Sana Maqsood CS-321 | Visual Programming 11
1.9 Static Members of a C# Class:
1.10 Reference Parameters (ref)
A reference parameter is a reference to a memory location of a variable. When you pass parameters by
reference, unlike value parameters, a new storage location is not created for these parameters. The
reference parameters represent the same memory location as the actual parameters that are supplied
to the method.
Lab 02 – Familiarization of Modifiers, Methods, Class,
Constructor/Destructor, Constructor Overloading, Properties,
ref/out, Boxing/Unboxing
Compiled By: Sana Maqsood CS-321 | Visual Programming 12
An argument that is passed to a ref parameter must be initialized before it is passed. This differs
from out parameters, whose arguments do not have to be explicitly initialized before they are passed.
You can declare the reference parameters using the ref keyword in both the method definition and the
calling method. The following example demonstrates this:
1.11 Output Parameters (out)
A parameter declared with an out modifier is an output parameter. Similar to a reference parameter, an
output parameter does not create a new storage location.
Every output parameter of a method must be definitely assigned before the method returns.
Output parameters are typically used in methods that produce multiple return values. For example:
using System;
namespace CalculatorApplication
{
Lab 02 – Familiarization of Modifiers, Methods, Class,
Constructor/Destructor, Constructor Overloading, Properties,
ref/out, Boxing/Unboxing
Compiled By: Sana Maqsood CS-321 | Visual Programming 13
class NumberManipulator
{
public void getValues(out int x, out int y )
{
Console.WriteLine("Enter the first value: ");
x = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the second value: ");
y = Convert.ToInt32(Console.ReadLine());
}
static void Main(string[] args)
{
NumberManipulator n = new NumberManipulator();
/* local variable definition */
int a , b;
/* calling a function to get the values */
n.getValues(out a, out b);
Console.WriteLine("After method call, value of a : {0}", a);
Console.WriteLine("After method call, value of b : {0}", b);
Console.ReadLine();
}
}
}
1.11 Boxing & Unboxing
When an object reference refers to a value type, a process known as boxing occurs, Boxing causes the
value of a value type to be stored in an object instance. Thus, a value type is “boxed” inside an object.
This object can now be used like any other object. In all cases, boxing is implicit.
Unboxing is a process of retrieving a value from a boxed object. This action is performed using an explicit
cast from the object reference to its corresponding value type.
Here’s a simple example to illustrate boxing and unboxing:
Lab 02 – Familiarization of Modifiers, Methods, Class,
Constructor/Destructor, Constructor Overloading, Properties,
ref/out, Boxing/Unboxing
Compiled By: Sana Maqsood CS-321 | Visual Programming 14
2 Student Tasks
2.1 Write C# code to find the output for the above examples and display the console.
2.2 Write a code for simple calculator, calculator operations are based on separate classes and their
methods using output/reference parameter.
2.3 Write a constructor overloading program in which you make 3 constructors and a destructor. One is
for default constructors with default message, next is parameterized constructor which accept a string
value and last one is also parameterized constructor which accept two numerical value and shows sum
of them. Initialize all constructors and shows output.
2.4 write a program which takes two numbers from users and swap them using reference parameters.
Show the console.
2.5 write a program which takes roll number, name and age of students from user if the age is below 25
and above 18 using properties and display them.
2.6 what is abstract property? Write an example program to demonstrate.