Question

QUESTION 18 According to class materials, the program is allowed to overload operators only if at...

QUESTION 18

  1. According to class materials, the program is allowed to overload operators only if at least one incoming parameter received by the operator has a class type.

    1.

    (a) True

    2.

    (b) False

QUESTION 19

  1. According to the course materials, a function can take value parameters and reference parameters. A value parameter is a separate variable from the one in main() or another calling function only if the function value parameter has a different name than the one in main() or the calling function.

    1.

    (a) True

    2.

    (b) False

QUESTION 19

  1. According to the course materials, a function can take value parameters and reference parameters. A value parameter is a separate variable from the one in main() or another calling function only if the function value parameter has a different name than the one in main() or the calling function.

    1.

    (a) True

    2.

    (b) False

QUESTION 21

  1. Suppose you have a programmer-defined class called Data and want to overload the << operator to output the private attributes of Data to the screen in the form cout << dataObject; where dataObject is an object of class Data. In your definition of the function to overload the << operator, you have:

    X              // X is first line of the definition of the function to overload the << operator   
    {
        // the body of the function to overload the << operator
    }


    What would the correct X, the first line of the definition of the function to overload the << operator?

    1.

    (a) ostream& operator<<(ostream& output, const Data& dataObject)

    2.

    (b) ostream operator<<( ostream& output, const Data& dataObject)

    3.

    (c) ostream& operator<<(ostream output, const Data& dataObject)

    4.

    (d) ostream operator<<(ostream output, const Data& dataObject)

    5.

    (e) ostream& operator<<(const Data & dataObject, ostream& output)

    6.

    (f) ostream operator<<(const Data & dataObject, ostream& output)

    7.

    (g) ostream operator<<(const Data & dataObject, ostream output)

    8.

    (h) None of (a) through (g) is a correct answer.

Class Book has a private attribute called price (double). A method of class Book is called set_price_the_same_as(). It has a Book object model_b as the incoming parameter (as shown below). This method is called for setting the value of the private attribute priceof the implicit parameter, so that its price will be the same as that of model_b. Besides, there is a get() method and a set() method for each private attribute of Book and they are working.

  class Book
   {
      public:
         .....
         void set_price_the_same_as(Book model_b);
       .....
      private:
         .....
         double price;
       .....
    } //end of classs Book declaration

    ........

   void Book::set_price_the_same_as(Book model_b)
    {
       price = model_b.price ;
       model_b.price = 10000.0;

    }

  int main()
   {
      Book b2("Model", 90.0); //instantiating a b2 Book object with name "Model" and price $90.0
      Book b1("C++ Text", 40.0); //instantiating a b1 Book object with name "C++ Text" and price $40.00

      b2.set_price_the_same_as(b1);

      cout << "Price of Book b1 is: " << b1.get_price() << endl; //<==Statement X

      cout << "Price of Book b2 is:" << b2.get_price() << endl; //<=== Statement Y

      .......
   } //end of main()


When Statement Y is executed, it will display:

Price of Book b2 is: ____________

1.

(a) 40.0

2.

(b) 90.0

3.

(c) 10000.0

4.

(d) 130.0

5.

(e) None of (a) through (d) is the correct answer.

QUESTION 23

  1. According to class materials, if we define class MyInterface, class Class1, and class Class2 as follows:
    class MyInterface  
    {
        public:
            void Display();
    };
    void MyInterface::Display()
    {
           cout << "#Weeks";
    }


    class Class1 : public MyInterface
    {
       public:
           void Display();
    };
    void Class1::Display()
    {
           string a = "#Week1";
           cout << a;
    }


    class Class2 : public MyInterface
    {
       public:
           void Display();
    };
    void Class2::Display()
    {
           cout <<"#Week2";
    }


    What is the output when the following main() function is executed?
    int main()
    {
        Class1 obj1;
       obj1.Display();
       Class2 obj2;
       obj2.Display();
       return 0;
    }

    1.

    (a) Nothing would be displayed.

    2.

    (b) #Weeks#Weeks

    3.

    (c) #Week1#Week1

    4.

    (d) #Week2#Week2

    5.

    (e) #Week1#Week2

    6.

    (f) None of (a) through (e) is a correct answer.

QUESTION 25

  1. According to the course material, when a derived class inherits from a base class, the base class is usually the more specialized class while the derived class the more general class.

    1.

    (a) True

    2.

    (b) False

QUESTION 26

  1. According to the class material, if we define class A and class B as follows:

    class A
    {
        public:
           A(double d);
    };
      
    A::A(double d)
    {
           cout << d;
    }
            
    class B : public A
    {
        public:
             B(int n, double d);             
    };

    B::B(int n, double d) : A(d)
    {
           cout << n;
    }



    In the main() program, when we create an object of class B by using the following statement:

    B b(5, 4.3);

    What would be displayed on the console by this statement, assuming that the rest of the code in main() function is compiled successfully and execute successfully?

    1.

    (a) Nothing would be displayed, but the object b would be created.

    2.

    (b) 5

    3.

    (c) 4.3

    4.

    (d) 9.3

    5.

    (e) 4.35

    6.

    (f) 54.3

    7.

    (g) None of (a) through (f) is a correct answer.

QUESTION 27

  1. According to class material, when we allocate a new heap variable/object using new operator, the memory allocator tells where it is located by providing the memory address, so we must declare a pointer variable to hold this address and we must use this pointer to manipulate the variable/object.

    1.

    (a) True

    2.

    (b) False

QUESTION 28

  1. According to class materials, the correct function name for overloading the addition (+) operator is:

    1.

    (a) operator(+)

    2.

    (b) operator+

    3.

    (c) operator:+

    4.

    (d) operator_+

    5.

    (e) None of (a) through (d) is a correct answer

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Dear Student ,

As per the requirement submitted above , kindly find the below solution.

Question 18:

Answer :a.true

Explanation :the program is allowed to overload operators only if at least one incoming parameter received by the operator has a class type.

**************************

Question 19:

Answer :(b) False

Explanation :Value parameters name can be same at the function call and function definition.

*************************

Question 23:

Answer :(e) #Week1#Week2

Explanation :Obj1.Display() will call method from Class1 and  obj2.Display(); will call method from Class2.

******************************

Question 25:

Answer :(b) False

Explanation :Base class is more generic class and derived class is more specific class.

***************************

Question 26:

Answer :e.4.35

Explanation :

  • While calling  B b(5, 4.3); , constructor of class B is called.
  • But the constructor calling A(d) and hence first value of d that is 4.3 will be printed by A's class constructor and then value of n is printed by class B 's constructor that is 5.

******************************

Question 21 :

Answer :(a) ostream& operator<<(ostream& output, const Data& dataObject)

******************************

Question 28 :

Answer :(b) operator+

Explanation :operator+ is the correct function name for overloading the addition (+) operator.

******************************

NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.

Add a comment
Know the answer?
Add Answer to:
QUESTION 18 According to class materials, the program is allowed to overload operators only if at...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Implement the friend function to overload the stream insertion operator (<<) so that the value contained...

    Implement the friend function to overload the stream insertion operator (<<) so that the value contained in the PlainBox object can be output using the << operator. The prototype is given in the header file. plainbox.h typedef double T; class PlainBox{ private: Titem; public: PlainBox(): PlainBox(const T& theltem): void setItem(const T& itemltem); T getitem() const; friend ostream& operator<<(ostream & out, const PlainBox & theBox); bool operator<(const PlainBox & theBox);

  • The following program contains the definition of a class called List, a class called Date and...

    The following program contains the definition of a class called List, a class called Date and a main program. Create a template out of the List class so that it can contain not just integers which is how it is now, but any data type, including user defined data types, such as objects of Date class. The main program is given so that you will know what to test your template with. It first creates a List of integers and...

  • Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor...

    Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &); //copy constror Matrix(int row, int col); Matrix operator+(const Matrix &)const; //overload operator“+” Matrix& operator=(const Matrix &); //overload operator“=” Matrix transpose()const; //matrix transposition void display()const; //display the data private: int row; //row int col; //column int** mat; //to save the matrix }; //main.cpp #include <iostream> #include"Matrix.h" using namespace std; int main() { int row, col; cout << "input the row and the col for Matrix...

  • Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &);...

    Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &); //copy constror Matrix(int row, int col); Matrix operator+(const Matrix &)const; //overload operator“+” Matrix& operator=(const Matrix &); //overload operator“=” Matrix transpose()const; //matrix transposition void display()const; //display the data private: int row; //row int col; //column int** mat; //to save the matrix }; //main.cpp #include <iostream> #include"Matrix.h" using namespace std; int main() { int row, col; cout << "input the row and the col for Matrix...

  • Overload the + operator as indicated. Sample output for the given program: First vacation: Days: 7,...

    Overload the + operator as indicated. Sample output for the given program: First vacation: Days: 7, People: 3 Second vacation: Days: 12, People: 3 #include <iostream> using namespace std; class FamilyVacation{ public: void SetNumDays(int dayCount); void SetNumPeople(int peopleCount); void Print() const; FamilyVacation operator+(int moreDays); private: int numDays; int numPeople; }; void FamilyVacation::SetNumDays(int dayCount) { numDays = dayCount; return; } void FamilyVacation::SetNumPeople(int peopleCount) { numPeople = peopleCount; return; } // FIXME: Overload + operator so can write newVacation = oldVacation +...

  • Trace the following program. What is the output after execution? typedef double T; class PlainBox{ private:...

    Trace the following program. What is the output after execution? typedef double T; class PlainBox{ private: T item; public: PlainBox();   PlainBox(const T& theItem); void setItem(const T& itemItem); T getItem() const; friend ostream & operator<<(ostream & out, const PlainBox & theBox);   bool operator<(const PlainBox & theBox); }; PlainBox::PlainBox(){} PlainBox::PlainBox( const ItemType & theItem){ item = theItem; } void PlainBox:: setItem(const T& theItem){ item = theItem; } T PlainBox:: getItem () const { return item; } //remaining implementation is not shown source.cpp...

  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

  • Redesign your Array class from lab6 as a class template to work with the application below....

    Redesign your Array class from lab6 as a class template to work with the application below. write your overloaded output stream operator as an inline friend method in the class declaration. Include the class template header file in your application as below. #include "Array.h" main() {   Array<char> c(3);   c.setValue(0,'c');   c.setValue(1,'s');   c.setValue(2,'c');   cout << c;   Array<int> i(3);   i.setValue(0,1);   i.setValue(1,2);   i.setValue(2,5);   cout << i;   Array<int> j(3);   j.setValue(0,10);   j.setValue(1,20);   j.setValue(2,50);   cout << j;   Array<int> ij;   ij = i + j;   cout << ij;...

  • Following class is only part of my program that uses a hash table to simulate a...

    Following class is only part of my program that uses a hash table to simulate a market's client database, client class is my main class which asks user to input client names and then creates a hash table which then users can look up client names. wasn't able to upload everything as it is too much code, but what I need is to modify my client class instead of me inputting data line by line, the program should read from...

  • Write a MyString class that stores a (null-terminated) char* and a length and implements all of...

    Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator: prints "Move assignment" and endl in addition...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT