MyTetra Share
Делитесь знаниями!
Перегрузка ++, --, ==, !=, >,<,>=,<=, Перегрузка присваивания копированием
Время создания: 13.06.2020 18:50
Раздел: C++ - Примеры кода - Работа на уроке
Запись: Shut913/Tetra-notes-Programming/master/base/1592063436scrnef51l0/text.html на raw.githubusercontent.com

#include <iostream>

#include <string>

using namespace std;


// Перегрузка ++ --

// ++a type operator++()

// a++ type operator++(int)

//

//class SomeClass

//{

// // операторная функция, которая перегружает префиксный оператор ++ (++X)

// type operator++()

// {

// // ...

// }

//

// // операторная функция, которая перегружает постфиксный оператор ++ (X++)

// type operator++(int)

// {

// // ...

// }

//

// // операторная функция, которая перегружает префиксный оператор -- (--X)

// type operator--()

// {

// // ...

// }

//

// // операторная функция, которая перегружает постфиксный оператор -- (X--)

// type operator--(int)

// {

// // ...

// }

//};)


//class Box

//{

//private:

// int _val;

//public:

// explicit Box(int val = 0) : _val{ val } {}

// Box& operator++();

// Box& operator--();

// Box operator++(int);

// Box operator--(int);

// void show();

//};

//

//Box& Box::operator++()

//{

// this->_val++;

//

// return *this;

//}

//

//Box& Box::operator--()

//{

// this->_val--;

//

// return *this;

//}

//

//Box Box::operator++(int)

//{

// Box tmp(this->_val);

// //++(*this);

// this->_val++;

// return tmp;

//}

//

//Box Box::operator--(int)

//{

// Box tmp(this->_val);

// --(*this);

// return tmp;

//}

//

//void Box::show()

//{

// cout << "val = " << _val <<endl;

//}

//

//

//void main()

//{

// Box a(7);

// /*(++(++a)).show();

// (--a).show();*/

//

// (a++).show(); // a.operator++(int)

// a.show();

// ++a;

//}


//class Block

//{

//private:

// int _arr[10];

//public:

// Block() : _arr{} {}

//

// Block& operator++();

// Block& operator--();

// Block operator++(int);

// Block operator--(int);

//

// void show();

//};

//

//Block& Block::operator++()

//{

// for (int i{}; i < 10; i++)

// _arr[i]++;

//

// return *this;

//}

//

//Block& Block::operator--()

//{

// for (int i{}; i < 10; i++)

// _arr[i]--;

//

// return *this;

//}

//

//Block Block::operator++(int)

//{

// Block tmp = *this;

// for (int i{}; i < 10; i++)

// _arr[i]++;

//

// return tmp;

//}

//

//Block Block::operator--(int)

//{

// Block tmp = *this;

// for (int i{}; i < 10; i++)

// _arr[i]--;

//

// return tmp;

//}

//

//void Block::show()

//{

// for (int i{}; i < 10; i++)

// cout << _arr[i] << " ";

// cout << endl;

//}

//

//void main()

//{

// Block a;

// (++a).show();

// (a++).show();

//}


// Перегрузка == !=

//class Car

//{

//private:

// string _model;

// string _company;

//public:

// Car(string m, string c): _model{m}, _company{c} {}

//

// bool operator==(const Car& obj) const;

// bool operator!=(const Car& obj) const;

//};

//

//bool Car::operator==(const Car& obj) const

//{

// return this->_model == obj._model && this->_company == obj._company;

//}

//

//bool Car::operator!=(const Car& obj) const

//{

// return !(*this == obj);

//}

//

//void main()

//{

// Car a("Mustang", "Ford");

// Car b("e200", "Mercedes");

//

// cout << (a != b);

//}


// Перегрузка > < >= <=

//class Money

//{

//private:

// int _val;

//public:

// explicit Money(int val = 0): _val{val} {}

//

// bool operator<(const Money& obj) const;

// bool operator<=(const Money& obj) const;

// bool operator>(const Money& obj) const;

// bool operator>=(const Money& obj) const;

//};

//

//bool Money::operator<(const Money& obj) const

//{

// return this->_val < obj._val;

//}

//

//bool Money::operator<=(const Money& obj) const

//{

// return this->_val <= obj._val;

//}

//

//bool Money::operator>(const Money& obj) const

//{

// return this->_val > obj._val;

//}

//

//bool Money::operator>=(const Money& obj) const

//{

// return this->_val >= obj._val;

//}

//

//void main()

//{

// Money a(7);

// Money b(10);

// cout << (a < b);

//}


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

//class Fraction

//{

//private:

// int _num;

// int _denom;

//public:

// Fraction(int num = 0, int denom = 1): _num{num}, _denom{denom} {}

//

// Fraction(const Fraction& obj)

// {

// cout << "copy constructor" << endl;

// }

//

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

// {

// if (this == &obj)

// return *this;

//

// this->_num = obj._num;

// this->_denom = obj._denom;

//

// return *this;

// }

//

// Fraction& operator=(int a)

// {

// this->_num = a;

//

// return *this;

// }

//

// void show()

// {

// cout << _num << "/" << _denom << endl;

// }

//};

//

//void main()

//{

// /*Fraction a(3, 7);

// Fraction c(12, 13);

// Fraction b(a);

// b = a = c;

//

// b.show();

//

// Fraction x(1, 2);

// x = x;*/

//

// /*Fraction a(3, 7);

// a.show();

// string b = "76";*/

//

// const int& a = 7;

//

//}


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