In Object-Oriented Programming (OOP), methods are functions that define the behaviors or actions that objects of a class can perform. They are typically used to manipulate or interact with an object's data (its attributes), or to provide functionality related to the object's behavior. Methods are defined inside classes and can operate on the object's internal state, or on external data passed to them.
Instance Methods:
Static Methods:
Constructor:
Destructor:
Accessors and Mutators (Getters and Setters):
The general syntax for defining methods within a class in C++ is:
class ClassName {
public:
// Method declaration
returnType methodName(parameters) {
// Method body
// operations on class data (attributes)
}
};
int, double, void).#include <iostream>
using namespace std;
class Calculator {
public:
// Instance method to add two numbers
int add(int a, int b) {
return a + b;
}
// Instance method to subtract two numbers
int subtract(int a, int b) {
return a - b;
}
// Instance method to multiply two numbers
int multiply(int a, int b) {
return a * b;
}
// Instance method to divide two numbers
double divide(int a, int b) {
if (b != 0)
return (double)a / b;
else {
cout << "Error: Division by zero!" << endl;
return 0;
}
}
};
int main() {
Calculator calc;
int x = 10, y = 5;
cout << "Sum: " << calc.add(x, y) << endl;
cout << "Difference: " << calc.subtract(x, y) << endl;
cout << "Product: " << calc.multiply(x, y) << endl;
cout << "Quotient: " << calc.divide(x, y) << endl;
return 0;
}
Calculator class contains instance methods (add(), subtract(), multiply(), and divide()) that perform basic arithmetic operations.a and b) and return the result of the operation.main() function creates an object calc of the Calculator class and calls its methods to perform calculations.Instance methods are the most common type of method in OOP. These methods operate on the instance data (attributes) of the object and can be used to manipulate or return the object's internal state.
Example (C++):
class Rectangle {
private:
int length;
int width;
public:
// Constructor to initialize the rectangle
Rectangle(int l, int w) : length(l), width(w) {}
// Instance method to calculate area
int getArea() {
return length * width;
}
// Instance method to set length
void setLength(int l) {
length = l;
}
// Instance method to set width
void setWidth(int w) {
width = w;
}
};
int main() {
Rectangle rect(10, 5);
cout << "Area: " << rect.getArea() << endl;
rect.setLength(15); // Change length using setter method
rect.setWidth(10); // Change width using setter method
cout << "Updated Area: " << rect.getArea() << endl;
return 0;
}
getArea() method is an instance method that calculates and returns the area of the rectangle by accessing the object's length and width attributes.setLength() and setWidth() methods are mutators (setters) that allow modifying the object's internal state.main() function creates a Rectangle object rect and uses instance methods to interact with the object.A static method belongs to the class itself, not to any particular instance. Static methods can be called using the class name, without creating an object. They can only access static members of the class (i.e., variables or methods marked as static).
Example (Static Methods in C++):
class MathUtility {
public:
// Static method to calculate the square of a number
static int square(int num) {
return num * num;
}
// Static method to calculate the cube of a number
static int cube(int num) {
return num * num * num;
}
};
int main() {
// Calling static methods using class name
cout << "Square of 5: " << MathUtility::square(5) << endl;
cout << "Cube of 5: " << MathUtility::cube(5) << endl;
return 0;
}
square() and cube() methods are static methods, meaning they belong to the class MathUtility itself, not to individual objects.MathUtility::square()), not an object of the class.Example of Constructor:
class Car {
private:
string model;
int year;
public:
// Constructor to initialize the car object
Car(string m, int y) : model(m), year(y) {}
void displayInfo() {
cout << "Model: " << model << ", Year: " << year << endl;
}
};
int main() {
Car car1("Toyota", 2022); // Constructor called during object creation
car1.displayInfo();
return 0;
}
~), and do not return a value or take parameters.Example of Destructor:
class Car {
private:
string model;
public:
// Constructor
Car(string m) : model(m) {
cout << "Car created: " << model << endl;
}
// Destructor
~Car() {
cout << "Car destroyed: " << model << endl;
}
};
int main() {
Car car1("Honda"); // Constructor is called here
// Destructor will be called automatically when car1 goes out of scope
return 0;
}
Example:
class Student {
private:
string name;
int age;
public:
// Getter method (accessor)
string getName() {
return name;
}
// Setter method (mutator)
void setName(string n) {
name = n;
}
// Getter method (accessor)
int getAge() {
return age;
}
// Setter method (mutator)
void setAge(int a) {
age = a;
}
};
int main() {
Student s;
s.setName("John");
s.setAge(20);
cout << "Name: " <<
Open this section to load past papers