ElansarGelmir
17-06-2004, 06:59 PM
Hey, I'm taking up this course in INTEC and i think i have some problems in understanding the concepts and applications of some of the things, especially the object oriented programming... Asked the lecturer, and she always threatens to post the questions we ask her in our exam questions. So i think it's safer to clarify all my doubts here...
Hmm... my question is, why do we need data hiding? Wouldn't it be a little tedious and cumbersome to access the hidden datas through base class functions through the derived class member functions?
__earth
17-06-2004, 10:03 PM
u mean, encapsulation?
if you do, its because you may not want the client program to edit some of the data. Also, it allows the programmer to setup privileges to certain user.
masterof_none
20-06-2004, 11:02 PM
IMO, C++ is a bad object oriented prog language. In fact, it's not even a full OOP lang.
i think the best language for introductory course in OOP to be thought is Java. (or , Objective-C, if you want). Java is fully OOP.
OOP is a confusing concept. you need to practice using it everyday.
basically, it is the way of programming, so that, it's easier for you to modify the code in the future (they have lot more advantages).
basically, you just create a template for something, and then create the object out of the template . programmer call the template 'class'.
for example, a template for a car :
1. move forward, move backward,
2. send_acceleration (give acceleration to this car).
3. change gear.
after that, our job now is to implement those behavior and define the data (the stuff we need to implement those behavior).
for example, to move the car forward, we obviously need 4 wheels. so, we include the wheel inside our car template. there are many things, that you can include as the data, other than wheels, for example, the accelerator, so that it can accelerate. the speedometer, to keep track of the speed of the car...etc.
after that, we are now ready to implement the behavior of our car.
it's up to you what you want to do with the behavior "move forward"
you can tell the car to jump, to go out and go crazy. but here, since we just want to move the car, we can tell the car, to start from rest, and then, supply the acceleration..
so, if I want to tell my car to accelerate 10 mph, I go :
"hey car, move forward with this acceleration(50mph). "
acceleration is the message sent to the car, so that it can move 50mph.
the problem is, which car do you want to send the message ? ???
there are millions of cars in the world. and they have lots of brands too. which one?
so, of course, we want to send the message (our message, acceleration) to our car. which one is our car?. the car that we just defined in the template above, that can only move forward, move backward, receive acceleration from someone else (the driver, of course), and change the gear.
but since the car above is only a template, it's not yet ready to be used. In other word, if you are the engineer at Proton, you don't yet have the Proton car to drive, you just have the specs for Proton.
can you drive with the specs?...of course not!.
so now, it's time to 'manufacture' the car, out of the template/specs that we just defined above.
I bring my template/specs to the Proton assembly factory in Shah Alam.After 5 hours negotiation, the nice engineers there agreed to help me to manufacture my own Proton!.
after 2-3 months waiting, finally , I received the car. now, I name my car, myProton.
Happily, I jump into myProton right away, without taking a shower. so, to tell myProton to move forward. I use this:
myProton->moveForward(50mph);
The proton, finally, move forward, with acceleration 50mph.
now, I just realized that I can't stop the car. HELP!.
so , the moral is, we have to design a fully functional car in our specs/template/class (don't let yourself confused with the term class, it's basically a specs for our Proton, in this case).
now, after landing in the cows farm and damaged the car badly, I redesigned my car's behavior, so that , it can be stopped.
I add the 'stop' behavior, and supply the magnitude of 'deceleration', so that, eventually, the speed reaches zero.
then, I go back to Proton factory, manufacture the car, and now by calling
myProton->stop(40mph);
the car eventually hit zero speed. and I return home safely.
that's pretty much the idea behind OOP. the best language to learn OOP is Java/Objective-C. but for some reason, ppl like C++ for introductory course in OOP. I strongly feel that it's an inappropriate language for beginner of OOP.
hope that help.
chenchow
21-06-2004, 08:43 AM
One more advantage for encapsulation would be that the user won't be flooded with tons of information. They only know what they need to know.
An analogy, is like when we use google to search, majority of users just care about the results, but not how google search, as long as they get the results. So, in this case, the data hiding would be all those processes involved, so that the user just get the results of their search and nothing more.
Feel free to post as many of your questions here!
da-hype
23-06-2004, 11:26 AM
yeah i agree with masterof_none, java is easier and interesting... but somehow c++ still does it for me. there just so much we can do.. especially since i'm into mirc stuff.. but seriously.. once you know the window libarary functions.. and know your c++, you can do wonders..
http://msdn.microsoft.com/library/
all the cool things you can do are available there. :D enjoy.
ElansarGelmir
24-06-2004, 11:38 AM
Hehe, thanks for enlightenning me... Well, it's not that i'm into C++... UiTM doesn't offer JAVA course. only C++. And recently we have to depict a long list of program that deals with List (Arghh.... Mind's blowing). Should have taken Calculus!
Anyway, one more question... can we overload ostream and istream operators without using friend function?
vBulletin® v3.7.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.