How can I calculate variables from base/derived class functions?

Hello,

I am trying to work out coordinates in a base class using a member function. Whilst I know I have the correct formula, I keep getting the following output if I comment out lines 78 relating to calls for the circle class and member functions

[output]
The coordinates are (10,10)
The coordinates are (1,1)
The distance between 2 points is 0
[\output]th

Furthermore, when I start to make the circle calls after I remove the omments I receive the following errors

"
C:\Users\shish\Desktop\C++ programmes\Exercise11.6_5\main.cpp||In function 'int main()':|
C:\Users\shish\Desktop\C++ programmes\Exercise11.6_5\main.cpp|82|error: assignment of function 'Circle Circle1()'|
C:\Users\shish\Desktop\C++ programmes\Exercise11.6_5\main.cpp|82|error: cannot convert 'Point' to 'Circle()' in assignment|
C:\Users\shish\Desktop\C++ programmes\Exercise11.6_5\main.cpp|82|error: cannot convert 'Point' to 'Circle()' in assignment|"



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
#include <iostream>
#include <stdlib.h>
#include <cmath>

using namespace std;

const double PI = 2.0 * asin(1.0);

class Point
{
    public:
        double x,y;
        Point (double = 1.0, double = 1.0);
        void show_point();
        double distance_func(Point &, Point &);
};

Point::Point(double X, double Y)
{
    x = X;
    y = Y;
}

void Point::show_point()
{
    cout << "The coordinates are (" << x << "," << y << ")" << endl;
}

double Point::distance_func(Point &A, Point &B)
{
    double ydiffsquared = 0.0;
    double xdiffsquared = 0.0;
    double calc_dist = 0.0;

    ydiffsquared = double (pow((B.y - A.y),2));
    xdiffsquared = double (pow((B.x - A.x),2));

    calc_dist = double (sqrt((xdiffsquared - ydiffsquared)));

    return (calc_dist);
}

class Circle : public Point
{
    protected:
        double radius;
    public:
        Circle(Point(double x, double y), double r = 1.0) : Point(x,y), radius(r) {}
        double Area();
        void show_circle_coords();
};

double Circle::Area()
{
    double calc_area = 0.0;

    calc_area = M_PI * pow(radius,2);

    cout << "The area of the circle is " << calc_area << endl;

    return 0;
}

void Circle::show_circle_coords()
{
    cout << "The center of the circle is " << "(" << Point().x << "," << Point().y << ")"<< endl;
    cout << "The radius of the circle is " << radius << endl;
}

int main()
{
    Point Point1(10.0,10.0);
    Point Point2 (1.0,1.0);

    Point1.show_point();
    Point2.show_point();

    cout << "The distance between 2 points is " << Point().distance_func(Point1, Point2) << endl;

    Circle Circle1();
    Circle1 = Point1;
    Circle1.Area();

    return 0;
}
Point() means "create an object of type point".

So this:
cout << "The center of the circle is " << "(" << Point().x << "," << Point().y << ")"<< endl;
makes no sense. You're trying to create a Point object right there, and get its x variable, and create a Point object right there, and get its y variable.

I suspect you meant:
cout << "The center of the circle is " << "(" << x << "," << y << ")"<< endl;

Circle class has a member variable x, and member variable y. You can just use them in any circle object.

Circle1 = Point1; If you want to do this, you're going to have top write code explaining to the compiler what you want it to do when you try to assign a Point to a Circle.


Circle Circle1();
This says "there exists a function, named Circle1, that takes no parameters and returns an object of type Circle". Perhaps you meant to create a Circle object using the Circle object's constructor, Circle(Point(double x, double y), double r = 1.0) ? That is the only way to create a Circle, since it has only that one constuctor. Although that constructor doesn't make much sense either... what are you trying to do with that constructor?




Last edited on
> Whilst I know I have the correct formula
no, you don't.

you've got
calc_dist = sqrt(xdiffsquared - ydiffsquared);
it should be +
Hello,

Thanks very much! I have since amended it all and come up with the following. I believe I will have to create an = operator in the circle class in order to assign a point object to a circle object.

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
#include <iostream>
#include <stdlib.h>
#include <cmath>

using namespace std;

const double PI = 2.0 * asin(1.0);

class Point
{
    protected:
        //Point(double x, double y) : Circle(radius) {}

    public:
        double x,y;
        Point (double = 1.0, double = 1.0);
        void show_point();
        double distance_func(Point &, Point &);
};

Point::Point(double X, double Y)
{
    x = X;
    y = Y;
}

void Point::show_point()
{
    cout << "The coordinates are (" << x << "," << y << ")" << endl;
}

double Point::distance_func(Point &A, Point &B)
{
    double ydiffsquared = 0.0;
    double xdiffsquared = 0.0;
    double calc_dist = 0.0;

    ydiffsquared = double (pow((B.y - A.y),2));
    xdiffsquared = double (pow((B.x - A.x),2));

    cout    << "sqrt ( (" << B.x << " - " << A.x << ")^2 - "
            << " (" << B.y << " - " << A.y <<")^2" << endl;

    calc_dist = double (sqrt((xdiffsquared + ydiffsquared)));

    return (calc_dist);
}

class Circle : public Point
{
    protected:
        double radius;
    public:
        Circle (double = 1.0);
        double Area();
        void show_circle_coords();
        
};

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

double Circle::Area()
{
    double calc_area = 0.0;

    calc_area = M_PI * pow(radius,2);

    cout << "The area of the circle is " << calc_area << endl;

    return 0;
}

void Circle::show_circle_coords()
{
    cout << "The center of the circle is " << "(" << x << "," << y << ")"<< endl;
    cout << "The radius of the circle is " << radius << endl;
}

int main()
{
    Point Point1(10.0,10.0);
    Point Point2 (1.0,1.0);

    Point1.show_point();
    Point2.show_point();

    cout << "The distance between 2 points is " << Point().distance_func(Point1, Point2) << endl;

    Circle Circle1(10);
    Circle1.show_circle_coords();
    Circle1.Area();


    return 0;
}
Hello,

I think I got it by using an operator function in the circle class by overloading the = operator. So here is my final 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
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
#include <iostream>
#include <stdlib.h>
#include <cmath>

using namespace std;

const double PI = 2.0 * asin(1.0);

class Point
{
    protected:
        //Point(double x, double y) : Circle(radius) {}

    public:
        double x,y;
        Point (double = 1.0, double = 1.0);
        void show_point();
        double distance_func(Point &, Point &);
};

Point::Point(double X, double Y)
{
    x = X;
    y = Y;
}

void Point::show_point()
{
    cout << "The coordinates are (" << x << "," << y << ")" << endl;
}

double Point::distance_func(Point &A, Point &B)
{
    double ydiffsquared = 0.0;
    double xdiffsquared = 0.0;
    double calc_dist = 0.0;

    ydiffsquared = double (pow((B.y - A.y),2));
    xdiffsquared = double (pow((B.x - A.x),2));

    cout    << "sqrt ( (" << B.x << " - " << A.x << ")^2 - "
            << " (" << B.y << " - " << A.y <<")^2" << endl;

    calc_dist = double (sqrt((xdiffsquared + ydiffsquared)));

    return (calc_dist);
}

class Circle : public Point
{
    protected:
        double radius;
    public:
        Circle (double = 1.0);
        double Area();
        void show_circle_coords();
        operator=(Point &);

};

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

double Circle::Area()
{
    double calc_area = 0.0;

    calc_area = M_PI * pow(radius,2);

    cout << "The area of the circle is " << calc_area << endl;

    return 0;
}

void Circle::show_circle_coords()
{
    cout << "The center of the circle is " << "(" << x << "," << y << ")"<< endl;
    cout << "The radius of the circle is " << radius << endl;
}

Circle::operator=(Point &A)
{
    x = A.x;
    y = A.y;
}

int main()
{
    Point Point1(10.0,10.0);
    Point Point2 (1.0,1.0);

    Point1.show_point();
    Point2.show_point();

    cout << "The distance between 2 points is " << Point().distance_func(Point1, Point2) << endl;

    Circle Circle1(10);
    Circle1.show_circle_coords();
    Circle1.Area();

    Circle1 = Point2;

    Circle1.show_circle_coords();

    Circle1 = Point1;

    Circle1.show_circle_coords();

    return 0;
}
Topic archived. No new replies allowed.