How to create an abstract class in C++?

Asked

Viewed 337 times

1

In C++ virtual uses

file . cpp

#ifndef TETES_H
#define TETES_H


    class Tetes
    {
        public:
            Tetes();
            virtual ~Tetes();
          virtual void exibeDados();
        protected:
        private:
    };

    #endif // TETES_H

file . h

#include "Tetes.h"

Tetes::Tetes()
{
    //ctor
}

Tetes::~Tetes()
{
    //dtor
}

file . cpp

#ifndef TETES1_H
#define TETES1_H


    class Tetes1 : public Testes
    {
        public:
            Tetes1();
            virtual ~Tetes1();
           void exibeDados()
         {
           cout << "Exibe na Tela" << endl; 
          }
        protected:
        private:
    };

    #endif // TETES1_H

file . h

#include "Tetes1.h"

Tetes1::Tetes1()
{
    //ctor
}

Tetes1::~Tetes1()
{
    //dtor
}

... classier teste2, test3 with the same but different function its content.

But it makes a mistake

undefined reference to Testes::exibeDados()

Where do I have to reference and how? That’s how you define an abstract class in C++?

1 answer

1


I don’t know if it’s the only problem, but besides the file names being reversed in the question, the abstract class is called Tetes and then Tetes1 is inherited from Teste. You already have a problem there. Possibly causing others.

Other than that, it may be that everything is not being compiled on the same build unit, or linked together, but only seeing the code can not know.

And it is also necessary to explicitly declare the virtual method in the abstract class as without implementation. This is done with = 0:

virtual void exibeDados() = 0;

I put in the Github for future reference.

  • It was my mistake in typing is not Test is Tests

  • Typos cause problems and your code seems to have several.

  • when shooting the virtual function displayDates () from the Test works

  • I had forgotten one detail.

  • now it worked

Browser other questions tagged

You are not signed in. Login or sign up in order to post.