1.#include <iostream>
using namespace std;
class Complex{
public:
Complex()
{
real=0;
imag=0;
}
Complex(double r,double i)
{
real=r;
imag=i;
}
double getreal()
{
return real;
}
double getimag()
{
return imag;
}
void display()
{
cout<<"("<<real<<"+"<<imag<<"i)"<<endl;
}
private:
double real;
double imag;
};
Complex operator +(Complex &c1,Complex &c2)
{
return Complex(c1.getreal()+c2.getreal(),c1.getimag()+c2.getimag());
}
int main()
{
Complex c1(1,2),c2(3,4),c3;
c3=c1+c2;
cout<<"c3=";
c3.display();
return 0;
}
[html] view plaincopy
2. #include <iostream>
using namespace std;
class Complex{
public:
Complex()
{
real=0;
imag=0;
}
Complex(double r,double i)
{
real=r;
imag=i;
}
Complex operator +(Complex &);
Complex operator -(Complex &);
Complex operator *(Complex &);
Complex operator /(Complex &);
void display()
{
cout<<"("<<real<<"+"<<imag<<"i)"<<endl;
}
private:
double real;
double imag;
};
Complex Complex::operator +(Complex &c2)
{
return Complex(real+c2.real,imag+c2.imag);
}
Complex Complex::operator -(Complex &c2)
{
return Complex(real-c2.real,imag-c2.imag);
}
Complex Complex::operator *(Complex &c2)
{
return Complex(real*c2.real,imag*c2.imag);
}
Complex Complex::operator /(Complex &c2)
{
return Complex(real/c2.real,imag/c2.imag);
}
int main()
{
Complex c1(2,4),c2(1,2),c3,c4,c5,c6;
c3=c1+c2;
cout<<"c3=c1+c2=";
c3.display();
c4=c1-c2;
cout<<"c4=c1-c2=";
c4.display();
c5=c1*c2;
cout<<"c5=c1*c2=";
c5.display();
c6=c1/c2;
cout<<"c6=c1/c2=";
c6.display();
return 0;
}
[html] view plaincopy
3.#include <iostream.h>
class Complex{
public:
Complex()
{
real=0;
imag=0;
}
Complex(double r,double i)
{
real=r;
imag=i;
}
Complex operator +(Complex &);
Complex operator +(int &);
friend Complex operator +(int &,Complex &);
void display()
{
cout<<"("<<real<<"+"<<imag<<"i)"<<endl;
}
private:
double real;
double imag;
};
Complex Complex::operator +(Complex &c2)
{
return Complex(real+c2.real,imag+c2.imag);
}
Complex Complex::operator +(int &i)
{
return Complex(real+i,imag);
}
Complex operator +(int &i,Complex &c3)
{
return Complex(i+c3.real,c3.imag);
}
int main()
{
int i=2;
Complex c1(2,4),c2(1,2),c3,c4,c5;
c3=c1+c2;
cout<<"c3=c1+c2=";
c3.display();
c4=c1+i;
cout<<"c4=c1+i=";
c4.display();
c5=i+c2;
cout<<"c5=i+c2=";
c5.display();
return 0;
}
[html] view plaincopy
4. #include <iostream.h>
class Matrix{
public:
Matrix();
friend Matrix operator +(Matrix &,Matrix &);
void input();
void display();
private:
int mat[2][3];
};
Matrix::Matrix()
{
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
mat[i][j]=0;
}
Matrix operator +(Matrix &a,Matrix &b)
{
Matrix c;
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
c.mat[i][j]=a.mat[i][j]+b.mat[i][j];
return c;
}
void Matrix::input()
{
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
cin>>mat[i][j];
}
void Matrix::display()
{
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
{cout<<mat[i][j]<<" ";
cout<<endl;}
}
int main()
{
Matrix a,b,c;
a.input();
b.input();
c=a+b;
c.display();
return 0;
}
[html] view plaincopy
5. #include <iostream.h>
class Matrix{
public:
Matrix();
friend Matrix operator +(Matrix &,Matrix &);
friend istream& operator >>(istream &,Matrix &);
friend ostream& operator <<(ostream &,Matrix &);
private:
int mat[2][3];
};
Matrix::Matrix()
{
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
mat[i][j]=0;
}
Matrix operator +(Matrix &a,Matrix &b)
{
Matrix c;
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
c.mat[i][j]=a.mat[i][j]+b.mat[i][j];
return c;
}
istream & operator >>(istream & in,Matrix &c)
{
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
in>>c.mat[i][j];
return in;
}
ostream & operator <<(ostream & out,Matrix &c)
{
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
{
out<<c.mat[i][j]<<" ";
cout<<endl;
}
return out;
}
int main()
{
Matrix a,b,c;
cin>>a;
cout<<a;
cin>>b;
cout<<b;
c=a+b;
cout<<c;
return 0;
}
[html] view plaincopy
6. #include <iostream>
using namespace std;
class Complex{
public:
Complex()
{
real=0;
imag=0;
}
Complex(double r)
{
real=r;
imag=0;
}
Complex(double r,double i)
{
real=r;
imag=i;
}
operator double()
{
return real;
}
void display()
{
cout<<"("<<real<<"+"<<imag<<"i)"<<endl;
}
private:
double real;
double imag;
};
int main()
{
int i=2;
Complex c1(2,4),c2;
double d1;
d1=2.5+c1;
cout<<"d1="<<d1<<endl;
c2=Complex(d1);
cout<<"c2="<<c2.display<<endl;
return 0;
}
[html] view plaincopy
7. #include <iostream>
using namespace std;
#include <cstring>
class Student{
public:
Student(int,char[],char,float);
int get_num()
{
return num;
}
char * get_name()
{
return name;
}
char get_sex()
{
return sex;
}
void display()
{
cout<<"num="<<num<<endl;
cout<<"name="<<name<<endl;
cout<<"sex="<<sex<<endl;
cout<<"score="<<score<<endl;
}
private:
int num;
char name[20];
char sex;
float score;
};
Student::Student(int n,char nam[],char s,float so)
{
num=n;
strcpy(name,nam);
sex=s;
score=so;
}
class Teacher{
public:
Teacher(){}
Teacher(Student &stud)
{
num=stud.get_num();
strcpy(name,stud.get_name());
sex=stud.get_sex();
pay=3000;
}
Teacher(int n,char nam[],char s,float p)
{
num=n;
strcpy(name,nam);
sex=s;
pay=p;
}
void display()
{
cout<<"num="<<num<<endl;
cout<<"name="<<name<<endl;
cout<<"sex="<<sex<<endl;
cout<<"pay="<<pay<<endl;
}
private:
int num;
char name[20];
char sex;
float pay;
};
//Teacher::Teacher(int n,char nam[],char s,float p){ num=n; strcpy(name,nam); sex=s; pay=p;}
//Teacher::Teacher(Student &stud){num=stud.get_num();strcpy(name,stud.get_name());sex=stud.get_sex();pay=3000;}
int main()
{
Teacher t1;
Student s1(1,"xian",'F',100);
cout<<"s1:"<<endl;
s1.display();
t1=Teacher(s1);
cout<<"t1:"<<endl;
t1.display();
return 0;
}
第四章
最新推荐文章于 2023-09-12 15:34:15 发布