Abstract class

T

Tony Johansson

Hello!

I'm reading a book about C++ and there is something that I don't understand
so I ask you out there.
The book says "an abstact class has a complete interface and only provides
the implementation of operations that can be specified at this general
level. These implementations can use other operations defined in the class,
both concrete and abstract"

Now to my question this sentence "These implementations can use other
operations defined in the class, both concrete and abstract" is that really
right. Assume you have 5 methods in the abstract class and we call them
a,b,c,d,e and a,b and c are concrete so d and e are pure cirtual. Then when
you implement the concrete methods a,b and c in the abstact class can you
then use the abstact methods d or e.

Many thanks

//Tony
 
T

Thomas Maier-Komor

Tony said:
Hello!

I'm reading a book about C++ and there is something that I don't understand
so I ask you out there.
The book says "an abstact class has a complete interface and only provides
the implementation of operations that can be specified at this general
level. These implementations can use other operations defined in the class,
both concrete and abstract"

Now to my question this sentence "These implementations can use other
operations defined in the class, both concrete and abstract" is that really
right. Assume you have 5 methods in the abstract class and we call them
a,b,c,d,e and a,b and c are concrete so d and e are pure cirtual. Then when
you implement the concrete methods a,b and c in the abstact class can you
then use the abstact methods d or e.

Many thanks

//Tony

yes, you can. The only situation where this is impossible is in the
constructor and destructor of your abstract class, because in the during
their execution all method calls are bound statically, as the object may
be already be constructed/destructed partially.

Tom
 
L

Lionel B

Hello!

I'm reading a book about C++ and there is something that I don't understand
so I ask you out there.
The book says "an abstact class has a complete interface and only provides
the implementation of operations that can be specified at this general
level. These implementations can use other operations defined in the class,
both concrete and abstract"

Now to my question this sentence "These implementations can use other
operations defined in the class, both concrete and abstract" is that really
right. Assume you have 5 methods in the abstract class and we call them
a,b,c,d,e and a,b and c are concrete so d and e are pure cirtual. Then when
you implement the concrete methods a,b and c in the abstact class can you
then use the abstact methods d or e.

Sure:

#include <iostream>

class Dog
{
public:
virtual void bark() =0; // pure virtual
void barkALot() // concrete
{
for (int i=0; i<10; ++i) bark();
}
};

class Peke : public Dog
{
public:
virtual void bark()
{
std::cout << "woof\n";
}
};

class GreatDane : public Dog
{
public:
virtual void bark()
{
std::cout << "WOOF\n";
}
};

int main()
{
Dog* pdog[3];
// pdog[0] = new Dog; --- error: Dog is abstract
pdog[1] = new Peke;
pdog[2] = new GreatDane;
pdog[1]->barkALot();
pdog[2]->barkALot();
delete pdog[1];
delete pdog[2];
return 0;
}
 
R

Rolf Magnus

Thomas said:
yes, you can. The only situation where this is impossible is in the
constructor and destructor of your abstract class, because in the during
their execution all method calls are bound statically, as the object may
be already be constructed/destructed partially.

Your claim is correct, but not the reason. The calls are not bound
statically. They still use polymorphism, but only up to the class that the
constructor that is currently being executed belongs to.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top