Write the PrintItem() function for the base class. Sample output for below program: Last name: Smith First and last name: Bill Jones Hint: Use the keyword const to make PrintItem() a virtual function. C++ program below, only lines 13-15 can be editted, the rest must stay the same. #include #include #include using namespace std; class BaseItem { public:    void SetLastName(string providedName) {       lastName = providedName;    };    // FIXME: Define PrintItem() member function    /* Your solution goes here  */ protected:    string lastName; }; class DerivedItem : public BaseItem { public:    void SetFirstName(string providedName) {       firstName = providedName;    };    void PrintItem() const override {       cout << "First and last name: ";       cout << firstName << " " << lastName << endl;    }; private:    string firstName; }; int main() {    BaseItem*    baseItemPtr    = nullptr;    DerivedItem* derivedItemPtr = nullptr;    vector itemList;    unsigned int i;    baseItemPtr = new BaseItem();    baseItemPtr->SetLastName("Smith");    derivedItemPtr = new DerivedItem();    derivedItemPtr->SetLastName("Jones");    derivedItemPtr->SetFirstName("Bill");    itemList.push_back(baseItemPtr);    itemList.push_back(derivedItemPtr);    for (i = 0; i < itemList.size(); ++i) {       itemList.at(i)->PrintItem();    }    return 0; }

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter11: Introduction To Classes
Section11.4: A Case Study: Constructing A Date Class
Problem 9E
icon
Related questions
Question

Write the PrintItem() function for the base class. Sample output for below program:

Last name: Smith

First and last name: Bill Jones

Hint: Use the keyword const to make PrintItem() a virtual function.

C++ program below, only lines 13-15 can be editted, the rest must stay the same.

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class BaseItem {
public:
   void SetLastName(string providedName) {
      lastName = providedName;
   };

   // FIXME: Define PrintItem() member function

   /* Your solution goes here  */

protected:
   string lastName;
};

class DerivedItem : public BaseItem {
public:
   void SetFirstName(string providedName) {
      firstName = providedName;
   };

   void PrintItem() const override {
      cout << "First and last name: ";
      cout << firstName << " " << lastName << endl;
   };

private:
   string firstName;
};

int main() {
   BaseItem*    baseItemPtr    = nullptr;
   DerivedItem* derivedItemPtr = nullptr;
   vector<BaseItem*> itemList;
   unsigned int i;

   baseItemPtr = new BaseItem();
   baseItemPtr->SetLastName("Smith");

   derivedItemPtr = new DerivedItem();
   derivedItemPtr->SetLastName("Jones");
   derivedItemPtr->SetFirstName("Bill");

   itemList.push_back(baseItemPtr);
   itemList.push_back(derivedItemPtr);

   for (i = 0; i < itemList.size(); ++i) {
      itemList.at(i)->PrintItem();
   }

   return 0;
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Reference Types in Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning