#include <iostream>
#include <string>
#include <utility>
/** Example class demonstrating constructors and operators.
*
* This class showcases various C++ constructors and operators:
* - Default constructor
* - Parameterized constructor
* - Copy constructor
* - Move constructor
* - Copy assignment operator
* - Move assignment operator
* - Comparison operators
* - Output operator
*/
class Person {
private:
std::string name; /*!< Person's name */
int age; /*!< Person's age */
public:
/** Default constructor.
*
* Initializes a Person with default values.
* Name is empty string, age is 0.
*/
Person() : name(""), age(0) {
std::cout << "Default constructor called" << std::endl;
}
/** Parameterized constructor.
*
* Initializes a Person with specified name and age.
*
* @param n The person's name.
* @param a The person's age.
*/
Person(const std::string & n, int a) : name(n), age(a) {
std::cout << "Parameterized constructor called for " << name << std::endl;
}
/** Copy constructor.
*
* Creates a copy of another Person object.
* Performs deep copy of the name string.
*
* @param other The Person object to copy from.
*/
Person(const Person & other) : name(other.name), age(other.age) {
std::cout << "Copy constructor called for " << name << std::endl;
}
/** Move constructor.
*
* Moves resources from another Person object.
* Transfers ownership of the name string.
*
* @param other The Person object to move from.
*/
Person(Person && other) noexcept : name(std::move(other.name)), age(other.age) {
other.age = 0; // Reset moved-from object
std::cout << "Move constructor called for " << name << std::endl;
}
/** Copy assignment operator.
*
* Assigns values from another Person object.
* Performs deep copy of the name string.
*
* @param other The Person object to assign from.
* @return Reference to this object.
*/
Person & operator=(const Person & other) {
if (this != &other) {
name = other.name;
age = other.age;
std::cout << "Copy assignment called for " << name << std::endl;
}
return *this;
}
/** Move assignment operator.
*
* Moves resources from another Person object.
* Transfers ownership of the name string.
*
* @param other The Person object to move from.
* @return Reference to this object.
*/
Person & operator=(Person && other) noexcept {
if (this != &other) {
name = std::move(other.name);
age = other.age;
other.age = 0; // Reset moved-from object
std::cout << "Move assignment called for " << name << std::endl;
}
return *this;
}
/** Destructor.
*
* Cleans up the Person object.
* No special cleanup needed for this simple class.
*/
~Person() {
std::cout << "Destructor called for " << name << std::endl;
}
/** Equality operator.
*
* Compares two Person objects for equality.
*
* @param other The Person object to compare with.
* @return true if both name and age are equal, false otherwise.
*/
bool operator==(const Person & other) const {
return (name == other.name) && (age == other.age);
}
/** Less than operator.
*
* Compares two Person objects by age.
*
* @param other The Person object to compare with.
* @return true if this person's age is less than other's age.
*/
bool operator<(const Person & other) const {
return age < other.age;
}
/** Output stream operator.
*
* Outputs Person information to a stream.
*
* @param os The output stream.
* @param person The Person object to output.
* @return Reference to the output stream.
*/
friend std::ostream & operator<<(std::ostream & os, const Person & person) {
os << "Person{name: \"" << person.name << "\", age: " << person.age << "}";
return os;
}
/** Get person's name.
*
* @return The person's name.
*/
const std::string & getName() const { return name; }
/** Get person's age.
*
* @return The person's age.
*/
int getAge() const { return age; }
};
/** Demonstrates usage of constructors and operators.
*
* This function creates Person objects using various constructors
* and demonstrates operator usage.
*/
void demonstrateConstructorsAndOperators() {
std::cout << "=== Constructor Demonstration ===" << std::endl;
// Default constructor
Person p1;
std::cout << "p1: " << p1 << std::endl;
// Parameterized constructor
Person p2("Alice", 25);
std::cout << "p2: " << p2 << std::endl;
// Copy constructor
Person p3 = p2; // Copy construction
std::cout << "p3 (copy of p2): " << p3 << std::endl;
// Move constructor
Person p4 = std::move(Person("Bob", 30)); // Move construction
std::cout << "p4: " << p4 << std::endl;
std::cout << "\n=== Operator Demonstration ===" << std::endl;
// Copy assignment
p1 = p2;
std::cout << "p1 after copy assignment: " << p1 << std::endl;
// Move assignment
Person temp("Charlie", 35);
p1 = std::move(temp);
std::cout << "p1 after move assignment: " << p1 << std::endl;
// Comparison operators
std::cout << "p2 == p3: " << (p2 == p3) << std::endl;
std::cout << "p2 < p4: " << (p2 < p4) << std::endl;
std::cout << "\n=== End of Demonstration ===" << std::endl;
}
int main() {
demonstrateConstructorsAndOperators();
return 0;
}