Constructor in C++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Allerdyce.John@gmail.com

    #1

    Constructor in C++

    Hi,

    I have 2 classes:

    class A {
    public:
    A();
    A(int i);
    };

    class B: public A {
    pubilc:
    B(int i);
    };

    If I don't have constructor B(), does it call A() automatically?

    Can I do this:
    B b; // B does not have default constructor but A has.

  • Alf P. Steinbach

    #2
    Re: Constructor in C++

    * Allerdyce.John@ gmail.com:[color=blue]
    > Hi,
    >
    > I have 2 classes:
    >
    > class A {
    > public:
    > A();
    > A(int i);
    > };
    >
    > class B: public A {
    > pubilc:[/color]

    See <url: http://www.google.com/search?q=pubilc >.

    [color=blue]
    > B(int i);
    > };
    >
    > If I don't have constructor B(), does it call A() automatically?[/color]

    If you don't have a constructor B() it doesn't exist; C++ only generates
    one for you if you don't declare any constructors at all.

    [color=blue]
    > Can I do this:
    > B b; // B does not have default constructor but A has.[/color]

    Nope.


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Comment

    • Allerdyce.John@gmail.com

      #3
      Re: Constructor in C++

      If I have
      class A {
      public
      A(int i);
      }

      public B{
      public:
      virtual void abstractFuncito n = 0;
      }

      public C{
      public:
      C(int i);
      };

      how can I call A' constructor A(int i) from C(int i).

      I try
      C::C(int i): A(i) {
      }
      it does not compile.

      Comment

      • Alf P. Steinbach

        #4
        Re: Constructor in C++

        * Allerdyce.John@ gmail.com:[color=blue]
        > If I have
        > class A {
        > public
        > A(int i);
        > }
        >
        > public B{
        > public:
        > virtual void abstractFuncito n = 0;
        > }
        >
        > public C{
        > public:
        > C(int i);
        > };
        >
        > how can I call A' constructor A(int i) from C(int i).
        >
        > I try
        > C::C(int i): A(i) {
        > }
        > it does not compile.[/color]

        C does not derive from A.



        --
        A: Because it messes up the order in which people normally read text.
        Q: Why is it such a bad thing?
        A: Top-posting.
        Q: What is the most annoying thing on usenet and in e-mail?

        Comment

        • Victor Bazarov

          #5
          Re: Constructor in C++

          Allerdyce.John@ gmail.com wrote:[color=blue]
          > If I have
          > class A {
          > public
          > A(int i);
          > }[/color]
          ;[color=blue]
          >
          > public B{
          > public:
          > virtual void abstractFuncito n = 0;[/color]

          virtual void pureFunction() = 0;
          [color=blue]
          > }[/color]
          ;[color=blue]
          >
          > public C{
          > public:
          > C(int i);
          > };
          >
          > how can I call A' constructor A(int i) from C(int i).
          >
          > I try
          > C::C(int i): A(i) {
          > }
          > it does not compile.
          >[/color]

          You need to derive 'C' from 'A' first to do that.

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

          Comment

          Working...