1/26/24, 10:23 PM C++ Class Methods
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
Delivering power efficiency sponsore
C++ Class Methods
❮ Previous Next ❯
Class Methods
Methods are functions that belongs to the class.
There are two ways to define functions that belongs to a class:
Inside class definition
Outside class definition
In the following example, we define a function inside the class, and we name it
" myMethod ".
Note: You access methods just like you access attributes; by creating an object of
the class and using the dot syntax ( . ):
Inside Example
class MyClass { // The class
public: // Access specifier
void myMethod() { // Method/function defined inside the class
cout << "Hello World!";
}
};
int main() {
MyClass myObj; // Create an object of MyClass
[Link](); // Call the method
return 0;
[Link] 1/7
1/26/24, 10:23 PM C++ Class Methods
}
Tutorials Exercises Services Sign Up Log in
Try itCSS
HTML YourselfJAVASCRIPT
» SQL PYTHON JAVA PHP HOW TO [Link] C
To define a function outside the class definition, you have to declare it inside the class
and then define it outside of the class. This is done by specifiying the name of the
class, followed the scope resolution :: operator, followed by the name of the
function:
Outside Example
class MyClass { // The class
public: // Access specifier
void myMethod(); // Method/function declaration
};
// Method/function definition outside the class
void MyClass::myMethod() {
cout << "Hello World!";
}
int main() {
MyClass myObj; // Create an object of MyClass
[Link](); // Call the method
return 0;
}
Try it Yourself »
ADVERTISEMENT
[Link] 2/7
1/26/24, 10:23 PM C++ Class Methods
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP
Smart Manufacturin
HOW TO [Link] C
Mitsubishi Electric is “Partn
is the leading manufacturer
AD Mitsubishi Electric
Parameters
You can also add parameters:
Example
#include <iostream>
using namespace std;
class Car {
public:
int speed(int maxSpeed);
};
int Car::speed(int maxSpeed) {
return maxSpeed;
}
int main() {
Car myObj; // Create an object of Car
cout << [Link](200); // Call the method with an argument
return 0;
}
Try it Yourself »
❮ Previous Log in to track progress Next ❯
[Link] 3/7
1/26/24, 10:23 PM C++ Class Methods
Tutorials Exercises Services Sign Up Log in
ADVERTISEMENT
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
SPONSORED BY
MITSUBISHI ELECTRIC
Smart
Manufacturing
LEARN MORE
COLOR PICKER
[Link] 4/7
1/26/24, 10:23 PM C++ Class Methods
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO [Link] C
ADVERTISEMENT
Safety and
Reliability
sponsored by: Mitsubishi E…
READ MORE
ADVERTISEMENT
ADVERTISEMENT
[Link] 5/7
1/26/24, 10:23 PM C++ Class Methods
Tutorials Exercises Services Sign Up Log in
HTML
CSS
SPACES
JAVASCRIPT SQL
UPGRADE
PYTHON JAVA
AD-FREE
PHP HOW TO [Link] C
NEWSLETTER GET CERTIFIED
REPORT ERROR
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
[Link] Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
[Link] Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference
Top Examples Get Certified
HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
[Link] Examples PHP Certificate
Bootstrap Examples jQuery Certificate
PHP Examples Java Certificate
Java Examples C++ Certificate
XML Examples C# Certificate
jQuery Examples XML Certificate
[Link] 6/7
1/26/24, 10:23 PM C++ Class Methods
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT
SQL
FORUM PYTHON
ABOUT JAVA PHP HOW TO [Link] C
W3Schools is optimized for learning and training. Examples might be simplified to
improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted our
terms of use, cookie and privacy policy.
Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by
[Link].
[Link] 7/7