MyTetra Share
Делитесь знаниями!
Константная ссылка, Константный метод
Время создания: 13.06.2020 18:38
Раздел: C++ - Примеры кода - Работа на уроке
Запись: Shut913/Tetra-notes-Programming/master/base/1592062715sweyrd62az/text.html на raw.githubusercontent.com

//#include <iostream>

//

//using namespace std;

//

//class Point

//{

//private:

// int* _x;

// int* _y;

//public:

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

// {

// cout << "default constructor";

// }

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

// {

// cout << "constructor with 2 params";

// }

// Point(const Point& obj)

// {

// _x = new int{ *(obj._x) };

// _y = new int{ *(obj._y) };

// cout << "copy constructor";

// }

//

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

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

//

// ~Point()

// {

// delete _x;

// delete _y;

// }

//};

//

//double length(Point p) // Point p = obj

//{

// return sqrt(pow(p.getX(), 2) + pow(p.getY(), 2));

//}

//

//Point getCenter(Point p)

//{

// int x = p.getX() / 2;

// int y = p.getY() / 2;

//

// // конструктор копирования не вызывается

// return Point(x, y);

//

// // конструктор копирования вызывается

// Point res(x, y);

// return res;

//}

//

//void main()

//{

// Point p(3, 2);

// cout << endl << length(p);

//

// Point p(Point(5, 6)); // конструктор копирования не вызывается (элизия)

//}

//

//

//// константная ссылка

//// void boo(const int& y) // y - это константная ссылка

//// {

//// y = 8; // ошибка компилятора: константная ссылка не может изменить своё же значение!

//// }


///////////////////////////////////////



#include <iostream>


using namespace std;


// const

// const User u;

// const User u(...);


//const returned_type FunName(parameters)

//{

// // function's body

// // ...

//}

//

// константный метод (метод с константным указателем this)

//class CMyClass

//{

//private:

// // скрытые члены данных и методы класса

//

//public:

// // функция объявлена с константным указателем this

// return_type MyFun1(parameters) const; // out-of-line

// return_type MyFun2(parameters) const // inline реализация

// {

// // тело функции

// }

//}

//

//// реализация out-of-line функции

//return_type CMyClass::MyFun1(parameters) const

//{

// // тело функции MyFun1()

//}


//class Box

//{

//private:

// double _x;

//public:

// Box() : _x{ 0.0 } {}

//

// void setX(double x) { _x = x; }

//

// double getX() // this ==> Box* const this

// {

// return this->_x;

// }

//

// double get() const // this ==> const Box* const this

// {

// this->_x = 4;

// }

//

//

//};

//

//void main()

//{

// Box a;

// cout << &a << endl;

// a.getX();

//}


//class Date

//{

//private:

// int _d, _m, _y;

//public:

// Date(int d, int m, int y) : _d{ d }, _m{ m }, _y{ y } {}

//

// int getD() const { return this->_d; }

// int getM() const { return this->_m; }

//

//

// int getY() { return this->_y; }

// const int getY() const { return this->_y; }

//};

//

//void print(const Date& date)

//{

// cout << date.getD() << "." << date.getM() << "." << date.getY() << endl;

//}

//

//void main()

//{

// Date d(23, 4, 2020);

// print(d);

//}


// explicit



class Fraction

{

private:

int _num;

int _denom;


public:

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


int getNum() { return _num; }

void setNum(int num) { _num = num; }


void show()

{

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

}

};


Fraction negative(Fraction f)

{

f.setNum(-f.getNum());

return f;

}


void main()

{

short a = 12;

Fraction f(a);

// Fraction f = negative(a);

f.show();

}


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