hey all im back as usual kicking my head ageist the door
line 41 is giving me a compile error of "cannot declare variable sortItem to be of abstract type" just learning C++ inheritance its a bit different than java. thanks for all the help.
Demo.cpp
/* File Name: Demo.cpp
Chapter No. 15 - Exercise No. 1
Programmer: Carl Sue
Date Last Modified: Apr 17, 2010
Problem Statement: (what you want the code to do)
Design a class AbstractSort that can be used to analyze the number of
comparisons performed by a sorting algorithm. The class should have a
member function compare that is capable of comparing two array elements,
and a means of keeping track of the number of comparisons performed.
The class should be an abstract class with a pure virtual member function
void sort(int arr[ ], int size)
which, when overridden, will sort the array by calling the compare function
to determine the relative order of pairs of numbers. Create a subclass of
AbstractSort that uses a simple sorting algorithm to implement the sort
function. The class should have a member function that can be called after
the sorting is done to retrieve the number of comparisons performed.
Input validation: do not accept negative numbers for test scores.
Overall Plan (step-by-step how you want the code to make it happen):
1. make an array
2. sort with a compare class and keep track of comparisons
3. output number of comparisons done
etc.
Classes needed and Purpose (Input, Processing, Output):
*/
#include <iostream>
#include "AbstractSort.h"
#include "SubAbstractSort.h"
using namespace std;
int main(int argc, char * const argv[]){
int siz = 5;
int array[] = {1,3,5,9,4};
SubAbstractSort sortItem;
sortItem.sort(array,siz);
cout << "number of compares: " << sortItem.getNumComparisons() << endl;
return 0;
}
AbstractSort.cpp
/*
* AbstractSort.cpp
*
* Created on: Apr 23, 2010
* Author: Carl
*/
#include "AbstractSort.h"
AbstractSort::AbstractSort() {
// TODO Auto-generated constructor stub
}
AbstractSort::~AbstractSort() {
// TODO Auto-generated destructor stub
}
int AbstractSort::getNumConversions(){
return numConversions;
}
void AbstractSort::setNumConversions(int x){
numConversions = x;
}
void AbstractSort::incrimentNumConversions(){
numConversions++;
}
AbstractSort.h
/*
* AbstractSort.h
*
* Created on: Apr 23, 2010
* Author: Carl
*/
#ifndef ABSTRACTSORT_H_
#define ABSTRACTSORT_H_
#include <iostream>
using namespace std;
class AbstractSort {
public:
AbstractSort();
virtual void sort(int*arr, int size) = 0;
virtual ~AbstractSort();
int getNumConversions();
void setNumConversions(int);
void incrimentNumConversions();
private:
static int numConversions;
};
#endif /* ABSTRACTSORT_H_ */
SubAbstractSort.cpp
/*
* SubAbstractSort.cpp
*
* Created on: Apr 23, 2010
* Author: Carl
*/
#include "SubAbstractSort.h"
SubAbstractSort::SubAbstractSort() {
numConversions = 0;
}
SubAbstractSort::~SubAbstractSort() {
// TODO Auto-generated destructor stub
}
void sort(int*arr, int size){
bool flag = false;
do {
for (int i = 0; i < size; i++) {
if (arr[i]>arr[i+1]) {
int temp;
temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
flag = true;
incrimentNumConversions();
}
}
} while (flag == true);
}
SubAbstractSort.h
/*
* SubAbstractSort.h
*
* Created on: Apr 23, 2010
* Author: Carl
*/
#ifndef SUBABSTRACTSORT_H_
#define SUBABSTRACTSORT_H_
#include <iostream>
using namespace std;
#include "AbstractSort.h"
class SubAbstractSort: public AbstractSort {
public:
SubAbstractSort();
int getNumComparisons();
virtual ~SubAbstractSort();
};
#endif /* SUBABSTRACTSORT_H_ */