task in c++

May 29, 2010 at 12:52pm
Define a class Circle that stores the center and radius of a circle, by keeping the numbers in a dynamically allocated array of doubles.
Supply the "big three" memory management functions. Use this class to demonstrate
(a) the difference between initialization
Circle s;
Circle t = s;
and assignment operation
Circle s;
Circle t;
s = t;
(b) the fact that all constructed objects are automatically destroyed
© the fact that the copy constructor is invoked if an object is passed by value to a function
(d) the fact that the copy constructor is not invoked when a parameter is passed by reference
(e) the fact that the copy constructor is used to copy a return value to the caller.
Supply a member functions that calculate the perimeter and the area of the circle. Overload the ++ operator (prefix and postfix forms) that add 1 to the radius of the circle and the > operator (with boolean return value) to compare two circles with respect to their areas. Overload the stream operators << and >>. Demonstrate all these functions and operators.
May 29, 2010 at 12:56pm
Interesting problem. Good luck with it.

If you have any problems with the code you have written, then perhaps we could help you with that if you post it here.
May 31, 2010 at 6:51am
this is Circle Implementation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "Shape.h"
#include "Circle.h"
#include <iostream.h>

// constructor
Circle::Circle(int newx, int newy, int newradius): Shape(newx, newy) {
   setRadius(newradius);
}

// accessors for the radius
int Circle::getRadius() { return radius; }
void Circle::setRadius(int newradius) { radius = newradius; }

// draw the circle
void Circle::draw() {
  cout << "Drawing a Circle at:(" << getX() << "," << getY() <<
      "), radius " << getRadius() << endl;
}


this is Circle Interface
1
2
3
4
5
6
7
8
9
10
11
class Circle: public Shape {

public:
   Circle(int newx, int newy, int newradius);
   int getRadius();
   void setRadius(int newradius);
   void draw();

private:
   int radius;
};



this is Shape Class (Shape.h)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Shape {

public:
   Shape(int newx, int newy);
   int getX();
   int getY();
   void setX(int newx);
   void setY(int newy);
   void moveTo(int newx, int newy);
   void rMoveTo(int deltax, int deltay);
   virtual void draw();

private:
   int x;
   int y;
};



and this is the final Shape Implementation (Shape.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
#include "Shape.h"

// constructor
Shape::Shape(int newx, int newy) {
   moveTo(newx, newy);
}

// accessors for x & y
int Shape::getX() { return x; }
int Shape::getY() { return y; }
void Shape::setX(int newx) { x = newx; }
void Shape::setY(int newy) { y = newy; }

// move the shape position
void Shape::moveTo(int newx, int newy) {
   setX(newx);
   setY(newy);
}
void Shape::rMoveTo(int deltax, int deltay) {
   moveTo(getX() + deltax, getY() + deltay);
}

// abstract draw method
void Shape::draw() {
}


this is what I have done but I can't get any idea how to do the other things after the definition of circle class :?
Last edited on May 31, 2010 at 6:52am
May 31, 2010 at 8:12am
You seem to be doing quite well. Did you post the whole question? Its just that the class Shape is not mentioned in the question, but you included it here.

Question wrote:

Define a class Circle that stores the center and radius of a circle, by keeping the numbers in a dynamically allocated array of doubles.


This is telling you specifically how to store the information in your circle. You have not done this.

In your example you are storing the centre of the circle in its parent class Shape. But that is not what the question asked you to do.

Also you are storing the radius as a member variable, not as part of the dynamic array.

So first of all this is what a "dynamically allocated array of doubles" looks like:

 
double* data = new double[3]; // [0] = x, [1] = y, [2] = r 


So instead of using your base class Shape to store the x & y values I think they should go into your dynamic array. I don't think the question even asks for a Shape class (unless there is some part you didn't post).

Question wrote:

Supply the "big three" memory management functions. Use this class to demonstrate
(a) the difference between initialization
Circle s;
Circle t = s;


This is talking about what happens when the Circle is 'constructed'. This happens when you do these two things:

1
2
Circle s; // default constructor
Circle t = s; // Copy constructor 


So you need to write TWO different constructors for your Circle. One with no parameters and another one taking another Circle as its parameter:

1
2
3
4
5
6
7
8
9
10
Circle::Circle() // default constructor
{
	// allocate your dynamic array here
}

Circle::Circle(const Circle& c) // Copy constructor
{
	// allocate your dynamic array here
	// fill the array with the same values with the ones in c
}


That should get you half way through question a.
May 31, 2010 at 8:34am
did you mean something like that :?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Circle: {

public:
   Circle(int newx, int newy, int newradius);
   void setRadius(int newradius);
   void draw();
   double* data = new double[3]
   void setX(int newx);
   void setY(int newy);
   void moveTo(int newx, int newy);
   void rMoveTo(int deltax, int deltay);
   virtual void draw();

private:
   int radius;
   int x;
   int y;
};
May 31, 2010 at 8:41am
Nearly. But you are still using member variables in addition to your dynamic array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Circle: {

public:
   Circle(int newx, int newy, int newradius);
   void setRadius(int newradius);
   void draw();
   double* data = new double[3] // This should be in the private section
   void setX(int newx);
   void setY(int newy);
   void moveTo(int newx, int newy);
   void rMoveTo(int deltax, int deltay);
   virtual void draw();

private:
   int radius;  // do not use this, use the dynamic array instead
   int x; // do not use this, use the dynamic array instead
   int y; // do not use this, use the dynamic array instead
};


Also you have added a lot of functions that are not in the original question, like setX(), setY(),moveTo() etc...
Jun 1, 2010 at 9:32am
1
2
3
4
5
6
7
8
9
10
11
class Circle: {

public:
   Circle(int newx, int newy, int newradius);
   void setRadius(int newradius);
   void draw();
   virtual void draw();

private:
  double* data = new double[3] 
};


Is this ok? this thing is very difficult and I am totally stuck
Jun 1, 2010 at 9:43am
To be honest the question is not too difficult if you have been taught all the elements required.

You, though, seem to be an absolute beginner to C++.

The question is clearly beyond an absolute beginner. I recommend you start learning C++ from the beginning and work your way up to the concepts this question requires.
Jun 1, 2010 at 2:22pm
I think we cannot allocate the memory to the data type in the deceleration of the class.
We can allocate the memory in the definition of the class.
please correct me if i am wrong.
Jun 1, 2010 at 5:04pm
closed account (S6k9GNh0)
The question is unclear. All equations with a circle are based on constants, its center, and its radius. In my opinion, it's stupid to hold the radius and center in an array of doubles, you would absolutely never do that and often people will doubt your code if they see something related to that.

1) It never said that he couldn't use extra functions.
2) double* data = new double[3] inside of a class declaration shouldn't work. You have to assign the variable at class construction. Even if it does, it's probably more formal to use an initialization list instead.
3) What is the point of giving instructions to do something you probably shouldn't get in the habit of? I see this all the time.

"To teach you the right thing, we're going to give you an unrealistic assignment that you'll never come across and tell you to complete the assignment in a horrible way that will make other programmers want to shank you with a USB stick. It's better for the long run."
Last edited on Jun 1, 2010 at 5:07pm
Jun 2, 2010 at 6:48am
I know that I am a beginner but for the first and last year it and learn it right on course work crashed and I can not make it if someone could help me :S I would be very grateful that my final deadline is 10,06,2010.
thanks in advance
Jun 2, 2010 at 10:43am
theAnonymous wrote:
I know that I am a beginner but for the first and last year it and learn it right on course work crashed and I can not make it if someone could help me :S I would be very grateful that my final deadline is 10,06,2010.
thanks in advance


I didn't understand everything you said here. But I think you are saying that something went wrong with your first year studies?

If that is true then you obviously have much catching up to do. But the thing is there is no point in one of us simply giving you the answer to give to your tutor.

If you are so far behind the others then, surely, your tutor needs to be aware of it? Then they can help you. But I don't see it doing anyone any good if your tutor thinks your are more advanced than you really are.

If I have misunderstood your situation, then I apologise. Your statement was not very clear.
Last edited on Jun 2, 2010 at 10:44am
Jun 2, 2010 at 11:31am
sorry it's my mistake I mean that we study c++ only the first year and it doesn't have any connection with our spicialty we are forced to learn it for nothing and this last task is very dificult for every one of our class and for this I searched help here :)
Jun 2, 2010 at 12:43pm
Well, with all due respect, I don't think any learning is for nothing. If you have no genuine interest in C++ then I for one am unlikely to spend any more of my time helping you. I come here out of gratitude to the people who helped me to learn, we don't get paid for this.

So like I said I'm here to help people who want to learn. I am not here to pass someone else's coursework for them because they don't want to learn.

If you want to be able to answer the question you have been given then you are going to have to start learning C++ from the beginning. You need to get a good book and set aside enough time each week to progress. If you are going to do that then I am more than happy to help you.

And I would recommend explaining to your tutor how difficult the question is. They should be able to help you get up to speed. But you need to take a pro-active interest in this subject even if you don't feel its your vocation.

Programming is hard. Learning to program will give you invaluable skills in logic, deduction, reasoning, patience, mental acuity, mental stamina and general all round accuracy. To name but a few.

You can not fudge computer programming.

And having an appreciation of how computers actually work can't be a bad thing whatever subject you are learning in this day and age.
Topic archived. No new replies allowed.