the output of this program should be this:
Can anybody explain why the output of this main is:
F1/2 F2/3 F5/4 F0/1 F0/1 F0/1 F0/1 F0/1
K0/1 K0/1
K?/? K2/3 K1/2
can you explain how we get the last 2 lines ? thanks
the constructor is initialize like this in fraction.h
Fraction(int n=0, int d=1);
/* fraction.cpp */
#include "fraction.h"
#include <iostream>
using namespace std;
Fraction::Fraction(int n, int d)
: numerateur(n)
{
dedominateur = d;
cout << "F" << n << '/' << d << ' ';
simplifier();
}
Fraction::~Fraction(){
//cout<<"destructeur";
cout << "K"
<< numerateur << '/'
<< dedominateur << ' ';
numerateur = dedominateur = 0;
}
void Fraction::simplifier(){/*...*/}
/* prog1.cpp */
#include <iostream>
#include "fraction.h"
using namespace std;
void test(Fraction a, Fraction& b){
Fraction* c = new Fraction(a);
a = b;
b = *c;
c = NULL;
cout<< "F";
return;
}
int main(){
Fraction f1(1,2), f2(2,3), f3(5,4);
Fraction* tab = new Fraction[5];
std::cout << std::endl;
test(f1, tab[2]);
test(tab[3], tab[4]);
f3 = tab[5];
std::cout << std::endl;
return 0;
}
The output with a K
obviously comes from the destructor. Two of them would be from the parameter a
which is passed by value (copy) to test
. The other three are the ones on the first line of main
.
The objects created by new
are never delete
'd, so they are never destructed.