C++第四章
来源:互联网 发布:汉客拉杆箱怎么样知乎 编辑:程序博客网 时间:2024/06/05 21:50
- (1)#include<iostream>
- using namespace std;
- class complex{
- public:
- complex(){real=0;imag=0;}
- complex(double r,double i){real=r;imag=i;}
- void display();
- double real;
- double imag;
- };
- complex operator +(complex &c1,complex &c2)
- {
- complex c;
- c.real=c1.real+c2.real;
- c.imag=c1.imag+c2.imag;
- return c;
- }
- void complex::display()
- {
- cout<<"("<<real<<","<<imag<<"i)"<<endl;
- }
- int main()
- {
- complex c1(3,4),c2(1,3),c;
- operator +(c1,c2);
- c=c1+c2;
- c.display();
- return 0;
- }
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 &c2);
complex operator -(complex &c2);
complex operator *(complex &c2);
complex operator /(complex &c2);
void display();
private:
double real;
double imag;
};
complex complex:: operator +(complex &c2)
{
complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
complex complex::operator -(complex &c2)
{
complex c;
c.real=real-c2.real;
c.imag=imag-c2.imag;
return c;
}
complex complex::operator *(complex &c2)
{
complex c;
c.real=real*c2.real;
c.imag=imag*c2.imag;
return c;
}
complex complex::operator /(complex &c2)
{
complex c;
c.real=real/c2.real;
c.imag=imag/c2.imag;
return c;
}
void complex::display()
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main()
{
complex c1(3,4),c2(1,3),c;
c=c1+c2;
c.display();
c=c1-c2;
c.display();
c=c1*c2;
c.display();
c=c1/c2;
c.display();
return 0;
}
(3)#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;}
friend complex operator +(complex c1,complex c2);
//operator double(){return real;}
void display();
private:
double real;
double imag;
};
complex operator +(complex c1,complex c2)
{
complex c;
c.real=c1.real+c2.real;
c.imag=c1.imag+c2.imag;
return c;
}
void complex::display()
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main()
{
complex c1(3,4),c2(1,3),c;
double i=2.5;
c=c1+c2;
c.display();
c=c1+i;
c.display();
c=i+c1;
c.display();
}
(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;
}
(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;
}
(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;}
//friend complex operator +(complex c1,complex c2);
operator double(){return real;}
void display();
double real;
double imag;
};
void complex::display()
{
cout<<"("<<real<<","<<imag<<"i)"<<endl;
}
int main()
{
complex c1(3,4),c;double dl;
double i=2.5;
dl=c1+i;
cout<<dl<<endl;
c.real=dl;
c.imag=c1.imag;
c.display();
}
(7)
#include<iostream>
#include<string>
#include<cstring>
using namespace std;
class Student
{
public:
int num;
char name[10];
char sex;
Student(int n, char nam[],char s):num(n),sex(s){
strncpy(name, nam,10);
}
void display();
};
class Teacher
{
public:
Teacher(Student &s):num(s.num),sex(s.sex)
{
//num=s.num;
strcpy(name,s.name);
//sex=s.sex;
}
void display();
private:
int num;
char name[10];
char sex;
};
void Student::display()
{
cout<<"nun:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
void Teacher::display()
{
cout<<"nun:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
int main()
{
Student s(10,"zhangsan",'m') ;
s.display();
Teacher t1(s);
t1.display();
return 0;
}
- 第四章 C语言程序设计
- C++Primer第四章
- C++primer 第四章
- 【C】第四章习题
- C++primer plus第四章
- C Primer plus 第四章
- Objective-C 程序设计 第四章
- C语言第四章:函数
- c++primer第四章 表达式
- C Primer Plus 第四章
- C++PRIMER第四章练习
- C++Primer习题第四章
- 【C语言基础】C语言第四章
- C++Primer (第四版) 第四章 数组和指针
- C++primer第四版第四章数组与指针
- Pointers on C(第三章 第四章)
- Visual C++.NET第四章开始
- (C语言教程)第四章: 数组
- $.ajax 异常信息处理
- ubifs mount 函数分析
- 基于DHT 的几种P2P 算法研究
- hibernate关系映射总结2
- 区分Java中的 extends 和 implements
- C++第四章
- gradle build https.....不能download gradle解决办法
- html5适应屏幕的方案
- accumulateWeighted函数简介
- poj 2486
- Have meal
- 导航栏设置透明
- Spring工作原理探秘
- Nginx系列(十四. nginx分离静态文件)