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. #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; }
OOPs
In today's technology-driven world, computer programming skills are in high demand. The object-oriented programming (OOP) approach is very much useful while designing and maintaining software programs. Object-oriented programming (OOP) is a basic programming paradigm that almost every developer has used at some stage in their career.
Constructor
The easiest way to think of a constructor in object-oriented programming (OOP) languages is:
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.
#include <iostream>
#include <string>
#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<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;
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images