Question

In: Computer Science

For C++ Consider the following code defining classes for assets and office equipment: #include<string> #include<iostream> using...

For C++

Consider the following code defining classes for assets and office equipment:

#include<string>
#include<iostream>

using namespace std;

// class definition for asset
class Asset{
protected:
  int value; // value of asset in cents
public:
  Asset(int value); // constructor
  int get_value();  // get the value
};

Asset::Asset(int val){   // implementation of constructor
   value=val;
}

int Asset::get_value(){  // implementation of get_value
   return value;
}

// abstract class for office equipment
class OfficeEquipment:public Asset{
public:
   OfficeEquipment(int val); // constructor
   virtual void use_item() =0;  // not implemented here 
};

// constructor
OfficeEquipment::OfficeEquipment(int val):Asset(val){  
   return; // nothing more to do
}

class Pen:public OfficeEquipment{
public:
   Pen(int val); // constructor
   virtual void use_item();  // will define 
};

Pen::Pen(int val):OfficeEquipment(val){
        return; // nothing more to do
}

void Pen::use_item(){
        cout << "scribble, scribble" << endl;
}

class Laptop:public OfficeEquipment{
public:
   Laptop(int val); // constructor
   virtual void use_item();  // will define 
};

Laptop::Laptop(int val):OfficeEquipment(val){
        return; // nothing more to do
}

void Laptop::use_item(){
        cout << "tappity, tap, tap" << endl;
}

int main(){
        //collection of assets
        int len=4;
        Asset* my_things[len];
      
       // complete the code below

       // (1)fill the assets up with 2 pens and two laptops (different values for each)

       // (2)calculate the total value of all assets in my_things and print it out

       // (3) call use_item on each of the items in my_things

}

The code in the main() function is incomplete. Write code as described in each of the comments marked (1), (2) and (3) above. Your code for (2) and (3) should take advantage of polymorphism to keep the code simple and short.

Solutions

Expert Solution

If you have any problem with the code feel free to comment.

Program

#include<string>
#include<iostream>

using namespace std;

// class definition for asset
class Asset
{
protected:
int value; // value of asset in cents
public:
Asset(int value); // constructor
int get_value(); // get the value
};

Asset::Asset(int val) // implementation of constructor
{
value=val;
}

int Asset::get_value() // implementation of get_value
{
return value;
}

// abstract class for office equipment
class OfficeEquipment:public Asset
{
public:
OfficeEquipment(int val); // constructor
virtual void use_item() =0; // not implemented here
};

// constructor
OfficeEquipment::OfficeEquipment(int val):Asset(val)
{
return; // nothing more to do
}

class Pen:public OfficeEquipment
{
public:
Pen(int val); // constructor
virtual void use_item(); // will define
};

Pen::Pen(int val):OfficeEquipment(val)
{
return; // nothing more to do
}

void Pen::use_item()
{
cout << "scribble, scribble" << endl;
}

class Laptop:public OfficeEquipment
{
public:
Laptop(int val); // constructor
virtual void use_item(); // will define
};

Laptop::Laptop(int val):OfficeEquipment(val)
{
return; // nothing more to do
}

void Laptop::use_item()
{
cout << "tappity, tap, tap" << endl;
}

int main()
{
//collection of assets
int len=4;
Asset* my_things[len];

// complete the code below

// (1)fill the assets up with 2 pens and two laptops (different values for each)
my_things[0] = new Pen(6);
my_things[1] = new Pen(8);
my_things[2] = new Laptop(4);
my_things[3] = new Laptop(3);

// (2)calculate the total value of all assets in my_things and print it out
int sum=0;
for(int i=0; i<len; i++)
{
sum+=my_things[i]->get_value();
}
cout<<"The total value of all assets: "<<sum<<endl;

// (3) call use_item on each of the items in my_things
for(int i =0; i<len; i++){
((OfficeEquipment*)my_things[i])->use_item();
}
}

Output


Related Solutions

C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using...
C++ CODE ONLY Using the following code. #include <iostream> #include <string> #include <climits> #include <algorithm> using namespace std; // M x N matrix #define M 5 #define N 5 // Naive recursive function to find the minimum cost to reach // cell (m, n) from cell (0, 0) int findMinCost(int cost[M][N], int m, int n) {    // base case    if (n == 0 || m == 0)        return INT_MAX;    // if we're at first cell...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using...
C++ code Why my code is not compiling? :( #include <iostream> #include <iomanip> #include <string> using namespace std; const int CWIDTH = 26; int main() {    int choice;    double convertFoC, converCtoF;    double starting, endvalue, incrementvalue;    const int CWIDTH = 13;    do {       cin >> choice;    switch (choice)    {        cin >> starting;    if (starting == 28){       cout << "Invalid range. Try again.";    }    while (!(cin >> starting)){       string  garbage;       cin.clear();       getline(cin, garbage);       cout << "Invalid data Type, must be a number....
Complete the following TODO: parts of the code in C++ #include <iostream> #include <string> #include <limits>...
Complete the following TODO: parts of the code in C++ #include <iostream> #include <string> #include <limits> #include <vector> using namespace std; // // CLASS: NODE // class Node{ public: int value = 0; // our node holds an integer Node *next = nullptr; // our node has a pointer to the next Node Node(int i){ // contructor for our Node class value = i; // store a copy of argument "i" in "value" next = nullptr; // be sure next...
C++ finish the AVL Tree code: #include "AVLNode.h" #include "AVLTree.h" #include <iostream> #include <string> using namespace...
C++ finish the AVL Tree code: #include "AVLNode.h" #include "AVLTree.h" #include <iostream> #include <string> using namespace std; AVLTree::AVLTree() { root = NULL; } AVLTree::~AVLTree() { delete root; root = NULL; } // insert finds a position for x in the tree and places it there, rebalancing // as necessary. void AVLTree::insert(const string& x) { // YOUR IMPLEMENTATION GOES HERE } // remove finds x's position in the tree and removes it, rebalancing as // necessary. void AVLTree::remove(const string& x) {...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to...
C++ Given Code: #include <iostream> #include <string> using namespace std; int main() { //declare variables to store user input bool cont = true; //implement a loop so that it will continue asking until the user provides a positive integer // the following provides ONLY part of the loop body, which you should complete { cout <<"How many words are in your message? \n"; cout <<"Enter value: "; // get user input integer here    cout <<"\nInvalid value. Please Re-enter a...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; //...
--- TURN this Code into Java Language --- #include <iostream> #include <string> using namespace std; // constants const int FINAL_POSITION = 43; const int INITIAL_POSITION = -1; const int NUM_PLAYERS = 2; const string BLUE = "BLUE"; const string GREEN = "GREEN"; const string ORANGE = "ORANGE"; const string PURPLE = "PURPLE"; const string RED = "RED"; const string YELLOW = "YELLOW"; const string COLORS [] = {BLUE, GREEN, ORANGE, PURPLE, RED, YELLOW}; const int NUM_COLORS = 6; // names...
Read Ch. 3. Using the C++ editor, type the following program: #include <iostream> #include <string> using...
Read Ch. 3. Using the C++ editor, type the following program: #include <iostream> #include <string> using namespace std; int main () {        //declarations string name;        //input cout <<"Enter your first name" << endl;        cin >> name;        //output        cout << "Hello " << name << "! " << endl;        return 0; } Compile and run. What does the cin statement do? What does the cout statement do? Is name a variable or a constant? What...
Add Bubble Sorting & Binary Search Functions to the following code (C++) #include<iostream> #include<fstream> #include<string> #include<iomanip>...
Add Bubble Sorting & Binary Search Functions to the following code (C++) #include<iostream> #include<fstream> #include<string> #include<iomanip> #include<algorithm> using namespace std; const int row = 10; const int col = 7;   ifstream name; ifstream grade; ofstream out; void readData(string stname[row], int stgrade[row][col]); void stsum(int stgrade[row][col], int total[row]); void staverage(int total[row], double average[row]); void stlettergrade(double average[row],char ltrgrade[row]); void displayData(string stname[row], int stgrade[row][col], int total[row], double staverage[row], char ltrgrade[row]); void swapltrgrade(char* xp, char* yp); int main() {    int STG[row][col] = { {0},{0}...
C++ please #include <iostream> using namespace std; /** * defining class circle */ class Circle {...
C++ please #include <iostream> using namespace std; /** * defining class circle */ class Circle { //defining public variables public: double pi; double radius; public: //default constructor to initialise variables Circle(){ pi = 3.14159; radius = 0; } Circle(double r){ pi = 3.14159; radius = r; } // defining getter and setters void setRadius(double r){ radius = r; } double getRadius(){ return radius; } // method to get Area double getArea(){ return pi*radius*radius; } }; //main method int main() {...
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h"...
Please comments this C++ code and show screenshots of the outputs main.cpp #include<iostream> #include<vector> #include<string> #include"BST.h" #include"BST.cpp" using namespace std; std::vector<std::string> tokenize(char line[]) {    std::vector<std::string> tok;        std::string word = "";        for (int i = 0; i < strlen(line); i++)        {            if (i == strlen(line) - 1)            {                word += line[i];                tok.push_back(word);                return tok;       ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT