Basic OOP
- Video up to
NumberWrapper. - Information
Basic Concepts
Section titled “Basic Concepts”-
What is a method?
Answer
A method is a function defined within a structure or class. -
What is a “member function”?
Answer
It is a synonym for a method. -
What is a class member?
Answer
A class member can be a method or field, as well as a constructor or destructor (which come later). -
How does an instance function differ from a static one?
Linker
staticfor methods is unrelated tostaticused for internal linkage. Do not confuse them.Answer
Instance functions receive a hidden
thisparameter — a pointer to the object. -
What is the syntax for calling methods?
Answer
Instance methods can only be called using member-access syntax:
obj.action(parameter), which passes&objas the hiddenthisparameter (ifobjis a pointer, useobj->action(parameter)instead).Static methods can be called via
obj.action(parameter)or asObject::action(parameter)(whereObjectis the type ofobj). No hiddenthisexists for them, so it is not passed. -
How can a method be declared and defined separately?
Answer
A method can be declared just like an ordinary function.
class Hello{static void world();}It can be defined either directly in the class or outside it as
void Hello::world().class Hello{static void world();}// ...void Hello::world(){std::cout << "Hello" << std::endl;}The same syntax applies to instance methods.
-
Can a method inside a class have internal linkage (so that it is defined only in the current compilation unit)?
Answer
You cannot give just one method internal linkage. Giving the class internal linkage gives all of its methods internal linkage. A nested class can be used as a workaround. See the documentation.
-
Is the
newoperator required to create an object of a class (a structure with methods)?Answer
No. Objects of class types can be allocated on the stack, just like ordinary structures without methods. -
Does the number of methods in a class affect how many bytes an object of that class type occupies?
Answer
No. Methods belong to the type itself, not to a particular object. The instructions in the methods themselves take up memory, but separately from each object's memory. -
Does an object with one method take more memory than an object without methods?
Answer
No, if the methods are not virtual. Virtual methods will be covered later, in the topic on polymorphism.
You can check how much memory any object occupies using
sizeof. -
What is the functional advantage of using methods instead of ordinary functions that explicitly take a parameter similar to
thisas their first parameter?Answer
Functionally—that is, in terms of what happens when the program runs—there is no difference. Therefore, the correct answer is that methods have no functional advantage.Methods have an advantage in organizing code: if all operations on data of a particular type are defined within the type definition, they are easier to find.
OOP also makes it possible to encapsulate data, which is harder to achieve using typical procedural programming.