Slide 1 : In this session, you will learn to:
Implement constructors
Implement destructors
Identify the life cycle of an object
Describe polymorphism
Implement function overloading
Identify need for operator overloading Objectives
Slide 2 : A constructor is a special type of method that is invoked when you create a new instance of a class.
A constructor is used to initialize the members of the class.
The name of a constructor is the same as the name of the class that contains it. Implementing Constructors
Slide 3 : A constructor is special member function within the class which is executed when an object of the class is created. The Need of Constructors
Slide 4 : The two types of constructors are:
Instance constructors: They are called whenever an instance of a class is created. These constructors are used to initialize the data members of the class.
Static constructors: They are used to initialize the static variables of a class. These variables are created using static keyword and they store values that can be shared by all the instances of a class. Types of Constructors
Slide 5 : A constructor can be modified to accept the user-supplied values at run time.
Objects can be initialized using the default constructor with values hard-coded in the program. But there might be a requirement where the variables need to be initialized with user supplied values. Constructors with Parameters
Slide 6 : Destructors are special methods that are used to release the instance of a class from memory.
A class can have only one destructor.
The purpose of the destructor is to perform the required memory cleanup action.
The .NET Framework automatically runs the destructor to destroy objects in the memory. Implementing Destructors
Slide 7 : A destructor has the same name as its class but is prefixed with a ~ , which is the symbol of tilde.
Destructors cannot be inherited or overloaded.
Garbage collection is a process that automatically frees the memory of objects that are no more in use.
The decision to invoke the destructor is made by a special program of C# known as the garbage collector.
The process of garbage collection happens automatically. It ensures that:
Objects get destroyed
Only unused objects are destroyed Declaration of Destructors
Slide 8 : C# provides the following methods to release the instance of a class from memory:
Finalize(): It is a special method that is called from the class to which it belongs or from the derived classes. The Finalize() destructor is called after the last reference to an object is released from the memory.
Dispose(): This method is called to release a resource, such as a database connection, as soon as the object using such a resource is no longer in use. The IDisposable interface contains the Dispose() method. Therefore, to call the Dispose() method, the class must implement the IDisposable interface. Declaration of Destructors (Contd.)
Slide 9 : Identifying the Life Cycle of an Object Let us understand the life cycle of an object with the help of the following code:
using System;
//Life Cycle of an Object
namespace Objects
{
class TestCalculator
{
TestCalculator()
{
Console.WriteLine("Constructor Invoked");
}
Slide 10 : Identifying the Life Cycle of an Object (Contd.) The Calc1 object has function scope. Therefore, its constructor is executed after the execution of Main() begins. The destructor of all the object is invoked when the garbage collector is invoked. ~TestCalculator()
{
Console.WriteLine ("Destructor Invoked");
}
public static void Main(string[] args)
{
Console.WriteLine("Main() Begins");
TestCalculator Calc1 = new TestCalculator();
Slide 11 : Identifying the Life Cycle of an Object (Contd.) ~Console.WriteLine("Inner Block Begins ");
TestCalculator Calc2 = new TestCalculator();
Console.WriteLine("Inner Block Ends");
}
Console.WriteLine("Main() ends");
}
}
} The Calc2 object has block scope. Therefore, its constructor is executed after the inner block begins.
Slide 12 : In Object-Oriented Programming (OOPs), polymorphism allows one interface to be used for multiple functions.
Polymorphism reduces the complexity within the functions of a class of a program.
Polymorphism can either be static or dynamic. Introducing Polymorphism
Slide 13 : Static polymorphism refers to an entity, which exists in various forms simultaneously.
C# uses two approaches to implement static polymorphism. These are:
Function overloading: This approach allows using the same name for two or more functions. Each redefinition of a function must use different types of parameters, sequence of parameters, or a number of parameters.
Operator overloading: This approach allows user-defined types such as structures and classes, to use overloaded operators for easy manipulation of their objects. Static Polymorphism
Slide 14 : In dynamic polymorphism, the decision about function execution is made at run time.
Dynamic polymorphism is more useful than static polymorphism as it provides much more flexibility for manipulating the objects.
C# uses two approaches to implement dynamic polymorphism:
Abstract classes: Are the special type of base classes that consist of abstract class members.
Virtual functions: Are the functions that do not really exist, however, appear to be present in some parts of the program. Dynamic Polymorphism
Slide 15 : Function overloading is implemented by defining two or more functions in a class sharing the same name.
In function overloading, each definition of a function must differ in its function signature. Implementing Function Overloading
Slide 16 : The signature of a function is defined by:
The number of parameters
The data types of parameters
The sequence of the parameters Function Signature
Slide 17 : Constructors can also be parameterized, and therefore, they can be overloaded.
Overloaded constructors are commonly used in C# to provide flexibility while creating an object. Constructor Overloading
Slide 18 : Demo: Displaying Days Using Function Overloading Problem Statement:
Tim has to develop a software application for a primary school. The application should accept the month entered by the student and display the total number of days of that month.
Slide 19 : Solution:
To develop the required application, Tim needs to perform the following tasks:
Create a console-based application.
Build and execute an application. Demo: Displaying Days Using Function Overloading (Contd.)
Slide 20 : Operator Overloading Operator overloading provides additional capabilities to C# operators when they are applied to user-defined data types.
Only the predefined set of C# operators can be overloaded.
Slide 21 : Need for Operator Overloading To use operators with user-defined data types, they need to be overloaded according to a programmer’s requirement.
The following table describes the overload ability of the operators in C#.
Slide 22 : In this lesson, you learned that:
Constructors are member functions of a class and are invoked when an instance of the class to which they belong is created.
A constructor has the same name as its class.
A destructor is invoked when any instance of a class ceases to exist.
A destructor has the same name as its class, but it is prefixed with a ~ (tilde).
Constructors are special methods that allow control over the initialization of objects.
Destructors are special methods that are used to release the instance of a class from memory.
Garbage collection is a process that automatically frees the memory of objects that is no more in use. Summary
Slide 23 : The Finalize() destructor is called after the last reference to an object is released from the memory.
The Dispose() method is called to release a resource, such as a database connection, when the object using such a resource is no longer in use.
The term polymorphism has been derived form the Greek words ‘poly’ and ‘morphos’, which mean ‘many’ and ‘forms’, respectively.
Polymorphism allows one interface to be used for multiple functions.
Static polymorphism refers to an entity, which exists in different forms simultaneously.
In dynamic polymorphism, the decision about function execution is made when code is executed. Summary (Contd.)
Slide 24 : Function overloading is the process of using the same name for two or more functions in a class.
The number, type, or sequence of parameters of a function is called its function signature.
Overloaded constructors are commonly used in C# to provide flexibility while creating an object.
Operator overloading provides additional capabilities to C# operators when they are applied to user-defined data types.
The predefined C# operators can be overloaded by using the operator keyword.
Operators may be considered as functions internal to the compiler. Summary (Contd.)