class
The class
keyword in C++ is used to define a user-defined type that encapsulates data and functions that operate on that data. It is a fundamental building block of object-oriented programming in C++, allowing for the creation of objects that combine state and behavior.
#include <iostream>
#include <string>
class Person {
private:
std::string name;
int age;
public:
Person(const std::string& n, int a) : name(n), age(a) {}
void introduce() const {
std::cout << "Hello, I'm " << name << " and I'm " << age << " years old." << std::endl;
}
void haveBirthday() {
age++;
std::cout << "Happy Birthday! " << name << " is now " << age << " years old." << std::endl;
}
};
int main() {
Person alice("Alice", 30);
alice.introduce();
alice.haveBirthday();
return 0;
}
Person
class with private data members (name
and age
)introduce
and haveBirthday
)main()
#include <iostream>
#include <string>
#include <vector>
class Shape {
public:
virtual double area() const = 0;
virtual void describe() const {
std::cout << "This is a shape." << std::endl;
}
virtual ~Shape() {}
};
class Circle : public Shape {
private:
double radius;
public:
Circle(double r) : radius(r) {}
double area() const override {
return 3.14159 * radius * radius;
}
void describe() const override {
std::cout << "This is a circle with radius " << radius << "." << std::endl;
}
};
class Rectangle : public Shape {
private:
double width, height;
public:
Rectangle(double w, double h) : width(w), height(h) {}
double area() const override {
return width * height;
}
void describe() const override {
std::cout << "This is a rectangle with width " << width << " and height " << height << "." << std::endl;
}
};
int main() {
std::vector<Shape*> shapes;
shapes.push_back(new Circle(5));
shapes.push_back(new Rectangle(4, 6));
for (const auto& shape : shapes) {
shape->describe();
std::cout << "Area: " << shape->area() << std::endl;
}
for (auto shape : shapes) {
delete shape;
}
return 0;
}
Shape
with a pure virtual function area()
Circle
and Rectangle
that inherit from Shape
#include <iostream>
class Complex {
private:
double real;
double imag;
public:
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
Complex operator+(const Complex& other) const {
return Complex(real + other.real, imag + other.imag);
}
friend std::ostream& operator<<(std::ostream& os, const Complex& c);
friend Complex operator*(const Complex& a, const Complex& b);
};
std::ostream& operator<<(std::ostream& os, const Complex& c) {
os << c.real;
if (c.imag >= 0) os << "+";
os << c.imag << "i";
return os;
}
Complex operator*(const Complex& a, const Complex& b) {
return Complex(a.real * b.real - a.imag * b.imag,
a.real * b.imag + a.imag * b.real);
}
int main() {
Complex a(1, 2);
Complex b(3, 4);
std::cout << "a = " << a << std::endl;
std::cout << "b = " << b << std::endl;
std::cout << "a + b = " << a + b << std::endl;
std::cout << "a * b = " << a * b << std::endl;
return 0;
}
Complex
class to represent complex numbers+
operator as a member function<<
operator overload for output*
operator to multiply complex numbers= default
and = delete
specifiers for explicitly defaulting or deleting special member functions.final
specifier can be used to prevent further inheritance or virtual function overriding.The class
keyword in C++ is a powerful feature that forms the foundation of object-oriented programming in the language. It allows developers to create custom types that encapsulate data and behavior, promoting code organization, reusability, and maintainability.
The examples provided demonstrate various aspects of classes in C++: 1. Basic class definition, showing encapsulation and member functions. 2. Inheritance and polymorphism, illustrating the creation of class hierarchies and dynamic dispatch. 3. Operator overloading and friend functions, demonstrating how to extend the behavior of classes to work seamlessly with C++ syntax.
Classes in C++ offer a wide range of capabilities, including access control, inheritance, polymorphism, and the ability to overload operators. They are essential for creating complex, well-structured programs and are particularly useful in large-scale software development.
Understanding how to effectively use classes is crucial for C++ programmers. They provide the means to model real-world entities and concepts in code, create reusable and modular components, and implement advanced programming paradigms. Mastery of classes and object-oriented principles is a key skill for developing robust and maintainable C++ applications.
Previous Page | Course Schedule | Course Content