Make all of your classes work correctly with sortingData.cpp. Note that sortingData.cpp creates a
mixture of both Circles and Participants; this means that your compare methods cannot assume
they can always simply downcast Sortable pointers to their own type. You will have to use
dynamic_casts and/or the typeid function to check what kind of object the compare method
receives and act accordingly.
When comparing two objects of the same type, use the same logic as in lab 8. But when asked to
compare a Circle with a Participant or vice-versa, by definition we will consider a Circle <
Participant, so that, after sorting, all Circles should show up in the Data vector before all of the
Participants. (See the expected output below.) Modify Circle.h and Participant.h classes. All 5
files are provided, you only need to modify Circle.h and Participant.h and make sure they work
correctly with sortingData.cpp.
sortingData.cpp:
#include
#include
#include "Data.h"
#include "Circle.h"
#include "Participant.h"
using namespace std;
int main( int argc, const char* argv[] )
{
Data myData;
myData.add( new Participant( "Waymond", 24, 100 ) );
myData.add( new Circle() );
myData.add( new Participant( "Mary", 27, 96 ) );
myData.add( new Circle( 3 ) );
myData.add( new Participant( "John", 32, 100 ) );
myData.add( new Circle( 2 ) );
myData.add( new Participant( "Eliza", 21, 105 ) );
myData.add( new Circle( 4 ) );
myData.add( new Participant( "Ezekiel", 27, 96 ) );
myData.add( new Circle( 1 ) );
myData.add( new Participant( "Alex", 20, 101 ) );
myData.print();
myData.sort();
myData.print();
return 0;
}
Data.h:
#ifndef DATA_H
#define DATA_H
#include
#include
#include
class Data {
private:
std::vector dataset;
public:
void add(int number);
void sort();
void print();
};
void Data::add(int number) {
dataset.push_back(number);
}
void Data::sort() {
std::sort(dataset.begin(), dataset.end());
}
void Data::print() {
for (auto element : dataset) {
std::cout << element << " ";
}
std::cout << std::endl;
}
#endif /* DATA_H */
Circle.h:
#ifndef CIRCLE_H
#define CIRCLE_H
#include "Sortable.h"
class Circle : public Sortable {
private:
float radius;
public:
Circle();
Circle(float r);
bool compare(const Sortable* other) override;
void print() override;
};
Circle::Circle() : radius(0.0f) {}
Circle::Circle(float r) : radius(r) {}
bool Circle::compare(const Sortable* other) {
const Circle* circle = dynamic_cast(other);
if (circle) {
return radius < circle->radius;
}
return false;
}
void Circle::print() {
std::cout << "Circle with radius: " << radius << std::endl;
}
#endif /* CIRCLE_H */
Participant.h:
#ifndef PARTICIPANT_H
#define PARTICIPANT_H
#include
#include "Sortable.h"
class Participant : public Sortable {
private:
std::string name;
int age;
double score;
public:
Participant(std::string name, int age, double score);
bool compare(const Sortable* obj) override;
void print() override;
};
Participant::Participant(std::string name, int age, double score) {
this->name = name;
this->age = .
Trauma-Informed Leadership - Five Practical Principles
Make all of your classes work correctly with sortingData.cpp. Note t.pdf
1. Make all of your classes work correctly with sortingData.cpp. Note that sortingData.cpp creates a
mixture of both Circles and Participants; this means that your compare methods cannot assume
they can always simply downcast Sortable pointers to their own type. You will have to use
dynamic_casts and/or the typeid function to check what kind of object the compare method
receives and act accordingly.
When comparing two objects of the same type, use the same logic as in lab 8. But when asked to
compare a Circle with a Participant or vice-versa, by definition we will consider a Circle <
Participant, so that, after sorting, all Circles should show up in the Data vector before all of the
Participants. (See the expected output below.) Modify Circle.h and Participant.h classes. All 5
files are provided, you only need to modify Circle.h and Participant.h and make sure they work
correctly with sortingData.cpp.
sortingData.cpp:
#include
#include
#include "Data.h"
#include "Circle.h"
#include "Participant.h"
using namespace std;
int main( int argc, const char* argv[] )
{
Data myData;
myData.add( new Participant( "Waymond", 24, 100 ) );
myData.add( new Circle() );
myData.add( new Participant( "Mary", 27, 96 ) );
myData.add( new Circle( 3 ) );
myData.add( new Participant( "John", 32, 100 ) );
myData.add( new Circle( 2 ) );
myData.add( new Participant( "Eliza", 21, 105 ) );
myData.add( new Circle( 4 ) );
myData.add( new Participant( "Ezekiel", 27, 96 ) );
5. return this->name < otherParticipant->name;
}
}
void Participant::print() {
std::cout << this->name << "t" << this->age << "t" << this->score << std::endl;
}
#endif /* PARTICIPANT_H */
Sortable.h:
#ifndef SORTABLE_H
#define SORTABLE_H
class Sortable {
public:
virtual bool compare( const Sortable* ) = 0;
virtual void print() = 0;
};
#endif
sortingData.cpp EXPECTED OUTPUT
Waymond 24 100
Circle with radius: 0
Mary 27 96
Circle with radius: 3
John 32 100
Circle with radius: 2
Eliza 21 105
Circle with radius: 4
Ezekiel 27 96
Circle with radius: 1
Alex 20 101
Circle with radius: 0
Circle with radius: 1
Circle with radius: 2
6. Circle with radius: 3
Circle with radius: 4
Eliza 21 105
Alex 20 101
Waymond 24 100
John 32 100
Ezekiel 27 96
Mary 27 96