C++:专题七实验(多文件结构)

C++:专题七实验(多文件结构)

实验目的:

  1. 学习将C++程序按照不同的模块分割在不同的文件中进行组织
  2. 学习编译预处理命令在多文件工程中的使用。

实验内容:

编写好的程序文件按照不同的模块分割成不同的文件,添加合适的include语句和预编译命令,将其改造为一个多文件工程。具体要求如下。
相关实验中(参见本书前面的实验),实现了复数类的算术运算和输入/输出流的重载,但这些功能个主函数中的测试语句都是放在一个cpp文件里实现的。本实验中要求将这些代码分装在3个文件中,第一个文件complex.h包含Complax的类定义,第二个文件complex.cpp包含Complax类的函数实现,第三个文件test.cpp包含主函数和相关的测试程序。在适当的文件中添加合适include语句和预编译语句(ifndef-define-endif结构),使其成为一个能够正确运行的工程。

编写好的程序文件按照不同的模块分割成不同的文件,添加合适的include语句和预编译命令,将其改造为一个多文件工程。
构建目标(以codeblocks为例):

在这里插入图片描述

构建方法(以codeblocks为例):

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

complex.h文件
#ifndef COMPLEX_H_INCLUDED
#define COMPLEX_H_INCLUDED

#include<iostream>
using namespace std;

class Complex
{
private:
    double real, image;
public:
    Complex(double r, double i);
    //知识点:运算符的重载、友元函数
    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);

    //知识点:输入输出运算符的重载:
    friend ostream &operator <<(ostream &output, const Complex &C);
    friend istream &operator >>(istream &input, Complex &C);
};

#endif // COMPLEX_H_INCLUDED
complex.cpp文件
#include <iostream>
#include "complex.h"

using namespace std;

Complex::Complex(double r = 0, double i = 0)
{
    real = r, image = i;
}

Complex operator +(Complex a, Complex b)
{
    Complex c;
    c.real = a.real + b.real;
    c.image = a.image + b.image;
    return c;
}
Complex operator -(Complex a, Complex b)
{
    Complex c;
    c.real = a.real - b.real;
    c.image = a.image - b.image;
    return c;
}
Complex operator *(Complex a, Complex b)
{
    Complex c;
    c.real = a.real * b.real - a.image * b.image;
    c.image = a.real * b.image + a.image * b.real;
    return c;
}
Complex operator /(Complex a, Complex b)
{
    Complex c;
    c.real = (a.real * b.real + a.image * b.image) / (b.real * b.real + b.image * b.image);
    c.image = (a.image * b.real - a.real * b.image) / (b.real * b.real + b.image * b.image);
    return c;
}

//为啥加上const?
//我们不希望在这个函数中对用来进行赋值的“原版”做任何修改。函数加上const后缀的作用是表明函数本身不会修改类成员变量。
//加上const,对于const和非const的实参,函数都能接受;如果不加,就只能接受非const的实参。
ostream &operator <<(ostream &output, const Complex &C)
{
    if(C.real != 0)
    {
        if(C.image > 0)
            output << C.real << "+" << C.image << "i";
        else if(C.image < 0)
            output << C.real << C.image << "i";
        else
            output << C.real;
    }
    else
    {
        if(C.image != 0)
            output << C.image << "i";
        else
            output << 0;
    }

    return output;
}

istream &operator >>(istream &input, Complex &C)//为啥这里不用const?
{
    input >> C.real >> C.image;
    return input;
}
test.cpp文件
#include <iostream>
#include "complex.h"
using namespace std;

int main()
{
    Complex x(0,0);
    Complex y(0,0);
    cout << "请输入复数x:";
    cin >> x;
    cout << "请输入复数y:";
    cin >> y;
    cout<<"x + y = "<<x+y<<endl;
    cout<<"x - y = "<<x-y<<endl;
    cout<<"x * y = "<<x*y<<endl;
    cout<<"x / y = "<<x/y<<endl;
    return 0;
}

在这里插入图片描述

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值