Difference between function and method in C++? [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Functions or methods?
I was thinking that they were both the same but I'm reading a book on C++ and I'm not really sure how they are different. Sorry I know this question has been asked but I'm still not really sure if they are different or not. Can someone please explain? Thanks.

Assuming you mean 'member functions' by 'methods', have a look at this
Member functions are functions declared inside a class.
THe difference between ordinary functions and (non-static) member functions is that non-static member functions take an implicit parameter: the pointer to the object they are being called on (this)

The C++ language definition talks about "functions" and "member functions". It does not talk about "methods". So the meaning of "function" and of "member function" is well defined. The meaning of "method" for C++ is whatever you think it means, and it's often used in precisely that way, that is, as a fuzzy term for "something I think I can call", without a precise meaning.

Related

Why does the scope of any member of a class extend throughout the entire class, regardless of where the member is declared?

If it's an ordinary local object, its scope begins when it's declared, and things before its declaration can't access it. However, if it's a member variable, then anything in this class, even written before the declaration of that member, has access to it.
What happens under the hood? Why is it designed this way?
I tried to google about it but only got the rule itself instead of the mechanism or the motive.
There's nothing to answer here. It is just that way.
Under the hood happens that the compiler tracks the scope as specified by the language.
Why that language is that way: I'd say it is intuitive, but you seem to disagree, so the only argument I could come up with has been devalued by the very question you're asking. 🤷🏼.

What is the meaning of multiple class methods separated by "::"? [duplicate]

This question already has answers here:
What does the "::" mean in C++?
(4 answers)
Closed last year.
I found this line of code where I'm working, but I don't quite understand its meaning:
virtual method1::method2::method3 f() = 0;
In the main function, I have method1::method2::method3.g1().g2(). I really don't understand. I know method::A where A is a class.
:: is a scope operator.
You may need to append multiple of them either when using nested classes (a class defined within another class) or when you use namespaces. Resolution is done the same way.
:: is the scope resolution operator. It allows you to statically traverse scopes such as namespaces and classes in order to reference the identifier you want.
I think what you have is not methods but namespaces and classes.

C++ Operator Overloading??? Isn't it more like override? [duplicate]

This question already has answers here:
Why is it called operator overloading?
(2 answers)
Closed 4 years ago.
In C++, why is operator overloading called "overloading"?
To me, this seems more like "override."
Because you're not changing the meaning of +, -, *, / etc. with respect to the fundamental datatypes. You can't change what those means for char, short, int, float, etc. Therefore, you're not truly overriding anything.
You are instead expanding the meaning of them to new contexts, which seems to fit with the term "overloading": you've loaded the symbols onto new meanings they did not previously have.
This is pretty subjective, and not easy to answer in specific terms.
But generally we use "override" to mean "replacing the behaviour of a function with another behaviour", as in when you have a polymorphic class hierarchy, and a call to a function whose various implementations are virtual can result in totally different behaviour. Certainly that's what the standard means by the term.
Isn't this also what happens with overloading? Kind of. But usually when you overload a function, you do so to give it different parameter lists, but would still expect each implementation to perform the same job. It doesn't have to, but one expects it to.
Similarly with overloaded operators, if you're overloading say operator+ then generally we expect that it actually still just does the normal, conventional "addition" logic — but overloaded so that it can take an argument of your new class type, instead of the existing overloads that take built-in types.
In practice, that breaks down a bit, because even the standard library makes operator<< mean something completely different (among other examples).
Still, the actual task of creating these new operators is accomplished by what the language considers to be function overloading (particularly as no virtual calls are involved at all).
In short, you're arguably not entirely wrong, but it's pretty arbitrary and this is what we ended up with.

What does "class className;" by itself do in C++? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++ - Forward declaration
so in my header file i have a class defined/declared with all it features named User
then in a .cpp source file there is near the top class User;
im new to c, but couldnt find an answer in the few tutorials i looked into so came here
what does it do ?
thankyou.
It's called a forward declaration and enables the compiler to recognise the class without actually knowing its internals. You just inform the compiler that such a class exists, and declaring a pointer to it will not raise an error.
The alternative would be to include the corresponding header file that declares the class, but that would be way too heavy if all you need is having pointers to that type.
It is a so called "Forward declaration": it makes the class known to the compiler, without actually defining it. Note that this is C++, not C.
It declares the class without actually defining it. In that sense, it's no different to:
void doSomething(void);
That prototype tells the compiler that doSomething() exists but doesn't actually specify what it does.
It's generally used where you need the class to exist (so you can reference it somehow) but you don't need to know anything about its properties.

In C++, what is the difference between a method and a function [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the difference between a method and a function
I'm trying to get my terminology correct.
What is the difference between a method and a function, in regards to C++ specifically.
Is it that a method returns nothing and just preforms operations on its class; while a function has a return value?
As far as the C++ standard is concerned, there is no such thing as a "method". This terminology is used in other OO languages (e.g. Java) to refer to member functions of a class.
In common usage, you'll find that most people will use "method" and "function" more or less interchangeably, although some people will restrict use of "method" to member functions (as opposed to "free functions" which aren't members of a class).
Sorry, but this is one of my pet peeves. Method is just a generic OO-type term. Methods do not exist in C++. If you open the C++ standard, you won't find any mention of "methods". C++ has functions, of various flavors.
A method is a member function of a class, but in C++ they are more commonly called member functions than methods (some programmers coming from other languages like Java call them methods).
A function is usually meant to mean a free-function, which is not the member of a class.
So while a member function is a function, a function is not necessarily a member function.
Example:
void blah() { } // function
class A {
void blah() { } // member function (what would be a "method" in other languages)
};
blah(); // free functions (non-member functions) can be called like this
A ainst;
ainst.blah(); // member functions require an instance to invoke them on
The term "Method" is not used in c++, but rather member function.
If you are thinking about the difference between a procedure and a function then the difference in c++ is none. Pascal was pretty much the last language to make that distinction. (ADA was constructed later and used the term Procedure, thanks Brian Neal.)
Any function, member or not, declared as void, would be a Procedure in the old vocabulary.
A member function is a complex beast, a function is a simple function.
A member function
is a member of a class
can be private
can be protected
can be public
can be virtual
can be pure virtual
Even a method can have a return value.
A method is a function of a class. For example class "car" has a method "accelerate".