Thread: Beginner question - please help

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    1

    Beginner question - please help

    I am asked this question as part of an assignment, and I have no clue what to say:

    What is risky about the following and how could you rewrite it to make it safer?

    Code:
    class Line
    {
         public:
              Point  p1;
              Point  p2;
              double length;
    }
    thank you all for your help.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    It's missing a ; at the closing } but I don't think that's the point.

    I think the issue here is encapsulation. You can set the values of all the variables cause they're defined as public members. There should be setter functions of some description which set the points and then recalculates the length between the points. If you have a point at (0,3) and another at (4,0) and the length between them as 5 and you were then to change the first point to (4,0), there's no way the code would change the length to zero. It would still say five. If you had a function which managed the changing of the variables it could calculate the new length on every change.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    http://www.cprogramming.com/tutorial/lesson12.html

    Class tutorial on public and private/protected definitions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner: Linked List question
    By WeatherMan in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2008, 07:16 AM
  2. Quick IF statement question (beginner)
    By jim.rattlehead in forum C Programming
    Replies: 23
    Last Post: 11-29-2007, 06:51 AM
  3. beginner question
    By Barrot in forum C++ Programming
    Replies: 4
    Last Post: 08-19-2005, 02:17 PM
  4. Question About External Files (Beginner)
    By jamez05 in forum C Programming
    Replies: 0
    Last Post: 08-11-2005, 07:05 AM
  5. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 08:39 PM