CS 2605 Lab 5: Operator Overloading in C++, Lab Reports of Data Structures and Algorithms

In this document, students are given a lab assignment for cs 2605, a c++ programming course, where they are required to design and implement a rational class and a polynomial class. The rational class should support arithmetic operations for rational numbers, while the polynomial class should support basic arithmetic operations and evaluation. Students are expected to submit their operator declarations for the rational class and implement the operators for the polynomial class.

Typology: Lab Reports

Pre 2010

Uploaded on 02/13/2009

koofers-user-47m
koofers-user-47m 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 2605 Lab 5 Spring 2008
1
Operator Overloading: Design and Implementation
Goal
In this lab, you will design the interface for a simple data type, and implement another simple data type in C++. Your
solutions should strive to achieve Stroustrup's goal for the inclusion of classes in C++:
The aim of the C++ class construct is to provide the programmer with a tool for creating new
types that can be used as conveniently as the built-in types.
Learning Objectives
discussion of the design of an appropriate interface for a realistic data type
use of appropriate C++ idiom in implementing a given interface for a different realistic data type
Part I: Designing a Class Interface (weight 20%)
Decide who will navigate and who will drive during this part. Recall from mathematics that a rationa l number is one that
can be expressed as the ratio of two integers, like 4/5 or -123/789. When performing computations on a computer, using
rational numbers instead of their decimal equivalents (or approximations) can lead to more accurate results, so it’s useful to
have a type that represents rational numbers.
A Rational class should support the kinds of arithmetic operations that a user would expect. Here’s a partial interface,
which must be extended:
class Rational {
private:
int mTop, mBottom;
// private member functions:
public:
Rational();
Rational(int T);
Rational(int T, int B);
// member operators:
// non-member (friend) operators:
};
Your task is to determine a set of reasonable operators to include in the class. You are not expected to provide any
implementations, just declarations. You will prepare a list of member and non-member operators you’d include in your
class design and submit that list as a plain text file to the Curator (for evaluation by a TA) under the heading Lab05PI.
There must be a single submission from your pair, listing the names and email PIDs of the group members at the beginning
of the file. The remainder of the file must simply be a list of the operator declarations you choose to include. You are
expected to use correct C++ syntax for the declarations, and to also include a one-sentence comment describing each
operator.
This part of the lab will be limited to 20 minutes. Submissions made more than 25 minutes after the lab
starts will not be graded.
pf3

Partial preview of the text

Download CS 2605 Lab 5: Operator Overloading in C++ and more Lab Reports Data Structures and Algorithms in PDF only on Docsity!

Operator Overloading: Design and Implementation

Goal

In this lab, you will design the interface for a simple data type, and implement another simple data type in C++. Your solutions should strive to achieve Stroustrup's goal for the inclusion of classes in C++:

The aim of the C++ class construct is to provide the programmer with a tool for creating new types that can be used as conveniently as the built-in types.

Learning Objectives

  • discussion of the design of an appropriate interface for a realistic data type
  • use of appropriate C++ idiom in implementing a given interface for a different realistic data type

Part I: Designing a Class Interface (weight 20%)

Decide who will navigate and who will drive during this part. Recall from mathematics that a rational number is one that can be expressed as the ratio of two integers, like 4/5 or -123/789. When performing computations on a computer, using rational numbers instead of their decimal equivalents (or approximations) can lead to more accurate results, so it’s useful to have a type that represents rational numbers.

A Rational class should support the kinds of arithmetic operations that a user would expect. Here’s a partial interface, which must be extended:

class Rational { private: int mTop, mBottom;

// private member functions:

public: Rational(); Rational(int T); Rational(int T, int B);

// member operators:

// non-member (friend) operators:

Your task is to determine a set of reasonable operators to include in the class. You are not expected to provide any implementations, just declarations. You will prepare a list of member and non-member operators you’d include in your class design and submit that list as a plain text file to the Curator (for evaluation by a TA) under the heading Lab05PI. There must be a single submission from your pair, listing the names and email PIDs of the group members at the beginning of the file. The remainder of the file must simply be a list of the operator declarations you choose to include. You are expected to use correct C++ syntax for the declarations, and to also include a one-sentence comment describing each operator.

This part of the lab will be limited to 20 minutes. Submissions made more than 25 minutes after the lab starts will not be graded.

Part II: Implementing Operators (weight 80%)

Switch roles several times during this part. Consider the following declaration for a class that represents polynomials:

class Polynomial { // Basic support for writing Polynomials (supplied): friend ostream& operator<<(ostream& Out, const Polynomial& F);

private: string mName; // descriptive name, mainly for the client's convenience vector P; // coefficients of the polynomial; P[i] is the // coefficient of x^i public: // Create default polynomial ("no name" and P[0] == 0) Polynomial(); // Create polynomial with specified name and coefficients: Polynomial(const string& N, const vector& C = vector());

// Basic arithmetic operations, just like HS algebra: Polynomial operator+(const Polynomial& RHS) const; Polynomial operator-(const Polynomial& RHS) const; // Extra credit part: // Polynomial operator*(const Polynomial& RHS) const;

// Evaluation; if P is a Polynomial object, calling P(a) will return // the value of P(a). int operator()(int x) const;

// Mixed expressions of form Polynomial op integer: Polynomial operator+(int RHS) const; Polynomial operator-(int RHS) const; Polynomial operator*(int RHS) const;

// More mixed expressions of form integer op Polynomial: friend Polynomial operator+(int LHS, const Polynomial& RHS); friend Polynomial operator-(int LHS, const Polynomial& RHS); friend Polynomial operator*(int LHS, const Polynomial& RHS); };

Your task is to implement the given interface. We have supplied a header file containing the class declaration shown above, and a source file containing implementations of the constructors and the I/O operators.

We suggest you implement the operators in the order they are declared. There is a certain natural logic to that, and you will find that you can often use the implementation of one operator as the basis for implementing another one, either by calling it or by adapting the code.

When you submit your solution to the Curator, under the heading Lab05PII , you must submit just the implementation file Polynomial.cpp (not zipped). That will be compiled with a simple test driver and a copy of the header file we’ve supplied. As before, both partners must submit the solution to the Curator before the end of the lab. The maximum score on this part is 300 points.

Naming Conventions:

Each of binary operators produces a new Polynomial object, and that object must have a name. You will use the

following convention: if the operation is P op Q then the name of the resulting Polynomial will be the string “P op Q”, where “P” is the name of the left operand, “op” is the appropriate symbol for the operation in question, and “Q” is