面向对象 - 运算符重载与友元函数

第1关:复数运算

在右侧编辑器中的Begin-End之间补充代码,设计一个复数类( Complex ),该类有两个成员变量和两个函数(成员变量访问性为私有,函数为公有),并重载+,-,*运算符,实现复数的加、减、乘运算,具体要求如下:

成员变量:float real,代表实部。

成员变量:float image,代表虚部。

构造函数:Complex(float r,float i),用两个参数设置 real 和 image 成员变量的值。

输出复数函数:void Print(),输出格式为:实部 +/- 虚部i,比如1+1i,0−5i。

#include <iostream>

using namespace std;



/********* Begin *********/

class Complex

{

    //复数类的声明

    public:

    friend Complex operator+(const Complex& p1,const Complex& p2);

    friend Complex operator-(const Complex& p1,const Complex& p2);

    friend Complex operator*(const Complex& p1,const Complex& p2);

    Complex(float r,float i)

    {

        real=r;

        image=i;

    }

    void Print()

    {if(this->image>=0)

    {

        cout<<this->real<<"+"<<this->image<<"i"<<endl;}

        else

        {

            cout<<this->real<<this->image<<"i"<<endl;

        }

    }



    private:

     float real;

     float image;

   

};

//复数类的定义

Complex operator+(const Complex& p1,const Complex& p2)

{

    Complex temp(0,0);

    temp.real=p1.real+p2.real;

    temp.image=p1.image+p2.image;

    return temp;

}

Complex operator-(const Complex& p1,const Complex& p2)

{

    Complex temp(0,0);

    temp.real=p1.real-p2.real;

    temp.image=p1.image-p2.image;

    return temp;

}

Complex operator*(const Complex& p1,const Complex& p2)

{

    Complex temp(0,0);

    temp.real=p1.real*p2.real-p1.image*p2.image;//ac-bd

    temp.image=p1.real*p2.image+p1.image*p2.real;//ad+bc

    return temp;

}



/********* End *********/



运行结果图:

                          图1 **运行结果图

第2关:病毒复制

在右侧编辑器中的Begin-End之间补充代码,设计病毒类( Virus ),实现病毒检测功能,具体要求如下:

成员变量:int Gen,代表当前病毒对象的年龄,默认值为 0。

拷贝构造函数:Virus(const Virus &v),拷贝到一个新的病毒对象时,并将新的病毒对象的成员变量年龄在原来病毒对象的年龄上加 1。

重载==运算符:bool operator==(const int& g,const Virus &v),用来比较g==virus[i],以找出年龄为参数 g 的病毒,并统计计数。

/********* Begin *********/
class Virus
{
    //病毒类的声明
    public:
    int Gen;
    Virus(const Virus &v);
    Virus();

};
//病毒类的定义以及其他内容
Virus::Virus(){//构造函数

}
bool operator == (const int &g,const Virus &v1){
    if(v1.Gen == g){
        return 1;
    }else
        return 0;
}

Virus:: Virus(const Virus &v){
    this -> Gen=v.Gen+1;
}

/********* End *********/

第3关:学生信息转换

在右侧编辑器中的Begin-End之间补充代码,设计学生和教师两个类,类中成员变量都为私有,成员函数都为公有,并实现它们之间的转换,具体要求如下:

学生类( Student )

编号:int number

姓名:string name

性别:string sex

带参构造函数:Student(int num,string nam,string se),用三个参数对应初始化内部的三个成员变量。

输出函数:void Print()函数,输出学生信息,格式为:学生:name,编号:number,性别:sex。

教师类( Teacher )

与 Student 类有三个同样的成员变量

输出函数:void Print(),输出教师信息,格式为:教师:name,编号:number,性别:sex的格式打印三个成员变量。

转换构造函数,用于从 Student 类转换到 Teacher 类,它将 Student 对象的成员变量对应复制 Teacher 对象的成员变量中。

#include <iostream>
#include <string>
using namespace std;

/********* Begin *********/
// 前置声明 Teacher 类
class Teacher;

class Student
{
    //学生类的声明
    friend Teacher;
    public:
    Student();
    void Print();
    Student(int num,string nam,string se);
    private:
    int number;
    string name;
    string sex;
    
};
//学生类的定义
Student::Student(){
 
}
 Student::Student(int num,string nam,string se)
 {
     number=num;
     name=nam;
     sex=se;
 }
void Student::Print()//返回类型放在类名前
{
    cout<<"学生:"<<name<<",编号:"<<number<<",性别:"<<sex<<endl;
}

class Teacher
{
    //教师类的声明
public:
void Print();
Teacher(const Student&student);  
  private:
    int number;
    string name;
    string sex;  
};
//教师类的定义
Teacher::Teacher(const Student&student)
{
name=student.name;
number=student.number;
sex=student.sex;
}
void Teacher:: Print()
{
    cout<<"教师:"<<name<<",编号:"<<number<<",性别:"<<sex<<endl;
}

/********* End *********/


4关:矩阵运算

在右侧编辑器中的Begin-End之间补充代码,设计一个矩阵类( Matrix ),并实现矩阵的简单运算,具体要求如下:

成员变量:这一部分学员可以自由发挥,但要求都是私有成员。

成员函数:

构造函数:Matrix(int r,int c),参数 r 和 c 分别代表矩阵的行和列。

全部设值函数:void Fill(int value),函数将矩阵内所有的元素都设置为参数 value 的值。

指定位置设值函数:void Set(int r,int c,int value),函数将矩阵第 r 行 c 列的元素设置为 value 的值。

获取元素函数:int Get(int r,int c)函数,函数返回矩阵第 r 行 c 列的元素。

打印函数:void Print(),函数按照矩阵的形状打印出矩阵内容,每一个值后跟着一个空格。比如一个2x4元素全为1的矩阵,打印结果为(更明显表示格式,空格均用下划线_代替):

1_1_1_1_

1_1_1_1_

普通函数:

Matrix operator+(Matrix &m1,Matrix &m2)函数,重载Matrix类的加法运算符,实现矩阵的加法运算。

Matrix operator-(Matrix &m1,Matrix &m2)函数,重载Matrix类的减法运算符,实现矩阵的减法运算。

Matrix operator*(Matrix &m1,Matrix &m2)函数,重载Matrix类的乘法运算符,实现矩阵的乘法运算。

#include <iostream>
#include <string>
using namespace std;

/********* Begin *********/
class Matrix
{
    //矩阵类的声明
    friend Matrix operator+(Matrix &m1,Matrix &m2);
    friend Matrix operator-(Matrix &m1,Matrix &m2);
    friend Matrix operator*(Matrix &m1,Matrix &m2);
      public:
    Matrix();
    Matrix(int r,int c);
void Fill(int value);
void Set(int r,int c,int value);
int Get(int r,int c);
void Print();
    private:
    int row;
    int colum;
    int v[100][100];//注意命名时不要重名
    
};
//矩阵类的定义
Matrix:: Matrix(){}
Matrix::Matrix(int r,int c)
{
    row=r;
    colum=c;
}
void Matrix::Set(int r,int c,int value)
{
    this->v[r][c]=value;
}

void Matrix::Fill(int value)
{
    
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < colum; j++)
        {
            v[i][j] = value;
        }
    }
    return;
}

int Matrix:: Get(int r,int c)
{
    return v[r][c];
}
void Matrix::Print()
{
    int i,j=0;
for(i=0;i<row;i++)
{
    for(j=0;j<colum;j++)
    {if(j==colum-1)
        cout<<v[i][j]<<" "<<endl;
    else
    cout<<v[i][j]<<" ";

    }
}
}

Matrix operator+(Matrix& m1,Matrix &m2)
{
    //实现矩阵加法
    int i,j=0;Matrix temp(m1.row,m1.colum);
for(i=0;i<m1.row;i++)
{
    for(j=0;j<m1.colum;j++)
    {
temp.v[i][j]=m1.v[i][j]+m2.v[i][j];

    }
    
}return temp;}

Matrix operator-(Matrix& m1,Matrix &m2)
{
    //实现矩阵减法
    
    int i,j=0;Matrix temp(m1.row,m1.colum);
for(i=0;i<m1.row;i++)
{
    for(j=0;j<m1.colum;j++)
    {
temp.v[i][j]=m1.v[i][j]-m2.v[i][j];

    }
    
}return temp;
    
}

Matrix operator*(Matrix& m1,Matrix &m2)
{
    //实现矩阵乘法
    
    int i,j=0;Matrix temp(m1.row,m2.colum);
    temp.Fill(0);//初始化
    for(i=0;i<m1.row;i++)
    {
        for(j=0;j<m2.colum;j++)
        {
            for(int k=0;k<m1.colum;k++)
            {
                temp.v[i][j]+=m1.v[i][k]*m2.v[k][j];
            }

        }
    }
    return temp;
}
/********* End *********/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值