Thread: Forgotten

  1. #16
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by Mario F.
    When we code public set and get member functions we aren't really for the most part following this concept.
    If your accessors are virtual, then it makes all the difference. A virtual accessor in a base class that just returns the value of a field makes plenty of sense.

    You really aren't doing yourself any favors by having public or protected fields. Make 'em all private and implement the accessors that you want. It makes it much easier for derived classes to implement additional functionality. For instance, say I have good ole class Foo with a bit of a change:
    Code:
    class Foo {
        int foo_data;
    public:
        virtual const int getfoo(void) const { return foo_data; }
        virtual void setfoo(const int foo_in) { foo_data = foo_in; }
    };
    Now suppose you wanted a derived class from Foo that did a bit more stuff when you setfoo. Good thing you have accessors instead of a public field.
    Code:
    class Bar : public Foo {
    public:
    	void setfoo(const int foo_in)
    	{
    		//do additional work before setting the variable
    		//i.e. validation
    		Foo::setfoo(foo_in);
    		//do additional work after setting the variable
    		//i.e. logging
    	}
    };
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  2. #17
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    For gods sake does nobody actually help me?

    Maybe a slightly simpler task as I can't get my head around this fire thing yet

  3. #18
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Now suppose you wanted a derived class from Foo that did a bit more stuff when you setfoo. Good thing you have accessors instead of a public field.
    Well maybe.

    Inheritance is a different world most of the time, but even then I probably wouldn't code get/set functions. I don't see how you couldn't do validation before calling Foo's constructor, and logging after it's done (i.e. code an inserter operator for Foo).

    I'd even prefer to have a public const reference to member data instead of a getter function:
    Code:
    class Foo {
       int foo_data;
    public:
       const int& foo;
       Foo(const int data_in = 0) : foo_data(data_in), foo(foo_data) { }
       virtual ~Foo(void) { }
    };
    
    class Bar : public Foo {
    public:
        Bar(const int foo_in = 0) {
            // validate foo
            Foo(foo_in);
            // log...   
        }
    };
    Last edited by whiteflags; 06-01-2006 at 03:31 PM.

  4. #19
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by bumfluff
    For gods sake does nobody actually help me?

    Maybe a slightly simpler task as I can't get my head around this fire thing yet
    Yes, well... I told you to go back to the tutorials and read them to get your mind back in the right place. It is my fault I hijacked this thread.

    If you're really confused by that home alarm program I told you to do, do't worry about it. It was just a suggestion. Go back to coding Cat and Dog classes if you really forgot it all.

  5. #20
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Well... that const reference is taking things a little too far

    But I guess this is mostly a matter of taste. I agree that (and I'll add this again this time in bold), for the most part, get and set functions make it look like my class was badly planned. And boy,... some classes...

    Code:
    class Foo {
    public:
        void seta(int);
        void setb(int);
        int geta();
        int getb();
        double something_nifty(int a, int b);
    private:
        int a;
        int b;
    };
    This is an obvious exageration. Because of something_nifty, a class was implemented that shouldn't ever exist. But heck! I did that a lot on the past. And I'm at least somewhat relieved to know I wasn't the only one.

    That was my main beef. Of course, get and set functions can be useful here and there. I happen to like Pianorain example. I just think though that when one codes these functions they must give themselves a good reason to do so. They basically transform a private member into a public one circumventing data abstraction and encapsulating. Trickery!
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #21
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by bumfluff
    Maybe a slightly simpler task as I can't get my head around this fire thing yet
    I'm also learing C++, bumbluff. I'm actually on my early stages with this language. However, from my experience with other programming languages, I came to realize that when I can't think of something to do is because I didn't understand how to do it.

    The best thing you should do is to go through the tutorials again. I'm hoping you can keep some money and buy a decent book as your main learning source. I happen to like Addison Wesley C++ Primer very much. Regardless, you should also try other tutorials on other websites. No tutorial is complete, no book is comprehensive (no matter what they say). As your knowledge consolidates ideas will start to flow

    If you really are yawning at all of this i'm saying...

    ... write a conversions tool. Ask the user if what he wants to convert, ask him for value and output the result.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #22
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by Mario F.
    I happen to like Pianorain example.
    Wait a minute!...

    That can be true only if my class is the only one deriving from that base class. If I have other classes deriving from it, I risk breaking their implementation, Pianorain.

    So, I'm assuming from now on that you meant that example in the context of the one implementing the hierarchy. That is, at the highest level, that type of construct will not be visible anymore and users of the hierarchy wanting to define they own derived classes will not have that kind of access. Correct?

    Sorry to have bumped the thread...
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #23
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    So, I'm assuming from now on that you meant that example in the context of the one implementing the hierarchy. That is, at the highest level, that type of construct will not be visible anymore and users of the hierarchy wanting to define they own derived classes will not have that kind of access. Correct?
    Not exactly. The world, and any derived classes will have the ability to use setfoo(). Of course it depends on the type of inheritance you use as well, and how members of the base classes are handled by the derived class. See the FAQ.

    I admit that I see Pianoran's point. However if you are going to implement a hierarchy, it's best not to code a set function in Foo so that user's of a plain Foo cannot use it. Instead, you can make a private set() function or in Bar, so that the method is not derived again. However you see how complicated and buried this gets, so when you construct Bar, it's best to construct Foo the way you want it.

  9. #24
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    First, some informal definitions. I'll be calling programmers that simply use the class "users" and programmers that extend the class through inheritence "designers".
    Quote Originally Posted by citizen
    However if you are going to implement a hierarchy, it's best not to code a set function in Foo so that users of a plain Foo cannot use it.
    Why? It may be perfectly logical for users to be able to set your fields. Since we have a virtual set, we give designers the oppertunity to validate whatever data is coming in (or whatever it is that they want to do).

    In cases where you do want a read-only sort of property, it's simple enough to make the set function protected in Foo and Bar. I don't advocate leaving out the function altogether and making the field protected because designers of a derived class may still want to do things when the set function is called.
    Quote Originally Posted by Mario F.
    That can be true only if my class is the only one deriving from that base class. If I have other classes deriving from it, I risk breaking their implementation, Pianorain.
    I'm not exactly sure what you're trying to say, but I'll give it my best shot. It is the responsibility of the derived class to call the base class's function if that's the desired behavior in the derived class. If I build a derived class from Foo that hi-jacks setfoo and stores the value it's supposed to set in newfoo (a private field of the derived class), should I really expect getfoo and any other methods of the class that depend on foo to work? It's simpler to just call the base class's setfoo in my setfoo. If the base class's setfoo is doing something that I don't like, then I'd have to override all of those other functions to work with my newfoo field. That's a design issue for the base class.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  10. #25
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Why? It may be perfectly logical for users to be able to set your fields. Since we have a virtual set, we give designers the oppertunity to validate whatever data is coming in (or whatever it is that they want to do).
    Fine, then code a function that does that and make it a member of Foo. Such a function like the one you described is fine.

    After all, it isn't just assigning something blindly and leaving.

    It's doing something necessary: though I'd like to think that if you're coding in a team, you don't even need a set of functions like these. Developers of a hierarchy should be able to code and test their classes without the aid of such functions, (since Foo can access it's own variables, what's the point?!) and just use the members themselves. It's all about how many people are using this code.

  11. #26
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by pianorain
    I'm not exactly sure what you're trying to say[...]
    I was confusing the syntax. Never mind me. Back to the study books
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #27
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    If I have a class house with nothing in it and all these room classes that derive from it...and then I have an array of houses so can I add rooms to this? If so how can I do this.

  13. #28
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Code:
    //Assuming House has a default constructor
    //Assuming addroom(string) is your member function to add a new room
    
    House mystreet[3];
    
    mystreet[0].addroom("Bathroom");
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  14. #29
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    As a second exersise, try doing a program about a lift in a building.
    Use classes like:

    Bell class
    people class
    light class
    floor class

    ect;

    Have the program make note of what floor it has reached. Make the whole program run for 60 seconds ( loop ). During this time, the program must display:

    # what floors have been reached ( radomise this )
    # if the bell rang or not on reaching a floor
    # how many people got in the lift at a floor
    # how many people left the lift
    # make the lift light turn on when a floor is reached, turn off when it is moving

    I have completed this program aleady. Good luck - it can be done!
    -pete

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1st Assignment
    By Zeff in forum C Programming
    Replies: 29
    Last Post: 09-19-2006, 07:21 PM
  2. forgotten how to print values :(
    By darfader in forum C Programming
    Replies: 3
    Last Post: 08-12-2003, 09:47 AM
  3. Getting from function error!!!
    By frigga in forum C Programming
    Replies: 2
    Last Post: 11-19-2002, 03:37 PM
  4. Handwriting: soon to be forgotten?
    By Aran in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 08-26-2001, 06:43 PM