Methods - heshawacooray/OOP-Heshawa GitHub Wiki
Definition: Methods, also known as member functions, are functions associated with a class and its objects. They define the actions or operations that an object can perform. Methods operate on the data members (attributes or properties) of the class and can modify the object's state or return information about it.
Example code:
#include <iostream>
using namespace std;
class Car {
private:
string brand;
int year;
public:
Car(string b, int y) {
brand = b;
year = y;
}
void setBrand(string b) {
brand = b;
}
string getBrand() {
return brand;
}
void display() {
cout << "Brand: " << brand << ", Year: " << year << endl;
}
};
int main() {
Car car1("Toyota", 2020);
car1.display();
car1.setBrand("Honda");
cout << "Updated brand: " << car1.getBrand() << endl;
return 0;
}