c++第四次作业
2015-06-02 18:54
567 查看
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; }
相关文章推荐
- 为什么使用C++
- c++ static 类成员
- c++第一次作业
- Tiny4412之C语言实现流水灯,Tiny4412裸机程序[3]
- vs中c++的反汇编分析
- iOS软件开发应用C语言之变量、运算符
- C++中的new 和delete 运算的使用
- 在C++中为数组动态分配内存
- 矩阵C++实现
- extern type variable name问题及解决方案
- zz[C++]合理的设计和使用消息队列
- 如何在eclipse上添加c语言工程头文件
- c语言模块化编程
- yate学习--yate的认证方式--从文件认证(regfile.cpp)
- c语言使用zlib实现文本字符的gzip压缩与gzip解压缩
- C++:按值传递的还有地址
- 64位C++编程课堂
- C++实现简单的String类
- VTK5.10.1+Cmake+vs2010整合安装
- c语言 指向函数的指针作函数参数