
- •Практичне заняття 8 Теоретична частина
- •Завдання
- •Int main()
- •Void push(int var)
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Лабораторне заняття 9 Теоретична частина
- •Зміст завдання
- •Лабораторне заняття 10 Теоретична частина
- •Практичне заняття 9 Теоретична частина
- •Завдання
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Практичне заняття 10-11 Теоретична частина
- •Завдання
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Лабораторне заняття 11-12 Теоретична частина
- •Завдання 1
- •Завдання 2
- •Завдання 3
- •Int main()
- •Практичне заняття 12-13 Теоретична частина
- •Завдання
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Int main()
- •Лабораторне заняття 13 Теоретична частина
- •Завдання 1
- •Int main()
- •Завдання 2
- •Практичне заняття 14 Теоретична частина
- •Завдання
- •Int main()
- •Int main()
- •Лабораторне заняття 14 Теоретична частина
- •Завдання
Практичне заняття 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();
}
};
///////////////