Classes and OOP
Classes and OOP
Classes, structs, access control, inheritance, virtual functions, and object design.
Classes and OOP
Classes, structs, access control, inheritance, virtual functions, and object design.
class Rectangle {
public:
Rectangle(double w, double h) : width_{w}, height_{h} {}
double area() const { return width_ * height_; }
private:
double width_{};
double height_{};
};
struct vs classstruct: default public accessclass: default private accessclass Shape {
public:
virtual ~Shape() = default;
virtual double area() const = 0;
};
class Circle : public Shape {
public:
explicit Circle(double r) : radius_{r} {}
double area() const override { return 3.14159 * radius_ * radius_; }
private:
double radius_{};
};
explicitoverridefinalvirtualpublic, protected, privateexplicit.class SocketOwner {
public:
SocketOwner(SocketOwner&&) noexcept = default;
SocketOwner& operator=(SocketOwner&&) noexcept = default;
SocketOwner(const SocketOwner&) = delete;
SocketOwner& operator=(const SocketOwner&) = delete;
};
struct Config {
std::string host;
int port{};
auto operator<=>(const Config&) const = default;
};
std::variant when the set of alternatives is closed and known.void show(Shape shape); // takes by value
Rectangle rect{3.0, 4.0};
show(rect); // derived data lost — always pass via reference or pointer
std::vector<std::unique_ptr<Shape>> shapes;
shapes.push_back(std::make_unique<Circle>(5.0));
shapes.push_back(std::make_unique<Rectangle>(3.0, 4.0));
Store polymorphic objects through smart pointers. Raw owning pointers in a container require manual cleanup and invite leaks.
virtual on the base destructor (causes partial destruction of derived objects)override but forgetting virtual on the base (becomes a hidden function instead)std::variant would be simplerstruct Widget {
int id{};
};
int main() {
Widget w{3};
return w.id;
}
Add a member function that keeps the invariants next to the data. That is the simplest way to move from passive structs to useful types.
struct Point {
int x{};
int y{};
int magnitude_squared() const {
return x * x + y * y;
}
};