fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <utility>
  4.  
  5. /** Example class demonstrating constructors and operators.
  6.  *
  7.  * This class showcases various C++ constructors and operators:
  8.  * - Default constructor
  9.  * - Parameterized constructor
  10.  * - Copy constructor
  11.  * - Move constructor
  12.  * - Copy assignment operator
  13.  * - Move assignment operator
  14.  * - Comparison operators
  15.  * - Output operator
  16.  */
  17. class Person {
  18. private:
  19. std::string name; /*!< Person's name */
  20. int age; /*!< Person's age */
  21.  
  22. public:
  23. /** Default constructor.
  24.   *
  25.   * Initializes a Person with default values.
  26.   * Name is empty string, age is 0.
  27.   */
  28. Person() : name(""), age(0) {
  29. std::cout << "Default constructor called" << std::endl;
  30. }
  31.  
  32. /** Parameterized constructor.
  33.   *
  34.   * Initializes a Person with specified name and age.
  35.   *
  36.   * @param n The person's name.
  37.   * @param a The person's age.
  38.   */
  39. Person(const std::string & n, int a) : name(n), age(a) {
  40. std::cout << "Parameterized constructor called for " << name << std::endl;
  41. }
  42.  
  43. /** Copy constructor.
  44.   *
  45.   * Creates a copy of another Person object.
  46.   * Performs deep copy of the name string.
  47.   *
  48.   * @param other The Person object to copy from.
  49.   */
  50. Person(const Person & other) : name(other.name), age(other.age) {
  51. std::cout << "Copy constructor called for " << name << std::endl;
  52. }
  53.  
  54. /** Move constructor.
  55.   *
  56.   * Moves resources from another Person object.
  57.   * Transfers ownership of the name string.
  58.   *
  59.   * @param other The Person object to move from.
  60.   */
  61. Person(Person && other) noexcept : name(std::move(other.name)), age(other.age) {
  62. other.age = 0; // Reset moved-from object
  63. std::cout << "Move constructor called for " << name << std::endl;
  64. }
  65.  
  66. /** Copy assignment operator.
  67.   *
  68.   * Assigns values from another Person object.
  69.   * Performs deep copy of the name string.
  70.   *
  71.   * @param other The Person object to assign from.
  72.   * @return Reference to this object.
  73.   */
  74. Person & operator=(const Person & other) {
  75. if (this != &other) {
  76. name = other.name;
  77. age = other.age;
  78. std::cout << "Copy assignment called for " << name << std::endl;
  79. }
  80. return *this;
  81. }
  82.  
  83. /** Move assignment operator.
  84.   *
  85.   * Moves resources from another Person object.
  86.   * Transfers ownership of the name string.
  87.   *
  88.   * @param other The Person object to move from.
  89.   * @return Reference to this object.
  90.   */
  91. Person & operator=(Person && other) noexcept {
  92. if (this != &other) {
  93. name = std::move(other.name);
  94. age = other.age;
  95. other.age = 0; // Reset moved-from object
  96. std::cout << "Move assignment called for " << name << std::endl;
  97. }
  98. return *this;
  99. }
  100.  
  101. /** Destructor.
  102.   *
  103.   * Cleans up the Person object.
  104.   * No special cleanup needed for this simple class.
  105.   */
  106. ~Person() {
  107. std::cout << "Destructor called for " << name << std::endl;
  108. }
  109.  
  110. /** Equality operator.
  111.   *
  112.   * Compares two Person objects for equality.
  113.   *
  114.   * @param other The Person object to compare with.
  115.   * @return true if both name and age are equal, false otherwise.
  116.   */
  117. bool operator==(const Person & other) const {
  118. return (name == other.name) && (age == other.age);
  119. }
  120.  
  121. /** Less than operator.
  122.   *
  123.   * Compares two Person objects by age.
  124.   *
  125.   * @param other The Person object to compare with.
  126.   * @return true if this person's age is less than other's age.
  127.   */
  128. bool operator<(const Person & other) const {
  129. return age < other.age;
  130. }
  131.  
  132. /** Output stream operator.
  133.   *
  134.   * Outputs Person information to a stream.
  135.   *
  136.   * @param os The output stream.
  137.   * @param person The Person object to output.
  138.   * @return Reference to the output stream.
  139.   */
  140. friend std::ostream & operator<<(std::ostream & os, const Person & person) {
  141. os << "Person{name: \"" << person.name << "\", age: " << person.age << "}";
  142. return os;
  143. }
  144.  
  145. /** Get person's name.
  146.   *
  147.   * @return The person's name.
  148.   */
  149. const std::string & getName() const { return name; }
  150.  
  151. /** Get person's age.
  152.   *
  153.   * @return The person's age.
  154.   */
  155. int getAge() const { return age; }
  156. };
  157.  
  158. /** Demonstrates usage of constructors and operators.
  159.  *
  160.  * This function creates Person objects using various constructors
  161.  * and demonstrates operator usage.
  162.  */
  163. void demonstrateConstructorsAndOperators() {
  164. std::cout << "=== Constructor Demonstration ===" << std::endl;
  165.  
  166. // Default constructor
  167. Person p1;
  168. std::cout << "p1: " << p1 << std::endl;
  169.  
  170. // Parameterized constructor
  171. Person p2("Alice", 25);
  172. std::cout << "p2: " << p2 << std::endl;
  173.  
  174. // Copy constructor
  175. Person p3 = p2; // Copy construction
  176. std::cout << "p3 (copy of p2): " << p3 << std::endl;
  177.  
  178. // Move constructor
  179. Person p4 = std::move(Person("Bob", 30)); // Move construction
  180. std::cout << "p4: " << p4 << std::endl;
  181.  
  182. std::cout << "\n=== Operator Demonstration ===" << std::endl;
  183.  
  184. // Copy assignment
  185. p1 = p2;
  186. std::cout << "p1 after copy assignment: " << p1 << std::endl;
  187.  
  188. // Move assignment
  189. Person temp("Charlie", 35);
  190. p1 = std::move(temp);
  191. std::cout << "p1 after move assignment: " << p1 << std::endl;
  192.  
  193. // Comparison operators
  194. std::cout << "p2 == p3: " << (p2 == p3) << std::endl;
  195. std::cout << "p2 < p4: " << (p2 < p4) << std::endl;
  196.  
  197. std::cout << "\n=== End of Demonstration ===" << std::endl;
  198. }
  199.  
  200. int main() {
  201. demonstrateConstructorsAndOperators();
  202. return 0;
  203. }
  204.  
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
=== Constructor Demonstration ===
Default constructor called
p1: Person{name: "", age: 0}
Parameterized constructor called for Alice
p2: Person{name: "Alice", age: 25}
Copy constructor called for Alice
p3 (copy of p2): Person{name: "Alice", age: 25}
Parameterized constructor called for Bob
Move constructor called for Bob
Destructor called for 
p4: Person{name: "Bob", age: 30}

=== Operator Demonstration ===
Copy assignment called for Alice
p1 after copy assignment: Person{name: "Alice", age: 25}
Parameterized constructor called for Charlie
Move assignment called for Charlie
p1 after move assignment: Person{name: "Charlie", age: 35}
p2 == p3: 1
p2 < p4: 1

=== End of Demonstration ===
Destructor called for 
Destructor called for Bob
Destructor called for Alice
Destructor called for Alice
Destructor called for Charlie