Help with Class Inheritance(Homework)

Hello guys this is actually due tonight at 12. I have been working on this for 2 hours now. I am having trouble understanding Class Inheritance. My program works completely fine in the event that a user enters 0, 0 ,0 for height, width, and length; however, when they enter a value greater than 0 for each parameter is where my problem occurs. I went into debug and it appears that as soon as my last cout statements occure (when I call my getters) my original inherited fields get set back to 7 and 8. I tested with values 10, 10, and 10 and they do successfully get set to that but once my cout statements occur at the end of the app, they get set back to 7 and 7. Why is this?

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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include<iostream>
#include<iomanip>
#include<string>
#include <stdio.h>
#include <math.h>
using namespace std;

class Triangle
{ 
   protected:
      double width, height;
   public:
      Triangle();
	  Triangle(double w, double h);
      double GetWidth();
      double GetHeight();
      double FindArea();
};

Triangle::Triangle ()
{   width = 7;
    height = 8;
}

Triangle::Triangle (double w, double h)
{
	width = w;
	height = h;
}

double Triangle::GetWidth()
{   return width;
}
double Triangle::GetHeight()
{   return height;
}
double Triangle:: FindArea()
{   
	return (width * height * 0.5);
}

class Prism : public Triangle
{
	private:
		double dblLength, dblVolume;
	public:
		Prism();
		//: Triangle(){};
		Prism(double l, double w, double h);
		double GetLength();
		double GetVolume();
		double FindArea();
		

};

Prism::Prism(double l, double w, double h)
{
	dblLength = l;
	width = w;
	height = h;
}

double Prism::FindArea()
{
	return (((width * height * .5) * 2) + ((dblLength * height) + (dblLength * height) + (dblLength * height)));
}

double Prism::GetVolume()
{
	dblVolume = ((width * height * .5) * dblLength);
	return dblVolume;
}

double Prism::GetLength()
{
	return dblLength;
}

Prism::Prism()
{
	dblLength = 10;
}

int main ()
{
	double dblWidth = 0;
	double dblHeight = 0;
	double dblLength = 0;

	cout<<"Lab 12 (Surface Area of a Triangular Prism) has started for Adrian Campos"<<endl<<endl;
	//Width
	do
	{
	cout<<"Enter the value for width: ";
	cin>>dblWidth;
	//cout<<""<<endl;
	if (dblWidth < 0)
		cout<<"The width may not be negative! Please re-enter!"<<endl;
	}while (dblWidth < 0);

	//Height
	do
	{
	cout<<"Enter the value for height: ";
	cin>>dblHeight;
	//cout<<""<<endl;
	if (dblHeight < 0)
		cout<<"The height may not be negative! Please re-enter!"<<endl;
	}while (dblHeight < 0);

	//Length
	do
	{
	cout<<"Enter the value for length: ";
	cin>>dblLength;
	//cout<<""<<endl;
	if (dblLength < 0)
		cout<<"The length may not be negative! Please re-enter!"<<endl;
	}while (dblLength < 0);

	Prism myPrism;
	
	if (dblLength == 0 & dblHeight == 0 & dblWidth == 0)
	{
		Prism();		
	}

	else	
	{
		
		Prism(dblLength, dblWidth, dblHeight);
	}
	
	cout<<"The width of the Prism is: "<<myPrism.GetWidth()<<endl;
	cout<<"The height of the Prism is: "<<myPrism.GetHeight()<<endl;
	cout<<"The length of the Prism is: "<<myPrism.GetLength()<<endl;
	cout<<"The Volume in square units is:  "<<myPrism.GetVolume()<<endl;
	cout<<"The Surface area in square units is: "<<myPrism.FindArea()<<endl;

	system("Pause");
	return 0; 

The problem is your Prism constructor will call the base class's (Triangle's in this case). This will reset your width and height members. You'll need to use an initializer list to call Triangle's constructor manually inside Prism's constructor.
firedraco, could you perhaps show me an example? either yours or a point in the right direction. I can understand what you are saying I just don't know how to get there. My Prism() seems to call the correct default constructor of Triangle(). I dont know why Prism(parameters) does not call Triangle(parameters) instead of triangle()
although you seem to be calling parameterized constructor of Triangle() by writing parameterized constructor of Prism class, infact only default construct of Triangle class is calling in your program. So as firedraco stated, call parameterized constructor of base class Triangle() from the derived class Prism() explicitly using member initializer list as below:
1
2
3
Prism::Prism(double l, double w, double h):triangle(w,h){
    dblLength = l;
}

Now Triangle(parameters) will be called.
But your answer will be same as passing 0, 0,0 for height, length and width. To avoid this error you have to erase base class default constructor Triangle().
Topic archived. No new replies allowed.