Method (computer programming): Difference between revisions

Content deleted Content added
m v2.04 - Fix errors for CW project (Reference list missing)
Tags: WPCleaner Reverted
revert vandalism
Line 1:
{{short description|Computer function or subroutine that is tied to a particular instance or class}}
 
[[Amazon (company)|A]] '''method''' in [[object-oriented programming]] (OOP) is a [[Procedure (computer science)|procedure]] associated with a [[Message passing|message]] and an [[Object (computer science)|object]]. An object consists of ''data'' and ''behavior''; these comprise an [[Interface (computing)|''interface'']], which specifies how the object may be utilized by any of its various consumers.<ref name="consumerdef001a">Consumers of an object may consist of various kinds of elements, such as other programs, remote computer systems, or computer programmers who wish to utilize the object as part of their own programs.</ref>
 
Data is represented as [[Property (programming)|properties]] of the object, and behaviors are represented as methods. For example, a <code>Window</code> object could have methods such as <code>open</code> and <code>close</code>, while its state (whether it is open or closed at any given point in time) would be a property.
Line 160:
 
===Virtual functions===
''[[Virtual function]]s'' are the means by which a C++ class can achieve polymorphic behavior. ''Non-virtual member functions'', or ''regular methods'', are those that do not participate in [[polymorphism (computer science)|polymorphism]].
 
C++ Example:
<syntaxhighlight lang="cpp">
#include <iostream>
#include <memory>
 
class Super {
public:
virtual ~Super() = default;
 
virtual void IAm() { std::cout << "I'm the super class!\n"; }
};
 
class Sub : public Super {
public:
void IAm() override { std::cout << "I'm the subclass!\n"; }
};
 
int main() {
std::unique_ptr<Super> inst1 = std::make_unique<Super>();
std::unique_ptr<Super> inst2 = std::make_unique<Sub>();
 
inst1->IAm(); // Calls |Super::IAm|.
inst2->IAm(); // Calls |Sub::IAm|.
}
</syntaxhighlight>
 
==See also==
* [[Property (programming)]]
* [[Remote method invocation]]
* [[Subroutine]], also called subprogram, routine, procedure or function
 
== Notes ==
{{Reflistreflist}}
 
== References ==
{{refbegin}}
*{{cite book|url=https://books.google.com/books?id=DnsM0WD-6iMC&pg=PA131|title=C++ AND OBJECT-ORIENTED PROGRAMMING PARADIGM|last=JANA|first=DEBASISH|date=1 January 2005|publisher=PHI Learning Pvt. Ltd.|isbn=978-81-203-2871-6}}
*{{cite book|url=https://books.google.com/books?id=ZLzt5WtsdzIC&pg=PA50|title=Object-Oriented Programming: Fundamentals And Applications|last=Sengupta|first=Probal|date=1 August 2004|publisher=PHI Learning Pvt. Ltd.|isbn=978-81-203-1258-6}}
*{{cite book|url=https://books.google.com/books?id=Miq73i_J1i4C&pg=PA36|title=Object-oriented Programming: Using C++ for Engineering and Technology|last=Svenk|first=Goran|publisher=Cengage Learning|year=2003|isbn=0-7668-3894-3}}
*{{cite book|url=https://books.google.com/books?id=WCHZAgAAQBAJ&pg=PA74|title=Object Oriented Programming with C++|publisher=Tata McGraw-Hill Education|year=2013|isbn=978-1-259-02993-6|author=Balagurusamy}}
*{{cite book|url=https://books.google.com/books?id=-yhuY0Wg_QcC&pg=PA181|title=A Complete Guide to Programming in C++|last2=Prinz|first2=Peter|publisher=Jones & Bartlett Learning|year=2002|isbn=978-0-7637-1817-6|last1=Kirch-Prinz|first1=Ulla}}
*{{cite book|url=https://books.google.com/books?id=1F6ipojt7DcC&pg=PA79|title=Creating Games in C++: A Step-by-step Guide|last=Conger|first=David|publisher=New Riders|year=2006|isbn=978-0-7357-1434-2}}
*{{cite book|url=https://books.google.com/books?id=fgGLZ7WYxCMC&pg=PA97|title=The Advanced C++ Book|last=Skinner|first=M. T.|publisher=Silicon Press|year=1992|isbn=978-0-929306-10-0}}
*{{cite book|url=https://books.google.com/books?id=NXVkcCjPblcC&pg=PA18|title=Linux Kernel Development|date=1 September 2005|publisher=Pearson Education|isbn=978-81-7758-910-8|author=Love}}
*{{cite book|url=https://books.google.com/books?id=fxUVrhjD4k0C&pg=PA78|title=OBJECT-ORIENTED PROGRAMMING USING C++|last2=JAGADEV|first2=ALOK KUMAR|last3=RATH|first3=AMIYA KUMAR|date=8 May 2007|publisher=PHI Learning Pvt. Ltd.|isbn=978-81-203-3085-6|last1=DEHURI|first1=SATCHIDANANDA}}
{{refend}}
 
{{DEFAULTSORT:Method (Computer Science)}}
<!--Categories-->
[[Category:Method (computer programming)| ]]
[[Category:Articles with example C++ code]]