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.