Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is this a abstract classe

Reply
Thread Tools

Is this a abstract classe

 
 
sudhir
Guest
Posts: n/a
 
      03-08-2006
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.
-------------------

 
Reply With Quote
 
 
 
 
Sunil Varma
Guest
Posts: n/a
 
      03-08-2006

sudhir wrote:
> 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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      03-08-2006
Sunil Varma wrote:
> [..]
> A class is said to be abstract if and only if it contains


.... or inherits ...

> at least one
> pure virtual function.
> And in any other case a class is not abstract.


V
--
Please remove capital As from my address when replying by mail


 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      03-08-2006

"sudhir" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.


 
Reply With Quote
 
sudhir
Guest
Posts: n/a
 
      03-08-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is this a abstract classe sudhir C Programming 1 03-04-2006 06:31 AM
How - Convert Classe/DLL to Web Service? Zean Smith ASP .Net 3 08-31-2005 03:35 PM
classe string Cloud Strife C++ 1 06-02-2005 07:02 AM
Passaggio puntatore di classe in CreateThread lamilla C++ 3 04-07-2005 02:09 PM
Application do global.asax numa classe Alexandre Martins ASP .Net 0 08-15-2003 02:40 PM



Advertisments