write a minimal driver program ( main()) test your solution
declare a classes function to model the rational number write minimal code for class declaration function including constructor set and get function and overload operator + and *
for C++
Given below is the code for the question. Please do rate the answer if it helped. Thank you.
Rational.h
=====
class Rational
{
private:
int numerator;
int denominator;
int gcd(int a, int b);
void simplify();
public:
Rational(int num = 0, int den =
1);
int getNumerator();
int getDenominator();
Rational operator +(const Rational
&rhs);
Rational operator *(const Rational
&rhs);
void display();
};
Rational.cpp
------
#include "Rational.h"
#include <iostream>
using namespace std;
void Rational::simplify(){
int g = gcd(numerator, denominator);
numerator /= g;
denominator /= g;
}
Rational::Rational(int num , int den){
numerator = num;
denominator = den;
if(den < 0){
numerator = -numerator;
denominator = -denominator;
}
simplify();
}
int Rational::getNumerator(){
return numerator;
}
int Rational::getDenominator(){
return denominator;
}
Rational Rational::operator + (const Rational &rhs){
int n = numerator * rhs.denominator + rhs.numerator *
denominator;
int d = denominator * rhs.denominator;
return Rational(n, d);
}
Rational Rational::operator *(const Rational &rhs){
int n = numerator * rhs.numerator;
int d = denominator * rhs.denominator;
return Rational(n, d);
}
void Rational::display(){
cout << numerator << "/" <<
denominator << endl;
}
int Rational::gcd(int a, int b){
if(a < 0)
a = -a;
if(b < 0)
b = -b;
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}
Test.cpp
-----
#include <iostream>
#include "Rational.h"
using namespace std;
int main(){
Rational r1(1, 2);
Rational r2(3, 4);
cout << "r1 = ";
r1.display();
cout << "r2 = ";
r2.display();
cout << "r1 + r2 = ";
Rational r3 = r1+ r2;
r3.display();
cout << "r1 * r2 = ";
Rational r4 = r1 * r2;
r4.display();
return 0;
}
output
----
r1 = 1/2
r2 = 3/4
r1 + r2 = 5/4
r1 * r2 = 3/8
write a minimal driver program ( main()) test your solution declare a classes function to model...
(C++ Program) 1. Write a program to allow user enter an array of structures, with each structure containing the firstname, lastname and the written test score of a driver. - Your program should use a DYNAMIC array to make sure user has enough storage to enter the data. - Once all the data being entered, you need to design a function to sort the data in descending order based on the test score. - Another function should be designed to...
C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...
C++ program to implement inheritance with following requirements, Classes :- Animal.cpp, bird.cpp, canine.cpp, main.cpp (to test it) :- -You may need getters and setters also. 1. Declare and define an animal base class: animal stores an age (int), a unique long ID (a boolean that is true if it is alive, and a location (a pair of double). animal requires a default constructor and a 3 parameter constructor. Both constructors should set the unique ID automatically. They should also set...
C++ IMPORTANT: Write a driver program (PackageDriver) that creates objects of each type of Package as pointers to a Package and cout the objects. Write a driver program (PackageDriver) that creates objects of each type of Package as pointers to a Package and cout the objects. Write a driver program (PackageDriver) that creates objects of each type of Package as pointers to a Package and cout the objects. Package-delivery services, such as FedEx®, DHL® and UPS®, offer a number of...
Write a C++ program which has a main-driver and creat a polygon class Poly which has an array of n pairs of floats, x[i] and y[i], creat a derived class Triangle, creat a derived class Quadrilateral class (which you may assume is convex and points given clockwise) You need to compute the area for the two derived classes but use inheritance to compute perimeter in all these classes. Constructors, accessors, mutators, and anything else needed should be written too. Make...
write a C++ program using classes, friend functions and overloaded operators. Computer class: Design a computer class (Computer) that describes a computer object. A computer has the following properties: Amount of Ram in GB (examples: 8, 16), defaults to 8. Hard drive size in GB (examples: 500, 1000), defaults to 500. Speed in GHz (examples: 1.6, 2.4), defaults to 1.6. Type (laptop, desktop), defaults to "desktop" Provide the following functions for the class: A default constructor (constructor with no parameters)...
I need the soft code ( Outline with comments ) of the main program that reads in the employee data with the sorting, reading, and average functions as well as the class ( header file ). No hard code is necessary. Thank you! Consider a program that will read employee data, sort the data by employee identification number, write out the sorted data, and compute various statistics about the data, such as the average age of an employee. Design a...
Please use Java to write the program The purpose of this lab is to practice using inheritance and polymorphism. We need a set of classes for an Insurance company. Insurance companies offer many different types of policies: car, homeowners, flood, earthquake, health, etc. Insurance policies have a lot of data and behavior in common. But they also have differences. You want to build each insurance policy class so that you can reuse as much code as possible. Avoid duplicating code....
Assignment Overview In Part 1 of this assignment, you will write a main program and several classes to create and print a small database of baseball player data. The assignment has been split into two parts to encourage you to code your program in an incremental fashion, a technique that will be increasingly important as the semester goes on. Purpose This assignment reviews object-oriented programming concepts such as classes, methods, constructors, accessor methods, and access modifiers. It makes use of...
/*--------------------------------------------------------------------------- // AUTHOR: // SPECIFICATION: This program is for practicing the use of classes, constructors, // helper methods, and the this operator. // INSTRUCTIONS: Read the following code skeleton and add your own code // according to the comments //-------------------------------------------------------------------------*/ import java.util.Scanner; public class { public static void main(String[] args) { // Let's make two students using all two constructors // Write code to create a new student alice using constructor #1 //--> Student alice = // Write code to...