Can somebody help me with my code please??? Thanks


Hi, I wrote the code for the Classes but can I have some guidance to do the following, please?

Write a program that defines a vector of shared pointers to the base class.

Then program puts the following objects into vector:

• Point with coordinates (1.0, 1.0)
• Circle at (2.0, 2.0) with radius 2.0
• Square at (5.0, 5.0) with width and height of 2.0
• Square whose data is asked from user before adding to the vector
• Circle whose data is asked from user before adding to the vector
• Point whose data is asked from user before adding to the vector

When vector has been filled program does the following:

1. Prints the object data
2. Sorts the vector by area
3. Prints the object data


MY CODE:


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
#pragma once
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <memory>

using namespace std;

bool CompareArea(const Point* lhs, const Point* rhs)
{
    return lhs->area() < rhs->area();
}

class Point {

public:
    Point(double xcoord = 0.0, double ycoord = 0.0);

    virtual ~Point() = default;
    virtual void input(const char* prompt);
    virtual void output() const;
    virtual double area() const;
    void move(double deltax, double deltay);

private:
    double x;
    double y;
};


class Circle : public Point {
public:
    Circle(double xcoord = 0.0, double ycoord = 0.0, float r0 = 0.0) : Point(xcoord, ycoord) { radius = r0; }

    virtual ~Circle() = default;
    void input(const char* prompt);
    void output() const;
    double area() const;

private:
    float radius;
};

class Square : public Point {
public:
    Square(double xcoord = 0.0, double ycoord = 0.0, float w = 0.0, float h = 0.0) : Point(xcoord, ycoord) { width = w, height = h; }

    virtual ~Square() = default;
    void input(const char* prompt);
    void output() const;
    double area() const;

private:
    float width;
    float height;
};


int main()
{
    vector<Point*>     shapes;
    Point               pt;
    Circle              cir;
    Square              sq;

    shapes.push_back(new Point(1, 1));
    shapes.push_back(new Circle(2, 2, 2));
    shapes.push_back(new Square(5, 5, 2, 2));
    sq.input("Enter square coordinates and height, width");
    shapes.push_back(&sq);
    cir.input("Enter circle coordinates and radius");
    shapes.push_back(&cir);
    pt.input("Enter point coordinates");
    shapes.push_back(&pt);
    for (auto p : shapes)
        p->output();
    sort(shapes.begin(), shapes.end(), CompareArea);
    for (auto p : shapes)
        p->output();
    return 0;
}


Last edited on
Your derived destructors are incorrect.
Line 28 should be: virtual ~Circle() = default;
Line 43 should be: virtual ~Square() = default;

Line 41: You should have only a single dimension for a square.
By allowing h and w, you allow them to be different resulting in a rectangle.

[edit] Updated program to include sort and user input.
Updated shapes to match instructions.
Implementation of your class functions is up to you.


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
#include <vector>
#include <algorithm>
bool CompareArea (const Point * lhs, const Point * rhs)
{   return lhs->area() < rhs->area();
}

int main ()
{   vector<Point *>     shapes;
    Point               pt;
    Circle              cir;
    Square              sq;

    shapes.push_back (new Point(1,1));
    shapes.push_back (new Circle (2,2,2));
    shapes.push_back (new Square(5,5,2,2));
    sq.input("Enter square coordinates and height, width");
    shapes.push_back(&sq); 
    cir.input("Enter circle coordinates and radius");
    shapes.push_back(&cir);
    pt.input ("Enter point coordinates");
    shapes.push_back (&pt);    
    for (auto p : shapes)
        p->output ();
    sort (shapes.begin(), shapes.end(), CompareArea);
    for (auto p : shapes)
        p->output();
    return 0;
}


PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
@AbstractionAnon: Thanks a lot! I updated the code with your suggestions. Could you read it one last time please? I appreciate
 
Circle(double xcoord = 0.0, double ycoord = 0.0, float r0 = 0.0) : Point(xcoord, ycoord) { radius = r0; }


or only using an initializer list:

 
Circle(double xcoord = 0.0, double ycoord = 0.0, float r0 = 0.0) : Point(xcoord, ycoord), radius(r0) {}


and the same for the other constructor.

Why float and not double for radius?
Topic archived. No new replies allowed.