"sudhir" <su*****************@gmail.com> wrote in message
news:11*********************@j33g2000cwa.googlegro ups.com...
Q 1. I defined a class with 10 functions . It contains declaration of 5
functions and 5 functions are declared and defined. Is this class is
said to a abstract class ?
If one of the functions (methods) is pure virtual, then the class is
abstract, other wise it isn't. If it inherits a pure virtual function it's
abstract, otherwise it isn't.
An abstract class is defined as a class with at least one pure virtual
method. That's the definition. If the class doesn't have at least one pure
virtual method (or inherit one) then it's not abstract.
Q 2. Which one is correct?
If a class contains a virtual class then it is said to a abstract
class ?
Only if it's pure virtual.
Or a abstract class always contains a virtual function ?
Only if it's pure virtual.
Or neither is true ?
If either case is pure virtual.
I know that a class containing the pure virtual function is always a
abstract
class but I am confused with class containing the virtual functions.
An abstract class is a class that can not be instatized. It can't be
instantized, becuase it contains at least one pure virutal method.
The following compiles. Comment out that one line and it won't because of a
linking error.
class MyClass
{
public:
MyMethod() {};
MyMethod2();
};
int main()
{
MyClass MyInstance;
// Uncomment the following line and it won't compile
// MyInstance.MyMethod2();
}
Why does this compile? Because the compiler doesn't know where MyMethod2()
is defined. It waits for the linker to do that. The linker never sees any
call to MyMethod2() so it doesn't even try to link it. It works. When
MyMethod2() is called, however, then there's a linking error.
If, however, MyMethod2 was pure virtual it wouldn't compile because it's an
abstract class.
So an abstract class is a class that has at least one pure virtual method.