Problem with inheritance and polymorphism

Hello, I have the written the following:

1. http://www2.hawaii.edu/~lilyllam/C++/animal.cpp
2. http://www2.hawaii.edu/~lilyllam/C++/animal.h
3. http://www2.hawaii.edu/~lilyllam/C++/bird.cpp
4. http://www2.hawaii.edu/~lilyllam/C++/bird.h
5. http://www2.hawaii.edu/~lilyllam/C++/cow.cpp
6. http://www2.hawaii.edu/~lilyllam/C++/cow.h
7. http://www2.hawaii.edu/~lilyllam/C++/mainAnimal.cpp

I want to be able to display the correct information for each animal (cows cannot fly and have 4 legs, or birds can fly and have 2 legs).  I also saved a copy of the error output - it was really long - here: http://www2.hawaii.edu/~lilyllam/C++/error_log.txt.

Thank you.
luna621Asked:
Who is Participating?
 
Sys_ProgConnect With a Mentor Commented:
Ok, here are the change I did to your files
Let me know if you dont get anything



// FILE: animal.cpp

#include<iostream>
#include "animal.h"

using namespace std;


animal::animal( char *sound1, const int &fly1)
{
    strcpy (sound, sound1);
    fly = fly1;
}


void animal::displaydata()
{
      cout << "Sound: " << sound << "\tFly: " << fly << endl;
}





// FILE: animal.h

#ifndef ANIMAL_H
#define ANIMAL_H

class animal
{
    protected:
        char sound[10];             // contains "moo", "quack"
        int fly;                    // 0 - cannot fly, 1 - can fly

    public:
        virtual void displaydata(); // prints the value of sound and fly
        animal (char *sound1, const int &fly1 ) ;
};

#endif





// FILE: bird.cpp

#include<iostream>

#include "bird.h"
using namespace std;


bird::bird():animal ("quack", 1){
}


void bird::displaydata()
{
      cout << "Sound: " << sound << "\tFly: " << fly << endl;
}





// FILE: bird.h

#ifndef BIRD_H
#define BIRD_H
#include "animal.h"
class bird: public animal
{
    public:
        void displaydata(); // prints the value of sound and fly
        bird () ;
};

#endif






// FILE: cow.cpp

#include<iostream>

#include "cow.h"
using namespace std;


cow::cow():animal ("moo", 0){
    numberoflegs = 4;
}


void cow::displaydata()
{
      cout << "Sound: " << sound << "\tLegs: " << numberoflegs << "\tFly: " << fly << endl;
}







// FILE: cow.h

#ifndef COW_H
#define COW_H
#include "animal.h"
class cow: public animal
{
    private:
        int numberoflegs;

    public:
        void displaydata(); // prints the values of sound, numberoflegs and fly
        cow () ;
};

#endif






// FILE: mainAnimal.cpp

#include<iostream>
#include "animal.h"
#include "bird.h"
#include "cow.h"

using namespace std;

void animaldisplaydata(animal *a)
{
    a -> displaydata();
}


int main()
{
     
     //bird b;
     //cow c;    
     animal *a = new bird ();
     animaldisplaydata( a );
     animal *b = new cow ();
     animaldisplaydata( b );
     system ( "PAUSE" );
     return 0;
}





HTH

Amit

0
 
AxterConnect With a Mentor Commented:
FYI:
Above main code has memory leaks.
You really don't need to use new in above code, and I recommend avoiding the use of new operator unless you really need it.

The following should work:
int main()
{
     
     bird b;
     cow c;    
     animaldisplaydata(&b);
     animaldisplaydata(&c);

     system ( "PAUSE" );
     return 0;
}

0
 
Sys_ProgCommented:
Axter,

I just wanted to illustrate the concept to the questioner
I did not intend to write some pefect code - delete the memory etc

Amit
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
luna621Author Commented:
@Sys_Prog: Oh, no wonder I was getting all those errors.  I need to look at this example slowly to have it sink into my brain.  I'll post back if there's something I don't get.

@Axter: So, "new" will cause memory leaks?
0
 
Sys_ProgCommented:
luna621 ,

Delete the allocated memory once u r done as per following

int main()
{
     
     //bird b;
     //cow c;    
     animal *a = new bird ();
     animaldisplaydata( a );
     animal *b = new cow ();
     animaldisplaydata( b );
    delete a ;
    delete b ;
     system ( "PAUSE" );
     return 0;
}



amit
0
 
luna621Author Commented:
Thank you!  I have one question.  What does the 'const' in the following do?:   animal ( char *sound1, const int &fly1 );
0
 
Sys_ProgCommented:

If u dont pass by ref, then a copy is created in memory and then passed
Hence int &fly1
which indicates pass by ref

However, by ref means any chanegs to fly1 in the function would get reflected outside from where its called

Hence, make it a const (readonly) so that no changes can be made

Thanks
Amit
0
 
luna621Author Commented:
Ah, const --> keep it constant.  I get it now.  Thank you for explaining!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.