I want to make a program that uses inheritance.
I basically just want my all the outputs in my classes to be displayed in my main function.
This is the code I have so far but it doesn't output the results. I have three errors but I don't know what they mean. I may be outputting them wrong. (I usually call an instance to my classes and it works though.)
#include <iostream>
#include <string>
usingnamespace std;
//super class DeerFamily
class DeerFamily
{
private:
string willRun;
public:
// deerfamily constructor
DeerFamily()
{
willRun = "The family of deer are running!";
cout << willRun << endl;
} // end deerfamily constructor
}; // end of super class DeerFamily
// Deer class derives from DeerFamily
class Deer : public DeerFamily
{
private:
// private static data member that is true for all objects in class.
bool withKid;
public:
Deer(){
// initialization of static data members
bool withKid = true;
if (withKid == true)
cout << "The deer is with her fawn. :)" << endl;
else
cout << "The deer is not with her fawn... :(" << endl;
} // end of Deer constructor
}; // end of deer sub class
// Fawn class derives from Deer
class Fawn : public Deer
{
private:
// private staticdata member that is true for all objects in class.
bool isAlone;
public:
Fawn(){
// initialization of static data members
bool isAlone = false;
if (isAlone == true)
cout << "The Fawn is alone!" << endl;
else
cout << "The fawn is not alone. :)" << endl;
} // end of Fawn constructor
}; // end of Fawn sub class
int main()
{
// outputs data in all classes.
Fawn();
cin.get();
system ("PAUSE");
return 0;
}
Thanks for the help guys. I did the changes you've recommend megatron.
And AbstractionAnon, my bad at 88, I'm probably supposed to simply call my clases using something like
Fawn baby(); right?
For like 25 the only animal category I'm dealing with are deer. should I have made DeerFamily my super class instead of Animal? I may do that.
for lines 44-54 and 70-80, I want these outputs to be inside these classes so I can call them in the main function. Something like in this website's heritage example:
1 2 3 4 5
class Mother {
public:
Mother ()
{ cout << "Mother: no parameters\n"; }
} // end of mother class
but when I try do my code in a similar style (by getting rid of the void function) I get the expected a declaration error.
for line 97 I like having cin.get() because other people in my class use mac and they prefer this over SYSTEM.("PAUSE")
Again, I appreciate the help. I'm apparently not very good at C++. Even reading these topics over and over it's not clicking with me.
I'm not just talking about classes. By the second week of my C++ class I was getting confused by almost every term and code concept. I am usually able to get a coding language after picking with it enough, but with C++ and more advanced JAVA programs I was completely stumped.
I have until sunday to turn to turn this in so I'm not freaking out or anything, I just wish I had a better understanding of general advanced coding techniques like the for (i = 0; i > 4; i++) stuff which requires me to check what that means every half hour. >_<
No don't get rid of that! Some compilers can throw a fit if you don't have the proper return types for data. Just put the cin.get before return 0; I'm hoping that should do the trick. If that doesn't work or you still get an error give me a shout.
I'm not just talking about classes. By the second week of my C++ class I was getting confused by almost every term and code concept. I am usually able to get a coding language after picking with it enough, but with C++ and more advanced JAVA programs I was completely stumped.
Have you told your teacher? I can't really give you a master class on C++ before the week ends but I can certainly give it a go.
Programming can be confusing and it appears teachers/professors have no idea how to teach it. You aren't the first and definitely not the last to be confused and frustrated by assignments given to you by people who ought to know how well their students are able to grasp concepts.
Yeah I usually e-mail my teacher...Alot. He does a good job though and gives me tips that's helps me. I just wish I didn't need so much help. Makes me feel really dumb when I can't grasp a concept.
EDIT: Oh yeah! I figured out my problem! I just had to type
I'm not one to mess with something that works but that is very peculiar behaviour.
The way a class works is by defining an object then creating an object:
1 2 3 4 5 6 7 8 9 10 11 12 13
class MegaHugeJapaneseSword
{
public:
MegaHugeJapaneseSword(int damage);
};
int main()
{
MegaHugeJapaneseSword SwordOfTheDaimyo(1000); // create a MegaHugeJapaneseSword called SwordOfTheDaimyo that takes a number of damage, in this case 1000
return 0;
}
Send it to your teacher, maybe he can figure that one out. :)
I don't mean to say my teacher doesn't just give me tips.
I mean that after the first few times I e-mail him he gives general tips like "make sure your declarations are initialized" or whatever but if he notices that I'm not getting it right he eventually gives me a piece of code.
So my teacher is helpful. I didn't really e-mail him about this assignment though.
I just e-mailed him like 10 times on my projects >_>