Is this a abstract classe

S

sudhir

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 ?

Q 2. Which one is correct?

If a class contains a virtual class then it is said to a abstract
class ?
Or a abstract class always contains a virtual function ?
Or neither is true ?

Please clarrify me these.
 
S

Sunil Varma

sudhir said:
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 ?

Q 2. Which one is correct?

If a class contains a virtual class then it is said to a abstract
class ?
Or a abstract class always contains a virtual function ?
Or neither is true ?

Please clarrify me these.
------------------
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.
-------------------

A class is said to be abstract if and only if it contains at least one
pure virtual function.
And in any other case a class is not abstract.
 
J

Jim Langston

sudhir said:
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.
 
S

sudhir

Thanks for such a nice solution....

I want to ask some more questions..

The pure virtual function is defined as ..

virtual void sum()=0;

then
Is this a pure virtual function ..
virtual void sum() {}

In above example...

class MyClass
{
public:
MyMethod() {};
MyMethod2();



};


int main()
{
MyClass MyInstance;
// Uncomment the following line and it won't compile
// MyInstance.MyMethod2();


}


MyMethod() {}; Is this a pure virtual function Or
MyMethod2(); It is pure virtual function

One more thing ... class Myclass is made a abstract class then how a
object creation is done on that...using Myclass MyInstance.

thanks
 

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,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top