Object-Oriented Programming:Defining a C# Class to Work with Fractions

As the final example in this chapter, Program 19.4 shows the fraction example written in C#, a programming language developed by Microsoft, Inc. C#  is one of the newer OOP languages, and is turning out to be quite popular. It has become the language of choice for developing applications with .NET.

Program 19.4   Working with Fractions in C#

using System;

class Fraction

{

private int numerator;

private int denominator;

public int Numerator

{

get

{

return numerator;

}

set

{

numerator = value;

}

}

public int Denominator

{

get

{

return denominator;

}

set

{

denominator = value;

}

}

public void print ()

{

Console.WriteLine(“The value of the fraction is {0}/{1}”,

numerator, denominator);

}

}

class example

{

public static void Main()

{

Fraction myFract = new Fraction();

myFract.Numerator = 1;

myFract.Denominator = 3;

myFract.print ();

}

}

Program 19.4   Output

The value of the fraction is 1/3

You can see the C#  program looks a little different from the other two OOP  programs, but you can probably still determine what’s happening. The Fraction class definition begins by declaring the two instance variables numerator and denominator as private. The Numerator and Denominator methods each have their getter and setter method defined as properties. Take a closer look at Numerator:

public int Numerator

{

get

{

return numerator;

}

set

{

numerator = value;

}

}

The “get” code is executed when the value of the numerator is needed in an expression, such as in

num = myFract.Numerator;

The “set” code is executed when a value is assigned to the method, as in

myFract.Numerator = 1;

The actual value that is assigned gets stored in the variable value when the method gets called. Note that parentheses do not follow the setter and getter methods here.

Naturally, you can define methods that optionally take arguments, or setter methods that take multiple arguments. For example, this C#  method invocation might be used to set the value of a fraction to 2/5 with a single call:

myFract.setNumAndDen (2, 5)

Returning to Program 19.4, the statement

Fraction myFract = new Fraction();

is used to create a new instance from the Fraction class and assign the result to the Fraction variable myFract. The Fraction is then set to 1/3 using the Fraction’s set- ters.

The print method is invoked next on myFract to display the value of the fraction. Inside the print method, the WriteLine method from the Console class is used to dis- play output. Similar to printf’s % notation, {0} specifies in the string where the first value is be substituted, {1} where the second value is to be displayed, and so on. Unlike the printf routine, you don’t need to worry here about the types being displayed.

As with the C++  example, the getter methods for the C#  Fraction class were not exercised here. They were included for illustrative purposes.

This concludes this brief introduction to object-oriented programming. Hopefully, this chapter has given you a better idea about what object-oriented programming is all about and how programming in an OOP  language differs from a language such as C. You have seen how you can write a simple program in one of three OOP  languages to work with objects that represent fractions. If you were serious about working with frac- tions in your programs, you would probably extend your class definition to support operations such as addition, subtraction, multiplication, division, inversion, and reduction of fractions, for example. This would be a relatively straightforward task for you to do.

To continue your studies further, get a good tutorial on a particular OOP  language, such as one listed in Appendix E.

Source: Kochan Stephen G. (2004), Programming in C: A Complete Introduction to the C Programming Language, Sams; Subsequent edition.

Leave a Reply

Your email address will not be published. Required fields are marked *