OOP class related questions

Hello,

I have a few, simple questions. Down is a code for a programm which lets user input three lines, all of them allowing to unput three variables a,b,c, given with ax +by = c.
x and y is unknown.
Programm determines if those three lines make a triangle and prints its area. If not, it prints which lines are collinear. For solution it uses determinants.

It's not my code, I'm learning to understand it. My questions are regarding OOP.

1. Why does this part of code consists of these exact values?
1
2
3
line _a = {0,1,2}; //In real code it's line 32
line _b = {1,0,2}; //line 33
line _c = {1,1,2}; //line 34 

If I switch the values to any kind, output (results) doesn't change. Are they meaningless and are there just to show programm that class line consists of three double variables?

2. Can I bring in the entire class with it's variables into function? For example, in this code struct point suddenly appears in point solve function. Line 101.

I'm new to OOP and I'm presuming that the same code could be written without object-oriented approach, but then it would be A LOT longer and more complex so I'm trying to logically understand how it works. I know the basics when You have class with functions in its body and then references to those functions, and given parameters but this here is a little different as functions are independent and here are no parameters.

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <math.h>
using namespace std;

struct point
{
    double x, y;
};

struct line
{
    double a,b,c;
};

point solve(line v1, line v2);

double distance(point A, point B);

double area(double a, double b, double c);

double d(line v1, line v2);

double d(line v1, line v2, line v3);

int main()
{
    int ok;

    do
    {

        line _a = {0,1,2};
        line _b = {1,0,2};
        line _c = {1,1,2};

        cout<<"line a:\n";
        cin>>_a.a>>_a.b>>_a.c;

        cout<<"line b:\n";
        cin>>_b.a>>_b.b>>_b.c;

        cout<<"line c:\n";
        cin>>_c.a>>_c.b>>_c.c;

        //checks if its triangle
        if (d(_a,_b) * d(_a,_c) * d(_b,_c) * d(_a,_b,_c) !=0)
        {
            //determines all the equation points
            point A = solve(_a, _b);
            cout<<"point A("<<A.x<<", "<<A.y<<")\n";
            point B = solve(_a, _c);
            cout<<"point B("<<B.x<<", "<<B.y<<")\n";
            point C = solve(_b, _c);
            cout<<"point C("<<C.x<<", "<<C.y<<")\n";

            double a = distance(A,B);
            cout<<"from A to B is "<<a<<" units\n";
            double b = distance(B,C);
            cout<<"from B to C is "<<b<<" units\n";
            double c = distance(C,A);
            cout<<"from C to A is "<<c<<" units\n";

            double tArea = area(a, b, c);

            cout<<"Area of triangle: "<<tArea<<endl;
        }
        else
        {
            cout<<"lines does not make triangle\n";
            if (d(_a,_b)==0) cout<<"Collinear: A and B\n";
            if (d(_a,_c)==0) cout<<"Collinear: A and C\n";
            if (d(_b,_c)==0) cout<<"Collinear: B and C\n";
        }

        cout << "To continue (1) or not (0)?" << endl;
        cin >> ok;
    }
    while (ok == 1);

    return 0;
}

double d(line v1, line v2)
{
    return v1.a * v2.b - v1.b * v2.a;
}


double d(line v1, line v2, line v3)
{
    double a1 = v1.a * ((v2.b * v3.c)-(v2.c * v3.b));
    double a2 = -1 * v1.b * ((v2.a * v3.c)-(v2.c * v3.a));
    double a3 = v1.c * ((v2.a * v3.b)-(v2.b * v3.a));

    return a1+a2+a3;
}


point solve(line v1, line v2)
{
    point a;

    a.x = 0;
    a.y = 0;

    double vd = d(v1,v2);
    double d2 = v1.c * v2.b - v1.b * v2.c;
    double d3 = v1.a * v2.c -  v1.c * v2.a;

    if (vd!=0)
    {
        a.x = d2 / vd;
        a.y = d3 / vd;
    }

    return a;
}

double distance(point A, point B)
{
    double dx = A.x - B.x;
    double dy = A.y - B.y;

    return sqrt(dx*dx + dy*dy);
}

double area(double a, double b, double c)
{
    double p = (a + b +c)/2;

    return sqrt(p * (p-a) * (p-b) * (p-c));
}
Last edited on
1. Those are initial values for those objects. The actual values are overwritten with user input, making the initialisation irrelevant.

2. Yes. An instance of a class is can object. These objects can be passed around as parameters to functions, just like built in types (int, double, and so on).
Thank You.

And if I define class without struct data type, like this:

1
2
3
4
5
class point
{
public:
         double x;
         double y;

I mean, if I have a class with only variables which are public, class becomes like a cluster of variables which then can be attached to any specific class unrelated function in the project. I understood it right?
Topic archived. No new replies allowed.