Question
What to do: 1. The class definition for Ternary in a text file named ternary.h that meets the specification above. Give brief but convincing arguments,
What to do: 1. The class definition for Ternary in a text file named ternary.h that meets the specification above. Give brief but convincing arguments, as comments, for your choice of class member access restriction (i.e., public/private). Here is an example of that:
#ifndef RATIONAL_H #define RATIONAL_H #includeusing namespace std; class Rational { public: Rational(); Rational(int, int); Rational plus(const Rational&) const; Rational minus(const Rational&) const; Rational times(const Rational&) const; Rational divide(const Rational&) const; void insert(ostream& ); void extract(istream&); private: int numerator; int denominator; }; #endif
2. In a text file named ternary.cc, implement the constructor functions and the member function insert. Here is an example of that:
Rational::Rational() {
numerator = 0;
denominator = 1;
}
Rational::Rational(int num, int den){
if (den == 0)
{
denominator = 1;
numerator = num;
cout
}
else {
numerator = num;
denominator = den;
}
}
Rational Rational::plus(const Rational& r) const{
int c = r.numerator;
int d = r.denominator;
int a = numerator;
int b = denominator;
return Rational(a*d + b*c, b*d);
}
Rational Rational::minus(const Rational&r) const{
int c = r.numerator;
int d = r.denominator;
int a = numerator;
int b = denominator;
return Rational(a*d - b*c, b*d);
}
Rational Rational::times(const Rational&r) const{
int c = r.numerator;
int d = r.denominator;
int a = numerator;
int b = denominator;
return Rational(a*c, b*d);
}
Rational Rational::divide(const Rational&r) const{
int c = r.numerator;
int d = r.denominator;
int a = numerator;
int b = denominator;
return Rational(a*d, b*c);
}
void Rational::insert(ostream& sout){
if (denominator == 1){
sout
return;
}
sout
return;
}
void Rational::extract(istream& sin){
sin >> numerator >> denominator;
return;
}
3. Write a test program in a file named test ternary.cc to test the implemented member functions (see, for example, Rational class test program). For the implemented constructors your test code should show the creation of Ternary objects with dierent values (negative, positive) for both valid and invalid Ternary objects. Here is an example of that:
#include
#include "rational.h"
using namespace std;
int main() { Rational p,q, r; Rational(); cout
Rational s(1,0); cout
r = p.plus(q); cout
r = p.times(q); cout
r = p.divide(q);
p.insert(cout); cout
}
Note: I do not want you to send me those three files, just the implemented codes inside them, but of course you have to create the required files to make sure everything works properly. Also do not forget to breifly comment on functions etc.
In this assignment you will define, mplement, and test a C++ class called Ternary to represent and use whole numbers (positive or negative) in base-three number system In the decimal system or base-ten number system we have 10 symbols or numerals 0, 1.2.9 representing values zero, one, two,., nine, respectively. In the ternary number system there are only three symbols 0, 1, 2 representing values zero, one, two, respectively. For a multi-digit whole number e.g., 122, its value in decimal system is computed as (122)1- 1102210210one undred twenty two. In the ternary number system s value ls (122h = 1+32 +2 * 31 + 2+30-1710. Your task is to design, mplement, and test a class named Ternary according to the following specification. A ternary number is represented by its equivalent decimal value in the class. Thus, for example, ternary umber 122)3 is actually stored as the decimal integer 17. Therefore, the only data member in class Ternary is an int object named decimal equivalent Code snippets and their intended meaning given below are used to ustrate the member functions that you need to include in class Ternary 1. Define two different constructors to create Ternary class objects in a consistent state. Ternary a b(122), c(241); // create objects of class type Meaning. Object a is created and the data member decimal equivalent is initial ized with value 0. Object b is a valid ternary number whose decimal value is 17; the data member decimal equivalent is initialized with value 17. Object c is an invalid ternary number and the constructor must give an error message and the data member decimal equivalent is initialized with value 0 2. Other member functions you need to have in class Ternary Ternary d (201) // d is created b-plus (d) ; Ternary class object a contains the sum of Ternary class objects b and b.minus (d) ; a Meaning. a Meaning. Ternary class object a contains the result of Ternary class object d subtracted from Ternary class object b a = b , times (d) ; Meaning. and d. Ternary class object a contains the product of Ternary class objects b a. insert (cout); Meaning. Ternary class object a is displayed on the terminal (standard output) c.extract(cin); Meaning. Ternary class object c is read from the keyboard (standard input)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started