Constructor

Pages: 12
When I am debugging this, the initial values of pi is no 3.14159 but some huge negative number as is the radius. Not sure what i am doing wrong here.

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
using namespace std;

Circle::Circle()
{
	radius = 0.0;
	pi = 3.14159;
}
Circle::Circle(double radius) 
{
	setRadius(radius);
}
void Circle::setRadius(double rad)
{
	radius = rad;
}
double Circle::getRadius()
{
	return radius;
}
double Circle::getArea()
{
	double area = pi * (radius*radius);
	return area;
}
double Circle::getDiameter()
{
	return radius + radius;
}
double Circle::getCircumference()
{
	return 2 * pi * radius;
}
// Demo program
int main()
{
   // To hold a radius
	
	
	double radius;
	cout << "Enter a radius" << endl;
   // Get the radius.
	cin >> radius;

   
   // Create a Circle object with the 
   // specified radius.
	Circle circularObject(radius);
	
	
   // Display the circle's data.
   cout << "Radius: " << circularObject.getRadius() <<endl;
   cout << "Area : " << circularObject.getArea() << endl;
   cout << "Diameter: " << circularObject.getDiameter()  <<endl;
   cout << "Circumference: " << circularObject.getCircumference() << endl;
   return 0;


#ifndef CIRCLE_H
#define CIRCLE_H
class Circle
{
private:
	// To hold a value for pi
	double pi;
	// To hold the radius
	double radius;


public:
	// The default constructor sets
	// radius to 0.0 and pi to 3.14159.
	Circle();
	Circle(double radius);
 
   // The overloaded constructor accepts
   // the radius as an arguemnt.
      
   // Mutator function for the radius
	void setRadius(double rad);
   // Accessor function for the radius
	double getRadius();

      
   // The getArea function returns the
   // circle's area.
	double getArea();


   // The getDiameter function returns the
   // circle's diameter.
	double getDiameter();


   // The getCircumference function returns
   // the circle's circumference.
	double getCircumference();
};
#endif

Are you sure your code compiles?

You did not close your function main() with a closing-brace (})
1
2
3
4
Circle::Circle(double radius) 
{
	setRadius(radius);
}


Should be :
1
2
3
4
5
Circle::Circle(double radius) 
{
	setRadius(radius);
	pi = 3.14159;
}
I have an instruction that says the default constructor sets the radius to 0.0 and pi to 3.14159, if i have to set pi in the overlaoded constructor then what is the point of the dafault.
yea it compiles. must have left taht off
Last edited on
I can't get what you mean.
there is two constructors, one that takes an argument and one that dosent
the one that dosent is the default. it should set the two values in the definition to what they should be. However, these values are not what they should be when the program runs, but some crazy numbers instead. c c c
So what is wrong with the result??

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
#include <iostream>
using namespace std;

class Circle
{
private:
	// To hold a value for pi
	double pi;
	// To hold the radius
	double radius;

public:
	// The default constructor sets
	// radius to 0.0 and pi to 3.14159.
	Circle();
	Circle(double radius);
 
   // The overloaded constructor accepts
   // the radius as an arguemnt.
      
   // Mutator function for the radius
	void setRadius(double rad);
   // Accessor function for the radius
	double getRadius();
      
   // The getArea function returns the
   // circle's area.
	double getArea();

   // The getDiameter function returns the
   // circle's diameter.
	double getDiameter();

   // The getCircumference function returns
   // the circle's circumference.
	double getCircumference();
};

Circle::Circle() : pi(3.14159)
{
	radius = 0.0;
}
Circle::Circle(double radius) : pi(3.14159)
{
	setRadius(radius);
}
void Circle::setRadius(double rad)
{
	radius = rad;
}
double Circle::getRadius()
{
	return radius;
}
double Circle::getArea()
{
	double area = pi * (radius*radius);
	return area;
}
double Circle::getDiameter()
{
	return radius + radius;
}
double Circle::getCircumference()
{
	return 2 * pi * radius;
}

// Demo program
int main()
{
   // To hold a radius
	
	
	double radius;
	cout << "Enter a radius : ";
   // Get the radius.
	cin >> radius;

   
   // Create a Circle object with the 
   // specified radius.
	Circle circularObject(radius);
	
	
   // Display the circle's data.
   cout << "Radius: " << circularObject.getRadius() <<endl;
   cout << "Area : " << circularObject.getArea() << endl;
   cout << "Diameter: " << circularObject.getDiameter()  <<endl;
   cout << "Circumference: " << circularObject.getCircumference() << endl;
   return 0;
}


Enter a radius : 25
Radius: 25
Area : 1963.49
Diameter: 50
Circumference: 157.08
Ive never seen a value in that spot, where you have pi...hmm
What exactly is your problem?
Have you ran this through the debugger? if i initialize the default constructor the values are not what I want them to be.
Why debugger?
The code is not supposed to produce any bugs.
to check the values in the first constructor....
Why? Do you even care if the final output is correct or not?
yes, however i would like to know why cant i initialize these mother fuckin variables :)
anyone? or am i just losing it
It seems you do not know about member initializer list in C++.
http://en.cppreference.com/w/cpp/language/initializer_list
The default constructor isn't called automatically.
Even when I create a Circle circularObject; and then create the overloaded constructor, i cnat compile
Last edited on
Did you modify something so your code could not even compile now?
Or you can post your compiler error logs here so that we can help.
No. I have returned to this tonight. Gonna give it another try
Pages: 12