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?
#include <iostream>
usingnamespace std;
class plant
{
public:
int height;
virtualvoid 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.
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:
#include <iostream>
usingnamespace std;
class plant
{
public:
int height;
virtualint 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."
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
|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) ===|
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