Connecting Tech Pros Worldwide Forums | Help | Site Map

Is this a abstract classe

sudhir
Guest
 
Posts: n/a
#1: Mar 8 '06
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.
-------------------


Sunil Varma
Guest
 
Posts: n/a
#2: Mar 8 '06

re: Is this a abstract classe



sudhir wrote:[color=blue]
> 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.
> -------------------[/color]

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.

Victor Bazarov
Guest
 
Posts: n/a
#3: Mar 8 '06

re: Is this a abstract classe


Sunil Varma wrote:[color=blue]
> [..]
> A class is said to be abstract if and only if it contains[/color]

.... or inherits ...
[color=blue]
> at least one
> pure virtual function.
> And in any other case a class is not abstract.[/color]

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


Jim Langston
Guest
 
Posts: n/a
#4: Mar 8 '06

re: Is this a abstract classe



"sudhir" <sudhir.kumar.sharma@gmail.com> wrote in message
news:1141792007.810245.43820@j33g2000cwa.googlegro ups.com...[color=blue]
>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 ?[/color]

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.
[color=blue]
> Q 2. Which one is correct?
>
> If a class contains a virtual class then it is said to a abstract
> class ?[/color]

Only if it's pure virtual.
[color=blue]
> Or a abstract class always contains a virtual function ?[/color]

Only if it's pure virtual.
[color=blue]
> Or neither is true ?[/color]

If either case is pure virtual.
[color=blue]
> 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.[/color]

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.


sudhir
Guest
 
Posts: n/a
#5: Mar 8 '06

re: Is this a abstract classe


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

Closed Thread