I need to do class inheritance. There are 3 classes: Base class Pair, and 2 derived clases from Pair :Rational and Complex.
I have no question about class Complex, so I will post code only for class Pair and Rational.
Class Pair must contain 2 real type data members, as you see I named them "double a" and "double b". In class Rational, "a=numerator" and "b=denominator".
In class Complex, "a=real" and "b=imaginary"
Base class Pair also contains operator <<.
Each class , Rational and Complex must contain:
*constructor;
*operator+;
*operator*;
My question is about Rational class. For example if I put 3/10 + 1/5, result must be 1/2, and I have no ideea how to do that, I'm beginner and I also lost 10 h browsing the internet about how to solve this, but I can't make it. If anyone of you can give me some hints about how to solve this, I will really appreciate :)
Also, I will put here main code, and it must remain untouched.
Code contain: main.cpp, Pair.h, Pair.cpp, Rational.h, Rational.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
>>>.................main.cpp......................<<<
#include <iostream>
#include "Pair.h"
#include "Rational.h"
#include "Complex.h"
using namespace std;
int main()
{
Rational r1(3,10), r2(1,5);
cout << "Rational: " << r1 <<" + "<< r2<<" = "<<r1+r2<< endl;
cout << "Rational: " << r1 <<" * "<< r2<<" = "<<r1*r2<< endl;
// Complex c1(3, 10), c2(1, 5);
// cout << "Complex: " << c1 <<" + "<< c2<<" = "<<c1+c2<< endl;
// cout << "Complex: " << c1 <<" * "<< c2<<" = "<<c1*c2<< endl;
//cout << "Complex: " << c1 + c2 << endl;
//cout << "Complex: " << c1 * c2 << endl;
return 0;
}
>>>.......................Pair.h......................<<<
#ifndef PAIR_H
#define PAIR_H
#include<iostream>
using namespace std;
class Pair
{
friend ostream& operator<<(ostream&, const Pair&);
public:
Pair();
virtual ~Pair();
protected:
double a;
double b;
private:
};
#endif // PAIR_H
>>>.................Pair.cpp...................<<<
#include "Pair.h"
Pair::Pair()
{
//ctor
}
ostream& operator<<(ostream& out, const Pair& val)
{
out<<"("<<val.a<<","<<val.b<<")";
return out;
}
Pair::~Pair()
{
//dtor
}
>>>......................Rational.h.........................<<<
#ifndef RATIONAL_H
#define RATIONAL_H
#include "Pereche.h"
class Rational: public Pair
{
public:
Rational(double=0,double=1);
Rational operator+(const Rational&);
Rational operator*(const Rational&);
//friend Rational operator+(const Rational, const Rational);
// friend Rational operator*(const Rational, const Rational);
int fractie(int , int);
virtual ~Rational();
protected:
private:
#endif // RATIONAL_H
>>>..................Rational.cpp................<<<
#include "Rational.h"
Rational::Rational(double numerator, double denominator)
{
a=numerator;
b=denominator;
}
Rational Rational::operator+(const Rational& r)
{
return Rational(this->a + r.a, this->b + r.b);
}
Rational Rational::operator*(const Rational& r)
{
return Rational(this->a * r.a,this->b * r.b);
}
Rational::~Rational()
{
//dtor
}
|