Simple heritage output

Updated Oct 1 at 3:13PM

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.)

Now nothing is displayed when I test my code.



Any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <string>


using namespace 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;
}	
Last edited on
Lines 49 and 75 can be fixed by

if(var == toSomething)

The other one appears to be a linker problem, it is having trouble locating the Deer() constructor

EDIT -

Also because you are declaring the functions inside the class body, try and part with the Class:: to just void Function() {}
Last edited on
Line 88: You instantiate Deer, but Deer has no default constructor.

Lines 23-27: These lines belong outside the calss declaration.

Line 25: You're assuming all animals are a deer. What if you derived a Salmon class from Animal then called jump()?

Line 44-54: These lines belong outside the class declaration.

Line 49, 75: You're using the assignment operator (=), not the equality operator (==).

Line 70-80: These lines belong outside the class declaration.

Line 97: This statement will never be reached.

Edit:
Line 46 implies line 47 is static, but it is not declared as such.

Line 47 will only be true if HasKid() is called, otherwise, it will be undefined.

Line 72 implies line 73 is static, but it is not declared as such.

Line 73: will only be true if aloneOrNot() is called, otherwise, it is undefined.
Last edited on
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.
Line 97 will not be reached, because return 0; will instantly exit the program, making the instruction unreachable. :)

Can we see updated code?

EDIT:

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.


Who said you aren't very good? Classes are a bugger for us all, especially when it comes to inheritance and polymorphism. :)

Maybe take it slower? Try other data types first, and come back to classes when you have a better grasp of the language?
Last edited on
Ah ok I will get rid of return 0 then.

Yeah I updated it on my first post.

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.

Oops, I'll put it back >_< My bad.

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

 
Fawn();


In my main function instead of Fawn SOMENAME();

And it output the data in my classes.

So thanks for the help, all!
Last edited on
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 >_>
Topic archived. No new replies allowed.