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

Практичне заняття 8 Теоретична частина

Базується на матеріалі лекцій 17-19: успадковування

Завдання

Ввести тексти програм, які розглядалися у лекціях 17-19. Прокомпілювати, проаналізувати результат роботи. До тексту звіту включити лістінги програм та результати їх роботи.

#include <iostream.h>

#include <conio.h>

#include <bios.h>

class Counter

{protected:

unsigned int count;

public:

Counter():count(0) //constructor

{ }

Counter (int c):count(c)

{ }

unsigned int get_count()

{return count;}

Counter operator++ ()

{return Counter(++count);

}

};

class CountOn:public Counter //похідний клас

{public:

Counter operator--()

{return Counter(--count);

}

};

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

int main()

{

clrscr();

CountOn c1;

cout <<"\nc1="<<c1.get_count();

++c1;++c1;++c1;

cout <<"\nc1="<<c1.get_count();

--c1;--c1;

cout <<"\nc1="<<c1.get_count();

cout <<endl;

bioskey(0);

return 0;

}

Програма 17.1

#include <iostream.h>

#include <conio.h>

#include <bios.h>

class Counter

{protected:

unsigned int count;

public:

Counter():count(0) //конструктор

{ }

Counter (int c):count(с)

{ }

unsigned int get_count()

{return count;}

Counter operator++ ()

{return Counter(++count);

}

};

class CountOn:public Counter //похідний клас

{public:

CountOn():Counter()

{ }

CountOn(int c):Counter(с)

{ }

CountOn operator—()

{return CountOn(--count);

}

};

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

Int main()

{

clrscr();

CountOn c1;

CountOn c2(100);

cout <<”\nc1=”<<c1.get_count();

cout <<”\nc2=”<<c2.get_count();

++c1;++c1;++c1;

cout <<”\nc1=”<<c1.get_count();

--c2;--c2;

cout <<”\nc1=”<<c1.get_count();

CountOn c3=--c2;

cout<<”\nc3=”<<c3.get_count();

cout <<endl;

bioskey(0);

return 0;

}

Програма 17.2

#include <iostream.h>

#include <conio.h>

#include <bios.h>

#include<process.h>

class Stack

{protected:

enum{MAX=10};

int st[MAX];

int top;

public:

Stack()

{top=0;}

void push(int var)

{st[++top]=var;}

int pop()

{return st[top--];}

};

//////////

class Stack2:public Stack

{public:

Void push(int var)

{if (top>=MAX-1)

{cout<<"Стек повний";exit(1); }

Stack::push(var);

}

int pop()

{

if (top<=0)

{cout<<"\nСтек порожній\n"; bioskey(0);exit(1); };

return Stack::pop();

}

};

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

Int main()

{

clrscr();

Stack2 s1;

s1.push(11);

s1.push(22);

s1.push(33);

cout<<endl<<s1.pop();

cout<<endl<<s1.pop();

cout<<endl<<s1.pop();

cout<<endl<<s1.pop();

cout<<endl;

bioskey(0);

return 0;

}

Програма 17.4

#include <iostream.h>

#include <conio.h>

#include <bios.h>

enum posneg{pos,neg};

class Distance

{protected:

int feet;

float inches;

public:

Distance():feet(0),inches(0.0) //конструктор без аргументів

{ }

//конструктор з 2 аргументами

Distance(int ft,float in):feet(ft),inches(in)

{ }

void getdist()

{cout <<"\nВведіть число футів "; cin >>feet;

cout << "Дюймів "; cin>>inches;

}

void showdist()

{cout <<feet << "\' "<< inches <<"\''";}

};

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

class DistSign:public Distance

{

private:

posneg sign;

public:

DistSign():Distance()

{sign=pos;}

DistSign(int ft,float in,posneg sg=pos):Distance(ft,in)

{

sign=sg;

}

void getdist()

{ Distance::getdist();

char ch;

cout<<"Введіть + чи -";

ch=getch();

if (ch=='+')

sign=pos;

else

sign=neg;

}

void showdist() const

{cout<<((sign==pos)?"(+)":"(-)");

Distance::showdist();

}

};

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