[C++] Ce este o clasa abstracta?

Despre limbajul de programare C++ si biblioteca standard STL (forum moderat)
Post Reply
User avatar
Ovidiu Cucu
Fondator
Fondator
Posts: 3776
Joined: 11 Jul 2007, 16:10
Judet: Iaşi
Location: Iasi
Contact:

[C++] Ce este o clasa abstracta?

Post by Ovidiu Cucu » 14 Aug 2011, 12:45

Intrebare
Ce este o clasa abstracta?

Raspuns
O clasa abstracta reprezinta un concept general care necesita variante concrete pentru a putea fi utilizat.
De exemplu, animal reprezinta un concept general iar variantele lui concrete por fi caine, pisica s.a.m.d.
In C++, o clasa este abstracta daca
  • are cel putin o functie virtuala pura;
    Exemplu [1]

    Code: Select all

    class Animal
    {
    public:
       // contains at least one pure virtual function 
       // (a function which has pure virtual specifier '=0').
       virtual void say_something() = 0;
       virtual void do_somethnig() = 0;
       // 'Animal' is an abstract class.
    };
  • mosteneste cel putin o functie la care ultima suprascriere este virtuala pura.
    Exemplu [2]

    Code: Select all

    class Mammal : public Animal
    {
    public:
       // (1) does not override 'say_something' pure virtual function;
       // (2) overrides 'do_somethnig' having pure virtual specifier ' = 0'.
       virtual void do_somethnig() = 0; 
       // given (1) or (2), 'Mammal' class is still abstract
    };
    Exemplu [3]

    Code: Select all

    class Dog : public Mammal
    {
    public:
       // Finally, 'Dog' is not an abstract class
       virtual void say_something()
       {
          std::cout << "Bark, bark!";
       }
       virtual void do_somethnig()
       {
          std::cout << "Bite you!";
       }
    };

O clasa abstracta nu poate fi instantiata
Exemplu [4]

Code: Select all

int main()
{
   Animal a; // Error: cannot instantiate abstract class
   Mammal* pM = new Mammal; // Error: cannot instantiate abstract class
   Dog rex; // OK, 'Dog' is not an abstract class
   // ...
}
Ca o consecinta, clasele abstracte nu pot fi folosite ca tip pentru parametri, return sau cast.
Exemplu [5]

Code: Select all

void f1(Animal a); // Error: cannot instantiate abstract class
Animal f2();       // Error: cannot instantiate abstract class
Dog f3(); // OK, Dog is not an abstract class

int main()
{
   Animal* p = &(Animal)f3(); // Error: cannot instantiate abstract class
   // ...
}
Totusi, pot fi folositi pointeri sau referinte la clase abstracte.
Exemplu [6]

Code: Select all

void f4(Animal& rA); // OK
void f5(Animal* pA); // OK
Animal& f6();        // OK
Animal* f7();        // OK

int main()
{
   Animal* p = (Animal*)&f3(); // OK
   Animal& r = (Animal&)f3();  // OK
   // ...
}
Resurse
<< Back to C++ Index



Viorel
Microsoft MVP
Microsoft MVP
Posts: 285
Joined: 13 Jul 2007, 12:26

Re: [C++] Ce este o clasa abstracta?

Post by Viorel » 14 Aug 2011, 19:21

Este oare abstractă clasa care conţine doar un constructor protejat (protected)?

User avatar
Ovidiu Cucu
Fondator
Fondator
Posts: 3776
Joined: 11 Jul 2007, 16:10
Judet: Iaşi
Location: Iasi
Contact:

Re: [C++] Ce este o clasa abstracta?

Post by Ovidiu Cucu » 15 Aug 2011, 12:07

Mecanismul standard de a defini clase abstracte in limbajul de programare C++ este cu ajutorul functiilor virtuale pure.
ISO/IEC 14882:2003 - 10.4 Abstract classes
2. An abstract class is a class that can be used only as a base class of some other class; no objects of an abstract
class can be created except as subobjects of a class derived from it. A class is abstract if it has at least
one pure virtual function
. [ Note: Such a function might be inherited ].
O clasa care contine doar un constructor protected, nu si functii virtuale pure, indeplineste doar prima parte a punctului 10.4.2 deci NU este o clasa abstracta.

De ce ar fi asa?
Cred ca un motiv ar fi pentru ca o functie virtuala pura te obliga sa o implementezi in clasa derivata "concreta".
Luand exemplul din primul post si stergand specificatorul pure virtual ('=0') de la functia Animal::say_something, un arhitect ar putea omite (din greseala, graba sau prostie) implementarea in clasa Dog.
Ca si concept general, un animal spune ceva ("ham", "miau", "behehe" etc). Daca Rex, catelul meu concret din clasa Dog nu poate spune nimic, atunci logic... nu-i animal. ;)

Post Reply