MyTetra Share
Делитесь знаниями!
l-value r-value, ссылки r-value, Конструктор переноса, default, delete, геттеры, сеттеры, перегруженный бинарный оператор '+', '-', Перегрузка [],(), Перегрузка оператора приведения типа
Время создания: 13.06.2020 18:56
Раздел: C++ - Примеры кода - Работа на уроке
Запись: Shut913/Tetra-notes-Programming/master/base/15920638093wdgvg9rzo/text.html на raw.githubusercontent.com

#include <iostream>

#include <string>

using namespace std;


// l-value r-value

// ссылки r-value

//

//class A

//{

//private:

// int _val;

//public:

// A(int val): _val{val} {}

//};

//

//

//void main()

//{

// // A&& rref = A(3);

//

// int&& rref = 7;

// rref = 12;

// cout << rref;

//}

//void fun(const int& lref)

//{

// cout << "lref\n";

//}

//

//void fun(int&& rref)

//{

// cout << "rref\n";

//}

//

//void main()

//{

// int a = 7;

// fun(a);

// fun(7);

//}

//void main()

//{

// int x;

//

// int& ref1 = x; // A

// int& ref2 = 7; // B

//

// const int& ref3 = x; // C

// const int& ref4 = 7; // D

//

// int&& ref5 = x; // E

// int&& ref6 = 7; // F

//

// const int&& ref7 = x; // G

// const int&& ref8 = 7; // H

//}


// Конструктор переноса

//class Point {

//private:

// int _x, _y;

// int* _arr;

//public:

// Point() : _x{}, _y{}

// {

// std::cout << "Point(x = " << _x << ", y = " << _y << ") created" << std::endl;

// }

//

// Point(int x, int y) : _x{ x }, _y{ y }

// {

// std::cout << "Point(x = " << _x << ", y = " << _y << ") created from 2 params" << std::endl;

// }

//

// ~Point() {

// std::cout << "Point(x = " << _x << ", y = " << _y << ") destroyed" << std::endl;

// }

//

// int getX() const { return _x; }

// int getY() const { return _y; }

// Point& setX(int x) { _x = x; return *this; }

// Point& setY(int y) { _y = y; return *this; }

//

// // copy constructor

// Point(const Point& obj) {

// _x = obj._x;

// _y = obj._y;

// std::cout << "Point(x = " << _x << ", y = " << _y << ") copied" << std::endl;

// }

//

// Point& operator=(const Point& obj)

// {

// if (&obj == this)

// return *this;

//

// _x = obj._x;

// _y = obj._y;

// std::cout << "Point(x = " << _x << ", y = " << _y << ") assigned(copy)" << std::endl;

// }

//

// // move constructor

// Point(Point&& obj)

// {

// this->_arr = obj._arr;

// obj._arr = nullptr;

//

// _x = obj._x;

// _y = obj._y;

// std::cout << "Point(x = " << _x << ", y = " << _y << ") moved" << std::endl;

// }

//

// Point& operator=(Point&& obj)

// {

// if (&obj == this)

// return *this;

//

// this->_arr = obj._arr;

// obj._arr = nullptr;

//

// _x = obj._x;

// _y = obj._y;

// std::cout << "Point(x = " << _x << ", y = " << _y << ") assigned(moved)" << std::endl;

// }

//};

//

//Point generate()

//{

// cout << "generate()\n";

// Point a(2, 3);

// return a;

//}

//

//int main()

//{

// /*Point c1(1, 2);

// Point c2(c1);

// Point c3 = c1;*/

//

// Point a;

// a = generate();

//

// return 0;

//}


// default delete

// ClassName(parameters) = default;

//class Float

//{

//private:

// float x;

//

//public:

// // Конструктор по умолчанию

// // ключевое слово default указывает компилятору самостоятельно формировать

// // соответствующий конструктор, если такой не объявлен в классе

// Float() = default;

//

// // конструктор с 1 параметром - не может быть применено default

// Float(float _x) :x(_x)

// { }

//

// // конструктор копирования - передача прав компилятору на формирование этого конструктора

// Float(const Float&) = default;

//

// // конструктор перемещения

// Float(Float&&) = default;

//

// // оператор присваивания копированием

// Float& operator=(const Float&) = default;

//

// // оператор присваивания перемещением

// Float& operator=(Float&&) = default;

//

// // деструктор

// ~Float() = default;

//

// // методы доступа

// float Get() { return x; }

// void Set(float _x) { x = _x; }

//};

//

//int main()

//{

// Float f1(7);

// Float f2 = f1;

//

// std::cout << "f2.x = " << f2.Get() << endl;

// Float f3;

// f3.Set(18);

// f3 = f2;

// cout << "f3.x = " << f3.Get() << endl;

//}

// delete

// ClassName(parameters1) = delete;

// ClassName(parameters2) = delete;

// class ClassName

// {

// // ...

// return_type Func(parameters) = delete;

// };

//class Point

//{

//private:

// int x = 0; // координаты точки

// int y = 0;

//

//public:

// // default-конструктор, доверить реализацию компилятору

// Point() = default;

//

// // конструктор с одним параметром типа double - запрещено использовать

// Point(double value) = delete;

//

// // конструктор с одним параметром типа int - разрешено использовать

// Point(int value)

// {

// x = y = value;

// }

//

// // конструктор с 2 параметрами

// Point(int nx, int ny)

// {

// x = nx;

// y = ny;

// }

//

// // методы доступа к членам класса

// int GetX(void) { return x; }

// int GetY(void) { return y; }

//

// // функции SetX(), SetY() c параметром типа int

// void SetX(int nx) { x = nx; }

// void SetY(int ny) { y = ny; }

//

// // запретить вызов функции SetX(), SetY() с параметром типа double

// void SetX(double) = delete;

//

// // информация для компилятора, что запрещено использовать

// // метод SetXY() с двумя параметрами int

// void SetXY(int, int) = delete;

//

// // сам метод SetXY - ненужен, компилятор выдает ошибку

// /*

// void SetXY(int nx, int ny)

// {

// x = nx;

// y = ny;

// }

// */

//

// // перегруженный бинарный оператор '+'

// Point operator+(Point& pt)

// {

// // p - временный объект, который создается с помощью конструктора без параметров

// Point p;

// p.x = x + pt.x;

// p.y = y + pt.y;

// return p;

// }

//

// // перегруженный унарный оператор '-'

// Point operator-(void)

// {

// Point p;

// p.x = -x;

// p.y = -y;

// return p;

// }

//

// void Show(const char* objName)

// {

// cout << objName << ":" << endl;

// cout << "x=" << x << "; y=" << y << endl;

// cout << endl;

// }

//};

//

//void main()

//{

// // 1. Объявление объекта с использованием конструктора с 1 параметром

// // 1.1. Ошибка компилятора

// // Point p1(5.5); //конструктор с 1 параметром типа double запрещен

//

// // 1.2. Разрешено использовать конструктор с 1 параметром типа int

// Point p1(5);

// p1.Show("p1");

//

// // 1.3. Запрещено использовать метод SetX() с параметром типа double

// // p1.SetX(5.5);

// p1.SetY(3.8); // а метод SetY() можно использовать

// p1.Show("p1");

//

// // 2. Объявление объекта без параметров - вызывается default-конструктор

// Point p3; // вызывается default-конструктор компилятора

// p3.Show("p3");

//

// // Объявление объекта с помощью конструктора с 2 параметрами

// Point p4(7, 8);

// p4.Show("p4");

//}


// Перегрузка []

//class Box

//{

//private:

// int _arr[10] {};

//public:

// int& operator[](const int index)

// {

// return _arr[index];

// }

// const int& operator[](const int index) const

// {

// return _arr[index];

// }

// int& operator[](const string str)

// {

//

// }

//};

//

//void main()

//{

// /*Box a;

// a[2] = 7;

// cout << a[2];*/

//

// /*Box* a = new Box;

// (*a)[2];*/

//

// Box a;

// a["sdf"];

//}


// Перегрузка ()

// type operator()() {}

//class Matrix

//{

//private:

// double _data[5][5] {};

//public:

// double& operator()(int r, int c)

// {

// return _data[r][c];

// }

// const double& operator()(int r, int c) const

// {

// return _data[r][c];

// }

// void operator()()

// {

// // clear array

// }

//};

//

//void main()

//{

// Matrix m;

// m(2, 2) = 5;

// cout << m(2, 2);

// m();

//}

// функторы

//class Storage

//{

//private:

// int _val;

//public:

// Storage() : _val{} {}

// int& operator()(int i)

// {

// return _val += i;

// }

//};

//

//void main()

//{

// Storage a;

// cout << a(20) << endl;

// cout << a(30) << endl;

//}

//


// Перегрузка оператора приведения типа

//

//class Pocket

//{

//private:

// int _usd;

//public:

// Pocket(int usd) : _usd{ usd } {}

// int getUSD() { return _usd; }

// void setUSD(int usd) { _usd = usd; }

//

// operator int()

// {

// return _usd;

// }

//

// operator Cents() { return Cents(_usd * 100); }

//};

//

//class Cents

//{

//private:

// int _cents;

//public:

// Cents(int c = 0): _cents{c} {}

// operator Pocket()

// {

// return Pocket(_cents / 100);

// }

//};

//

//void main()

//{

// //Pocket p(100);

// //cout << (int)p; // cout << static_cast<int>(p)

//

// Cents c(1000);

// cout << (int)((Pocket)c);

//

//}

Так же в этом разделе:
 
MyTetra Share v.0.59
Яндекс индекс цитирования