Inheritance and sample phrases

Inheritance and sample phrases

Class Heiarchy

You are developing a class heiarchy for this project. An Abstract Base Class (ABC),‘Smalltalk’ defines the heiarchybehaviour.
Classes derived from Smalltalk must implement populatePhrases(). A function that
initializes the baseclass vector with phrases that are unique to that class type. For
instance, Smalltalk_American will populate the vector with the american phrases
found in constants.h.
Additionally you are given a complete watch object. You may give or take a watch
from any instance of Smalltalk_American, ST_American_DonutEnthusiest or
Smalltalk_Brit. Note that watches cannot be created out of thin air, if you give one toan instance you no longer have that watch, the instance does. Because of that fact,this is one case where a shallow pointer copy is appropriate.

Please also provide a function (as specifies in Functions.h and outlined in
Functions.cpp) that generates a vector of unique pointers.

Library
you have to develop a static library with the following name and file structure.

All classes inherit publicly. The class hierarchy is as follows;

Testing
Please develop a test application that has the following name and file structure:

This application should link statically to the above library. The projects as they
appear in the eclipse workspace.

Functions.cpp

/*

* Functions.cpp

*

*  Created on: Nov 5, 2017

*      Author: keith

*/

#include “./includes/Functions.h”

#include “./includes/Smalltalk_American.h”

#include “./includes/ST_American_DonutEnthusiest.h”

#include “./includes/Smalltalk_Brit.h”

#include “./includes/Watch.h”

#include “./includes/constants.h”

//create a vector with a brit,american, donut enthusiest using unique pointers

//use c++11 and then returning by value is fine since the compiler will move the vector

//on return rather than recreate it(if using C++11).

std::vector<std::unique_ptr<Smalltalk>>getPeople(intnumBrit,

intnumAmerican, intnumbAmericanDonutEnthusiest,

intnumWatches) {

//create a vector to hold SmallTalk unique pointers

//add brits to vector

//add americans  to vector

//add american donut enthusiest  to vector

//create some watches (as long as number watches <= numb people)

//then give the watches away to first NUM_WATCHES people in the vector

//keep in mind that you are responsible for deleting any pointers to

//watches allocated on the heap when you are finished using the vector you return

//from this function(see Smalltalk header for hints)

//return your vector

} 

Watch.cpp 

/*

* Watch.cpp

*

*  Created on: Nov 5, 2017

*      Author: keith

*/

#include <ctime>

#include “./includes/Watch.h”

Watch::Watch() {

}

Watch::~Watch() {

}

std::string Watch::getTime() {

constint CONVERT_TO_12_HOUR = 12;

time_trawtime;

struct tm * timeinfo;

time(&rawtime);

timeinfo = localtime(&rawtime);

std::stringcurtime = “The time is: ”

+ std::to_string(timeinfo->tm_min) + ” minutes after”

+ std::to_string(timeinfo->tm_hour % CONVERT_TO_12_HOUR);

returncurtime;

} 

327_Proj4.cpp 

// smalltalk_VirtualAbstractVector.cpp : Defines the entry point for the console application.

//

#include <iostream>

using namespace std;

//TODO make sure you have the right number of watches

//TODO make sure you consider the case where you have more watches than people

//TODO make sure you do not do a deep copy any watches when giving or taking them

//     the simplest way is give and take the watch a few times and verify that the old recipients have

//     a null pWatch pointer

//     and that the pointer returned from the last takeWatch is the same as the one you began with on giveWatch

//TODO make sure saySomething() cycles through appropriate phrases

void demo(){

intnumEachGroup = 2;

intnumwatches = 2;

std::vector<std::unique_ptr<Smalltalk>> myv1 = getPeople(numEachGroup,

numEachGroup, numEachGroup, numwatches);

intcntr = 0;

for (int i = 0; i < myv1.size(); i++) {

std::cout<<myv1[i]->saySomething() <<endl;

std::cout<<myv1[i]->getTime() <<endl;

}

}

int main() {

demo();

//the above call produces the following console output

//    BRIT 0:Im a bit knackered mate

//    BRIT 0:The time is: 27 minutes after1

//    BRIT 1:Im a bit knackered mate

//    BRIT 1:The time is: 27 minutes after1

//    AMERICAN 0:Why yes, I would like to supersize that

//    AMERICAN 0:I do not have a watch

//    AMERICAN 1:Why yes, I would like to supersize that

//    AMERICAN 1:I do not have a watch

//    AMERICAN_DONUT_ENTHUSIEST 0:Dunkin Donuts is a horrendous, disappointing compromise

//    AMERICAN_DONUT_ENTHUSIEST 0:I do not have a watch

//    AMERICAN_DONUT_ENTHUSIEST 1:Dunkin Donuts is a horrendous, disappointing compromise

//    AMERICAN_DONUT_ENTHUSIEST 1:I do not have a watch

} 

Solution 

327_Proj4.cpp 

// smalltalk_VirtualAbstractVector.cpp : Defines the entry point for the console application.

//

#include <iostream>

#include <memory>

#include <vector>

#include “./../../327_Proj4_Lib/includes/Functions.h”

using namespace std;

//TODO make sure you have the right number of watches

//TODO make sure you consider the case where you have more watches than people

//TODO make sure you do not do a deep copy any watches when giving or taking them

//     the simplest way is give and take the watch a few times and verify that the old recipients have

//     a null pWatch pointer

//     and that the pointer returned from the last takeWatch is the same as the one you began with on giveWatch

//TODO make sure saySomething() cycles through appropriate phrases

void demo(){

intnumEachGroup = 2;

intnumwatches = 2;

// intnumwatches = 200; // test more watches then people

std::vector<std::unique_ptr<Smalltalk>> myv1 = getPeople(numEachGroup,

numEachGroup, numEachGroup, numwatches);

intcntr = 0;

for (int i = 0; i < myv1.size(); i++) {

std::cout<<myv1[i]->saySomething() <<endl;

std::cout<<myv1[i]->getTime() <<endl;

}

// test watch pointer

//Watch* p = myv1[0]->takeWatch();

//bool b;

//b = myv1[0]->giveWatch(p);

//b = myv1[0]->giveWatch(p);

// test saySomething() cycles

//std::cout<<endl;

//for(cntr = 0; cntr< 10; cntr++) {

//    for (int i = 0; i <myv1.size(); i++) {

//          std::cout<<myv1[i]->saySomething() <<endl;

//          std::cout<<myv1[i]->getTime() <<endl;

//    }

//}

}

int main() {

demo();

//the above call produces the following console output

//    BRIT 0:Im a bit knackered mate

//    BRIT 0:The time is: 27 minutes after1

//    BRIT 1:Im a bit knackered mate

//    BRIT 1:The time is: 27 minutes after1

//    AMERICAN 0:Why yes, I would like to supersize that

//    AMERICAN 0:I do not have a watch

//    AMERICAN 1:Why yes, I would like to supersize that

//    AMERICAN 1:I do not have a watch

//    AMERICAN_DONUT_ENTHUSIEST 0:Dunkin Donuts is a horrendous, disappointing compromise

//    AMERICAN_DONUT_ENTHUSIEST 0:I do not have a watch

//    AMERICAN_DONUT_ENTHUSIEST 1:Dunkin Donuts is a horrendous, disappointing compromise

//    AMERICAN_DONUT_ENTHUSIEST 1:I do not have a watch

} 

Functions.cpp 

/*

* Functions.cpp

*

*  Created on: Nov 5, 2017

*      Author: keith

*/

#include “./includes/Functions.h”

#include “./includes/Smalltalk_American.h”

#include “./includes/ST_American_DonutEnthusiest.h”

#include “./includes/Smalltalk_Brit.h”

#include “./includes/Watch.h”

#include “./includes/constants.h”

//create a vector with a brit,american, donut enthusiest using unique pointers

//use c++11 and then returning by value is fine since the compiler will move the vector

//on return rather than recreate it(if using C++11).

std::vector<std::unique_ptr<Smalltalk>>getPeople(intnumBrit,

intnumAmerican, intnumbAmericanDonutEnthusiest,

intnumWatches) {

//create a vector to hold SmallTalk unique pointers

std::vector<std::unique_ptr<Smalltalk>> v;

//add brits to vector

for (int i = 0; i <numBrit; i++) {

v.push_back(std::unique_ptr<Smalltalk_Brit>(new Smalltalk_Brit(i)) );

}

//add americans  to vector

for (int i = 0; i <numAmerican; i++) {

v.push_back(std::unique_ptr<Smalltalk_American>(new Smalltalk_American(i)) );

}

//add american donut enthusiest  to vector

for (int i = 0; i <numbAmericanDonutEnthusiest; i++) {

v.push_back(std::unique_ptr<ST_American_DonutEnthusiest>(new ST_American_DonutEnthusiest(i)) );

}

//create some watches (as long as number watches <= numb people)

//then give the watches away to first NUM_WATCHES people in the vector

//keep in mind that you are responsible for deleting any pointers to

//watches allocated on the heap when you are finished using the vector you return

//from this function(see Smalltalk header for hints)

intnum = numWatches;

if (num> (int)v.size()) {

num = (int)v.size();

}

for (int i = 0; i <num; i++) {

v[i]->giveWatch(new Watch());

}

//return your vector

return v;

} 

Smalltalk.cpp 

/*

* Smalltalk.cpp

*

*  Created on: Nov 8, 2017

*      Author:

*/

#include “./includes/Smalltalk.h”

#include “./includes/constants.h”

Smalltalk::Smalltalk(std::string myNationality, intiPerson) :

nationality(myNationality),

iPerson(iPerson),

current_phrase(0),

pWatch(NULL) {

}

Smalltalk::~Smalltalk(void) {

if (pWatch != NULL) {

deletepWatch;

pWatch = NULL;

}

}

std::string Smalltalk::saySomething() {

std::string say(nationality + ” ” + std::to_string(iPerson) + “:”);

if (mySmallTalk.size() > 0) {

current_phrase = (current_phrase + 1) % mySmallTalk.size();

say += mySmallTalk[current_phrase];

}

return say;

}

std::string Smalltalk::getTime() {

std::stringstr(nationality + ” ” + std::to_string(iPerson) + “:”);

if (pWatch == NULL) {

str += I_DO_NOT_HAVE_A_WATCH;

} else {

str += pWatch->getTime();

}

returnstr;

}

Watch* Smalltalk::takeWatch() {

 

Watch* p = this->pWatch;

if (p != NULL) {

this->pWatch = NULL;

}

return p;

}

bool Smalltalk::giveWatch(Watch * pWatch) {

if (this->pWatch != NULL) {

return false;

} else {

this->pWatch = pWatch;

return true;

}

} 

Smalltalk_American.cpp 

/*

* Smalltalk_American.cpp

*

*  Created on: Nov 8, 2017

*      Author:

*/

#include “./includes/Smalltalk_American.h”

#include “./includes/constants.h”

Smalltalk_American::Smalltalk_American(intiPerson)

: Smalltalk(AMERICAN, iPerson) {

populatePhrases();

}

Smalltalk_American::Smalltalk_American(std::string myNationality, intiPerson)

: Smalltalk(myNationality, iPerson) {

}

Smalltalk_American::~Smalltalk_American(void) {

}

voidSmalltalk_American::populatePhrases() {

mySmallTalk.push_back(AMERICAN_PHRASE_1);

mySmallTalk.push_back(AMERICAN_PHRASE_2);

mySmallTalk.push_back(AMERICAN_PHRASE_3);

mySmallTalk.push_back(AMERICAN_PHRASE_4);

mySmallTalk.push_back(AMERICAN_PHRASE_5);

} 

Smalltalk_Brit.cpp 

/*

* Smalltalk_Brit.cpp

*

*  Created on: Nov 8, 2017

*      Author:

*/

#include “./includes/Smalltalk_Brit.h”

#include “./includes/constants.h”

Smalltalk_Brit::Smalltalk_Brit(intiPerson)

:Smalltalk(BRIT, iPerson) {

populatePhrases();

}

Smalltalk_Brit::~Smalltalk_Brit(void) {

}

voidSmalltalk_Brit::populatePhrases() {

mySmallTalk.push_back(BRIT_1);

mySmallTalk.push_back(BRIT_2);

mySmallTalk.push_back(BRIT_3);

mySmallTalk.push_back(BRIT_4);

mySmallTalk.push_back(BRIT_5);

mySmallTalk.push_back(BRIT_6);

mySmallTalk.push_back(BRIT_7);

} 

ST_American_DonutEnthusiest.cpp 

/*

* ST_American_DonutEnthusiest.cpp

*

*  Created on: Nov 8, 2017

*      Author:

*/

#include “./includes/ST_American_DonutEnthusiest.h”

#include “./includes/constants.h”

ST_American_DonutEnthusiest::ST_American_DonutEnthusiest(intiPerson)

: Smalltalk_American(AMERICAN_DE, iPerson) {

populatePhrases();

}

ST_American_DonutEnthusiest::~ST_American_DonutEnthusiest(void) {

}

voidST_American_DonutEnthusiest::populatePhrases() {

mySmallTalk.push_back(AMERICAN_DE_PHRASE_1);

mySmallTalk.push_back(AMERICAN_DE_PHRASE_2);

mySmallTalk.push_back(AMERICAN_DE_PHRASE_3);

mySmallTalk.push_back(AMERICAN_DE_PHRASE_4);

mySmallTalk.push_back(AMERICAN_DE_PHRASE_5);

} 

Watch.cpp 

/*

* Watch.cpp

*

*  Created on: Nov 5, 2017

*      Author: keith

*/

#include <ctime>

#include “./includes/Watch.h”

Watch::Watch() {

}

Watch::~Watch() {

}

std::string Watch::getTime() {

constint CONVERT_TO_12_HOUR = 12;

time_trawtime;

struct tm * timeinfo;

time(&rawtime);

timeinfo = localtime(&rawtime);

std::stringcurtime = “The time is: ”

+ std::to_string(timeinfo->tm_min) + ” minutes after”

+ std::to_string(timeinfo->tm_hour % CONVERT_TO_12_HOUR);

returncurtime;

}