Object-Oriented Programming (OOP) in C++
Object-oriented programming (OOP) is a programming paradigm that revolves around the concept of objects, which can contain data and code to manipulate that data. In C++, objects are created from classes, which define the properties and behavior of the objects. Here's an example of a simple class in C++:
c++
#include <iostream>
class Rectangle {
private:
int width;
int height;
public:
Rectangle(int w, int h) {
width = w;
height = h;
}
int area() {
return width * height;
}
};
int main() {
Rectangle r(4, 5);
std::cout << "Area of rectangle is: " << r.area() << std::endl;
return 0;
}
In this example, we've defined a Rectangle class with two private member variables, width and height, and two public member functions, a constructor and an area function. The constructor initializes the width and height variables, and the area function returns the area of the rectangle. In the main function, we create a Rectangle object r with a width of 4 and a height of 5, and then call the area function on that object to print out the area of the rectangle.
No comments:
Post a Comment