2017C++基础——网课笔记(54到58)

五十四.对象返回自身



#include <iostream>

using namespace std;

class Test
{
public:
    Test(int a,int b)
    {
        this->a = a;
        this->b = b;
    }

    void printT()
    {
        cout<<"a="<<a<<"  b="<<b<<endl;
    }

    int getA()
    {
        return this->a;
    }

    int getB()
    {
        return this->b;
    }

    //在局部提供一个,两个相加的函数
    Test TestAdd(Test &another)
    {
        Test temp(this->a+another.a,this->b+another.b);

        return temp;
    }

    //+= 方法
    Test& TestAdd2(Test& another)
    {
        this->a += another.a;
        this->b += another.b;
        //如果想返回一个对象的本身,在成员方法中,用*this返回
        return *this;
    }


private:
    int a;
    int b;
};

//1在全局提供一个,两个相加的函数
/*
Test TestAdd(Test& t1, Test &t2)
{
    Test temp(t1.getA()+t2.getA(),t1.getB()+t2.getB());

    return temp;
}
*/

int main()
{
    Test t1(10,30);
    Test t2(100,200);

    //Test t3 = t1.TestAdd(t2);

    //t3.printT();

    cout<<"--------------------"<<endl;
    t1.printT();
    // (((t1+= t2) +=t2)+=t2);

    //如果想对一个对象,连续调用成员方法。
    //每次都会改变对象本身,成员方法需要返回引用。
    t1.TestAdd2(t2).TestAdd2(t2);

    t1.printT();

    return 0;
}



五十五. 强化练习,自定义的数组类

这个例子呢,一共分为3个文件,分别是:
main.cpp

#include <iostream>
#include "MyArray.h"

using namespace std;

int main()
{
    MyArray array1(10);

    for(int i=0; i<10;i++){
        array1.setData(i,i+10);
    }

    cout<<"-------------------------"<<endl;
    cout<<"array1:"<<endl;
    for(int i=0;i<10;i++){
        cout<<array1.getData(i)<<" ";
    }
    cout<<endl;

    MyArray array2= array1;
    cout<<"array2:"<<endl;
    for(int i=0;i<array2.getLen();i++){
        cout<<array2.getData(i)<<" ";
    }
    cout<<endl;

    return 0;
}

第二个文件是 MyArray.h

#ifndef MYARRAY_H
#define MYARRAY_H


class MyArray
{
    public:
        MyArray();
        MyArray(int len);
        MyArray(const MyArray & another);
        ~MyArray();

        void setData(int index,int data);
        int getData(int index);
        int getLen();
    protected:

    private:
        int len;
        int *space;
};

#endif // MYARRAY_H

第三个文件 MyArray.cpp


#include <iostream>
#include "MyArray.h"

using namespace std;

MyArray::MyArray()
{
    cout<<" MyArray()..."<<endl;//ctor
    this->len=0;
    this->space= NULL;
}

MyArray::MyArray(int len)
{
    if(len<0){
        this->len=0;
        return;
    }
    else{
        this->len = len;

        //¸øspace¿ª±Ù¿Õ¼ä
        this->space= new int[this->len];
        cout<<" MyArray::MyArray(int len)..."<<endl;//ctor
    }
}

MyArray::MyArray(const MyArray & another)
{
    if(another.len >= 0){
        this->len = another.len;

        //Here we have deep copy
        this->space= new int[this->len];
        for(int i=0; i<this->len;i++){
            this->space[i] = another.space[i];
        }
        cout<<" MyArray::MyArray(const MyArray & another)..."<<endl;//ctor
    }
}


MyArray::~MyArray()
{
    if(this->space != NULL){//dtor
        delete []this->space;
        this->space = NULL;
        len=0;
        cout<<"MyArray::~MyArray()... "<<endl;
    }
}

void MyArray::setData(int index, int data)
{
    if(this->space !=NULL){
        this->space[index] =data;
    }
}

int MyArray::getData(int index)
{

    return this->space[index];
}

int MyArray::getLen()
{
    return this->len;
}

五十六. 中午回顾(略)
五十七. 友元函数1

#include <iostream>
#include <math.h>
using namespace std;

class Point
{
public:
    //声明全局函数PointDistance是我Point类的一个友元函数
    friend double PointDistance(Point &p1, Point & p2);

    Point(int x,int y){
        this->x = x;
        this->y = y;
    }

    int getX()
    {
        return this->x;
    }

    int getY()
    {
        return this->y;
    }
private:
    int x;
    int y;
};

double PointDistance(Point &p1, Point & p2){
    double distance;
    int dis_x= p1.x - p2.x;
    int dis_y= p1.y - p2.y;

    distance=sqrt(dis_x * dis_x +dis_y * dis_y);

    return distance;
}

int main()
{
    Point p1(1,2);
    Point p2(2,1);

    cout<<"Distance between two points is: "<<PointDistance(p1,p2)<<endl;

    return 0;
}

五十八.友元函数2

#include <iostream>
#include <math.h>
using namespace std;

class Point;

class PointManager
{
public:
    double PointDistance(Point &p1, Point &p2);
private:
};

class Point
{
public:
    friend double PointDistance(Point &p1, Point &p2);

    Point(int x, int y)
    {
        this->x = x;
        this->y = y;
    }

    int getX()
    {
        return this->x;
    }

    int getY()
    {
        return this->y;
    }
private:
    int x;
    int y;
};

double PointManager::PointDistance(Point &p1, Point &p2)
{
    double dis;
    int dd_x = p1.getX() - p2.getX();
    int dd_y = p1.getY() - p2.getY();
    dis = sqrt(dd_x*dd_x + dd_y*dd_y);

    return dis;
}

int main()
{
    Point p1(2,4);
    Point p2(3,6);

    PointManager pm;
    cout<<"The distance between two points are:"<<pm.PointDistance(p1,p2)<<endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值