Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Chapter 9 - Programming Paradigms

The document provides an overview of Object-Oriented Programming (OOP) concepts in C#, including terminology such as classes, objects, inheritance, and polymorphism. It explains how to create a console application, define classes, and implement features like method overloading and overriding through practical examples. Additionally, it illustrates the creation of employee classes and their subclasses to demonstrate OOP principles in action.

Uploaded by

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

Chapter 9 - Programming Paradigms

The document provides an overview of Object-Oriented Programming (OOP) concepts in C#, including terminology such as classes, objects, inheritance, and polymorphism. It explains how to create a console application, define classes, and implement features like method overloading and overriding through practical examples. Additionally, it illustrates the creation of employee classes and their subclasses to demonstrate OOP principles in action.

Uploaded by

mohsulai2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

9 Object Oriented programming with C#

As we have previously stated that Object Oriented paradigm (often written O-O) focusses on the
objects that a program is representing, and on allowing them to exhibit "behavior". This focus on
creating programming constructs called Objects which binds data with their operating procedures.

9.1 Overview of OOP Terminology


· Class: A user-defined prototype for an object that defines a set of attributes that characterize
any object of the class. The attributes are data members (class variables and instance variables)
and methods, accessed via dot notation.
· Data member: A class variable or instance variable that holds data associated with a class and
its objects.
o Class variable: A variable that is shared by all instances of a class. Class variables are
defined within a class but outside any of the class's methods. Class variables aren't used
as frequently as instance variables are used.
o Instance variable: A variable that is defined inside a method and belongs only to the
current instance of a class.
· Method (Member Function): A special kind of function that is defined in a class definition. These
method implements the behaviors of the class.
· Method overloading: The assignment of more than one behavior to a function. The operation
performed varies by the types of objects (arguments) involved. This allows a class to have many
functions with the same name as long as their arguments are different.
· Object (Instance): A unique instance of a data structure that's defined by its class. An object
comprises both data members and methods. A process of creating a new object is called
instantiation.
· Constructor: A special function included in the class which has the same name as the class name.
They too can be overloaded just like any other function. When a new object is created a
constructor is called.
· Inheritance: The transfer of the characteristics of a class (Parent class) to other classes (Sub
class) that are derived from it. For example: Manager class can derive characteristics from it’s
parent class Employee.
· Method Overriding: In a sub class a method can be redefined to give a specialized behavior for
the sub class if it need to be different from the parent class behavior. This is called method
overriding.

9.2 A Console Application


A console application is the simplest form of a C# program which can take input and displays output
at a command line console. We will be using a console Application to demonstrate the features of
object-oriented programming. Let us now see the steps to start a new project in Visual Studio for a
C# console application.
· Open Visual studio 2013
· Click on create project tab: Then it will open new project window

Unit 01: Programming 61


· In the window, select Windows under Visual C# and then select Console Application and name
the project with the location
9.3 Creating Classes
Let’s create a new class “Employee”

· Right click the project name on Solution Explorer


· select Add à select Class à type the class name as “Employee”
· Type the code given here in the Employee class

class Employee
{
protected int empNo;
protected string empName; // Instance variables

public static int empCount = 0; // Class Variable

public Employee() // Default constructor without arguments


{
Console.WriteLine("In base class 0 arg constructor");
Employee.empCount++;
}

public Employee(int eno, string ename) // Overloaded constructor (2 args)


{
this.empNo = eno;
this.empName = ename;
Employee.empCount++; // Class variable is access using class name
}

public void displayEmployee() // A function without a return value


{
Console.WriteLine("-------------------------");
Console.WriteLine("Emp No : " + this.empNo);
Console.WriteLine("Emp Nanme : " + this.empName);
Console.WriteLine("Emp Salary : " + this.getNetSalary());
Console.WriteLine("-------------------------");
Console.WriteLine(">> Total Employees = " + Employee.getEmpCount());
}

public static int getEmpCount() // A static member function


{
return empCount;
}

public virtual double getNetSalary()// Declared virtual as for overriding


{
return 0.0;
}
}

9.3.1 Creating instance objects


· Click on “Program.cs” file on the Solution Explorer
· Type the following code in the Main() method

62 Unit 01: Programming


static void Main(string[] args)
{
Console.WriteLine(">> Total Employees = " + Employee.getEmpCount());

Employee emp1 = new Employee() // This calls default constructor


emp1.displayEmployee();

Employee emp2 = new Employee(100, "David"); // This call 2 arg const.


Emp2.displayEmployee();
}

Execution of the program will produce the following output:

Note, that in the above output, since emp1 object is created using the default constructor the
empNo and empName attributes are not initialized.

9.4 Class Inheritance


Instead of starting from scratch, you can create a class by deriving it from a preexisting class by listing
the parent class after the new class name:

The child class inherits the attributes of its parent class, and you can use those attributes as if they
were defined in the child class. A child class can also override data members and methods from the
parent. We will be 2 sub classes derived from the “Employee” class. Following class hierarchy
diagram illustrates this.

Unit 01: Programming 63


Let us now see the code “SalariedEmployee” class. Add a new class in the same way as you added
the Employee class.

class SalariedEmployee : Employee


{
protected double empSalary;

public SalariedEmployee(int eno, string ename, float esal)


{
base.empNo = eno;
base.empName = ename;
this.empSalary = esal;
}

public override double getNetSalary()


{
double epf = this.empSalary * 0.08;
return this.empSalary - epf;
}
}

Note the following points in the above class definition.

· In the constructor here, inherited attributes are accessed using the base keyword.
· this keyword is used to access the attributes of the same class.
· getNetSalary() method is overridden in this class to implement the exact salary calculation
process for a Salaried Employee.
· If override keyword is replaced with new keyword the getNetSalary() method here will be
added as a new method without overriding the parent class method. However, to implement
the polymorphism (see below) the method should be successfully overridden.

Similarly, let us now see the code “HourlyPayEmployee” class. Add a new class in the same way as
you added the SalariedEmployee class.

class HourlyPayEmployee: Employee


{
protected double empRate;
protected double empHrsWorked;

public HourlyPayEmployee(int eno, string ename, double erate, double ehours)


: base(eno, ename)
{
this.empRate = erate;
this.empHrsWorked = ehours;
}

public override double getNetSalary()


{
double netPay = empRate * empHrsWorked;
return netPay;
}
}

64 Unit 01: Programming


Note the following points in the above class definition.

· The constructor here makes a call to the appropriate constructor in the base class.
· this keyword is used to access the attributes of the same class, but it is not compulsory.
· getNetSalary() method is overridden in this class to implement the exact salary calculation
process for a Hourly Pay Employee which is different to how it is calculated for a Salaried
Employee.
Now modify the Main() as given below

class Program
{
static void Main(string[] args)
{
Console.WriteLine(">> Total Employees = " + Employee.getEmpCount());

Employee emp1 = new Employee(100, "David");


emp1.displayEmployee();

Employee emp2 = new SalariedEmployee(200, "Anne", 25000);


emp2.displayEmployee();

Employee emp3 = new HourlyPayEmployee(300, "Saman", 450, 80);


emp3.displayEmployee();

Console.ReadLine();
}
}

Note the following points in the above Main() function.

· There are 3 Employee class object references emp1, emp2 and emp3.
· Employee class object reference can not only point to an object of the same classes but also
any object of sub classes derived from that.

Let’s see the output of the program now

Unit 01: Programming 65


9.5 Polymorphism
Polymorphism is another main concept in OOP. That is, each object can show different behavior for
the same operation. In this example, we have 2 different salary calculation methods for the 2 types
of employees. This is implemented in OOP languages by using method overriding.

Did you notice the salary of David is 0 (This is calculated using base class getNetSalary() method).

Salary of Anne is 23000 (This is calculated using getNetSalary() method of SalariedEmployee class)

Salary of Saman is 36000 (This is calculated using getNetSalary() method of HourlyPayEmployee


class)

How did this happen?

Actually, when displayEmployee() method is called for each of the following object references, the
following happens.

emp1.displayEmployee() – this.getNetSalary() called the base class method as emp1 now points to
Employee object.

emp2.displayEmployee() – this.getNetSalary() called “SalariedEmployee” class method as now emp2


points to SalariedEmployee object.

emp3.displayEmployee() – this.getNetSalary() called “HourlyPayEmployee” class method as now


emp3 points to HourlyPayEmployee object.

This is a demonstration of polymorphism principle.

66 Unit 01: Programming

You might also like