- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Define a Class
#include <iostream>
using std::cout;
using std::endl;
class CRational
{
int numerator;
int denominator;
};
int main()
{
CRational A;
A.numerator = 1;
A.denominator = 3;
cout << A.numerator << '/' << A.denominator << endl;
return 0;
}
- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Constructor
#include <iostream>
using std::cout;
using std::endl;
class CRational
{
int numerator;
int denominator;
public:
CRational(int p, int q = 1)
{
numerator = p; denominator = q;
cout << numerator << '/' << denominator
<< " created." << endl;
}
};
int main()
{
CRational A(1, 3);
CRational B(2);
return 0;
}
- (10%)
Determine whether the following code is correct or not. If it is
correct, predict its output. If it is incorrect, point out the
mistake(s).
// Member Function
#include <iostream>
using std::cout;
using std::endl;
class CRational
{
int numerator;
int denominator;
public:
CRational(int p, int q = 1)
{ numerator = p; denominator = q; }
void Print()
{ cout << numerator << '/' << denominator << endl; }
};
int main()
{
CRational A(2, 5);
Print(A);
return 0;
}