?
Solved

Problem with inheritance and polymorphism

Posted on 2005-04-30
8
Medium Priority
?
377 Views
Last Modified: 2010-04-01
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.
0
Comment
Question by:luna621
  • 4
  • 3
8 Comments
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 1600 total points
ID: 13902607
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
 
LVL 30

Assisted Solution

by:Axter
Axter earned 400 total points
ID: 13902694
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
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 13902705
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:luna621
ID: 13902708
@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
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 13902716
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
 

Author Comment

by:luna621
ID: 13902804
Thank you!  I have one question.  What does the 'const' in the following do?:   animal ( char *sound1, const int &fly1 );
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 13902812

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
 

Author Comment

by:luna621
ID: 13902821
Ah, const --> keep it constant.  I get it now.  Thank you for explaining!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question