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

Program 19.3 shows how a program to implement a Fraction class might be written using the C++  language. C++  has become an extremely popular programming language for software development. It was invented by Bjarne Stroustroup at Bell Laboratories, and was the first object-oriented programming language based on C—at least to my knowledge!

Program 19.3   Working with Fractions in C++

#include <iostream>

class Fraction

{

private:

int numerator;

int denominator;

public:

void setNumerator (int num);

void setDenominator (int denom);

int Numerator (void);

int Denominator (void);

void print (Fraction f);

};

void Fraction::setNumerator (int num)

{

numerator = num;

}

void Fraction::setDenominator (int denom)

{

denominator = denom;

}

int Fraction::Numerator (void)

{

return numerator;

}

int Fraction::Denominator (void)

{

return denominator;

}

void Fraction::print (void)

{

std::cout << “The value of the fraction is ” << numerator << ‘/’

<< denominator << ‘\n’;

}

int main (void)

{

Fraction myFract;

myFract.setNumerator (1);

myFract.setDenominator (3);

myFract.print ();

return 0;

}

Program 19.3   Output

The value of the fraction is 1/3

The C++  members (instance variables) numerator and denominator are labeled private to enforce data encapsulation; that is, to prevent them from being directly accessed from outside the class.

The setNumerator method is declared as follows:

void Fraction::setNumerator (int num)

The method is preceded by the notation Fraction:: to identify that it belongs to the Fraction class.

A new instance of a Fraction is created like a normal variable in C, as in the follow- ing declaration in main:

Fraction myFract;

The numerator and denominator of the fraction are then set to 1 and 3, respectively, with the following method calls:

myFract.setNumerator (1);

myFract.setDenominator (3);

The value of the fraction is then displayed using the fraction’s print method.

Probably the oddest-appearing statement from Program 19.3 occurs inside the print method as follows:

std::cout << “The value of the fraction is ” << numerator << ‘/’

<< denominator << ‘\n’;

cout is the name of the standard output stream, analogous to stdout in C. The << is known as the stream insertion operator, and it provides an easy way to get output.You might recall that << is also C’s left shift operator. This is one significant aspect of C++: a feature known as operator overloading that allows you to define operators that are associat- ed with a class. Here, the left shift operator is overloaded so that when it is used in this context (that is, with a stream as its left operand), it invokes a method to write a format- ted value to an output stream, instead of trying to actually perform a left shift operation.

As another example of overloading, you might want to override the addition operator + so that if you try to add two fractions together, as in

myFract + myFract2

an appropriate method from your Fraction class is invoked to handle the addition.

Each expression that follows the << is evaluated and written to the standard output stream. In this case, first the string “The value of the fraction is” gets written, followed by the fraction’s numerator, followed by a /, the fraction’s denominator, and then a newline character.

The C++  language is rich with features. Consult Appendix E, “Resources,” for rec- ommendations on a good tutorial.

Note that in the previous C++  example, the getter methods Numerator () and Denominator () were defined in the Fraction class but were not used.

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 *