Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- class Student {
- private:
- char* name;
- int age;
- public:
- // Конструктор по умолчанию
- Student() : name(nullptr), age(0) {}
- // Конструктор с параметрами
- Student(const char* n, int a) : age(a) {
- int nameLength = strlen(n);
- name = new char[nameLength + 1];
- strcpy(name, n);
- }
- // Конструктор переноса
- Student(Student&& other) : name(other.name), age(other.age) {
- other.name = nullptr;
- other.age = 0;
- }
- // Деструктор
- ~Student() {
- delete[] name;
- }
- // Геттеры
- const char* getName() const {
- return name;
- }
- int getAge() const {
- return age;
- }
- };
- int main() {
- // Создание объекта с помощью конструктора с параметрами
- Student s1("Alice", 20);
- std::cout << "Name: " << s1.getName() << ", Age: " << s1.getAge() << std::endl;
- // Создание объекта с помощью конструктора переноса
- Student s2(std::move(s1));
- std::cout << "Name: " << s2.getName() << ", Age: " << s2.getAge() << std::endl;
- // Проверка, что объект s1 теперь пустой
- std::cout << "Name: " << (s1.getName() ? s1.getName() : "null") << ", Age: " << s1.getAge() << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement