Question
4-2 Define and implement a class named sort_by_name that has a single public static function but no constructors: static void sort(animal **animals,int n) // sorts
4-2
Define and implement a class named sort_by_name that has a single public static function but no constructors:
static void sort(animal **animals,int n) // sorts the array of n animals into ascending order using their names
You may use your favourite sorting algorithm.
animal.h file:
#ifndef ANIMAL_H
#define ANIMAL_H
#include
class animal {
protected:
std::string name ; // the animal's name
static int count;
int animalID ; // the animal's unique ID
int volume ; // the volume of the animal's body
public:
animal();
animal(std::string n, int v) ; // creates an animal with name n and body volume v.
// animals are allocated a unique ID on creation
void set_name(std::string n);
void set_volume(int v);
virtual std::string get_name() = 0;
int get_animalID();
int get_volume();
};
#endif
animal.cpp file:
#include "animal.h"
#include
int animal::count =1;
animal::animal(){
name ="?";
volume = 0;
}
animal::animal(std::string n, int v) {
name=n;
animalID=count;
count++;
volume=v;
}
void animal::set_name(std::string n){
name=n;
}
void animal::set_volume(int v){
volume=v;
}
// virtual std::string get_name() = 0;
int animal::get_animalID(){
return animalID;
}
int animal::get_volume(){
return volume;
}
These are the files needed for this question. Please present the correct code. i want the full code for the class.
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