[菜鸟学C++]抽象类

菜鸟学C++
http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
理论不懂就实践,实践不行就理论

AbstractClass.h

#ifndef ABSTRACTCLASS_H_
#define ABSTRACTCLASS_H_

class AbstractClass
{
  public:
    AbstractClass();
    virtual ~AbstractClass();
  
  public:
    virtual void im() = 0;
};
#endif /* ABSTRACTCLASS_H_ */

AbstractClass.cc

#include "AbstractClass.h"

AbstractClass::AbstractClass()
{
}

AbstractClass::~AbstractClass()
{
}

void AbstractClass::im()
{}

Main.cc

#include <iostream>

#include "AbstractClass.h"

using namespace std;

class ConcreteClass : public AbstractClass
{
  public:
    ConcreteClass() {};
    virtual ~ConcreteClass() {};

  public:
    virtual void im()
    {
        cout << "ConcreteClass::im" << endl;
    };
};

void hello(AbstractClass *ac)
{
    ac->im();
}

int main(int argc, char* argv[])
{
    AbstractClass *pAC = new ConcreteClass();
    hello(pAC);
    delete pAC;
    pAC = NULL;
    return 0;
}
~/dev/workspace/labs/ccpp/cc_abstract_class$ g++ Main.cc 
/tmp/ccQrfLyb.o: In function `ConcreteClass::ConcreteClass()':
Main.cc:(.text._ZN13ConcreteClassC2Ev[_ZN13ConcreteClassC5Ev]+0x14): undefined reference to `AbstractClass::AbstractClass()'
/tmp/ccQrfLyb.o: In function `ConcreteClass::~ConcreteClass()':
Main.cc:(.text._ZN13ConcreteClassD2Ev[_ZN13ConcreteClassD5Ev]+0x1f): undefined reference to `AbstractClass::~AbstractClass()'
/tmp/ccQrfLyb.o:(.rodata._ZTI13ConcreteClass[typeinfo for ConcreteClass]+0x10): undefined reference to `typeinfo for AbstractClass'
collect2: ld returned 1 exit status

然后这样编译就可以了

~/dev/workspace/labs/ccpp/cc_abstract_class$ g++ Main.cc AbstractClass.cc