abstract classes in C++

Jan 10, 2017 at 6:44am
Is there any differences among pure virtual class,an abstract class and an interface? Aren't they're synonyms of the same thing?
Jan 10, 2017 at 7:42am
To my understanding, they're same thing in C++
Jan 10, 2017 at 9:08am
@liuyang ! I also thought the same,but still had a doubt !
Jan 10, 2017 at 10:00am
An abstract class is a class that you can't use to create objects. It's main use is to let other classes inherit from it. An abstract class has at least one pure virtual function. If a class that inherits from an abstract class doesn't provide a definition of all the pure virtual functions it is itself also an abstract class.

When talking about the interface of classes I think you can say it's all the public members that you are supposed to use to interact with the class from outside. An abstract class is often used to define an interface (a minimum set of functions) that its subclasses must provide.

The C++ equivalent of an interface in Java is an abstract class that's only got functions which are public and pure virtual, and no member variables. I don't know what "pure virtual class" means but if I heard it I would assume this is what they meant.
Last edited on Jan 10, 2017 at 10:13am
Jan 10, 2017 at 11:05am
C++ has virtual functions and pure virtual functions (methods):
http://stackoverflow.com/questions/2652198/difference-between-a-virtual-function-and-a-pure-virtual-function

Since an ABC has at least one pure virtual function I've seen such classes called, by extension, pure virtual classes sometimes
Jan 10, 2017 at 12:58pm
A logical interface of a class can include non-members too:
1
2
3
4
5
6
class Foo {
 // members
};

std::ostream & operator<< ( std::ostream &, const Foo  & );
std::istream & operator>> ( std::istream &, Foo  & );

These operators << and >> are not members, but they are supplied with Foo and affect how Foo can be used. Therefore, part of interface. (The example is not polymorphic though.)
Topic archived. No new replies allowed.