Constructor Exercise difficulty.

This is a discussion on Constructor Exercise difficulty. within the C++ Programming forums, part of the General Programming Boards category; Hi guys, Having a little difficulty with an exercise from a book that I am working through. Currently looking at ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    329

    Constructor Exercise difficulty.

    Hi guys,

    Having a little difficulty with an exercise from a book that I am working through. Currently looking at classes using a very simple graphics library: circles, polygons, lines, etc.

    An example they have used is a Polygon class. You simply create as Polygon poly. And then call a member function to add points to the object to create the shape.

    The exercise is to create a similar class that passes the points to the constructor to ensure that there are more than 2 points, ie, to ensure it fits with geometric rules about what a polygon is. My question is how do I check an arbitrary number of points within a constructor?

    Obviously, I know how to pass a set number of arguments, it would simply look as such:

    Code:
    poly::poly(Point a, Point b, Point c)
    {
    //code
    }
    Any help is appreciated.

    Thanks

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    Are you saying your constructors needs to accept a variable amount of points, with a requirement that it be > 2?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    18
    Can you not pass a container of points into the constructor. Unless I am missing the point, I would add a number of points to e.g. a STL vector, format

    std::vector<point> myVec;

    (assuming 'point' is a defined class). Then you can iterate over the vector and do whatever you need to. Obvisouly, if you just wanted to check the number of points passed, you can call the size() method on the vector.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Elysia View Post
    Are you saying your constructors needs to accept a variable amount of points, with a requirement that it be > 2?
    Yes, that's what I am thinking.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by CodeMonkey62 View Post
    Can you not pass a container of points into the constructor. Unless I am missing the point, I would add a number of points to e.g. a STL vector, format

    std::vector<point> myVec;

    (assuming 'point' is a defined class). Then you can iterate over the vector and do whatever you need to. Obvisouly, if you just wanted to check the number of points passed, you can call the size() method on the vector.
    I did think of vectors, would this require a vector to be created outside the scope of the class and then passed by ref to the constructor?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    Yes, it would. And that would be the best solution available today.
    In coming versions, we should get initializer lists which should do exactly what you require. But we're not there yet.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Quote Originally Posted by Elysia View Post
    Yes, it would. And that would be the best solution available today.
    In coming versions, we should get initializer lists which should do exactly what you require. But we're not there yet.
    The reason why I didn't implement this, is that I thought this would have broken the encapsulation rule?

    As a newcomer to programming, I thought classes/objects etc, was to ensure all data/functions are self contained.

    Is it okay to break that convention?

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    18
    Quote Originally Posted by darren78 View Post
    I did think of vectors, would this require a vector to be created outside the scope of the class and then passed by ref to the constructor?
    Yes, and sorry, I should have mentioned the reference passing, we don't want any unecessary copying, now, do we.

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Also Elysia, I notice that you are using C++0x. Is there much difference in that that the previous standard? I'm a little worried that some of the stuff that I am learning may be becoming redundant?

    Thanks

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    18
    Quote Originally Posted by darren78 View Post
    The reason why I didn't implement this, is that I thought this would have broken the encapsulation rule?

    As a newcomer to programming, I thought classes/objects etc, was to ensure all data/functions are self contained.

    Is it okay to break that convention?
    Well, your points are all being created outside the poly class, all you are doing is collecting them together to pass in a single, variable sized container. If you want your class to have copies of the data for itself, as private members with appropriate accessors, then you'll need to implement this in the poly class.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,420
    Quote Originally Posted by darren78 View Post
    The reason why I didn't implement this, is that I thought this would have broken the encapsulation rule?

    As a newcomer to programming, I thought classes/objects etc, was to ensure all data/functions are self contained.

    Is it okay to break that convention?
    You're not breaking encapsulation. It is perfectly fine to pass in data to a class, no matter what type of indata that is. You would break encapsulation by trying to access those points while stored inside the class. All you are doing is telling the object that these are the points I want this polygon to have. This cannot possibly mean breaking encapsulation.

    Quote Originally Posted by darren78 View Post
    Also Elysia, I notice that you are using C++0x. Is there much difference in that that the previous standard? I'm a little worried that some of the stuff that I am learning may be becoming redundant?

    Thanks
    Some thing will certainly change, others will not.
    It's not fully finished, but some features are implemented in various compilers.
    If you look around for some tutorials on the web, I'm sure they can teach you a little about what's coming and how to use them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Okay, I understand now. Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Difficulty with destroying elements in a tree
    By bhdavis1978 in forum C Programming
    Replies: 4
    Last Post: 04-06-2010, 10:20 AM
  2. Replies: 4
    Last Post: 09-06-2009, 06:56 PM
  3. Replies: 4
    Last Post: 08-23-2009, 09:47 PM
  4. got difficulty on this
    By gtr_s15 in forum C++ Programming
    Replies: 4
    Last Post: 02-01-2006, 08:37 AM
  5. Game difficulty
    By pdstatha in forum C++ Programming
    Replies: 5
    Last Post: 06-30-2002, 04:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21