Question

OUTCOMES After you finish this assignment, you will be able to do the following: Define an...

OUTCOMES

After you finish this assignment, you will be able to do the following:

  • Define an abstract class
  • Create concrete classes from an abstract class
  • Overload an operator
  • Split classes into .h and .cpp files
  • Open files for reading
  • Write to files
  • Use output manipulators such as setw, fixed, and setprecision

DESCRIPTION

A binary arithmetic operation takes two double operands (left and right) to perform addition, subtraction, multiplication, or division on. For example, 10 + 11 is an addition (+) whose two operands are 10 and 11. When this operation is performed, the result will be 21. We can also write this operation as + 10 11.

Write a program that reads basic arithmetic operation instructions (without results) from a given input file named operations-in.txt(see below),  performs the operations, and saves their results to an output file named operations-out.txt. For example, if the input file has the following operation commands:

  +  20  11
  -  96  75
  *  87  70
  /  22   9

The program output file should be like this:

  20.00 +    11.00 =      31.00
  96.00 -    75.00 =      21.00
  87.00 *    70.00 =    6090.00
  22.00 /     9.00 =       2.44

While there are many ways to implement this program, this assignment asks you to do it in an Object-Oriented Programming (OOP) way using what you learned in the past few weeks. That means creating an abstract class called Operation with two members one for the left operand and the other for the right operand. Then we create a class Addition that inherits from Operation and can perform the actual addition of the left operand to the right operand. Similarly, we create three more classes (also inheriting from Operation): Subtraction, Multiplication, and Division.

The following instructions should guide you through the implementation of this program:

  • In a header file named operation.h, define an abstract class named Operation with two protected double data members (left and right) and with the following public member functions:
    • Operation(double l, double r): a two-argument constructor initializing the left and right data members of this class.
    • double perform() const: a pure virtual function that actually performs the operation. This function will be actually implemented in the Addition, Subtraction, Multiplication, and Division classes below.
    • char symbol() const: a pure virtual function also implemented in the Addition, Subtraction, Multiplication, and Division classes.
    • ~Operation(): a virtual empty constructor.

    The Operation class should also define a friend function that overloads the <<operator. The prototype of this function should be like this:

    friend ostream& operator<<(ostream& out, const Operation& opr); This operator prints out the operation and its result to the output stream out in a format similar to that of the output file above. Use the setw, fixed, and setprecision manipulators to achieve that format.

  • Define a class named Addition that inherits from the Operation class and represents the addition operation. This class must have:
    • a public two-argument constructor, delegating the initialization of the left and right data members to the constructor of the Operation class;
    • an empty destructor;
    • an implementation for the perform() function that returns the result of adding the left data member to the right data member;
    • an implementation for the symbol() that returns the character `+` (which is the symbol for addition).
    Split the code of this class into a header file name addition.h and an implementation file named addition.cpp.
  • Repeat the previous step for the subtraction operation. The class should be named Subtraction and its code should be split into a header file name subtraction.h and an implementation file named subtraction.cpp.
  • Repeat the previous step for the multiplication operation. The class should be named Multiplication and its code should be split into a header file name multiplication.h and an implementation file named multiplication.cpp.
  • Repeat the previous step for the division operation. The class should be named Division and its code should be split into a header file name division.h and an implementation file named division.cpp.
  • In a separate .cpp file, write a  main() function that does the following
    • Open the given input file operations-in.txt for reading and read it one operation command at a time.
    • Open an output file named operations-out.txt for writing.
    • For each operation command read from the input operations-in.txt file do the following:
    • Based on the operation symbol, create a heap object (using the new operator) of the appropriate class (Addition, Subtraction, Multiplication, and Division) based using the new operator
    • Use the << operator to print the operation and its result to the console.
    • Use the << operator to write the operation along with its result to the output file.
    • Delete the object when it's no longer needed.
    • Close both input and output files

OPERATIONS-IN.TXT

+ 20 11

- 96 75

- 84 44

+ 19 54

/ 20 46

* 99 74

* 87 70

/ 22 9

/ 3 24

+ 39 17

+ 12 23

* 80 77

- 3 65

+ 10 16

* 52 8

/ 71 77

+ 27 14

* 80 87

+ 37 35

/ 65 22

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Short Summary:

  • Provided the source code files ie., all .h and .cpp files with  sample console output and output in a file as per the requirements mentioned.
  • Hope you are satisfied with the answer.
  • Please upvote the answer and appreciate our time.

Source Code:

Operation.h:

#ifndef OPERATION_H
#define OPERATION_H
#include <string>
using namespace std;

class Operation{
protected:
// two members one for the left operand and the other for the right
double left;
double right;
  
public:
//parameterized constrctor
Operation(double, double);
// pure virtual functions
virtual double perform() const = 0;
virtual char symbol() const = 0;
//default destructor
~Operation();
// friend function
friend ostream& operator<<(ostream& , const Operation&);
};
#endif


Operation.cpp:

#include "Operation.h"
#include <iostream>
#include <iomanip>
using namespace std;

//parameterized constrctor
Operation :: Operation(double l, double r){
left = l;
right = r;
}

//default destructor
Operation :: ~Operation(){}

/**
* Function: operator << overload
* It uses ostream and an Operation object
* It displays the result in proper format
*/
ostream& operator<<(std::ostream& out, const Operation &opr)
{
   out << fixed << setprecision(2) << opr.left << setw(5) << opr.symbol() << setw(10) << opr.right << " = " << setw(10) << opr.perform();
   //return the output stream
   return out;
}

addition.h:

#ifndef ADDITION_H
#define ADDITION_H
#include <iostream>
#include "Operation.h"
using namespace std;

class Addition : public Operation {
public:
Addition(double,double);
~Addition();
double perform() const;
char symbol() const;
};
#endif

addition.cpp:

#include "addition.h"
#include <iostream>
using namespace std;

Addition :: Addition(double l, double r):Operation(l, r){
}

Addition :: ~Addition(){}

double Addition :: perform() const {
return left + right;
}

char Addition :: symbol() const{
return '+';
}

subtraction.h:

#ifndef SUBTRACTION_H
#define SUBTRACTION_H
#include <iostream>
#include "Operation.h"
using namespace std;

class Subtraction : public Operation{
public:
Subtraction(double,double);
~Subtraction();
double perform() const;
char symbol() const;
};
#endif

subtraction.cpp:

#include "subtraction.h"
#include <iostream>
using namespace std;

Subtraction :: Subtraction(double l, double r):Operation(l, r){
}

Subtraction :: ~Subtraction(){}

double Subtraction :: perform() const {
return left - right;
}

char Subtraction :: symbol() const{
return '-';
}


multiplication.h:

#ifndef MULTIPLICATION_H
#define MULTIPLICATION_H
#include <iostream>
#include "Operation.h"
using namespace std;

class Multiplication : public Operation{
public:
Multiplication(double,double);
~Multiplication();
double perform() const;
char symbol() const;
};
#endif

multiplication.cpp:

#include "multiplication.h"
#include <iostream>
using namespace std;

Multiplication :: Multiplication(double l, double r):Operation(l, r){
}

Multiplication :: ~Multiplication(){}

double Multiplication :: perform() const {
return left * right;
}

char Multiplication :: symbol() const{
return '*';
}

division.h:

#ifndef DIVISION_H
#define DIVISION_H
#include <iostream>
#include "Operation.h"
using namespace std;

class Division : public Operation{
public:
Division(double,double);
~Division();
double perform() const;
char symbol() const;
};
#endif

division.cpp:

#include "division.h"
#include <iostream>
using namespace std;

Division :: Division(double l, double r):Operation(l, r){
}

Division :: ~Division(){}

double Division :: perform() const {
return left / right;
}

char Division :: symbol() const{
return '/';
}

InputFile:

operations-in.txt

+ 20 11

- 96 75

- 84 44

+ 19 54

/ 20 46

* 99 74

* 87 70

/ 22 9

/ 3 24

+ 39 17

+ 12 23

* 80 77

- 3 65

+ 10 16

* 52 8

/ 71 77

+ 27 14

* 80 87

+ 37 35

/ 65 22

Sample Run:

Console output:

= 20.00 11.00 = 31.00 96.00 75.00 = 21.00 84.00 44.00 = 40.00 19.00 54.00 = 73.00 20.00 46.00 = 0.43 99.00 74.00 = 7326.00 87

OUTPUT FILE : operations-out.txt :

+ / main.cpp operations- 1 20.00 2 96.00 3 84.00 4 19.00 5 20.00 / 6 99.00 7 87.00 8 22.00 / 93.00 10 39.00 11 12.00 12 80.00

**************************************************************************************

Feel free to rate the answer and comment your questions, if you have any.

Please upvote the answer and appreciate our time.

Happy Studying!!!

**************************************************************************************

Add a comment
Know the answer?
Add Answer to:
OUTCOMES After you finish this assignment, you will be able to do the following: Define an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ A rational number is of the form a/b, where a and b are integers, and...

    C++ A rational number is of the form a/b, where a and b are integers, and b is not equal 0. Develop and test a class for processing rational numbers. Details: Your program should have 3 files: a driver file to test the operations, a header file for the class definition and any operator overloads you need, and an implementation file with the definitions of the items in the header file. The class should read and display all rational numbers...

  • This is a quick little assignment I have to do, but I missed alot of class...

    This is a quick little assignment I have to do, but I missed alot of class due to the Hurricane and no one here knows what theyre doing. please help! this is Java with intelli-J Overview In this project students will build a four-function one-run calculator on the command line. The program will first prompt the user for two numbers, then display a menu with five operations. It will allow the user to select an option by reading input using...

  • Complex number class:: Question Changed to just the Java portion Design a class in and Java...

    Complex number class:: Question Changed to just the Java portion Design a class in and Java that represents complex numbers and supports important operations such as addition, subtraction, multiplication and division. (the following is for the c++ and python version, not sure if its needed for this) op: Complex × Complex → Complex op: Complex × double → Complex op: double × Complex → Complex Where op is one of +, -, *, or /. In addition, you will need...

  • Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int...

    Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int = 0, int = 1 ); // default constructor Rational addition( const Rational & ) const; // function addition Rational subtraction( const Rational & ) const; // function subtraction Rational multiplication( const Rational & ) const; // function multi. Rational division( const Rational & ) const; // function division void printRational () const; // print rational format void printRationalAsDouble() const; // print rational as...

  • C++ must use header files and implementation files as separate files. I’ll need a header file,...

    C++ must use header files and implementation files as separate files. I’ll need a header file, implementation file and the main program file at a minimum. Compress these files into one compressed file. Make sure you have adequate documentation. We like to manipulate some matrix operations. Design and implement a class named matrixMagic that can store a matrix of any size. 1. Overload the addition, subtraction, and multiplication operations. 2. Overload the extraction (>>) and insertion (<<) operators to read...

  • Please use 2 new classes! Overview Develop an interactive multi-purpose calculator to input, calculate, and display...

    Please use 2 new classes! Overview Develop an interactive multi-purpose calculator to input, calculate, and display results for operations on a variety of mathematical types. The different types include a basic Floating-point type, a Vector type, and a Fraction type. Write a system that allows a user to perform basic operations on each of these types by entering the type of calculator, operation, and left and right operands. The system will perform the selected operation, print the results, and continue...

  • General Description: A rational number is of the form a/b, where a and b are integers,...

    General Description: A rational number is of the form a/b, where a and b are integers, and b is not equal 0. Develop and test a class for processing rational numbers. Details: Your program should have 3 files: a driver file to test the operations, a header file for the class definition and any operator overloads you need, and an implementation file with the definitions of the items in the header file. The class should read and display all rational...

  • A protocol is a set of rules that define some operation. For example, the Internet Protocol...

    A protocol is a set of rules that define some operation. For example, the Internet Protocol (IP) specifies how messages are routed throughout the internet. In this problem, you are asked to implement your 1st messaging protocol (MP1). MP1 is a binary protocol that is used to efficiently encode a set of arithmetic operations using a 4-byte data type (e.g., int). The specification of the protocol is as follow. Using a 4-byte (32-bit stream), MP1 messages encode the ‘+’, ‘-‘,...

  • C++ A rational number is of the form a/b, where a and b are integers, and...

    C++ A rational number is of the form a/b, where a and b are integers, and b is not equal 0. Develop and test a class for processing rational numbers.Pointers aren't needed and it should be able to handle all of these examples and more. Details: Your program should have 3 files: a driver file to test the operations, a header file for the class definition and any operator overloads you need, and an implementation file with the definitions of...

  • Objectives: 1. Classes and Data Abstraction?2. User-defined classes?3. Implementation of a class in separate files Part...

    Objectives: 1. Classes and Data Abstraction?2. User-defined classes?3. Implementation of a class in separate files Part 1: In this assignment, you are asked: Stage1:?Design and implement a class named memberType with the following requirements: An object of memberType holds the following information: • Person’s first name (string)?• Person’s last name (string)?• Member identification number (int) • Number of books purchased (int)?• Amount of money spent (double)?The class memberType has member functions that perform operations on objects of memberType. For the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT