In: Computer Science
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.
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