C++类和对象(七)

day07

2023年3月11日

知识点1.友元函数

语法:在类、类成员函数、全局函数声明处添加friend关键字即可声明为友元;

友元函数不是类的成员,不带this指针;友元函数可以访问对象任意成员属性,包括私有属性;

1、普通全局函数作为类的友元

#include <iostream>

using namespace std;

class Room{
private:
    string bedRoom;
public:
    string sittingRoom;
    Room(){
        this->bedRoom = "卧室";
        this->sittingRoom = "客厅";
    }
    //将goodFriendVisit声明为类的友元函数,可以访问类中所有数据
    friend void goodFriendVisit(Room &room);
};

//普通全局函数作为类的友元
void goodFriendVisit(Room &room)
{
    cout << "好朋友要参观" << room.bedRoom << endl;
    cout << "好朋友要参观" << room.sittingRoom << endl;

}

int main()
{
    Room room;
    goodFriendVisit(room);

    return 0;
}

结果:

2、类的某个成员函数作为另一个类的友元

3、整个类作为另一个类的友元

#include <iostream>

using namespace std;

//提前声明,防止GoodFriend类使用报错
class Room;

class GoodFriend{
public:
    void visit(Room &room);//类中声明
};

class Room{
private:
    string bedRoom;
public:
    string sittingRoom;
    Room(){
        this->bedRoom = "卧室";
        this->sittingRoom = "客厅";
    }
    //GoodFriend的成员函数作为Room类的友元
    //friend void GoodFriend::visit(Room &room);
    //整个GoodFriend类作为Room类的友元
    friend class GoodFriend;
};

//类外定义
void GoodFriend::visit(Room& room)
{
    cout << "好朋友要参观" << room.bedRoom << endl;
    cout << "好朋友要参观" << room.sittingRoom << endl;
}

int main()
{
    Room room;
    GoodFriend goodFriend;

    goodFriend.visit(room);

    return 0;
}

结果:

知识点2.案例:看电视

问题描述:请编写一个电视机类,电视机有开机和关机状态,有音量和频道设置,提供音量和频道的操作方法。由于电视机只能逐一调整频道,不能指定频道,增加遥控器类,遥控器类除了拥有电视机已有功能,再增加根据输入调台功能。

提示:遥控器类可以作为电视机类的友元类

#include <iostream>

using namespace std;

class Remote;

class TV{
    //将遥控器类设置为电视机类的友元类
    friend class Remote;
private:
    int state;//电视机状态,开机或关机
    int volume;//电视机音量
    int channel;//电视机频道
public:
    enum{ On, Off };
    enum{ minVol, maxVol = 100 };
    enum{ minChannel = 1, maxChannel = 255 };
    //利用构造函数初始化
    TV(){
        this->state = Off;
        this->volume = minVol;
        this->channel = minChannel;
    }
    //开关电视机
    void onOrOff(){
        this->state = (this->state == On? Off : On);
    }
    //调高音量
    void volumeUp(){
        if (this->volume >= maxVol) return;
        this->volume ++;
    }
    //调低音量
    void volumeDown(){
        if (this->volume <= minVol) return;
        this->volume --;
    }
    //更换电视频道
    void channelUp(){
        if (this->channel >= maxChannel) return;
        this->channel ++;
    }
    void channelDown(){
        if (this->channel >= maxChannel) return;
        this->channel --;
    }
    //显示当前电视机的信息
    void showTV(){
        cout << "开关机状态:" << (this->state == On ? "开机" : "关机") << endl;
        if (this->state == On){
            cout << "当前音量:" << this->volume << endl;
            cout << "当前频道:" << this->channel << endl;
        }
    }
};
class Remote{
private:
    TV *tv;
public:
    Remote(TV *tv){
        this->tv = tv;
    }
    //通过遥控器开关电视机
    void onOrOff(){ this->tv->onOrOff(); }
    //通过遥控器调高音量
    void volumeUp(){ this->tv->volumeUp(); }
    //通过遥控器调低音量
    void volumeDown(){ this->tv->volumeDown(); }
    //通过遥控器更换电视频道
    void channelUp(){ this->tv->channelUp(); }
    void channelDown(){ this->tv->channelDown(); }
    //通过遥控器显示当前电视机的信息
    void showTV(){ this->tv->showTV(); }
    //通过遥控器设置频道
    void setChannel(int num){
        //判断频道是否在有效区间
        if (num < TV::minChannel || num > TV::maxChannel) return;
        this->tv->channel = num;
    }
};

int main()
{
    TV tv;
    Remote rem(&tv);

    rem.onOrOff();
    rem.showTV();
    rem.volumeUp();
    rem.volumeUp();
    rem.volumeUp();
    rem.volumeUp();

    rem.channelUp();
    rem.channelUp();
    rem.channelUp();
    rem.showTV();

    rem.setChannel(45);
    rem.showTV();

    return 0;
}

结果:

知识点3.实现数组类

myarray.h头文件

#ifndef MYARRAY_H
#define MYARRAY_H

class MyArray
{
private:
    int capacity;//数组总共能存放元素的个数
    int size;//数组实际存放的元素个数
    int *addr;//数组首元素地址
public:
    MyArray();
    MyArray(int capacity);
    ~MyArray();
    //往数组尾部插入数据
    void pushBack(int data);
    //获得指定位置的数据
    int getData(int pos);
    //修改指定位置的值
    void setData(int pos, int data);
    //获取数组的容量(能存放的最大元素个数)
    int getCapacity();
    //获取数组的实际大小(实际元素个数)
    int getSize();
    //打印数组中的内容
    void printMyArray();
};

#endif // MYARRAY_H

myarray.cpp文件

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

using namespace std;

MyArray::MyArray()
{
    //假如数组的容量为100
    this->capacity = 100;
    //数组的size为0
    this->size = 0;
    //根据容量给数组申请空间
    this->addr = new int[this->capacity];
}

MyArray::MyArray(int capacity)
{
    this->capacity = capacity;
    this->size = 0;
    this->addr = new int[this->capacity];
}

MyArray::~MyArray()
{
    if (this->addr != NULL){
        delete [] this->addr;
        this->addr = NULL;
    }
}

void MyArray::pushBack(int data)
{
    if (this->size >= this->capacity){
        cout << "数组已满" << endl;
        return;
    }
    addr[this->size] = data;
    this->size ++;
}

int MyArray::getData(int pos)
{
    if (pos >= this->size || pos < 0){
        cout << "位置越界" << endl;
        return -1;
    }
    return addr[pos];
}

void MyArray::setData(int pos, int data)
{
    if (pos >= this->size || pos < 0){
        cout << "位置越界" << endl;
        return;
    }
    addr[pos] = data;
}

int MyArray::getCapacity()
{
    return this->capacity;
}

int MyArray::getSize()
{
    return this->size;
}

void MyArray::printMyArray()
{
    for (int i = 0; i < this->size; i ++){
        cout << this->addr[i] << " ";
    }
    cout << endl;
}

main.cpp文件

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

using namespace std;

int main()
{
    //实例化一个数组对象
    MyArray arr1;
    cout << "容量:" << arr1.getCapacity() << endl;
    cout << "大小:" << arr1.getSize() << endl;
    cout << "----------" << endl;

    MyArray arr2(50);
    cout << "容量:" << arr2.getCapacity() << endl;
    cout << "大小:" << arr2.getSize() << endl;
    cout << "----------" << endl;

    //往数组arr2中插入20个数据
    for (int i = 0; i < 20; i ++){
        arr2.pushBack(i);
    }
    cout << "容量:" << arr2.getCapacity() << endl;
    cout << "大小:" << arr2.getSize() << endl;
    cout << "----------" << endl;

    //打印数组arr2中的数据
    arr2.printMyArray();
    cout << "----------" << endl;

    //更改pos = 9的值为100
    arr2.setData(9, 100);
    arr2.printMyArray();
    cout << "----------" << endl;

    //得到下标为9的值
    cout << arr2.getData(9) << endl;

    return 0;
}

结果:

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值