3 constructors and destructor error

Pages: 12
I write 3 constructor class and splits into c.h c.cpp but I have got extra definition in my cpp program. My destructor show error
My program *.h and *.cpp are

c2.h
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
#ifndef CIRCLE_H
#define CIRCLE_H
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <iomanip>
//Circle class declaration
class circle
{
   private:
         double x;
         double y;
   
  public:
         double r;

         circle()
         { x=0; y=0;}

         circle(double radius)
         { r = radius; }

         circle(double x, double y , double radius)
         { x=0; y=0; r = radius; }

         ~circle()
         { delete [] circle; }


cp.cpp
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
#include "c2.h"
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <cmath>

using namespace std;


//*************************************************
int main()
//*************************************************

{
 
  double xc, xpoint, radius;
  double yc, ypoint;

  // get x & y axis
   cout << "This program will calculate whether the specific point is inside the circle ";

   cout << "\nWhat is the radius of the circle ";
   cin >> radius;

   cout << "\nWhat is x - axis of the interest point? ";
   cin >> xpoint;
//40
   cout << "\nWhat is y - axis of the interest point? ";
   cin >> ypoint;
   cout << "\n";
   circle c(xpoint, ypoint);


Line 44 which is the last line got error that I have declare extra definition 3 constructors in c.h file.
Pls help me how to solve this problem.

Thank you.
Last edited on
Your question doesn't make any sense. You haven't shown us line 44 of your code, so how can we tell what might be wrong with it?

You're trying to delete the circle object within its own destructor. That makes no sense. The destructor is what gets called automatically when the object it deleted, so what you've written would lead to infinite recursion.

Last edited on
line 44 is circle c(xpoint, ypoint);

Thank you for explain me for destructor which I was confused. Now I know it need to destruct object which I;ve created class object .

Thank you MikeyBoy and pls hints me for 3 constructors.
Thank you very much
Your class Circle has no constrcutor with two parameters. So the compiler issues the error in your line 44.
Also there is no any sense in the constructor

1
2
         circle(double x, double y , double radius)
         { x=0; y=0; r = radius; }
Last edited on
closed account (3qX21hU5)
Another tip would be to use initializer lists which give you more functionality for your constructor, like for example you can initialize const variables with a initializer list but not with your constructors.

They would look like this.

1
2
3
4
5
circle() : x(0), y(0) {}

circle(double radius) : x(0), y(0), r(radius) {}

circle(double X, double Y , double radius) : x(X), y(Y), r(radius) {}


Just a tip :)
Should I use as follows?
circle::circle(){ x=-0; y=0; }

circle::circle(double radius) { r= radius;}

circle::circle(double X, double Y , double radius) {x =0 ; y=0 ; r=radius; }

Please correct me. I am so much confused about constructors in one single class.

Thank you
Last edited on
Can you read? I already wrote your what is the problem and that the last constructor has no sense.
closed account (3qX21hU5)
If you are using the initializer lists you don't need to for simple things like what you have. Here I will go through one of them really quick so you know what everything does.

circle() : x(0), y(0) {}

basically it works exactly like the code you have. Meaning x(0) assigns 0 to the member X when you use that constuctor, and y(0) assigns 0 to the member y.


For this one circle(double radius) : x(0), y(0), r(radius) {}

It is doing the exact same thing for x and y. But it is assigning whatever you pass as the argument radius to the member variable r. So if I called the constructor like this circle one(5); it would assign the number 5 to radius.

Also notice that we have the blank {} after each constructor that is telling the compiler we have already defined the constructor. So we wouldn't need these circle::circle(){ x=-0; y=0; }


Oh and another thing to note is you can do expressions in the list also like
circle(double radius) : x(0), y(0), r(radius * 2) {}


Set all the members to appropriate values in every constructor.
Write an additional two argument constructor (or use default values) if you find it handy.

And what do you think you are doing here?
1
2
~circle()
{ delete [] circle; }


Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class circle
{
   private:
         double x;
         double y;
   
  public:
         double r; // why is this alone public?

         circle()
         { x=0; y=0; radius = 1 ; }

         circle(double radius)
         { x = 0 ; y = 0 ; r = radius; }

         // give a default value for radius
         circle(double x, double y , double radius = 1 )
         { x=0; y=0; r = radius; }

         ~circle() {}
         // { delete [] circle; } // *** remove this
 };
Instead of

1
2
         circle(double x, double y , double radius = 1 )
         { x=0; y=0; r = radius; }


should be something as

circle(double x, double y , double radius = 1 ) : x( x ), y( y ), r( radius ) {}
> Instead of blah
> should be something as blah blah

Don't be ludicrous. Objects of type double are trivially constructible and trivially copyable.

Don't be ludicrous. Objects of type double are trivially constructible and trivially copyable.

It's still a good habit for somebody learning C++ to get into. "Ludicrous" is unnecessarily rude.
@JLBorges
> Instead of blah
> should be something as blah blah

Don't be ludicrous. Objects of type double are trivially constructible and trivially copyable.



I have understood nothing.
@booradley60
To vlad's point: http://www.parashift.com/c++-faq/init-lists.html


Could you say what you wanted to say pointing this reference?
closed account (3qX21hU5)
> Instead of blah
> should be something as blah blah

Don't be ludicrous. Objects of type double are trivially constructible and trivially copyable.


1
2
3
4
5
6
7
8
class MyClass
{
public:
    MyClass() : myDouble(0.5) {}
    // Or
    MyClass(double number) : myDouble(number) {}
    const double myDouble;
};


Since double's are trivial there must be a easy way to initialize the const double member without using init lists ;p.

You have to admit it is a good habit to get into from the beginning.
Last edited on
@Zereo


The problem is not in trivially constructibility and so on. The problem is that the constructor has no any sense because its first two parameters are not used.:)
And I pointed out this three times.:) This is the forth time.:)
> Since double's are trivial there must be a easy way to initialize the const double member ;p

An object of type const double is not trivially constructible.


> It's still a good habit for somebody learning C++ to get into.

That is a matter of opinion. IMNSHO, it is a good idea not to use member initializers for members that are trivially constructible and trivially copyable, and thereby evade the nasty 'order-of-initialization problem'.


> "Ludicrous" is unnecessarily rude.

Be that as it may, the "should be" in that post remains unquestionably ludicrous.
vlad from moscow wrote:
Could you say what you wanted to say pointing this reference?

I think the reference speaks for itself. You instructed the OP to change the structure of his code and this link provides some background and justification for your suggestion.
@booradley60
I think the reference speaks for itself. You instructed the OP to change the structure of his code and this link provides some background and justification for your suggestion.


Thanks.
Pages: 12