实验八

来源:网络收集 时间:2025-08-05 下载这篇文档 手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xuecool-com或QQ:370150219 处理(尽可能给您提供完整文档),感谢您的支持与谅解。点击这里给我发消息

实验八 友元函数和友元类

, 一,实验目的

1. 熟悉类和模板类的使用。

2. 掌握友元函数和友元类的声明格式。

3. 了解友元函数和友元类的作用,使用友元的利弊。

4. 掌握静态数据成员和函数成员的使用方法,它们同一般成员的差异。 5. 掌握栈和队列的运算特点和有关算法。 二、实验内容

1. 定义复数类Complex,使用友元,完成复数的加法、减法、乘法、除法运算,以及

对复数的输出。

# include class complex {

float real,imag; public:

complex(float r=0,float i=0) {real=r;imag=i;} void print();

friend complex operator+(complex a,complex b); friend complex operator-(complex a,complex b); friend complex operator*(complex a,complex b); friend complex operator/(complex a,complex b); };

void complex::print() {

cout<

if(imag>0)cout<<\

if(imag!=0)cout<

complex operator+(complex a,complex b) {

complex temp;

temp.real=a.real+b.real; temp.imag=a.imag+b.imag; return temp; }

complex operator-(complex a,complex b) {

complex temp;

temp.real=a.real-b.real; temp.imag=a.imag-b.imag;

return temp; }

complex operator*(complex a,complex b) {

complex temp;

temp.real=a.real*b.real-a.imag*b.imag; temp.imag=a.real*b.imag+a.imag*b.real; return temp; }

complex operator/(complex a,complex b) {

complex temp; float tt;

tt=1/(b.real*b.real+b.imag*b.imag);

temp.real=(a.real*b.real+a.imag*b.imag)*tt; temp.imag=(b.real*a.imag-a.real*b.imag)*tt; return temp; }

void main() {

complex c1(2.3,4.6),c2(3.6,2.8),c3; c1.print(); c2.print(); c3=c1+c2; c3.print(); c3=c1-c2; c3.print(); c3=c1*c2; c3.print(); c3=c1/c2; c3.print(); }

2,定义矩阵类,使用友元实现矩阵的常用运算

#include using namespace std;

class Str {

private:

int m,n;//表示矩阵的行数和列数

double s[100][100];//定义二维数组存储矩阵 public: int getm(); int getn(); void input(); void output();

friend Str operator+(Str A,Str B); friend Str operator-(Str A,Str B);

friend Str operator*(Str A,Str B);//运算符的重载 };

Str operator+(Str A,Str B)//矩阵相加函数 {

Str C; int i,j;

for(i=0;i

Str operator-(Str A,Str B)//矩阵相减函数 {

Str C; int i,j;

for(i=0;i

Str operator*(Str A,Str B)//矩阵相乘函数 {

Str C; int i,j,k;

for(i=0;i

C.m=A.m;C.n=B.n; return C; }

void Str::input()//矩阵输入函数 {

int i,j; do {cout<<\请输入矩阵的行数、列数:\ cin>>m>>n;}while(m<1||m>100||n<1||n>100);//保证输入矩阵的行数和

列数有效

cout<<\请输入矩阵:\

for(i=0;i

for(j=0;j >s[i][j]; } }

void Str::output()//矩阵输出函数 {

int i,j; cout<<\得到的矩阵是:\ for(i=0;i

for(j=0;j

int Str::getm() {

return m; }

int Str::getn() {

return n; }

void menu() {

cout<<\、矩阵相加运算\ cout<<\、矩阵相减运算\ cout<<\、矩阵相乘运算\ cout<<\、退出运算系统\}

int main() {

int choice,i=1; Str A,B,C; cout<

cout<<\ 欢迎使用矩阵运算系统\ cout<

百度搜索“70edu”或“70教育网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,70教育网,提供经典综合文库实验八在线全文阅读。

实验八.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印 下载失败或者文档不完整,请联系客服人员解决!
本文链接:https://www.70edu.com/wenku/614972.html(转载请注明文章来源)
Copyright © 2020-2025 70教育网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:370150219 邮箱:370150219@qq.com
苏ICP备16052595号-17
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:7 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:xuecool-com QQ:370150219