Текст програми
#include<iostream>
#include<iomanip>
#include<conio.h>
using namespace std;
class mas
{
private:
int n;
double *a;
public:
mas(int);
mas(const mas &b);
~mas();
friend void operator >>(std::istream& in,mas& b);
friend void operator <<(std::ostream& out,mas& b);
mas operator +(mas);
mas &operator =(mas);
bool operator ==(mas);
mas operator *(double);
friend mas operator -=(mas&,double);
friend mas operator *(double,mas&);
};
mas::mas(const mas &b)
{
int i;
n=b.n;
a=new double[n];
for(i=0;i<n;i++)
a[i]=b.a[i];
}
mas operator *(double t,mas& b)
{
int i;
for(i=0;i<b.n;i++)
b.a[i]*=t;
return b;
}
mas operator -=(mas& b, double t)
{
int i;
for(i=0;i<b.n;i++)
b.a[i]-=t;
return b;
}
mas mas::operator *(double t)
{
int i;
for(i=0;i<n;i++)
a[i]=a[i]*t;
return *this;
}
bool mas::operator ==(mas b)
{
int i;
for(i=0;i<n;i++)
if(a[i]!=b.a[i])
return false;
return true;
}
mas &mas:: operator =(mas b)
{
int i;
for(i=0;i<n;i++)
a[i]=b.a[i];
return *this;
}
mas mas::operator +(mas b)
{
int i;
for(i=0;i<n;i++)
a[i]+=b.a[i];
return *this;
}
void operator <<(std::ostream& out,mas& b)
{
int i;
for(i=0;i<b.n;i++)
cout<<"A["<<i+1<<"]="<<b.a[i]<<endl;
i=0;
}
void operator >>(std::istream& in,mas& b)
{
int i;
for(i=0;i<b.n;i++)
{
cout<<"Zadaite "<<i+1<<"element"<<endl;
cin>>b.a[i];
}
}
mas::mas(int m)
{
int i;
n=m;
a=new double[n];
for(i=0;i<n;i++)
a[i]=0;
}
mas::~mas()
{
delete[] a;
}
void main(void)
{
int m;
cout<<"Zadaite rozmir masyva ";
cin>>m;
mas A(m),B(m),C(m),D(m),F(m),G(m);
cout<<"Masyv A"<<endl;
cin>>A;
cout<<"Masyv B"<<endl;
cin>>B;
if(A==B)
cout<<"Masyvy rivni"<<endl;
else
cout<<"Masyvy ne rivni"<<endl;
B+A;
C=A;
D=A;
G=A;
double f;
cout<<"Zadaite chuslo ";
cin>>f;
F=C*f;
D-=f;
f*G;
cout<<endl;
cout<<"A"<<endl;
cout<<A;
cout<<"B"<<endl;
cout<<B;
cout<<"C"<<endl;
cout<<C;
cout<<"D"<<endl;
cout<<D;
cout<<"F"<<endl;
cout<<F;
cout<<"G"<<endl;
cout<<G;
getch();
}Результат виконання програми
В результаті роботи програми на екран буде виведено
Висновок
На цій лабораторній роботі я глибше ознайомився з принципами об’єктно-орієнтованого програмування, зокрема поліморфізмом та інкапсуляцією, а також з перевантаженням операторів у вигляді як членів класу, так і дружніх функцій.
