Class question

#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
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.
Hmm..how should I define it?
that's funny a cow inherits from a fish ^^
first please fix your code, http://cplusplus.com/articles/firedraco1/

you should implement it like this..
1
2
3
cow::cow(string name) {
  Name = name;
}
Last edited on
woah i think this is wrong..
1
2
3
4
5
6
fish::fish(string name)
{
  name = Name;
  //shouldn't it be
  //Name = name; ?
}
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;
}
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
Aw man, don't just solve the problem....
@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
lol Funniest thig I've seen all day!!

that's funny a cow inherits from a fish ^^
first please fix your code, http://cplusplus.com/articles/firedraco1


I was confused too, but after I read this I just laughed and forgot about it; good times.
haha. yeah me too ^^
IMO it is very funny! ; )
lol does that mean the cow can swim and eat?? lol swimming cows....
OMG I"m still laughing at this....
Last edited on
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
hey me too, i'm a n00b and i'm bored.. hey do you like game programming? maybe we could make a n00b team.
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...
Topic archived. No new replies allowed.