Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Titulnyy_list_otcheta_C++.doc
Скачиваний:
0
Добавлен:
01.03.2025
Размер:
306.18 Кб
Скачать

6.1 Ассоциация 1:1

1 : 1

// lab3.cpp: определяет точку входа для консольного приложения.

#include "stdafx.h"

#include "conio.h"

#include <iostream>

#include <locale>

using namespace std;

class F;

class E {

F *f;//private

int e;

public:

E() {e = 6; cout << "E e = " << e << endl;}

void link(F &f) {this->f = &f; }

void M() { cout << "class E e\n" ; }

F* getF() { return f; }

};

class F {

E* e;//private

int f;

public:

F() {f = 66; cout << "F f = " << f << endl;}

void link(E &e) {this->e = &e; }

void M() { cout << "class F f\n"; }

E* getE() { return e; }

};

Int main()

{

setlocale (LC_ALL,"Russian");

E e;

F f;

e.link(f);

f.link(e);

e.getF()->M();

f.getE()->M();

cout << "class E e\a" << endl;

cout << "class F f\a" << endl;

_getche();

return 0;

}

6.2 Ассоциация 1:n

1 : N

// lab3_1-n.cpp Ассоциация 1-N

#include "stdafx.h"

#include <iostream>

using namespace std;

const int N = 5;

class E;

class D {

public:

D() {epp = &ep[0]; i = -1; j = -1;}

int i, j;

void link(E &e) {

*epp = &e;

i++;

cout << "link D -> E[" << i << "]; pointer = " << *epp << endl;

if ( epp == &ep[N-1] ) {

epp = &ep[0];

} else {

epp++;

}

}

void M(){cout << "class D\n";}

E* getNext() {

if (j < i) {

j++;

return ep[j];

}

j = 0;

return ep[j];

}

protected:

E **epp;

private:

E *ep[N];

};

class E {

public:

E() {}

void link(D &d) {

cout << "link E -> D\n";

this->d = &d;

}

void M() {cout << "class E\n";}

D* getD() {return d;}

protected:

D* d;

};

int _tmain(int argc, _TCHAR* argv[])

{

D d; E e[N]; int i;

for (i = 0; i < N; i++) {

e[i].link(d);

d.link(e[i]);

}

d.getNext()->M();

d.getNext()->M();

d.getNext()->M();

return 0;

}

7.1 Использование: клиент-сервер

use

// lab4.cpp: Использование: клиент-сервер

#include "stdafx.h"

#include <iostream>

using namespace std;

class D { //сервер

int d;

public:

D():d(5) {}

int getD() {return d;}

};

class E { //клиент

int e;

public:

E():e(2) {}

void me(D &d) {e += d.getD();} //передача для метода me объекта класса D

int getE() {return e;}

};

int _tmain(int argc, _TCHAR* argv[])

{

D d; E e;

cout << "d = " << d.getD() << endl;

cout << "e = " << e.getE() << endl;

e.me(d); // категория клиент-сервер между объектами

cout << "======================\n";

cout << "d = " << d.getD() << endl;

cout << "e = " << e.getE() << endl;

return 0;

7.2 Использование: общность

D

public

use

Private f, E

protected

// lab4_obsh.cpp: Использование: общность

#include "stdafx.h"

#include <iostream>

using namespace std;

class D {

int d;

friend class E;//дружественный класс для Е, можно просмотреть элементы класса D

public:

D():d(100) {}

int getd() {return d;}

};

class E {

int e;

public:

E(D *dp) :e(dp->d) {}

int getE() {return e;}

};

int _tmain(int argc, _TCHAR* argv[])

{

D *dp = NULL;

dp = new D;

E e(dp);

cout << e.getE() << endl;

return 0;

}

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]