Problem with classes

what is a class?
Last edited on
What is your question?
write the methods of the Class Line:

bool vertical();
bool horizontal();
double slope();
bool parallel(Line r);
Point intersection(Line r);
That is... not a question.

You are being given lines in Standard Form, Ax + By = C.
I would suggest reading an article such as this: http://courses.wccnet.edu/~palay/precalc/22mt01.htm
It seems to explain the basics pretty well.

Your first two methods are determining whether or not a line is vertical or horizontal, respectively.
To tackle this, you first need to know what a vertical line looks like in standard form.

An example of a vertical line is x = 4. Notice that it doesn't depend on y at all.
An example of a horizontal line is y = 3. Notice that it doesn't depend on x at all.
From this, we can deduce that if a line doesn't depend on y, then B must be equal to 0:
1x + 0y = 4 --> 1x = 4
If a line doesn't depend on x, then A must be equal to 0:
0x + 1y = 3 --> 1y = 3

From this, we can conclude that if a line is vertical, B = 0, and if a line is horizontal, A = 0.

Do you know how to write this statement in code? I'll start it off for you.
1
2
3
4
5
6
7
bool Line::vertical()
{
    if ( /* B is equal to 0 */)
        return true;
    else
        return false;
}


For completeness, you would also check for the degenerate form where both A and B are equal to 0, which is not a valid line. But I wouldn't worry about that for now.

__________________

To get the slope of a line in standard form, Ax + By = C,
you need to tranform it into slope-intercept form, y = mx + b (note: lowercase b is not the same as B).
m is then the slope, and b is the y-intercept.

Ax + By = C
By = C - Ax
y = (C/B) - (A/B) x
y = -(A/B)x + (C/B)
The slope is -A/B.
Last edited on

The theory is very clear to me. My problem is just building the c ++ file
Could you complete all the methods exhaustively, please? Thank you so much.
You need to put in methods that implement vertical, horizontal, slope, parallel, and intersection.

I'll start you off with a bit more. Insert this code after the definition of your class, but before main().

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
bool Line::vertical()
{
    return false;
}

bool Line::horizontal()
{
    return false;
}

double Line::slope()
{
    return 0.0;
}

bool Line::parallel(Line r)
{
    return false;
}

Point Line::intersection(Line r)
{
    Point p;
    return p;
}


You also need to provide a definition for the constructor. Do you know how to write a constructor?
1
2
Line::Line(double A, double B, double C)
: a_param(A), b_param(B), c_param(C) {} 


Now you need to fill in the functions with actual logic.
e.g.
1
2
3
4
bool Line::vertical()
{
    return b_param == 0;
}
Last edited on
Could you complete all the methods exhaustively, please?

Programming is a lot like football: you have to practice. You're going to have to knuckle down and write some code yourself.
VERY USEFUL.......
keskiverto FYI the OP deleted their original post.
Ahh, that explains why the responses did not quite match the (current version of) OP:
what is a class?
The 10 cent answer is that a class is a user defined type, much like 'int' or 'double'.

The language provides many classes for you, such as std::string, or std::vector, etc.

There is a LOT more to it when talking about object oriented programming designs and techniques, but at the beginner level, a class is just a type that can be used to create variables (objects) of your new custom type.

The first thing a beginner will see in classes is that it organizes code nicely.
consider your line program... you need m, x, and b to define a line in one of its formats, 3 variables. If you just define them in main (double x,m,b) they don't really have anything that ties them together as a 'thing'. If you want to do something like plot your line, you need to do something like plot(m,x,b); If you had a class that contains m,x,b inside, then that becomes myline.plot() and the m,x,b variables are removed from main where they are clutter. Now imagine you had 10 lines. In example 1 you now need in main m1,m2,m3,m4, ... x1,x2,x3,.. etc and its a huge mess. With the class, you just need line1, line2, ... of course you can manage either lines or m/x/bs in a vector or something, but the effort reduction to manage things is still huge. You will see many other benefits as you learn more.

Topic archived. No new replies allowed.