Class methods in C++ are functions that are defined within a class and are used to manipulate the data members of the class. They provide the functionality to encapsulate data and operations on data within a single unit.
A simple method can be defined to perform operations like displaying a message or calculating a value. Here is an example:
class MyClass {
public:
void displayMessage() {
std::cout << "Hello, World!" << std::endl;
}
};
In this example, a class named `MyClass` has a method `displayMessage` that prints a message to the console. This method does not return any value and does not take any parameters.
Methods can accept parameters to work with different data inputs. Here's how you can define a method with parameters:
class Calculator {
public:
int add(int a, int b) {
return a + b;
}
};
The `add` method in the `Calculator` class takes two integer parameters and returns their sum. This allows the method to perform addition on any two integers provided as arguments.
Methods can return values after performing specific operations. Here's an example:
class Rectangle {
public:
int area(int length, int width) {
return length * width;
}
};
The `area` method in the `Rectangle` class calculates and returns the area of a rectangle given its length and width.
Methods often interact with class data members. Here's an example of how a method can access and modify class members:
class Circle {
private:
double radius;
public:
void setRadius(double r) {
radius = r;
}
double getRadius() {
return radius;
}
};
The `Circle` class has a private member `radius`. The `setRadius` method assigns a value to `radius`, and the `getRadius` method returns the current value of `radius`.
Const methods are those that do not modify any data members of the class. They are declared with the `const` keyword:
class Student {
private:
string name;
public:
Student(string n) : name(n) {}
string getName() const {
return name;
}
};
The `getName` method in the `Student` class is a const method, meaning it cannot modify the member `name` and is safe to call on const objects.
Static methods belong to the class rather than any object instance. They can be called without creating an object of the class:
class MathUtils {
public:
static int square(int x) {
return x * x;
}
};
The `square` method in the `MathUtils` class is static, allowing it to be called using the class name without instantiating an object.
Method overloading allows multiple methods to have the same name but different parameter lists:
class Print {
public:
void display(int i) {
std::cout << "Integer: " << i << std::endl;
}
void display(double d) {
std::cout << "Double: " << d << std::endl;
}
};
The `Print` class has two overloaded `display` methods, one for integers and another for doubles. The appropriate method is called based on the argument type.
Inline methods are defined inside the class definition and are expanded at the point where they are called:
class Box {
public:
inline int volume(int length, int width, int height) {
return length * width * height;
}
};
The `volume` method in the `Box` class is defined as inline, which suggests to the compiler to insert the method's code directly into the calling code to reduce function call overhead.
Virtual methods allow derived classes to override methods in base classes, enabling polymorphism:
class Base {
public:
virtual void show() {
std::cout << "Base class show function." << std::endl;
}
};
class Derived : public Base {
public:
void show() override {
std::cout << "Derived class show function." << std::endl;
}
};
The `show` method in the `Base` class is virtual, allowing the `Derived` class to override it. This enables the correct method to be called based on the object type at runtime.
Pure virtual methods define an interface in abstract base classes that must be implemented by derived classes:
class AbstractBase {
public:
virtual void pureVirtualFunction() = 0;
};
class ConcreteClass : public AbstractBase {
public:
void pureVirtualFunction() override {
std::cout << "Implementation of pure virtual function." << std::endl;
}
};
The `pureVirtualFunction` in `AbstractBase` is a pure virtual method, making `AbstractBase` an abstract class. The `ConcreteClass` provides an implementation for this method.
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies