Is this a abstract classe

Discussion in 'C++' started by sudhir, Mar 8, 2006.

  1. sudhir

    sudhir Guest

    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.
    -------------------
    sudhir, Mar 8, 2006
    #1
    1. Advertising

  2. sudhir

    Sunil Varma Guest

    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.
    Sunil Varma, Mar 8, 2006
    #2
    1. Advertising

  3. 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
    Victor Bazarov, Mar 8, 2006
    #3
  4. sudhir

    Jim Langston Guest

    "sudhir" <> wrote in message
    news:...
    >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.
    Jim Langston, Mar 8, 2006
    #4
  5. sudhir

    sudhir Guest

    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
    sudhir, Mar 8, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandre Martins

    Application do global.asax numa classe

    Alexandre Martins, Aug 15, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    401
    Alexandre Martins
    Aug 15, 2003
  2. Zean Smith

    How - Convert Classe/DLL to Web Service?

    Zean Smith, Aug 31, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    8,597
    Kevin Spencer
    Aug 31, 2005
  3. lamilla
    Replies:
    3
    Views:
    394
    Victor Bazarov
    Apr 7, 2005
  4. Cloud Strife

    classe string

    Cloud Strife, Jun 2, 2005, in forum: C++
    Replies:
    1
    Views:
    494
    Cloud Strife
    Jun 2, 2005
  5. sudhir

    Is this a abstract classe

    sudhir, Mar 4, 2006, in forum: C Programming
    Replies:
    1
    Views:
    281
    Richard Heathfield
    Mar 4, 2006
Loading...

Share This Page