Choose the best prototype for a method to input an object of the class Rational
ID: 3858105 • Letter: C
Question
Choose the best prototype for a method to input an object of the class Rational from the user. (Recall that a rational number - or improper fraction - has a numerator and denominator: 1/2, -4/5, 7/3, 6, etc.) But not 3 2/3 nor 0.96. A) Rational Rational:: input (void) const: B) Rational Rational:: input(void): C) bool Rational:: input(void): D) bool Rational:: input(long & numer, long & denom) const: E) bool Rational:: input(Rational & rat) const: Now show a typical call to the method whose prototype you chose above. (Typical meaning how you should use it in a program that needed to input a Rational number.) (Include any necessary declarations or other statements for it to work.) The purpose of an accessor method in a class is ______ A) allowing the caller direct access to a private data member of the class B) allowing the caller to view a copy of the value in a private data member of the class C) allowing novel usage patterns not conceived of by the class programmer D) allowing the caller to change the private data members of a class E) singing The purpose of a mutator method in a class is _____. A) encapsulating any error checking and/or validity checking codes necessary to ensure that the class' data members are not butchered, garbled, or otherwise messed up/destroyed B) allowing the caller to view the private data member s current value C) allowing the caller to perform controlled changes to the specified private data member of the class D) allowing novel usage patterns not conceived of by the class programmer E) dancing A class' copy constructor is automatically called in three (3) situations. What/When are they? A) when one object (which already exists) is assigned to be an exact copy of another object (which also already exists) B) when one object is declared and at the same time initialized with another object that already exists C) when a function accepts an argument using the value mechanism, the formal argument is copy constructed from the actual argument D) when a function returns its result using the value mechanism, the result given back to the caller is copy constructed from the value of the return expression E) when the programmer forgets to initialize a newly declared object to a particular value/state In relation to other parts of a program, where would a class definition be placed? What about the definitions of [non-inline) class methods?Explanation / Answer
Ans 18) a) Rational Rational::input (void) const;
Ex: #include <iostream>
#include <conio.h>
using namespace std;
class Rational
{
private:
float a;
public:
Rational(): nInput(0)
{}
void getValues()
{
cout << "Enter number: ";
cin >> nInput;
}
void showValues()
{
cout << nInput << endl;
}
Rational operator + (Rational) const;
};
Rational Rational::operator + (Rational arg2) const
{
Rational temp;
temp.numInput = numInput + arg2.numInput;
return temp;
}
Ans 19) The purpose of accessor method in class is
a) allowing the caller direct access to private data member of the class.
Explaination: Accessors are an entity which enables the class private data members which we cannot access easily. We can understand the use of accessor as:
class A{
private: int x;
public: int calc();
};
int A::calc(){
return x*x;
}
The purpose of mutator method in class is
c) allowing th caller to perform controlled changes to specified private data member of the class.
Explaination: Mutators are an entity by which we can modify the private data member. We can understand this as:
class B{
private: int a;
public: void CalcSq(int);
};
void B::CalcSq(int y){
a=y*(y+1);
}
Ans 20) a) when one object is assigned to be an exact copy of another object.
c) when a function accepts an argument using the value mechanism, the formal argument is copy constructed from actual argument.
d) when a function returns its result using the value mechanism, the result given backto the caller is copy constructed from the value of return expression.
Explaination: A copy constructor is called in following 3 scenarios:
1) When we forge copy of an object.
2) When we pass an object as argument by value to a method.
3) When we return an object from a method by value.
Ans 21) A class definition is placed in header file because it is easy to define and understand the class. Also, an inline class definition enables the compiler to access the definition in a quick fashion to compile the code.
Non-inline methods of a class can be defined outside the class but are marked and defined with keyword 'inline'. We can define them as:
class Box{
void calcArea(int a, int b);
}
inline void Box::calcArea(int x,int y){
//code stub of the method
}