添加友元

This commit is contained in:
zhuyijun 2021-09-02 00:23:59 +08:00
parent 2ac1af184d
commit 93a74b0d46
3 changed files with 136 additions and 1 deletions

View File

@ -59,4 +59,5 @@ add_executable(studentc reusing/studentc.cpp)
add_executable(uses_stuc reusing/uses_stuc.cpp)
add_executable(stacktp stl/stacktp.cpp)
add_executable(stacktp1 stl/stacktp1.cpp)
add_executable(arraytp stl/arraytp.cpp)
add_executable(arraytp stl/arraytp.cpp)
add_executable(tv friend/tv.cpp)

50
base/friend/tv.cpp Normal file
View File

@ -0,0 +1,50 @@
//
// Created by nicemoe on 2021/9/2.
//
#include <iostream>
#include "tv.h"
bool Tv::volup() {
if (volume < MaxVal) {
volume++;
return true;
}
return false;
}
bool Tv::voldown() {
if (volume > MinVal) {
volume--;
return true;
}
return false;
}
bool Tv::chanup() {
if (channel < maxchannel) {
channel++;
} else {
channel = 1;
}
}
void Tv::chandown() {
if (channel > 1) {
channel--;
} else {
channel = maxchannel;
}
}
void Tv::settings() const {
using std::cout;
using std::endl;
cout << "TV is " << (state == OFF ? "OFF" : "ON") << endl;
if (state == ON) {
cout << "Volume setting = " << volume << endl;
cout << "Channel setting = " << channel << endl;
cout << "Mode = " << (mode == Antenna ? "Antenna" : "Cable") << endl;
cout << "Input =" << (input == TV ? "TV" : "DVD") << endl;
}
}

84
base/friend/tv.h Normal file
View File

@ -0,0 +1,84 @@
//
// Created by nicemoe on 2021/9/2.
//
#ifndef BASE_TV_H
#define BASE_TV_H
class Tv {
public:
friend class Remote;
enum {
OFF, ON
};
enum {
MinVal, MaxVal = 20
};
enum {
Antenna, Cable
};
enum {
TV, DVD
};
Tv(int s = OFF, int mc = 125) : state(s), volume(5), maxchannel(mc), channel(2), mode(Cable), input(TV) {}
void onoff() { state = (state == ON) ? OFF : ON; }
bool ison() const {
return state == ON;
}
bool volup();
bool voldown();
bool chanup();
void chandown();
void set_mode(){
mode = (mode == Antenna) ?Cable :Antenna;
}
void set_input(){
input = (input == TV) ?DVD :TV;
}
void settings() const ;
private:
int state;
int volume;
int maxchannel;
int channel;
int mode;
int input;
};
class Remote{
private:
int mode;
public:
Remote(int m = Tv::TV):mode(m){}
bool volup(Tv & t){
return t.volup();
};
bool voldown(Tv & t){
return t.voldown();
};
void onoff(Tv & t) { t.onoff(); }
bool chanup(Tv & t){
t.chanup();
};
void chandown(Tv & t){
t.chandown();
};
void set_chan(Tv & t,int c){
t.channel = c;
}
void set_mode(Tv & t){
t.set_mode();
}
void set_input(Tv & t){
t.set_input();
}
};
#endif //BASE_TV_H