Need Help with Inheritance assignment

This is a discussion on Need Help with Inheritance assignment within the C++ Programming forums, part of the General Programming Boards category; Here are my instructions: Create an “Animal” base class. This class will have size, weight, and eyeColor as instance variables. ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    17

    Question Need Help with Inheritance assignment

    Here are my instructions:

    Create an “Animal” base class. This class will have size, weight, and eyeColor as instance variables. It will have walk() and print() as methods.

    Create a “Dog” class and a “Cat” class. Both these classes will inherit from “Animal”—i.e. “Dog” and “Cat” are subclasses of “Animal.” The “Dog” class will have a bool claws instance variable (the boolean value indicates whether the dog’s claws are clipped or not). The “Dog” class will also have bark() and print() methods. The “Cat” class will have a bool retractableClaws instance variable (the boolean value indicates whether the cat’s class are retracted or not). The “Cat” class will also have meow() and print() methods.

    Create two subclasses of “Dog.” Create two subclasses of “Cat.” Each of these subclasses will have an instance variable of your choice. They will also each have a print() method.

    Print methods will invoke the print method of their superclass (if any--the top superclass has no superclass), and they will output status information of the instance variables that belong to that particular class.

    Constructors will explicitly invoke their superconstructors (if any), and will indicate the class name of that particular class. They will also invoke the print method of that particular class.

    Destructors will indicate the class name of that particular class. They will also invoke the print method of that particular class.





    This is my code, without the cat part added since it will be pretty much the same as the dog part:

    Code:
    **Animal.h**
    #ifndef ANIMAL_H
    #define ANIMAL_H
    
    #include "stdafx.h"
    #include <string>
    using std::string;
    
    class Animal
    {
    public:
    	Animal( const string &, const string &, const string & );
    	~Animal();
    
    	void setSize( const string &);
    	string getSize() const;
    
    	void setWeight( const string &);
    	string getWeight() const;
    
    	void setEyeColor( const string &);
    	string getEyeColor() const;
    
    	void print() const;
    	void walk() const;
    
    private:
    	string Size;
    	string Weight;
    	string EyeColor;
    };
    
    #endif
    Code:
    **Animal.cpp**
    #include <iostream>
    using std::cout:
    using std::endl;
    
    #include "stdafx.h"
    #include "Animal.h"
    
    Animal::Animal( const string &size, const string &weight, const string &eyecolor)
    	: size( size ), weight( weight ), eyeColor( eyecolor )
    {
    	cout << "Animal Constructor: "<< endl;
    	print();
    		cout <<"\n\n";
    }
    
    void Animal::setSize ( const string &size )
    {
    	Size = size;
    }
    
    string Animal::getSize() const
    {
    	return Size;
    }
    void Animal::setWeight( const string &weight)
    {
    	Weight = weight;
    }
    string Animal::getWeight() const
    {
    	return Weight;
    }
    void Animal::setEyeColor( const string &eyecolor)
    {
    	EyeColor = eyecolor;
    }
    string Animal::getEyeColor() const
    {
    	return EyeColor;
    }
    void Animal::print() const
    {
    	cout << "Animal: "
    		<< "\nSize: " << getSize()
    		<< "\nWeight: " << getWeight()
    		<< "\nEye Color: " << get EyeColor();
    }
    Code:
    **Dog.h**
    #ifndef DOG_H
    #define DOG_H
    
    #include "stdafx.h"
    #include <string>
    using std::string;
    
    #include "Animal.h"
    
    class Dog : public Animal
    {
    public:
    	Dog ( const string &, const string &, const string &, const string & );
    	~Dog();
    
    	void setClaws( const string &);
    	string getClaws() const;
    
    	private:
    	string Claws;
    
    	void print() const;
    	void walk() const;
    };
    
    #endif
    Code:
    **Dog.cpp**
    #include <iostream>
    using std::cout:
    using std::endl;
    
    #include "stdafx.h"
    #include "Dog.h"
    
    Dog::Dog( const string &size, const string &weight, const string &eyecolor, const string &claws)
    	:Animal ( size, weight, eyecolor )
    	set Claws
    
    
    Dog::~Dog()
    {
    	cout << "Dog destructor: " << endl;
    	print();
    	cout << "\n\n";
    }
    Code:
    **main.cpp**
    #include <iostream>
    using std::cout;
    using std::endl;
    using std::fixed;
    
    #include <iomanip>
    
    #include "Animal.h"
    
    int main()
    {
    	{
    		Dog dog1(
    			"Big", "140", "Brown", "0");
    	}
    
    	cout << endl;
    
    	{
    		Dog dog2(
    			"Small", "20", "Hazel", "1");
    	}
    
    	cout << endl;
    	return 0;
    }
    How do I make this work!?!?!?!
    Thanks

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> How do I make this work!?!?!?!

    Start by telling us what doesn't work. Include error messages if you are getting errors, include sample input and expected output if it compiles but doesn't run as you expect it to.

    BTW, to my knowledge, if you are using pre-compiled headers (which you really don't need at all here), then the #include "stdafx.h" should be only in cpp files, and only at the top of the file before any other includes. Also, your Dog constructor doesn't look finished. What is "set Claws" supposed to do?
    Last edited by Daved; 11-07-2005 at 08:19 PM.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Check the proper format for a comment in C++, then compare that to the first line in all your files. By the way, that error could easily be picked up with an editor that uses syntax highlighting, i.e. different color text for different parts of your code.
    Last edited by 7stud; 11-07-2005 at 08:11 PM.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    17
    Is there a way to make visual c++ express do that?

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    just by skimming, don't you have to make print functions for all instances? So make the print function in animal virtual. In you main, make pointers to the base type and set the from there.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> Is there a way to make visual c++ express do that?

    If you are talking about the syntax highlighting, it should already be doing that. 7stud was just referring to **Dog.h** that you have at the top of the code blocks. Presumably you added that here and you don't actually have in it your files.

    BTW, you never did say why/how it doesn't work.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    17
    the stdafx placement helped me out a bit

    I was able to fix all the problems from there
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 03:34 AM
  2. Replies: 16
    Last Post: 06-08-2009, 04:03 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Replies: 1
    Last Post: 10-27-2006, 02:21 PM
  5. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21