Реализовал класс комплексных чисел ComNum. Переопределить операции сложения, вычитания и ввод/вывод в поток. Создал шаблон класса матриц.
#include<iostream>
#include<conio.h>
using namespace std;
template<class Neo> class Ma3X
{
public:
int a,b;
Neo **mas;
Ma3X(){}
Ma3X(int aa, int bb)
{
a=aa; b=bb;
Neo **mas=new Neo*(a);
for (int j=0;j<a;j++) mas[j]=new Neo[b];
}
void ToPrint()
{
for(int j=0;j<a;j++)
{
for(int i=0;i<b;i++) cout<<mas[i][j];
}
}
void ToFill()
{
for(int j=0;j<a;j++)
for(int i=0;i<b;i++) cin>>mas[i][j];
}
void ToAdd(Ma3X ma3x)
{
for(int j=0;j<a;j++)
for(int i=0;i<b;i++) mas[i][j]=mas[i][j]+ma3x.mas[i][j];
}
void ToSub(Ma3X ma3x)
{
for(int j=0;j<a;j++)
for(int i=0;i<b;i++) mas[i][j]=mas[i][j]-ma3x.mas[i][j];
}
};
class ComNum
{
friend ostream& operator << (ostream& os,ComNum& obj);
friend iostream& operator >> (istream& os,ComNum& obj);
public:
int re;
int im;
ComNum operator+ (ComNum oo);
ComNum operator- (ComNum oo);
ComNum()
{
re=im=0;
};
ComNum(int RE,int IM)
{ re=RE;
im=IM;
};
};
ComNum ComNum::operator+ (ComNum oo)
{
ComNum ExNum;
ExNum.re=re+oo.re;
ExNum.im=im+oo.im;
return ExNum;
}
ComNum ComNum::operator- (ComNum oo)
{
ComNum ExNum;
ExNum.re=re-oo.re;
ExNum.im=im-oo.im;
return ExNum;
}
ostream& operator << (ostream& os,ComNum& obj)
{
os<<"Re = "<<obj.re<<" "<<"Im = "<<obj.im<<endl;
}
iostream& operator >> (istream& os,ComNum& obj)
{ cout<<"Enter Re"<<endl;
os>>obj.re;
cout<<"Enter Im"<<endl;
os>>obj.im;
}
int main()
{
ComNum a(1,2);
ComNum b;
ComNum c;
cout<<"A: "<<a;
cout<<"B: "<<b;
cin>>b;
cout<<"B: "<<b;
c=a+b;
cout<<"C: "<<c;
Ma3X <int> mint(3,3);
Ma3X <float> mflo(4,4);
Ma3X <ComNum> mcom(2,2);
mint.ToFill();
mint.ToPrint();
getch();
}
template<class Neo> class Ma3X {И не путайся в индексах! Первый индекс - номер строки, второй - номер столбца!
public:
int a,b;
Neo **mas;
Ma3X() {
}
Ma3X(int aa, int bb) {
a=aa; b=bb;
// Neo **mas=new Neo*[a];
// Не создаем новую переменную, а используем поле класса
mas = new Neo*[a];
for(int i=0;i<a;i++) { // И не путайся в индексах!!!
mas[i]=new Neo[b];
}
}
void ToPrint() {
for(int i=0;i<a;i++) {
for(int j=0;j<b;j++) cout<<mas[i][j];
cout << endl;
}
}
void ToFill() {
for(int i=0;i<a;i++) {
for(int j=0;j<b;j++) {
cin>>mas[i][j];
}
}
}
void ToAdd(Ma3X ma3x) {
for(int i=0;i<a;i++) {
for(int j=0;j<b;j++) mas[i][j]=mas[i][j]+ma3x.mas[i][j];
}
}
void ToSub(Ma3X ma3x) {
for(int i=0;i<a;i++) {
for(int j=0;j<b;j++) mas[i][j]=mas[i][j]-ma3x.mas[i][j];
}
}
};
А вот ещё: реализовал шаблон класса стек, в котором размер стека задается параметром шаблона.
#include<iostream>
#include<conio.h>
using namespace std;
template<class Sx> class StackX
{
public:
int size;
int ct;
struct St
{
Sx data;
St *next;
};
St *top;
StackX(int sz)
{
size=sz;
top=NULL;
}
int Empty()
{
if(top==NULL) return 1;
else return 0;
}
void ToPrint()
{
St *temp;
temp=top;
while(temp!=NULL)
{
cout<<temp->data<<endl;
temp=temp->next;
}
}
void ToFill(Sx VAL)
{
Sx *temp;
if (Empty())
{
top=new St;ct=0;
top->data=VAL;
top->next=NULL;
ct++;
}
else
{
if (ct>=size)
{
cout<<"Stack is FULL!"<<endl;
return;
}
temp=top;
top=new St;
ct++;
top->data=VAL;
top->next=temp;
}
}
Sx ToDelete()
{
Sx *temp;
int oo;
if(Empty())
{
cout<<"Stack is EMPTY!!!"<<endl;
return 0;
}
else
{
temp=top;
oo=top->data;
top=top->next;
delete temp;
ct--;
return oo;
}
}
};
int main()
{
StackX<int> stack1(5);
stack1.ToFill(1);
stack1.ToFill(10);
stack1.ToPrint();
getch();
}
void ToFill(Sx VAL)
{
// Sx *temp;
St *temp; // Вот теперь должно компилироваться