I'm a student, and my c++ class has an online homework called zybooks and it's a mixture of participation and some challenges. We've begun classes, and it's 98% a self learning course, and I'm having trouble understanding what's going on in the code.
It's a snippet of code pre-coded by zybooks and I have to add in a piece of code to make it work.
#include <iostream>
#include <string>
#include <vector>
usingnamespace std;
class BaseItem {
public:
void SetLastName(string providedName) {
lastName = providedName;
};
// i can only edit from here...
// FIXME: Define PrintItem() member function .... // this is my snippet.
void PrintItem(){
cout << "Last name: " << " " << lastName << endl;
}
// to here. I can't touch any of the rest of the code.
protected:
string lastName;
};
class DerivedItem : public BaseItem {
public:
void SetFirstName(string providedName) {
firstName = providedName;
};
void PrintItem() {
cout << "First and last name: ";
cout << firstName << " " << lastName << endl;
};
private:
string firstName;
};
int main() {
BaseItem* baseItemPtr = nullptr;
DerivedItem* derivedItemPtr = nullptr;
vector<BaseItem*> itemList;
unsignedint 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;
}
My code inserts this into console stream:
Last name: Smith
Last name: Jones
It needs to insert this:
Last name: Smith
First and last name: Bill Jones
I know there's something to do with there being two void print functions being called, and I'm sure the problem lies therein, but I am so lost on classes right now.