Question
Using C++ Template functions and classes. Be sure to create all necessary driver file, .h file and .cpp files. ( 60 minutes coding ) 1.
Using C++
Template functions and classes. Be sure to create all necessary driver file, .h file and .cpp files. (60 minutes coding)
1. Coding: Write a template function for bubble sort.
2. Coding: Dynamically allocate an array of Square objects, using the Square Class provided in Lab4/Exercise2 (Square.h, Square.cpp) (with overloaded "<<" and "<" operators implemented).
3. Coding: Use the bubble sort function you created in step 1 to sort the array of Square objects and print out the sorted result.
4. Interactive Coding: Update your Square class to be a template class
5. Coding: Use the template bubble sort function you created in step 1 to sort the array of Square objects of a specific type of your choice.
----------------------------------------------------------------------------------------------
Square.h
#ifndef SQUARE_H #define SQUARE_H class Square { friend std::ostream& operator<<(std::ostream&, const Square&); public: Square & operator=(const Square&); bool operator<(const Square&); void setSize(int newSize); int getSize(void) const; private: int theSize; }; #endif
----------------------------------------------------------------------------------------------
Square.cpp
#include#include "Square.h" using namespace std; void Square::setSize(int newSize) { theSize = newSize; } int Square::getSize(void) const { return theSize; } Square & Square::operator=(const Square& other) { theSize = other.getSize(); return *this; } ostream& operator<<(ostream &os, const Square &c) { os << c.getSize(); return os; } bool Square::operator<(const Square& other) { return theSize < other.getSize(); }
----------------------------------------------------------------------------------------------
Driver.cpp
#include#include "Square.h" using namespace std; void MySwap(Square &s1, Square &s2) { Square t = s1; s1 = s2; s2 = t; } void MyBubbleSort(Square *data, int size) { for (int i = 0; i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started