Sign in to follow this  
Tradone

What are Virtual Methods?

Recommended Posts

edit2: What are Virtual Methods? edit: I am reading a good book on C++ oop Visual C++ .NET Step by Step, but still can not distinguish between classes / objects / instances. I have read about private and public methods but my textbook only states that private methods are protected, I think I can't understand private vs public classes because the I haven't been in a real life situation where I need private methods. Can you sort of explain in very easy words? [Edited by - Tradone on March 14, 2006 11:39:36 AM]

Share this post


Link to post
Share on other sites
Private members (including methods and variables) are there so that the class itself can do things that outside code can't. For example, consider a SecretMachine that lets the user get a special number (say, a winning lottery ticket number) if they give it the right password.

class SecureThing
{
public:
int GetLotteryTicketNumber(string Password);

private:
string GetThePassword();
};



Now, the GetLotteryTicketNumber() method can call GetThePassword() to find the real password, then compare it with the password it was given to see if they are the same. However, nobody else can come from the outside and ask for the real password - they have to know it first.


This is a useful thing for security, but it is more useful for something else: hiding implementation details. A class should let you get stuff done without caring about how the class actually does all of the work. Private members let the class do its thing without revealing to the rest of the program how it works internally. This is good because if you ever need to change the way the class works inside, you don't have to touch the rest of the code. You guarantee that none of the rest of the code makes assumptions about how the class works, which means you can also use that class in other programs if you like.

Share this post


Link to post
Share on other sites
Quote:
Original post by ApochPiQ
Private members (including methods and variables) are there so that the class itself can do things that outside code can't. For example, consider a SecretMachine that lets the user get a special number (say, a winning lottery ticket number) if they give it the right password.

class SecureThing
{
public:
int GetLotteryTicketNumber(string Password);

private:
string GetThePassword();
};



Now, the GetLotteryTicketNumber() method can call GetThePassword() to find the real password, then compare it with the password it was given to see if they are the same. However, nobody else can come from the outside and ask for the real password - they have to know it first.


This is a useful thing for security, but it is more useful for something else: hiding implementation details. A class should let you get stuff done without caring about how the class actually does all of the work. Private members let the class do its thing without revealing to the rest of the program how it works internally. This is good because if you ever need to change the way the class works inside, you don't have to touch the rest of the code. You guarantee that none of the rest of the code makes assumptions about how the class works, which means you can also use that class in other programs if you like.



Hm...
What if I had something like this in contrast?

int GetLotteryTicketNumber(string Password){
//do work
}

string GetThePassword(){
//get direct password
}


I mean, your code and my code, they both do the same thing don't they?

Share this post


Link to post
Share on other sites
Looking at private members as "protected" isn't a really good way to look at it, as it might confuse you later on when you learn about the protected keyword. Having a member private just means that only that class can access it. If a member of a class is public it means that everything can access it. Anything came come in and change the value, call the method, etc. A good example is below:


class Tradone
{
public:
void SomeMethod1()
{
std::cout << "SomeMethod1 called!";
}
private:
void SomeMethod2()
{
std::cout << "SomeMethod2 called!";
}
}


Now, in your main method, make an instance of Tradone and try to call SomeMethod2 [wink].

Share this post


Link to post
Share on other sites

#include &lt;iostream&gt;

class Tradone
{
public:
void SomeMethod1()
{
std::cout &lt;&lt; "SomeMethod1 called!";
}
private:
void SomeMethod2()
{
std::cout &lt;&lt; "SomeMethod2 called!";
}
};

int main(){
//new object
Tradone thats_me;

//call some method1
thats_me.SomeMethod1();

//call some method2
thats_me.SomeMethod2();


return 0;
}


error message:
151# g++ -o tradone tradone.cpp
tradone.cpp: In function `int main()':
tradone.cpp:12: error: `void Tradone::SomeMethod2()' is private
tradone.cpp:25: error: within this context
tradone.cpp:29:2: warning: no newline at end of file

Can't I just do this though?


#include &lt;iostream&gt;

void SomeMethod1()
{
std::cout &lt;&lt; "SomeMethod1 called!";
}

void SomeMethod2()
{
std::cout &lt;&lt; "SomeMethod2 called!";
}


int main(){
//new object

//call some method1
SomeMethod1();

//just don't call.
//thats_me.SomeMethod2();


return 0;
}



It would give me the same results.

[Edited by - Tradone on March 12, 2006 2:16:35 PM]

Share this post


Link to post
Share on other sites
Imagine you run a company with banana plantations where the farmhands are treated really bad. You don't want your business partners to talk and interact with the farmhands, only to you or one of the PR and sales managers.

So you declare your farmhands as "private", and yourself and the managers as "public".

Share this post


Link to post
Share on other sites
Emphasis is mine:

Quote:
Original post by Tradone
Quote:
Original post by ApochPiQ
This is a useful thing for security, but it is more useful for something else: hiding implementation details. A class should let you get stuff done without caring about how the class actually does all of the work. Private members let the class do its thing without revealing to the rest of the program how it works internally. This is good because if you ever need to change the way the class works inside, you don't have to touch the rest of the code. You guarantee that none of the rest of the code makes assumptions about how the class works, which means you can also use that class in other programs if you like.



Hm...
What if I had something like this in contrast?

int GetLotteryTicketNumber(string Password){
//do work
}

string GetThePassword(){
//get direct password
}


I mean, your code and my code, they both do the same thing don't they?


It may do the same thing, but that's not important in this context.

What is important is the concept of separation between the interface and the implementation. C++ and the other object oriented languages are using a programming paradigm where you defines objects as a set of behaviors and a bunch of properties. The behavior of the object defines its interface - ie the public methods. Now, the implementation of this behavior may be very complex - so you have basically two choices:

  1. you write lengthy methods to do the work
  2. you separate these long methods into smaller methods


In solution 1, you only defines public methods in your class. But as you easily understand, this is not very efficient because you may write ten times the same code here, here and here. In solution 2, you end up with a lot of functions, but some of them don't belong to the interface because there is no point in calling them from the outside (they only implement a small part of the behavior and their call mey depends on the setup that is done in another méthode).

To forbid the class user to call these methods, you use the language access control (in C++, those private/protected/public keyword).

I hope it is clearer now :)

Regards,

Share this post


Link to post
Share on other sites
I think I don't have a clear understanding of the object oriented programming paradigm. you see i'm only used to procedural programming.... and I was never introduced to any oop, and that may be a problem. When I download a source code on the internet and they're made of classes, I break them down into functions, and use them as functions. Again, as the two examples that I have provided, both source codes, the one that uses functions or the one that uses classes both do the same kind of work, but I still can't understand, no matter how many times I've read your thread.

can you briefly in easy words kind of explain to me oop?
I've read documentations but I'm having trouble understanding exactly what it is.

the only thing that I do understand in oop is that it uses a lot of classes, and reuses class methods many many times. But I don't think I'm grasping the heart of oop, because you can reuse functions many many times as well.

Share this post


Link to post
Share on other sites
http://javaboutique.internet.com/articles/ITJ/qanda/q20.html
excellent description of oo vs procedural
almost there :(

edit:
http://www.aquaphoenix.com/research/oop/page10.html
another excellent source.
I think I just about got it, this website aquaphoenix has superb real life examples.

[Edited by - Tradone on March 12, 2006 7:10:50 PM]

Share this post


Link to post
Share on other sites
'object' and 'instance' refer to the same thing. A 'class' is the blueprint for these.


class Person {
int age;
std::string name;
};
// That defines the 'class', which is simply the concept of a Person.

Person tradone; // This is an instance.


Or, if you prefer, a class is a kind of type; an object (or instance) is a kind of variable (which is of a class type).

Share this post


Link to post
Share on other sites
I see, so a class is like a template,
and the object is like the real thing.

now they call it object oriented programming.
please correct my definition.
oop = "programming style in which the data and the flow of data is the main focus."

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this