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

4.2 Наследование: комбинирование(граф изменём)

Конструирование

Специализация

Спецификация

Расширение

Конструирование

protected

// lab2_comb.cpp:

#include "stdafx.h"

#include "conio.h"

#include <iostream>

#include <locale>

using namespace std;

class E {

protected:

int e;

public:

E():e(2) {}

int fe() {return e;}

};

class F {

protected:

int f;

public:

F():f(3) {}

int ff() {return f;}

};

class C:public E, protected F {

protected:

int c; // расширение по данным (для всех)

public:

C():c(5) {}

int fc() {return f*e;} // конструирование для (B->E и B->D)

};

class D {

protected:

int d;

public:

D():d(4) {}

virtual int fd() {return d;}

};

class B:public D {

protected:

int b;

public:

int fd() {return d*d;} // специализация (для C->F)

};

class J {

public:

virtual int fj() = 0;

};

class A: public J, public B, private C {

protected:

int a;

public:

int fd() {return d*d*d;} // специализация (для A->B)

// конструирование (для A->C и A->E)

int fj() {return a*fc()*B::fd();} // спецификация (для A->E)

// конструирование (для A->C и A->E)

// расширение по методам (для A->B)

};

int main(int argc, char* argv[])

{

setlocale (LC_ALL,"Russian");

//принцип подстановки выполняется для B->D:

D *d = NULL;

cout << "Принцип подстановки для B->D:\n";

d = new D;

cout << d->fd() << endl;

d = new B;

cout << d->fd() << endl;

//принцип подстановки частично выполняется для A->B:

B* b = NULL;

cout << "Принцип подстановки для A->B:\n";

b = new B;

cout << b->fd() << endl;

b = new A;

cout << b->fd() << endl;

// по остальным веткам принцип подстановки не выполняется

_getche();

return 0;

}

5. Наследование: комбинирование через общих предков

// lab2_commnasl.cpp:

#include "stdafx.h"

#include <iostream>

using namespace std;

class A {

public:

A() {a = 0;cout << "A() a = " << a << endl;}

A(int a) {this->a = a; cout << "A(int a) a = " << a << endl;}

virtual ~A() {}

virtual int fa() {return a;}

private:

int a;

};

class C:public virtual A {

public:

C() {c = 2; cout << "C() c = " << c << endl;}

C(int a):A(a) {c = 2; cout << "C(int a) c = " << c << endl;}

int fc() {return c;}

int fC() {return c + A::fa();}

private:

int c;

};

class B:virtual A {

public:

B() {b = 1; cout << "B() b = " << b << endl;}

B(int a):A(a) {b = 1; cout << "B(int a) b = " << b << endl;}

int fb() {return b;}

int fB() {return b + A::fa();}

private:

int b;

};

class E:public B,public C {

public:

E():C(),B() {e = 6; cout << "E() e = " << e << endl;}

E(int a):C(),A(a) {e = 6; cout << "E(int a0) e = " << e << endl;}

E(int a0, int a1):B(),A(a0+a1),C() {e = 6; cout << "E(int a0, int a1) e = " << e << endl;}

int fa() {return A::fa()+1000;}

int fE() {return fb()+fc()+A::fa()+e;}

private:

int e;

};

Int main()

{

A *a = NULL;

a = new A;

cout << "A::fa = " << a->fa() << endl;

delete a;

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

a = new E(10,5);

cout << "A::fa() = " << a->A::fa() << endl;

cout << "E::fa() = " << a->fa() << endl;

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

delete a;

E e;

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

return EXIT_SUCCESS;

}

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