virtual function problem

F

foodic

Hi all,
I am new to c++ I have a problem compiling this program,

#include <iostream.h>
class iq
{
protected :
int k;
public :
iq(int k){
this->k=k;
}
virtual void printiq(){}
};
class im : public iq
{
int l;
public :
im(int k):iq(k) {
this->l=k;
}
void printiq(){
cout <<l;
}

};
int main(){
im l(10);
iq *l;
((l)iq*)->printiq();
}
Basically I want to acess the derived class member function
through pointer to the Base class. When i compile this program
it gives error like,
inherit.c: In function `int main ()':
inherit.c:26: conflicting types for `iq *l'
inherit.c:25: previous declaration as `im l'
inherit.c:27: parse error before `*'
How to solve this problem,

divya
 
A

Alf P. Steinbach

* foodic:
#include <iostream.h>

Don't. It's not a standard C++ header. Use


#include <iostream>


class iq
{
protected :
int k;
public :
iq(int k){
this->k=k;

This indicates you're using a bad tutorial or book. Technically okay
but bad style.

}
virtual void printiq(){}
};
class im : public iq
{
int l;

Don't use lowercase ell for a name (easy to confuse with 1).

Don't use uppercase oh for a name (easy to confuse with 0).

public :
im(int k):iq(k) {
this->l=k;
}
void printiq(){
cout <<l;
}

};
int main(){
im l(10);

iq *l;
((l)iq*)->printiq();
}


int main
{
im myIq( 10 );
iq& myMoreAbstractIq = im;

myMoreAbstractIq.printiq();
}


Steer well clear of raw pointers, and burn that book (or whatever).

Try e.g. "Accelerated C++".

Cheers, & hope this help,

- Alf
 
R

Rolf Magnus

foodic said:
Hi all,
I am new to c++ I have a problem compiling this program,

#include <iostream.h>

Note that <iostream.h> is an obsolete non-standard header that should be
avoided. Use said:
class iq
{
protected :
int k;
public :
iq(int k){
this->k=k;
}

Better use initialization instead of assignment. For built-in types like
int, it doesn't make much of a difference, but for class types, it does. So
write:

iq(int k)
: k(k)
{
}

And though it's possible to have the same name for the parameter as for the
member variable, the code is usually considered more readable if they have
different names.
virtual void printiq(){}
};
class im : public iq
{
int l;
public :
im(int k):iq(k) {
this->l=k;
}
void printiq(){
cout <<l;
}

};
int main(){
im l(10);
iq *l;
((l)iq*)->printiq();
}
Basically I want to acess the derived class member function
through pointer to the Base class. When i compile this program
it gives error like,
inherit.c: In function `int main ()':
inherit.c:26: conflicting types for `iq *l'
inherit.c:25: previous declaration as `im l'

The compiler says it quite clearly. You're trying to declare two variables
of different type (one of type im, the other of type pointer to iq) that
have the same name.
inherit.c:27: parse error before `*'

What is ((l)iq*) supposed to do? It looks kind of like a C style cast, but
with type and value swapped. That doesn't make sense, neither to me, nor to
the compiler. ;-)
And if it was supposed to be a cast, drop it. You don't need one here.
How to solve this problem,

Give your variables different names:

int main(){
im l(10);
iq *m = &l;
m->printiq();
}
 

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

Similar Threads

LEETCODE 3 3
Java matrix problem 3
Virtual function 2
Problem with codewars. 5
problem in calling pure virtual function 4
My Status, Ciphertext 2
Non virtual and inheritance 7
C++ Template Virtual Function 6

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,655
Latest member
eizareri

Latest Threads

Top