-3

I want to expose only the CreateSort() for the client. it was to create an object for the implementation of the sort class i.e imSort then return it to the client.but the compiler says that it cannot create an object of an abstract class eventhough all the functions have been defined in the derived class.

/////sort.h

class __declspec(dllexport) Sort {
public:
virtual int* BSort() const=0;
virtual void getdata() const=0;
};

extern "C" Sort *CreateSort();

/////imSort.h
#include "Sort.h"
class imSort : public Sort{
private:
int i,j,num;
int temp;
int *a;
public:
imSort();
int* BSort();
void getdata();
}

/////imSort.cpp

#include <iostream>
#include "imSort.h"
Sort *CreateSort()
{
return new imSort(); /* object of abstract class type "imSort" is not allowed: */
}
imSort::imSort()
{
i=j=num=0;
*a=0;
}

void imSort::getdata()
{
std::cout<<"\nEnter the number of elements..";
std::cin>>num;
for(i=0;i<num;i++)
{
    std::cin>>*a;
    *(a++);
}
}
int* imSort::BSort()
{
for(i=0;i<num;i++)
    for(j=i+1;j<num;j++)
    {
        if(*(a+i)<*(a+j))
        {
            temp=*(a+i);
            *(a+i)=*(a+j);
            *(a+j)=temp;
        }
    }
    return a;
}
Janak
  • 1
  • 1

2 Answers2

2

Your base class has:

virtual int* BSort() const=0;
virtual void getdata() const=0;

But your derived class has:

int* BSort();
void getdata();

Repeating the virtual keyword is optional, but without the const these are separate functions, unrelated to the virtual base functions.

As a result, those pure virtual functions remain un-overridden in the derived class, and so imSort (silly name for a type if you ask me) is still abstract.

Your fixed derived class definition is thus:

class imSort : public Sort {
   private:
      int  i, j, num;
      int  temp;
      int* a;

   public:
      imSort();
      int* BSort() const;     // <--- const
      void getdata() const;   // <--- const
};                            // <--- ;

(Notice how indentation improves the legibility of your code? And you forgot the ; at the end of your class definition.)


Please write a fully-formed question next time, and reduce your problem to a minimal testcase.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • added the const's...but now i get a compiler error for int* imSort::BSort() declaration in imSort.h is incompatible.. – Janak Sep 08 '11 at 16:13
  • Your function definitions need to match, too, so: `int* imSort::BSort() const { ... }`. Which C++ book are you using? – Lightness Races in Orbit Sep 08 '11 at 16:17
  • 1
    @Janak: I am not familiar with a C++ book called "essential com". Get one from [this list of recommended resources](http://jcatki.no-ip.org/fncpp/Resources) so that you can learn C++ properly! – Lightness Races in Orbit Sep 08 '11 at 16:25
  • @Janak - If you mean "Essential COM" by Don Box it's a good, if old, book for COM programming. You should also get a good general C++ book from the list Tomalak posted. – Tony Sep 09 '11 at 09:01
0

If the virtual functions in the abstract Sort class are declared const, so should the implementations in the imSort class, but they are not.

So just add const here and there...

rodrigo
  • 94,151
  • 12
  • 143
  • 190