Overloading functions??/inheritance

Hi, is there a way to overload functions (i think that's the right term?) with inheritance?
as in, is there a way to make this work? I know I could do this same thing other (fundamentally different) ways, but is there a way to do it like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace std;

class plant
{
public:
    int height;
    void grow();
};

class seaweed: public plant
{
    void grow()
    {
         height*=5;
    }
};
class oak: public plant
{
    void grow()
    {
         height*=1.1;
    }
};

seaweed seaweed1;
oak oaktree1;

plant listingArray[2] = {seaweed1,oaktree1};

int main()
{
    seaweed1.height=1;
    oaktree1.height=1;  
    for (int i=0; i<2; i++)
    {
        listingArray[i].height=1;
    }

for (int years=0; years<50; years++)
{
    for (int i=0; i<2; i++)
    {

        listingArray[i].grow; //this doesn't work
    }
    cout << "after "<<years<<" years:\n oak = "<<oaktree1.height<<"\nseaweed = "<<seaweed1.height<<endl;
}
return 0;
}
Last edited on
On line 8, you forgot the keyword virtual before void. You will also want to add = 0 before the semicolon.

On line 42, you are missing the function argument list - just add () before the semicolon.
Last edited on
I did forget the function argument list on line 45. But adding 'virtual' and '=0' as recommended gives a whole bunch of errors upon compiling..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
using namespace std;

class plant
{
public:
    int height;
    virtual void grow()= 0;
};

class seaweed: public plant
{
    void grow()
    {
         height*=5;
    }
};
class oak: public plant
{
    void grow()
    {
         height*=1.1;
    }
};

seaweed seaweed1;
oak oaktree1;

plant listingArray[2] = {seaweed1,oaktree1};

int main()
{
    seaweed1.height=1;
    oaktree1.height=1;
    for (int i=0; i<2; i++)
    {
        listingArray[i].height=1;
    }

for (int years=0; years<50; years++)
{
    for (int i=0; i<2; i++)
    {
        listingArray[i].grow();
    }
    cout << "after "<<years<<" years:\n oak = "<<oaktree1.height<<"\nseaweed = "<<seaweed1.height<<endl;
}
return 0;
}



E:\Programming\C++ projects and files\Temp\temp4cpp.comquestion\main.cpp|29|error: invalid abstract type 'plant' for 'listingArray'|
E:\Programming\C++ projects and files\Temp\temp4cpp.comquestion\main.cpp|4|note: because the following virtual functions are pure within 'plant':|
E:\Programming\C++ projects and files\Temp\temp4cpp.comquestion\main.cpp|8|note: virtual void plant::grow()|
E:\Programming\C++ projects and files\Temp\temp4cpp.comquestion\main.cpp|29|error: cannot allocate an object of abstract type 'plant'|
E:\Programming\C++ projects and files\Temp\temp4cpp.comquestion\main.cpp|4|note: since type 'plant' has pure virtual functions|
E:\Programming\C++ projects and files\Temp\temp4cpp.comquestion\main.cpp|29|error: cannot allocate an object of abstract type 'plant'|
E:\Programming\C++ projects and files\Temp\temp4cpp.comquestion\main.cpp|4|note: since type 'plant' has pure virtual functions|

Ah, yes - you can't have an array of an abstract type, but you can have an array of pointers to it. Try changing line 29 to be an array of pointers instead - you will also need to change lines 37 and 44 to use the -> operator instead of the dot operator.
Sweet. Thank you:)
Drat no I wrote that program wrong. I wrote it just to isolate one problem I'm having with a larger program, and so the grow functions should be type int and not void; I should have written:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
using namespace std;

class plant
{
public:
    int height;
    virtual int grow()= 0;
};

class seaweed: public plant
{
    int grow(int x)
    {
         return x*5;
    }
};
class oak: public plant
{
    int grow(int x)
    {
         return x*1.1;
    }
};

seaweed seaweed1;
oak oaktree1;

plant *listingArray[2] = {&seaweed1,&oaktree1};

int main()
{
    seaweed1.height=1;
    oaktree1.height=1;
    for (int i=0; i<2; i++)
    {
        listingArray[i]->height=1;
    }

for (int years=0; years<50; years++)
{
    for (int i=0; i<2; i++)
    {
        listingArray[i]->height=listingArray[i]->grow();
    }
    cout << "after "<<years<<" years:\n oak = "<<oaktree1.height<<"\nseaweed = "<<seaweed1.height<<endl;
}
return 0;
}


It gives me the same type of errors as last time, but this time its giving them for my declaration of seaweed1 and oaktree1. I tried changing them to pointers as well, but now it compiles but stops working when it gets to " seaweed1->height=1; oaktree1->height=1;" and the debugger gives a "Program received signal SIGSEGV, Segmentation fault."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
using namespace std;

class plant
{
public:
    int height;
    virtual int grow()= 0;
};

class seaweed: public plant
{
    int grow(int x)
    {
         return x*5;
    }
};
class oak: public plant
{
    int grow(int x)
    {
         return x*1.1;
    }
};

seaweed *seaweed1;
oak *oaktree1;

plant *listingArray[2] = {seaweed1,oaktree1};

int main()
{
    seaweed1->height=1;
    oaktree1->height=1;
    for (int i=0; i<2; i++)
    {
        listingArray[i]->height=1;
    }

for (int years=0; years<50; years++)
{
    for (int i=0; i<2; i++)
    {
        listingArray[i]->height=listingArray[i]->grow();
    }
    cout << "after "<<years<<" years:\n oak = "<<oaktree1->height<<"\nseaweed = "<<seaweed1->height<<endl;
}
return 0;
}


thank you, sorry for all the questions
Last edited on
Why does grow in seaweed and plant need that integer parameter? I am not sure what you expect to happen when you call grow() with no parameters from plant - do you want the compiler to magically know what to pass for x?
Nope I forgot to put that in, very sorry.
If it matters/helps, I'm writing a chess program with a parent class tChessPiece, and daughter classes Pawn,Rook, etc., and I wanted to give the daughter chess piece classes each a function which would take arguments along the lines of (current position, intended position) and would return a value of true or false, depending on whether or not it is a valid move.

heres the fixed code for the (hopefully sufficiently) analogous plant program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace std;

class plant
{
public:
    int height;
    virtual int grow(int x)= 0;
};

class seaweed: public plant
{
    int grow(int x)
    {
         return x*5;
    }
};
class oak: public plant
{
    int grow(int x)
    {
         return x*1.1;
    }
};

seaweed *seaweed1;
oak *oaktree1;

plant *listingArray[2] = {seaweed1,oaktree1};

int main()
{
    seaweed1->height=1;
    oaktree1->height=1;
    for (int i=0; i<2; i++)
    {
        listingArray[0]->height;
    }

for (int years=0; years<50; years++)
{
    cout << "here";
    for (int i=0; i<2; i++)
    {
        listingArray[i]->height=listingArray[i]->grow(listingArray[i]->height);
    }
    cout << "after "<<years<<" years:\n oak = "<<oaktree1->height<<"\nseaweed = "<<seaweed1->height<<endl;
}
return 0;
}


thank you
Last edited on
Why did you change lines 26 and 27? Go back to when you had this:
26
27
28
29
seaweed seaweed1;
oak oaktree1;

plant *listingArray[2] = {&seaweed1,&oaktree1};
Try it again and copy and paste the exact error(s) you are getting.
I don't understand how you are getting those errors, it works here:

http://ideone.com/UyDiyo

Of course, since you used integers, oak will never grow because 1.1 is truncated to 1, meaning you just multiply by 1 over and over.
Last edited on
|In function 'int main()':|
33|error: base operand of '->' has non-pointer type 'seaweed'|
34|error: base operand of '->' has non-pointer type 'oak'|
37|warning: statement has no effect [-Wunused-value]|
47|error: base operand of '->' has non-pointer type 'oak'|
47|error: base operand of '->' has non-pointer type 'seaweed'|
||=== Build finished: 4 errors, 1 warnings (0 minutes, 7 seconds) ===|
Oh, it looks like you deleted your post while I was writing mine and then reposted. See above.

I guess you forgot to change back the -> to . for seawead1 and oak1?
Last edited on
woah no I copied your code into my thing and it worked.
I'm officially going crazy, cause I was sure I changed exactly what you said and it gave me those errors, and then I undid it and tried again and it worked. Thank you very much, I'm sorry for my confusion:p
Topic archived. No new replies allowed.