Thread: Classes & Inheritance

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    37

    Classes & Inheritance

    This is a simple Animal class. The Dog class inherits from the Animal class. I have to show functions. The 1st one (belonging to the Animal class seems to work fine). The 2nd show function seems to produce some wierd output that I cannot understand why. Can anybody explain why it does this and how to fix it? This is my first attempt at inheritance, so please keep it basic.


    Main File:
    // Animal main file

    #include <iostream>

    using namespace std;

    /* CLS Added to clear screen after showing calculations */
    #define CLS system("cls")

    #include "Animal.hpp"


    int main()
    {
    // creates a new Animal object
    Animal fourLegged(12, "Wart", "Whinee", 1289.0);

    // calls the Animal.show function for fourLegged object
    fourLegged.show();

    cout << endl << endl;

    // Convert fourLegged into new Animal type
    Dog Puppy(fourLegged);

    // sets AKC Number by calling Dog::setAKCnumber function
    Puppy.setAKCnumber("123456");

    // sets the dogs "fixed status by calling the Dog::setFixed function
    Puppy.setFixed("Yes");

    // calls the Dog.show functions (I believe that this is where the
    // messes up)
    Puppy.show(2);

    return 0;
    }

    /* (OUTPUT)
    Age: 12 Name: Wart Says: Whinee Weight: 1289


    2

    Age: -858993460 Name: Wart Says: ¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦¦. Weight: -1.07
    374e+008

    Your Dog's AKC number is: 123456
    Is your Dog spayed or nuetered? Yes

    Press any key to continue


    */

    Header File:
    // Animal.hpp file (header file)

    #ifndef ANIMAL_HPP
    #define ANIMAL_HPP

    // sets constants
    const int ANIMAL_NAME = 25;
    const int ANIMAL_SOUND = 15;
    const int AKC_NUMBER = 15;
    const int FIXED = 5;

    class Animal
    {
    private:
    int animalAge;
    char *animalName; // pointer to a string
    char animalSound[ANIMAL_SOUND];
    float animalWeight;
    public:
    Animal(); // default constructor
    Animal(const char *name); // for type conversions
    Animal(int a, const char *name, const char *sound, double d);
    ~Animal(); // deconstructor
    Animal(const Animal &an); // copy constructor
    void show() const;
    // friend functions
    friend ostream & operator << (ostream &os, const Animal &an);
    };



    class Dog : Animal
    {
    char AKCnumber[AKC_NUMBER];
    char Fixed[FIXED];
    public:
    Dog(int a = 0, const char *name = "No Name",
    const char *sound = "No Sound", double d = 0.0,
    const char *akcNumber = "No Number", const char *fixed = "Well");
    Dog(const Animal & an, const char *akcNumber = "No Number",
    const char *fixed = "Well");

    char* getAKCnumber();
    char* getFixed();

    void setAKCnumber(const char *akcNumber);
    void setFixed(const char *fixed);
    void show(int) const;

    };




    #endif


    Animal.cpp file (functions)
    // Animal.cpp file (maintains functions)

    #include <iostream>
    using namespace std;

    #include <cstring>
    #include "Animal.hpp"


    // Constructors

    // 1) Default Constructor
    Animal::Animal()
    {
    animalAge = 0;

    animalName = new char[ANIMAL_NAME]; // allot storage
    strcpy(animalName, "No Name"); // default name
    animalName[ANIMAL_NAME - 1] = '\0';

    strcpy(animalSound, "No Sound");
    animalSound[14] = '\0';

    animalWeight = 0.0;
    }
    // 2) Defined Constructor
    Animal::Animal(int a, const char *name, const char *sound, double d)
    {
    animalAge = a;

    animalName = new char[ANIMAL_NAME]; // allot storage
    strcpy(animalName, name); // initialize pointer
    animalName[ANIMAL_NAME - 1] = '\0';

    strncpy(animalSound, sound, 15);
    animalSound[14] = '\0';

    animalWeight = d;
    }
    // 3) For type conversions
    Animal::Animal(const char *name)
    {
    animalAge = 0;

    animalName = new char[ANIMAL_NAME]; // allot storage
    strcpy(animalName, name); // initialize pointer
    animalName[ANIMAL_NAME - 1] = '\0';

    strcpy(animalSound, "No Sound");

    animalWeight = 0.0;
    }
    // 4) Copy Constructor
    Animal::Animal(const Animal &an)
    {
    animalName = new char[ANIMAL_NAME]; // allot storage
    strcpy(animalName, an.animalName); // copy animalName to new location
    animalName[ANIMAL_NAME - 1] = '\0';
    }
    // Destructor
    Animal::~Animal()
    {
    delete [] animalName; // delete objects
    }
    void Animal::show() const
    {

    // Setup Dog::show() format
    ios_base::fmtflags initialState =

    cout.setf(ios::right, ios::adjustfield);
    cout.width(2);

    cout.setf(ios::left, ios::adjustfield);
    cout.width(10);

    cout.setf(ios::left, ios::adjustfield);
    cout.width(10);

    cout.setf(ios::right, ios::adjustfield);
    cout.width(8);


    cout << " Age: "
    << animalAge
    << " Name: "
    << animalName
    << " Says: "
    << animalSound
    << " Weight: "
    << animalWeight
    << endl;

    cout.setf(initialState); // restores the original format

    }
    // Overloaded operator functions
    // 1) Allows us to use cout with our Animal class.
    ostream & operator << (ostream &os, const Animal &an)
    {
    os << an.animalName;

    //Returns ostream object to allow additional << after Animal object
    return os;
    }

    // Dog Constructors
    Dog:og(int a, const char *name, const char *sound,
    double d, const char *akcNumber,
    const char *fixed) : Animal(a, name, sound, d)
    {

    strcpy(AKCnumber, akcNumber);
    AKCnumber[14] = '\0';

    strcpy(Fixed, fixed);
    Fixed[4] = '\0';
    }
    Dog:og(const Animal & an, const char *akcNumber,
    const char *fixed) : Animal(an)
    {

    strcpy(AKCnumber, akcNumber);
    AKCnumber[14] = '\0';

    strcpy(Fixed, fixed);
    Fixed[4] = '\0';
    }

    char* Dog::getAKCnumber()
    {
    return AKCnumber;
    }
    char* Dog::getFixed()
    {
    return Fixed;
    }
    void Dog::setAKCnumber(const char *akcNumber)
    {
    strncpy(AKCnumber, akcNumber, AKC_NUMBER);
    AKCnumber[AKC_NUMBER - 1] = '\0';
    }
    void Dog::setFixed(const char *fixed)
    {
    strncpy(Fixed, fixed,FIXED);
    Fixed[FIXED - 1] = '\0';
    }

    void Dog::show(int n) const
    {
    cout << n << endl;

    // Setup Dog::show() format
    ios_base::fmtflags initialState =
    cout.setf(ios::right, ios::adjustfield);
    cout.width(2);
    cout.setf(ios::left, ios::adjustfield);
    cout.width(10);
    cout.setf(ios::left, ios::adjustfield);
    cout.width(10);
    cout.setf(ios::right, ios::adjustfield);
    cout.width(8);
    cout << endl;

    Animal::show(); //Display base portion

    cout << endl << "Your Dog's AKC number is: " << AKCnumber
    << endl
    << "Is your Dog spayed or nuetered? " << Fixed << endl << endl;

    cout.setf(initialState);
    }

    As always, any help is greatly appreciated,
    Alan

  2. #2
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    You could have used the code tags to make it minutely readable...

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    37

    Code Tags

    What are code tags? I placed extra comments in main. What more could I do to make it more readable?

  4. #4
    Registered User Sekti's Avatar
    Join Date
    Feb 2002
    Posts
    163

    you would do this...

    first disable smilies that way some thing like :) won't make a face then before and after your code put
    [.code]
    //blah
    [./code]
    with out the periods and it will indent your code and stuff that way it is easier to read for us
    +++
    ++
    + Sekti
    ++
    +++

  5. #5
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Thumbs up Also, you...

    Also, on the line of the Animal.hpp where it says:

    class Dog : Animal


    I think you have to have:

    class Dog : public Animal

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    37

    Problem solved

    The last post was correct. I did have to add "Public" to my Dog class declaration.

    The problem has been solved and I am posting this to help anybody who was reading my problem and might have a similar one.

    The problem was with the copy constructor in the Animal class. It was originally made to only handle just the animalName. That is why there was junk in the Dog class show function.

    Once I completed the Animal copy constructor the program worked fine.

    Thanks for the help,
    Alan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Defining classes following user input. Inheritance.
    By Swerve in forum C++ Programming
    Replies: 7
    Last Post: 03-17-2009, 09:21 AM
  2. classes, inheritance and include problems
    By baniakjr in forum C++ Programming
    Replies: 6
    Last Post: 12-12-2006, 01:45 PM
  3. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  4. virtual classes and inheritance
    By skora in forum C++ Programming
    Replies: 5
    Last Post: 10-28-2003, 05:28 PM
  5. Inheritance Classes w/ same function name
    By Diamonds in forum C++ Programming
    Replies: 3
    Last Post: 03-11-2003, 01:11 AM