why it fails

Discussion in 'C++' started by cpluszaap, Feb 1, 2005.

  1. cpluszaap

    cpluszaap Guest

    This program fails i.e it is basically an example of overloading and
    inheritence . The same program with minor modification is able to run
    in java . please update me on the same.

    #include <iostream.h>

    class Base {
    public:
    void func() {
    cout << "Base :: func(float) " << endl;
    }



    };


    class D : public Base {
    public:


    int func(int f) {
    cout << "D :: func(int) " << endl;
    return 0;

    }
    };


    void main() {

    D *pobj = new D();
    pobj->func( );
    }



    The Error is


    ompiling...
    typ.cpp
    typ.cpp(29) : error C2660: 'func' : function does not take 0
    parameters
    Error executing cl.exe.

    typ.obj - 1 error(s), 0 warning(s)
     
    cpluszaap, Feb 1, 2005
    #1
    1. Advertising

  2. cpluszaap wrote:
    > This program fails i.e it is basically an example of overloading and
    > inheritence . The same program with minor modification is able to run
    > in java . please update me on the same.
    >
    > #include <iostream.h>
    >
    > class Base {
    > public:
    > void func() {
    > cout << "Base :: func(float) " << endl;
    > }
    >
    >
    >
    > };
    >
    >
    > class D : public Base {
    > public:
    >


    using Base::func;

    >
    > int func(int f) {
    > cout << "D :: func(int) " << endl;
    > return 0;
    >
    > }
    > };
    >
    >
    > void main() {
    >
    > D *pobj = new D();
    > pobj->func( );
    > }
     
    Gianni Mariani, Feb 1, 2005
    #2
    1. Advertising

  3. cpluszaap

    Guest

    Thats fine. But i want it without using USING.
     
    , Feb 1, 2005
    #3
  4. wrote:
    > Thats fine. But i want it without using USING.
    >


    void main() {
    D *pobj = new D();
    pobj->Base::func( );
    }
     
    Gianni Mariani, Feb 1, 2005
    #4
  5. Gianni Mariani wrote:
    > wrote:
    > > Thats fine. But i want it without using USING.
    > >

    >
    > void main() {
    > D *pobj = new D();
    > pobj->Base::func( );
    > }


    hey where would you put "using" clause.?. sorry i am newbe
     
    puzzlecracker, Feb 1, 2005
    #5
  6. cpluszaap

    Guest

    " Using " is be put in the begining of derived class .

    i.e


    class D : public Base {
    public:
    using Base func;
    int func(int f) {
    cout << "D :: func(int) " << endl;
    return 0;


    }
     
    , Feb 1, 2005
    #6
  7. cpluszaap

    Mike Wahler Guest

    "cpluszaap" <> wrote in message
    news:...
    > This program fails i.e it is basically an example of overloading and
    > inheritence . The same program with minor modification is able to run
    > in java .


    C++ is not Java. They're two completely separate, distinct
    languages. You cannot assume one will behave like the other,
    regardless of any similar syntax.

    >please update me on the same.
    >
    > #include <iostream.h>


    #include <iostream>
    #include <ostream>

    using std::cout;
    using std::endl;

    >
    > class Base {
    > public:
    > void func() {
    > cout << "Base :: func(float) " << endl;


    Why does your message indicate that this
    function takes a 'float' argument? It
    takes no arguments at all.

    > }
    >
    >
    >
    > };
    >
    >
    > class D : public Base {
    > public:
    >
    >
    > int func(int f) {
    > cout << "D :: func(int) " << endl;
    > return 0;
    >
    > }
    > };
    >
    >
    > void main() {


    int main()

    >
    > D *pobj = new D();
    > pobj->func( );
    > }
    >
    >
    >
    > The Error is
    >
    >
    > ompiling...
    > typ.cpp
    > typ.cpp(29) : error C2660: 'func' : function does not take 0
    > parameters


    This is exactly true. You've defined D::func() as taking
    one argument of type 'int', but your call doesn't supply
    any arguments. Write:

    pobj->func(0); /* or whatever value argument you want */

    Or perhaps you might explain exactly what you're trying
    to do. (If you're trying to achieve polymorphism, note
    that it must be done via a pointer (or reference) to
    the *base* class.)


    -Mike
     
    Mike Wahler, Feb 1, 2005
    #7
  8. cpluszaap

    GTO Guest

    Do you want something like:

    #include <iostream>

    using namespace std;

    class base
    {
    public:
    void func()
    {
    cout << "base::func() " << endl;
    }
    };


    class D : public base
    {
    public:
    int func(int i)
    {
    cout << "D::func(" << i << ") " << endl;
    return 0;
    }
    };

    void main()
    {
    D *d = new D();
    base *b;
    b = d;
    d->func(5);
    b->func();
    }

    or do you plan to implement polymorphism?

    Gregor

    "cpluszaap" <> wrote in message
    news:...
    > This program fails i.e it is basically an example of overloading and
    > inheritence . The same program with minor modification is able to run
    > in java . please update me on the same.
    >
    > #include <iostream.h>
    >
    > class Base {
    > public:
    > void func() {
    > cout << "Base :: func(float) " << endl;
    > }
    >
    >
    >
    > };
    >
    >
    > class D : public Base {
    > public:
    >
    >
    > int func(int f) {
    > cout << "D :: func(int) " << endl;
    > return 0;
    >
    > }
    > };
    >
    >
    > void main() {
    >
    > D *pobj = new D();
    > pobj->func( );
    > }
    >
    >
    >
    > The Error is
    >
    >
    > ompiling...
    > typ.cpp
    > typ.cpp(29) : error C2660: 'func' : function does not take 0
    > parameters
    > Error executing cl.exe.
    >
    > typ.obj - 1 error(s), 0 warning(s)
     
    GTO, Feb 1, 2005
    #8
  9. cpluszaap

    Old Wolf Guest

    Gianni Mariani wrote:
    >
    > void main() {
    > D *pobj = new D();
    > pobj->Base::func( );
    > }


    main must have return type 'int'.
     
    Old Wolf, Feb 1, 2005
    #9
    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. John Saunders
    Replies:
    1
    Views:
    685
    John Saunders
    Nov 18, 2003
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    917
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,030
    Smokey Grindel
    Dec 2, 2006
  4. Replies:
    2
    Views:
    300
    Grant Wagner
    Dec 23, 2004
  5. dayo
    Replies:
    11
    Views:
    353
    Ilya Zakharevich
    Dec 16, 2005
Loading...

Share This Page