SlideShare a Scribd company logo
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 = age;
this->score = score;
}
bool Participant::compare(const Sortable* obj) {
const Participant* otherParticipant = dynamic_cast(obj);
if (this->score != otherParticipant->score) {
return this->score > otherParticipant->score;
}
else if (this->age != otherParticipant->age) {
return this->age < otherParticipant->age;
}
else {
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
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

More Related Content

PPTX
Object Oriented Programming - Basic Concepts
PDF
C++ normal assignments by maharshi_jd.pdf
PPT
C++lecture9
PPTX
C++ Object Oriented Programming Lecture Slides for Students
PDF
Program 1 (Practicing an example of function using call by referenc.pdf
PPT
OBJECTS IN Object Oriented Programming .ppt
PDF
C++Sortable ObjectsStudy the file Sortable.h. It contains a simp.pdf
PPT
Programming Fundamentals, C++, Object Oriented Programming
Object Oriented Programming - Basic Concepts
C++ normal assignments by maharshi_jd.pdf
C++lecture9
C++ Object Oriented Programming Lecture Slides for Students
Program 1 (Practicing an example of function using call by referenc.pdf
OBJECTS IN Object Oriented Programming .ppt
C++Sortable ObjectsStudy the file Sortable.h. It contains a simp.pdf
Programming Fundamentals, C++, Object Oriented Programming

Similar to Make all of your classes work correctly with sortingData.cpp. Note t.pdf (20)

DOCX
Comp 220 ilab 7 of 7
PDF
C++ practical
DOCX
#include stdafx.h #include iostream using namespace std;vo.docx
PDF
DEBUG3 This program creates student objects and overl.pdf
DOC
Pads lab manual final
PPT
standard template library(STL) in C++
PDF
An Introduction to Part of C++ STL
PDF
PPT
Vectors Intro.ppt
DOCX
Comp 220 ilab 7 of 7
DOCX
Comp 220 ilab 7 of 7
PPTX
ch16.pptx
PPTX
ch16 (1).pptx
PDF
Ch 4
DOCX
Comp 220 ilab 7 of 7
PDF
Header #include -string- #include -vector- #include -iostream- using.pdf
PDF
Programming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdf
PPTX
Time and Space Complexity Analysis.pptx
PDF
Ds lab handouts
PDF
Object Oriented Programming (OOP) using C++ - Lecture 3
Comp 220 ilab 7 of 7
C++ practical
#include stdafx.h #include iostream using namespace std;vo.docx
DEBUG3 This program creates student objects and overl.pdf
Pads lab manual final
standard template library(STL) in C++
An Introduction to Part of C++ STL
Vectors Intro.ppt
Comp 220 ilab 7 of 7
Comp 220 ilab 7 of 7
ch16.pptx
ch16 (1).pptx
Ch 4
Comp 220 ilab 7 of 7
Header #include -string- #include -vector- #include -iostream- using.pdf
Programming For Big Data [ Submission DvcScheduleV2.cpp and StaticA.pdf
Time and Space Complexity Analysis.pptx
Ds lab handouts
Object Oriented Programming (OOP) using C++ - Lecture 3

More from pratikradia365 (20)

PDF
M.B. is a 65-year-old male who is being admitted from the emergency .pdf
PDF
L�neas de cruceros Carnival Aunque los viajes por mar han tenido.pdf
PDF
Los virus que infectan a los eucariotas tambi�n han evolucionado con.pdf
PDF
Los t�cnicos m�dicos de emergencia (EMT, por sus siglas en ingl�s) n.pdf
PDF
Los sistemas de control pueden hacer que las personas Pregunta 13 .pdf
PDF
Los tres tipos principales de pron�sticos utilizados por las organiz.pdf
PDF
Los trabajadores 1,..., n est�n actualmente inactivos. Supongamos qu.pdf
PDF
Los tipos de sangre humana se heredan en un patr�n co-dominante. Hay.pdf
PDF
Los valores m�s grandes de tienen la desventaja de aumentar la proba.pdf
PDF
Los siguientes son motivos razonables para las fusiones I) evitar.pdf
PDF
Los proveedores de atenci�n m�dica son responsables de documentar y .pdf
PDF
mauriland is a fictitious country with only two politically active g.pdf
PDF
Los puntos principales, subpuntos y subsubpuntos deben escribirse en.pdf
PDF
Mathew siempre llega tarde a la oficina. Al gerente de Mathew no le .pdf
PDF
Los riesgos que pueden resultar en que un sistema o proceso no fun.pdf
PDF
Match up the following The chance of a type 2 error is reduced.pdf
PDF
Match the Late Paleozoic time period with the appropriate life. You.pdf
PDF
Match the following proteins with the best description of their func.pdf
PDF
Materia Comunicaci�n empresarialGui�n Esta es la historia de Ch.pdf
PDF
Match the STRIDE threat with its description. Match the STRIDE threa.pdf
M.B. is a 65-year-old male who is being admitted from the emergency .pdf
L�neas de cruceros Carnival Aunque los viajes por mar han tenido.pdf
Los virus que infectan a los eucariotas tambi�n han evolucionado con.pdf
Los t�cnicos m�dicos de emergencia (EMT, por sus siglas en ingl�s) n.pdf
Los sistemas de control pueden hacer que las personas Pregunta 13 .pdf
Los tres tipos principales de pron�sticos utilizados por las organiz.pdf
Los trabajadores 1,..., n est�n actualmente inactivos. Supongamos qu.pdf
Los tipos de sangre humana se heredan en un patr�n co-dominante. Hay.pdf
Los valores m�s grandes de tienen la desventaja de aumentar la proba.pdf
Los siguientes son motivos razonables para las fusiones I) evitar.pdf
Los proveedores de atenci�n m�dica son responsables de documentar y .pdf
mauriland is a fictitious country with only two politically active g.pdf
Los puntos principales, subpuntos y subsubpuntos deben escribirse en.pdf
Mathew siempre llega tarde a la oficina. Al gerente de Mathew no le .pdf
Los riesgos que pueden resultar en que un sistema o proceso no fun.pdf
Match up the following The chance of a type 2 error is reduced.pdf
Match the Late Paleozoic time period with the appropriate life. You.pdf
Match the following proteins with the best description of their func.pdf
Materia Comunicaci�n empresarialGui�n Esta es la historia de Ch.pdf
Match the STRIDE threat with its description. Match the STRIDE threa.pdf

Recently uploaded (20)

PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Cell Types and Its function , kingdom of life
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
01-Introduction-to-Information-Management.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
Lesson notes of climatology university.
PPTX
GDM (1) (1).pptx small presentation for students
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Cell Structure & Organelles in detailed.
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Trump Administration's workforce development strategy
PPTX
master seminar digital applications in india
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
STATICS OF THE RIGID BODIES Hibbelers.pdf
Cell Types and Its function , kingdom of life
Final Presentation General Medicine 03-08-2024.pptx
O7-L3 Supply Chain Operations - ICLT Program
01-Introduction-to-Information-Management.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
Lesson notes of climatology university.
GDM (1) (1).pptx small presentation for students
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Pharmacology of Heart Failure /Pharmacotherapy of CHF
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Cell Structure & Organelles in detailed.
Microbial diseases, their pathogenesis and prophylaxis
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
VCE English Exam - Section C Student Revision Booklet
Trump Administration's workforce development strategy
master seminar digital applications in india
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape

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 ) );
  • 2. 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()); }
  • 3. 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;
  • 4. } #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 = age; this->score = score; } bool Participant::compare(const Sortable* obj) { const Participant* otherParticipant = dynamic_cast(obj); if (this->score != otherParticipant->score) { return this->score > otherParticipant->score; } else if (this->age != otherParticipant->age) { return this->age < otherParticipant->age; } else {
  • 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