c++ 11、面向对象(友元函数和友元类)

2020-05-24  本文已影响0人  八戒无戒

类的友元函数是定义在类外部,但有权访问类的所有成员,包括public,protected和private成员。友元用friend声明:

1、友元函数

友元函数在类的成员函数中定义,但友元函数并不是类的成员函数。用friend void func();声明

#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;

class Rectangle
{
public:
    static int objectCount;                                     // 类静态变量
    Rectangle();                                                // 不带参数构造方法
    Rectangle(double len, double wid);                          // 带参数构造方法
    ~Rectangle();                                               // 析构方法
    void setLength(double len);
    void setWidth(double wid);
    double getLength();
    double getWidth();
    double getSquare();
    friend void printRectangleInfo(Rectangle rectangle);         // 友元函数,实例传参
    friend void printRectangleInfo(Rectangle *r);                // 友元函数,指针传参
    friend class Print;                                          // 友元类,友元类可以访问该类中任意成员 
private:
    double length;
    double width;
};

int Rectangle::objectCount = 0;                                 // 定义类静态变量

Rectangle::Rectangle()
{
    objectCount++;
    cout << "create object Rectangle" << endl;
}

Rectangle::Rectangle(double len, double wid)
{
    objectCount++;
    length = len;
    width = wid;
    cout << "create object Rectangle(length=" << length << ", width=" << width << ")" << endl;
}

Rectangle::~Rectangle()
{
}

void Rectangle::setLength(double len)
{
    length = len;
}

void Rectangle::setWidth(double wid)
{
    width = wid;
}

double Rectangle::getLength()
{
    return length;
}

double Rectangle::getWidth()
{
    return width;
}

double Rectangle::getSquare()
{
    return length * width;
}

void printRectangleInfo(const Rectangle rectangle)
{
    cout << "[友元函数对象传参]Rectangle length=" << rectangle.length << endl;
    cout << "[友元函数对象传参]Rectangle width=" << rectangle.width << endl;
}

void printRectangleInfo(Rectangle *r)
{
    cout << "[友元函数指针传参]Rectangle length=" << r->length << endl;
    cout << "[友元函数指针传参]Rectangle width=" << r->width << endl;
}

int main()
{
    Rectangle r(15, 10);
    double square;
    printRectangleInfo(r);
    square = r.getSquare();
    cout << "rectangle面积=" << square << endl;
    cout << "objectCount=" << Rectangle::objectCount << endl;
    return 0;
}

运行结果如下:

create object Rectangle(length=15, width=10)
[友元函数对象传参]Rectangle length=15
[友元函数对象传参]Rectangle width=10
rectangle面积=150
objectCount=1

2、友元类

友元也可以是一个类,成为友元类,在这种情况下,整个类及其所有成员都是友元。用friend class ClassName;声明

#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;

class Rectangle
{
public:
    static int objectCount;                                     // 类静态变量
    Rectangle();                                                // 不带参数构造方法
    Rectangle(double len, double wid);                          // 带参数构造方法
    ~Rectangle();                                               // 析构方法
    void setLength(double len);
    void setWidth(double wid);
    double getLength();
    double getWidth();
    double getSquare();
    friend class Print;                                          // 友元类,友元类可以访问该类中任意成员 
private:
    double length;
    double width;
};

int Rectangle::objectCount = 0;                                 // 定义类静态变量

Rectangle::Rectangle()
{
    objectCount++;
    cout << "create object Rectangle" << endl;
}

Rectangle::Rectangle(double len, double wid)
{
    objectCount++;
    length = len;
    width = wid;
    cout << "create object Rectangle(length=" << length << ", width=" << width << ")" << endl;
}

Rectangle::~Rectangle()
{
}

void Rectangle::setLength(double len)
{
    length = len;
}

void Rectangle::setWidth(double wid)
{
    width = wid;
}

double Rectangle::getLength()
{
    return length;
}

double Rectangle::getWidth()
{
    return width;
}

double Rectangle::getSquare()
{
    return length * width;
}

class Print
{
public:
    void PrintInfo(Rectangle rectangle);
    void PrintSquare(Rectangle rectangle);
};

void Print::PrintInfo(Rectangle rectangle)
{
    cout << "[友元类]Rectangle length=" << rectangle.length << endl;
    cout << "[友元类]Rectangle width=" << rectangle.width << endl;
}

void Print::PrintSquare(Rectangle rectangle)
{
    double square;
    square = rectangle.getSquare();
    cout << "[友元类]rectangle面积=" << square << endl;
}

int main()
{
    Rectangle r(15, 10);
    Print print;
    print.PrintInfo(r);
    print.PrintSquare(r);
    cout << "objectCount=" << Rectangle::objectCount << endl;
    return 0;
}

运行结果如下:

create object Rectangle(length=15, width=10)
[友元类]Rectangle length=15
[友元类]Rectangle width=10
[友元类]rectangle面积=150
objectCount=1
上一篇下一篇

猜你喜欢

热点阅读