C++ Questions, please help TRUE/FALSE By passing a string argument to a function
ID: 3858317 • Letter: C
Question
C++ Questions, please help
TRUE/FALSE By passing a string argument to a function, we can often make that function more re-usable. TRUE/FALSE This is especially true for functions that control input-prompts make ideal candidates for string arguments. TRUE/FALSE As always, we like to pass strings by reference to avoid the nasty copying issues of value arguments. TRUE/FALSE But, since we aren't going to change the prompts, we typically make the argument a constant reference-fast from not copying and still protected from accidental change like a value argument! TRUE/FALSE Being constant reference (instead of plain reference), we can even provide default values for these arguments! (A common one for prompt is ""-the empty string.) Could a class have the following two methods in it and still compile/run fine? void f(void): void f(void) const: How would the compiler know which one to call?! Is the inline keyword needed to inline a class method? Why/Why not? Show how to rewrite the following method definition as a single line. (See the explanation in #18 about what a rational number is.) Rational Rational:: divide (const Rational & r) const { Rational t: t = r. reciprocal(): t = multiply(t): return t: } Other than those you chose above (#24), what methods might/should a complex class include? A) Complex add(const Complex & c) const: B) Complex add(double x) const: C) Complex reciprocal (void) const: D) Complex conjugate (void) const: E) double magnitude (void) const: Why is it that the mutator of a rational class is difference in that it works with both data members simultaneously whereas a Complex class has separate mutators for its two [numeric] data members? Don't we normally follow that latter tactic? What's up with the Rational class mutator?!Explanation / Answer
38
a True
we can send different strings as argument to function to make the function more reusable
b True
c True
d True
e True
example:
void f(const string &s = " ")
{
}
int main() {
f("abc");
return 0;
}
39. Yes , it will compile.
#include <iostream>
using namespace std;
class c
{
private:
int x;
public:
void f(void)
{
x = 12;
cout<<" "<<x;
}
void f(void)const
{
// x++;// error as const function cannot change member variables and cannot call any non const member function
cout<<" "<<x;
}
};
int main() {
c obj;
obj.f();
return 0;
}
output:
12
Only the const object of the class can call the const function of the class by the compiler.
40.
No you can define function inside the class definition to make it inline.
41.
Rational::Rational divide(const Rational &r) const
{
return Rational(multiply(r.reciprocal));
}
42.
Complex add(const Complex &c) const;
43.
Because in complex numbers, real and imaginary numbers are added,subtracted etc separately where as in rational numbers the numerator and denominator are both used together in operations. We can reduce the rational number but we don't need to reduce the complex numbers.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.