Question

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

#include "plainbox.h"

...

void main(){

PlainBox box1, box2(7.9);

    box1.setItem(1.5);

cout<< box1.getItem()<<endl;

  cout<< box2.getItem()<<endl;

if( box1.getItem() < box2.getItem() )

   box2.setItem( box1.getItem() );

  cout<< box1.getItem()<<endl;

  cout<< box2.getItem()<<endl;

double d =  box1.getItem() * 10;

box2.setItem(d);

  cout<< box1.getItem()<<endl;

  cout<< box2.getItem()<<endl;

}

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

Here is the solution to above program . Please read step by step for full explanation also give a thumbs up if you like the solution

1) The above class is of template type hence it can make plain Box containg any datatype of members

The first output statement in main are

PlainBox box1, box2(7.9);

box1.setItem(1.5);

cout<< box1.getItem()<<endl;

cout<< box2.getItem()<<endl;

Here we are using constructor and mutator/accessor method to set box1 item to 1.5 and box2 item to 7.9 hence output will be

1.5

7.9

2)

if( box1.getItem() < box2.getItem() )

   box2.setItem( box1.getItem() );

  cout<< box1.getItem()<<endl;

  cout<< box2.getItem()<<endl;

In the above lines we are comparing if box1 is lesser than box2 which is true since 1.5<7.9 therefore box 2 item will be set to box1 item value hence both box1 and box2 hold 1.5 as value

there output will be

1.5

1.5

3)

double d =  box1.getItem() * 10;

box2.setItem(d);

cout<< box1.getItem()<<endl;

cout<< box2.getItem()<<endl;

In the final statements we are again modifying the value of box2 by box1*10 hence the value of box2 will be 1.5*10 =15 and box1 value will be 1.5 so output will be

1.5

15

HENCE COMBINING STEP 1 , 2 and 3 we get final output as

1.5

7.9

1.5

1.5

1.5

15

Add a comment
Know the answer?
Add Answer to:
Trace the following program. What is the output after execution? typedef double T; class PlainBox{ private:...
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);

  • C++ Error. I'm having trouble with a pointer. I keep getting the error "Thread 1: EXC_BAD_ACCESS...

    C++ Error. I'm having trouble with a pointer. I keep getting the error "Thread 1: EXC_BAD_ACCESS (code=1, address=0x18)" The objective of the assignment is to revise the public method add in class template LinkedBag so that the new node is inserted at the end of the linked chain instead of at the beginning. This is the code I have: linkedBag-driver.cpp BagInterface.hpp LinkedBag.hpp Node.hpp int main) LinkedBag<string> bag; cout << "Testing array-based Set:" << endl; cout << "The initial bag is...

  • The following code is for Chapter 13 Programming Exercise 21. I'm not sure what all is...

    The following code is for Chapter 13 Programming Exercise 21. I'm not sure what all is wrong with the code written. I get errors about stockSym being private and some others after that.This was written in the Dev C++ software. Can someone help me figure out what is wrong with the code with notes of what was wrong to correct it? #include <cstdlib> #include <iostream> #include <iomanip> #include <fstream> #include <cassert> #include <string> using namespace std; template <class stockType> class...

  • C++ programming language: In this program you will create a simplified bag that acts like a...

    C++ programming language: In this program you will create a simplified bag that acts like a stack meaning that the Last item inserted is the First Item that comes out. Your backend implementation must use a linked list. The code should pass the test (there's only 1) and there should be no memory leaks. Note that passing the test does not ensure full credit! The functions are listed in the suggested order of implementation but you may implement them in...

  • In this assignment you are required to complete a class for fractions. It stores the numerator...

    In this assignment you are required to complete a class for fractions. It stores the numerator and the denominator, with the lowest terms. It is able to communicate with iostreams by the operator << and >>. For more details, read the header file. //fraction.h #ifndef FRACTION_H #define FRACTION_H #include <iostream> class fraction { private: int _numerator, _denominator; int gcd(const int &, const int &) const; // If you don't need this method, just ignore it. void simp(); // To get...

  • A library maintains a collection of books. Books can be added to and deleted from and...

    A library maintains a collection of books. Books can be added to and deleted from and checked out and checked in to this collection. Title and author name identify a book. Each book object maintains a count of the number of copies available and the number of copies checked out. The number of copies must always be greater than or equal to zero. If the number of copies for a book goes to zero, it must be deleted from the...

  • // thanks for helping // C++ homework // The homework is to complete below in the...

    // thanks for helping // C++ homework // The homework is to complete below in the stack.h : // 1. the copy constructor // 2. the assignment operator // 3. the destructor // 4. Write a test program (mytest.cpp) to test copy and assignment // 5. Verify destructor by running the test program in Valgrind // This is the main.cpp #include <iostream> #include "stack.h" using namespace std; int main() { Stack<int> intStack; cout << "\nPush integers on stack and dump...

  • Double linked list implementation of PutItem function. How to fix my code to get desired output b...

    Double linked list implementation of PutItem function. How to fix my code to get desired output below: Output: 2 5 8 #ifndef ITEMTYPE_H #define ITEMTYPE_H enum RelationType { LESS, GREATER, EQUAL}; class ItemType { public:     ItemType();     void setValue(int newValue);     int getValue() const;     RelationType ComparedTo(ItemType newItem); private:     int value; }; #endif // ITEMTYPE_H // ItemType.cpp #include "ItemType.h" ItemType::ItemType() {     value = 0; } void ItemType::setValue(int newValue) {     value = newValue; } int ItemType::getValue() const {     return value; } RelationType ItemType::ComparedTo(ItemType newItem)...

  • For the LinkedList class, create a getter and setter for the private member 'name', constructing your...

    For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...

  • Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and...

    Please zoom in so the pictures become high resolution. I need three files, FlashDrive.cpp, FlashDrive.h and user_main.cpp. The programming language is C++. I will provide the Sample Test Driver Code as well as the codes given so far in text below. Sample Testing Driver Code: cs52::FlashDrive empty; cs52::FlashDrive drive1(10, 0, false); cs52::FlashDrive drive2(20, 0, false); drive1.plugIn(); drive1.formatDrive(); drive1.writeData(5); drive1.pullOut(); drive2.plugIn(); drive2.formatDrive(); drive2.writeData(2); drive2.pullOut(); cs52::FlashDrive combined = drive1 + drive2; // std::cout << "this drive's filled to " << combined.getUsed( )...

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