Программирование >>  Расширенная версия языка c++ 

1 ... 180 181 182 [ 183 ] 184 185 186 ... 227


Приложение веты на вопросы и решение ажиений 555

I/ Перегрузка оператора = для класса dynarray dynarray sdynarray;:operator={dynarray Sob) {

int i;

if(size!=ob.size] {

cou Нельзя овать массивы разных размеров ! \п ; exit(l);

for(i=0; i<size +) p[i] =ob.p[iJ;

return

Перегрузка оператора []

int i)

if (i<0 I I i>size) {

cou начение индекса ;

cou i выходит за границы массиваХп ;

exit{l);

return p[i] ;

int i ;

dynarray obi(10), ob2{10), ob3(100);

obl[3] = 10;

i = obl[3];

cout i \n ;

ob2 = obi;

cout i \n ;

Выполнение слеющей инструкции ведет к ошибке

= присваивание массивов разных размеров

return 0;

Проверка усвоения материала главы 6

1. Перегрузка операторов << и



556 . Самоучитель .. C++

ftinclude <iostream>

using namespace std; coord I

int x, у; значения координат public: . - .

coord () I x - 0; у 0; }

coord (inti, int j) { x = i; у = j; }

void get xy(int Si, int &j) { i = x; j = y; )

coord operator (int i) ;

coord operators (int i);

Перегрузка оператора

coord coord: :operator (int i)

coord temp;

= x i; = у i;

return temp;

Перегрузка оператора coor x.:..,: . operator (int i)

coord temp;

temp.x = >> i;

у i;

return temp;

int main

coord ol (4, 4) , o2; * .

int x, y;

2; int

o2.get xy (x, y) ;

cout (ol 2) у \n ;

o2 = 2; int

o2.get xy(X,y! ;

cou (o> 2) , у \n ;

return 0;



Приложение В. Ответы на вопросы и решения упражнений 557

2. tinclude <iostream> using namespace std,*

class th3:ee d {

int x, y, z; public:

three d(int i, in , int k)

x y = j; z=k;

three d() {x=0; y=0; z = 0; } voidgetdnt &i, int И, int Sk) i

i = x; j = y; k = z;

three d operator+ (three d ob2) ;

three d operator- (three dob2) ; three d operator++() ; three doperator-! ) ;

three rator+;tbree dob2)

three d temp;

temp.x = x + ob2.x; -

temp.у = у + оЬ2.у;

return temp;

three d three d::operator- (three dob2)

three d temp;

temp.x = x - ob2.x; temp, у = у - оЬ2.у; temp,г = z - ob2.z;

return temp;

three d three d::operator++{

Z++;

return



1 ... 180 181 182 [ 183 ] 184 185 186 ... 227

© 2006 - 2025 pmbk.ru. Генерация страницы: 0
При копировании материалов приветствуются ссылки.
Яндекс.Метрика