Jan 21, 2010 at 12:54am UTC
#include<iostream>
#include<string>
using namespace std;
class
fish
{
public
:
fish(string name);
void
swim();
void
eat();
protected
:
string Name;
};
class
cow:public
fish
{
public
:
cow(string name);
void
sleep(int
count);
};
fish::fish(string name)
{
name = Name;
}
void
fish::swim()
{
cout << Name << "just learned how to swim" << endl;
}
void
fish::eat()
{
cout << Name << "just learned how to eat" << endl;
}
void
cow::sleep(int count)
{
cout << Name << "just slept for " << count << " times, YOU LOSE!" << endl;
}
int
main()
{
fish Fish("FISH");
cow Cow("COW");
char
a;
cout << "U wanna play (y/n)" << endl;
cin >> a;
if
(a == 'y' || a == 'Y')
{
cout << "Ok sweet" << endl;
}
else
{
cout << "WTH" << endl;
cin.get();
return 1
;
}
cout << "Pick a number" << endl;
short int
b;
cin >> b;
cout << "Ok here's how you play, you have a pet and let's see if you win or lose" << endl;
Fish.swim();
Fish.eat();
Cow.sleep(b);
cout << "Done" << endl;
cin.get();
return 0
;
}
I dunno why but apparently this is the problem:
[Linker error] undefined reference to `cow::cow(std::string)'
ld returned 1 exit status '
Last edited on Jan 21, 2010 at 12:55am UTC
Jan 21, 2010 at 1:35am UTC
In class cow you have:
cow(string name);
But you never define it anywhere.
Btw, to use code tags, you put them around all your code, not just the keywords and stuff.
Jan 21, 2010 at 2:05am UTC
Hmm..how should I define it?
Jan 21, 2010 at 2:19am UTC
Last edited on Jan 21, 2010 at 2:19am UTC
Jan 21, 2010 at 3:08am UTC
K i did that and this problem shows up:
cow::cow(string name) {
Name = name;
}
C:\Dev-Cpp\Untitled1.cpp In constructor `cow::cow(std::string)':
and this "32 C:\Dev-Cpp\Untitled1.cpp no matching function for call to `fish::fish()' " is for:
fish::fish(string name)
{
Name = name;
}
Jan 21, 2010 at 3:41am UTC
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#include<iostream>
#include<string>
using namespace std;
class fish {
public :
fish();
fish(string name);
void swim();
void eat();
protected :
string Name;
};
class cow : public fish {
public :
cow(string name);
void sleep(int count);
};
fish::fish() {
}
fish::fish(string name) {
fish::Name = name;
}
void fish::swim() {
cout << Name << " just learned how to swim" << endl;
}
void fish::eat() {
cout << Name << " just learned how to eat" << endl;
}
cow::cow(string name) : fish(name) {
}
void cow::sleep(int count) {
cout << Name << " just slept for " << count << " times, YOU LOSE!" << endl;
}
int main() {
fish Fish("FISH" );
cow Cow("COW" );
char a;
cout << "U wanna play (y/n)" << endl;
cin >> a;
if (a == 'y' || a == 'Y' ) {
cout << "Ok sweet" << endl;
} else {
cout << "WTH" << endl;
cin.get();
return 1;
}
cout << "Pick a number" << endl;
short int b;
cin >> b;
cout << "Ok here's how you play, you have a pet and let's see if you win or lose" << endl;
Fish.swim();
Fish.eat();
Cow.sleep(b);
cout << "Done" << endl;
cin.get();
return 0;
}
Last edited on Jan 21, 2010 at 4:03am UTC
Jan 21, 2010 at 3:48am UTC
Aw man, don't just solve the problem....
Jan 21, 2010 at 3:58am UTC
@tummychow
sorry, i didn't mean to.. it's just his code above is hard to read.
so here's how it works.
by default a derive class calls the the default constructor (constructor w/o param) of the base class. so in this case the base class fish doesn't have a default constructor so we explicitly have to call the constructor with string parameters in line 38;
please read more..
http://cplusplus.com/doc/tutorial/inheritance/
Last edited on Jan 21, 2010 at 3:59am UTC
Jan 21, 2010 at 5:15am UTC
haha. yeah me too ^^
IMO it is very funny! ; )
Jan 21, 2010 at 8:11am UTC
lol does that mean the cow can swim and eat?? lol swimming cows....
OMG I"m still laughing at this....
Last edited on Jan 21, 2010 at 8:12am UTC
Jan 21, 2010 at 8:27am UTC
lol....I'm bored does this code accomplish anything?
I'm having a hard time understanding because I'm a noob :(
Apparently you lose no matter what???
Still funny though lol
Jan 21, 2010 at 8:58am UTC
hey me too, i'm a n00b and i'm bored.. hey do you like game programming? maybe we could make a n00b team.
Jan 23, 2010 at 11:40pm UTC
Well actually, Game Software Development is my major and I'm just getting into my 3rd week of programming so as soon as I have a good handle on data manipulation; I just started learning classes...