哪位大哥帮我看看这个C++程序错在哪了,明天要交作业,要命啊
#include<iostream.h>classinteger{public:integer(){}integer(intnumber){num1=number;}pr...
#include<iostream.h>
class integer
{
public:
integer()
{}
integer(int number)
{
num1=number;
}
protected:
int num1;
};
class real
{
public:
real()
{}
real(double number)
{
num=number;
}
protected:
double num;
};
class intreal:public real,public integer
{
public:
intreal(){}
double num2;
intreal(int a)
{
num2=a;
}
intreal operator +(intreal b)
{
intreal c;
c.num2=num2+b.num2;
return c;
}
intreal operator -(intreal b)
{
intreal c;
c.num2=num2-b.num2;
return c;
}
intreal operator *(intreal b)
{
intreal c;
c.num2=num2*b.num2;
return c;
}
};
class Vector : public intreal
{
public:
void print()
{
cout<<"(";
for (int i=0;i<L;i++)
{
cout<<AA[i].num2;
cout<<",";
}
cout<<")"<<endl;
}
int L;
intreal *AA;
Vector(int w)
{
AA=new intreal[w];
L=w;
}
Vector operator +(Vector b)
{
Vector c(b.L);
for (int i=0;i<L;i++)
{
c.AA[i]=AA[i]+b.AA[i];
}
return c;
}
Vector operator -(Vector b)
{
Vector c(b.L);
for (int i=0;i<L;i++)
{
c.AA[i]=AA[i]-b.AA[i];
}
return c;
}
double operator *(Vector b)
{
double temp=0;
for (int i=0;i<L;i++)
{
temp+=(AA[i].num2*b.AA[i].num2);
}
return temp;
}
};
class matrix:public Vector
{
public:
int L2;
Vector *AA2;
matrix(int w)
{
AA2=new Vector[w];
L2=w;
}
matrix operator +(matrix b)
{
matrix c(b.L2);
for (int i=0;i<L2;i++)
{
for(int j=0;j<c.AA2[i].L;j++)
c.AA2[i].AA[j]=AA2[i].AA[j]+b.AA2[i].AA[j];
}
return c;
}
matrix operator -(matrix b)
{
matrix c(b.L2);
for (int i=0;i<L2;i++)
{
for(int j=0;j<c.AA2[i].L;j++)
c.AA2[i].AA[j]=AA2[i].AA[j]-b.AA2[i].AA[j];
}
return c;
}
void print()
{
for(int i=0;i<L2;i++)
{
for (int j=0;j<L;j++)
cout<<AA2[i].AA[j].num2<<'\t';
cout<<endl;
}
}
};
void main()
{
cout<<"输入维数1:";
int n1,n2;
cin>>n1;
cin>>n2;
matrix d1(n1),d2(n1),d(n1);
cout<<"输入向量1,以空格间隔";
for (int i=0;i<n1;i++)
{
for(int j=0;j<n2;j++)
cin>>d1.AA2[i].AA[j].num2;
}
cout<<"输入向量2,以空格间隔";
for ( i=0;i<n1;i++)
{
for(int j=0;j<n2;j++)
cin>>d2.AA2[i].AA[j].num2;
}
d=d1+d2;
cout<<"加法结果是";
d.print();
cout<<"减法结果是";
d=d1-d2;
d.print();
}
这个是用来矩阵运算的,matrix就是矩阵类,继承向量类,可是构造函数报错,怎么回事啊? 展开
class integer
{
public:
integer()
{}
integer(int number)
{
num1=number;
}
protected:
int num1;
};
class real
{
public:
real()
{}
real(double number)
{
num=number;
}
protected:
double num;
};
class intreal:public real,public integer
{
public:
intreal(){}
double num2;
intreal(int a)
{
num2=a;
}
intreal operator +(intreal b)
{
intreal c;
c.num2=num2+b.num2;
return c;
}
intreal operator -(intreal b)
{
intreal c;
c.num2=num2-b.num2;
return c;
}
intreal operator *(intreal b)
{
intreal c;
c.num2=num2*b.num2;
return c;
}
};
class Vector : public intreal
{
public:
void print()
{
cout<<"(";
for (int i=0;i<L;i++)
{
cout<<AA[i].num2;
cout<<",";
}
cout<<")"<<endl;
}
int L;
intreal *AA;
Vector(int w)
{
AA=new intreal[w];
L=w;
}
Vector operator +(Vector b)
{
Vector c(b.L);
for (int i=0;i<L;i++)
{
c.AA[i]=AA[i]+b.AA[i];
}
return c;
}
Vector operator -(Vector b)
{
Vector c(b.L);
for (int i=0;i<L;i++)
{
c.AA[i]=AA[i]-b.AA[i];
}
return c;
}
double operator *(Vector b)
{
double temp=0;
for (int i=0;i<L;i++)
{
temp+=(AA[i].num2*b.AA[i].num2);
}
return temp;
}
};
class matrix:public Vector
{
public:
int L2;
Vector *AA2;
matrix(int w)
{
AA2=new Vector[w];
L2=w;
}
matrix operator +(matrix b)
{
matrix c(b.L2);
for (int i=0;i<L2;i++)
{
for(int j=0;j<c.AA2[i].L;j++)
c.AA2[i].AA[j]=AA2[i].AA[j]+b.AA2[i].AA[j];
}
return c;
}
matrix operator -(matrix b)
{
matrix c(b.L2);
for (int i=0;i<L2;i++)
{
for(int j=0;j<c.AA2[i].L;j++)
c.AA2[i].AA[j]=AA2[i].AA[j]-b.AA2[i].AA[j];
}
return c;
}
void print()
{
for(int i=0;i<L2;i++)
{
for (int j=0;j<L;j++)
cout<<AA2[i].AA[j].num2<<'\t';
cout<<endl;
}
}
};
void main()
{
cout<<"输入维数1:";
int n1,n2;
cin>>n1;
cin>>n2;
matrix d1(n1),d2(n1),d(n1);
cout<<"输入向量1,以空格间隔";
for (int i=0;i<n1;i++)
{
for(int j=0;j<n2;j++)
cin>>d1.AA2[i].AA[j].num2;
}
cout<<"输入向量2,以空格间隔";
for ( i=0;i<n1;i++)
{
for(int j=0;j<n2;j++)
cin>>d2.AA2[i].AA[j].num2;
}
d=d1+d2;
cout<<"加法结果是";
d.print();
cout<<"减法结果是";
d=d1-d2;
d.print();
}
这个是用来矩阵运算的,matrix就是矩阵类,继承向量类,可是构造函数报错,怎么回事啊? 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询