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