why it fails

C

cpluszaap

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)
 
G

Gianni Mariani

cpluszaap said:
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;
 
A

athresh

" 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;


}
 
M

Mike Wahler

cpluszaap said:
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
 
G

GTO

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,085
Latest member
cryptooseoagencies

Latest Threads

Top