Help! Constructs Not working?

Hello, I want my default constructor - CylinderClass() to reset the height and diameter of the cylinder to 1 every time the user chooses 6 and my constructor with parameters - CylinderClass(double x, double y) to reset the height and diameter to 5 and 2 respectively. Right now, when I first run the program (before setting a new height or diameter) my default constructor works correctly. However, after a new height or diameter has been set neither constructor correctly resets them. It just gives me back the old height and diameter. Any idea what I'm doing wrong? Thanks a lot for the help!
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cmath>
using namespace std;

#define PI		3.14159

class CylinderClass
{
public:

CylinderClass();		//default constructor			
CylinderClass(double x, double y);		//parameter constructor

void	set_height(int height);
double	get_height();

void	set_diameter(int diameter);
double	get_diameter();

double	get_volume();
double 	get_extended_volume();

void 	reset_original(int x, int y);
void	reset_specific(int x, int y);

void set_extended_height(int height);
	
private:
    struct	a_cylinder
    {
	double  height;
	double  diameter;
    } the_cylinder, new_cylinder;
};

int choose_function();

int main()
{
CylinderClass	TheCylinder, NewCylinder;
int		task, height, diameter, new_height;

    task = choose_function();
    while ( task != 10 )
    {
	switch (task)
	{
	    case 1:	cout << " Enter height: ";
				cin  >>  height;
				TheCylinder.set_height(height);
				cout << "\n";
			break;
	    case 2:	cout << "The current height is " << TheCylinder.get_height(); 
				cout << "\n";
			break;
	    case 3:	cout << "Enter diameter: ";
				cin  >> diameter;
				TheCylinder.set_diameter(diameter);
				cout << "\n";
			break;
	    case 4:	cout << "The current diameter is " << TheCylinder.get_diameter();
				cout <<"\n";
			break;
	    case 5:	cout << "Volume is " << TheCylinder.get_volume();
				cout <<"\n";
			break;
		case 6: CylinderClass();
		case 7: CylinderClass(5.0, 2.0);
	    default:	cout << "please select from the menu choices\n\n";
	}
	task = choose_function();
    }
    cout << "thank you\n\n";
    return(0);
}


int choose_function()
{
int selection;

    cout << "\n\nplease select from the following:\n\t1) Set height\n\t2) Get current height\n"
	 << "\t3) Set diameter\n\t4) Get current diameter\n\t5) Get volume "
	 << "\n\t6) Reset to original "
	 << "\n\t7) Reset to specific dimensions "
	 << "\n\t8) Get an extended Cylinder "
	 << "\n\n\t10) exit this program\n\nplease enter your selection: ";
    cin  >> selection;
	
    return(selection);
}

CylinderClass::CylinderClass()
{
    the_cylinder.height  = 1;
    the_cylinder.diameter = 1;
}

CylinderClass:: CylinderClass(double x, double y)
{
    the_cylinder.height  = x;
    the_cylinder.diameter = y;   
}

void CylinderClass::set_height(int height)
{
    the_cylinder.height = height;
}

void CylinderClass::set_extended_height(int height)
{
    new_cylinder.height = height;
}

double CylinderClass::get_height()
{
    return(the_cylinder.height);
}

void CylinderClass::set_diameter(int diameter)
{
    the_cylinder.diameter = diameter;
}

double CylinderClass::get_diameter()
{
    return(the_cylinder.diameter);
}

double CylinderClass::get_volume()
{
	double volume;
	double radius;
	double radius_squared;
	
	radius = (the_cylinder.diameter / 2);
	radius_squared = pow(radius,2);
	
	volume = PI * radius_squared * the_cylinder.height;
	
	return (volume);
}

double CylinderClass::get_extended_volume()
{
	double extended_volume;
	double radius;
	double radius_squared;
	
	radius = (the_cylinder.diameter / 2);
	radius_squared = pow(radius,2);
	
	extended_volume = PI * radius_squared * new_cylinder.height;
	
	return (extended_volume);
}
you can't use a constructor to make changes to a current class object

you would need a function to set them...

double CylinderClass::set_something(take some variables)
Line 69: Not sure if that's really kosher C++, that.
Line 70: Alright. So you created a new CylinderClass object. ...what are you doing with it?

Constructors are for creating new objects, not changing all the objects that already exist. Maybe you'd like to look into static members and functions for that?

http://www.learncpp.com/cpp-tutorial/811-static-member-variables/

-Albatross
Last edited on
this is a project I'm working on and I'm supposed to use constructors as I previously mentioned. Line 70 is my constructor with parameter call
My point is that you initialized a new CylinderClass object and nothing is being done with it. For example, no assignments...

-Albatross
Topic archived. No new replies allowed.