C++数组类封装
1.C++数组类封装
任务描述:实现动态数组功能,尾插法,得到值,用类封装,内部定义,外部提供接口。
- Myaray.hpp
#pragma once
#include <iostream>
using namespace std;
class MyArray
{
public:
MyArray();
// MyArray(int mCapacity);
MyArray(const MyArray &p);
~MyArray();
//设置容量
void SetData(int pos,int val);
int GetData(int pos);
int GetLength();
void PushBack(int val);
private:
int mCapacity;
int mSize;
int *pAddress;
};
MyArray::MyArray()
{
cout << "构造函数调用!" << endl;
this->mCapacity = 100;
this->mSize = 0;
pAddress = new int[this->mCapacity];
}
MyArray::MyArray(const MyArray &p)
{
this->mCapacity = p.mCapacity;
this->mSize = p.mSize;
int *pAddress = new int[p.mCapacity];
int i = 0;
for(i = 0;i<p.mSize;i++)
{
pAddress[i] = p.pAddress[i];
}
}
MyArray::~MyArray()
{
if(this->pAddress != NULL)
{
delete pAddress;
pAddress = NULL;
}
}
//设置数据
void MyArray::SetData(int pos, int val)
{
if(pos>mSize | pos < 0 )
{
return;
}
this->pAddress[pos] = val;
}
int MyArray::GetData(int pos)
{
cout<<"调用得到数据的函数"<<endl;
return this->pAddress[pos];
}
int MyArray::GetLength()
{
return this->mSize;
}
//尾插法
void MyArray::PushBack(int val)
{
cout << "调用尾插法的函数" << endl;
this->pAddress[mSize] = val;
cout << pAddress[mSize] << endl;
this->mSize++;
}
- main.cpp
#include "Myarray.hpp"
#include <string>
int main()
{
MyArray arr;
arr.PushBack(10);
arr.PushBack(20);
int ret = arr.GetData(1);
cout << "得到位置:"<<ret<<"的值";
// get。
return 0;
}
2.编写电视机类
请编写电视机类,电视机有开机和关机两种状态,有音量有频道,提供音量操作和频道操作的方法,由于电视机只能逐一调整频道,不能指定频道,增加遥控类,遥控类除了拥有电视机的已有功能,再增加根据输入调台的功能
- main.cpp
#include "Television.hpp"
#include "Remote.hpp"
void test01()
{
Television T1;
T1.onOrOff();
T1.showCurrentState();
T1.volumeUp(); //音量加1
T1.channelUp(); //频道加1
T1.showCurrentState();
}
void test02()
{
Television T2;
Remote R1(&T2);
R1.onOrOff();
R1.channelUp();
R1.showCurrentState();
R1.setChanel(15);
R1.showCurrentState();
}
int main()
{
// test01();
test02();
return 0;
}
- Remote.hpp
#pragma once
#include <iostream>
using namespace std;
class Remote
{
public: //加权限******
//构造函数,初始化指向电视
Remote(Television* pTeletision)
{
cout << "学习!" <<endl;
mTeletision = pTeletision;
}
void onOrOff()
{
mTeletision->onOrOff();
}
//音量增加
void volumeUp()
{
mTeletision->volumeUp();
}
//音量减少
void volumeDown()
{
mTeletision->volumeDown();
}
//频道增加
void channelUp()
{
mTeletision->channelUp();
}
//频道减少
void channelDown()
{
mTeletision->channelDown();
}
//查看当前电视机状态
void showCurrentState()
{
mTeletision->showCurrentState();
}
//实现可以跳台的功能 也就是设置
void setChanel(int val)
{
//同时也需要判断是否越界跳台
if (mTeletision->mChannel >= mTeletision->maxChannel || mTeletision->mChannel <= mTeletision->minChannel)
return;
mTeletision->mChannel = val;
}
private:
Television* mTeletision; //一个指向电视机的属性
};
- Television.hpp
#pragma once
#include <iostream>
using namespace std;
class Television
{
//遥控器根据输入调台的功能,拥有电视机的功能:设计友元函数来使用
friend class Remote; //声明
public:
enum{ON,OFF}; //电视机状态
enum{minVol,maxVol=100}; //音量从0到100
enum{minChannel=1,maxChannel=255}; //频道从0到255
//构造函数
Television()
{
this->mState = OFF;
this->mVolume = minVol;
this->mChannel = minChannel;
}
//打开电视机和关闭电视机
void onOrOff()
{
this->mState = this->mState == ON ? OFF : ON ; //***条件表达式赋值
if(this->mState == ON)
{
cout << "电视机开启!" <<endl;
}
else{
cout <<"电视机关闭!" << endl;
}
}
//音量增加
void volumeUp()
{
if(this->mVolume >= maxVol)
return;
this->mVolume++;
}
//音量减少
void volumeDown()
{
if (this->mVolume <= minVol)
return;
this->mVolume--;
}
//频道增加
void channelUp()
{
if(this->mChannel >= maxChannel)
return;
this->mChannel++;
}
//频道减少
void channelDown()
{
if (this->mChannel >= minChannel)
return;
this->mChannel--;
}
void showCurrentState()
{
if(this->mState == ON)
{
cout<<"电视机状态为:开启" <<endl;
cout<<"当前音量:"<<this->mVolume<<endl;
cout<<"当前频道:"<< this->mChannel<<endl;
}
else
{
cout<<"电视机已关闭"<<endl;
cout<<"---------------------"<<endl;
}
}
//电视机属性
private:
int mState; //电视机状态
int mVolume; //电视机音量
int mChannel; //电视机频道
};