添加友元
This commit is contained in:
parent
93a74b0d46
commit
6701a8994a
@ -60,4 +60,4 @@ 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(tv friend/tv.cpp)
|
||||
add_executable(tv friend/tv.cpp friend/use_tv.cpp friend/tvfm.h)
|
109
base/friend/tvfm.h
Normal file
109
base/friend/tvfm.h
Normal file
@ -0,0 +1,109 @@
|
||||
//
|
||||
// Created by zhuyi on 2021/9/3.
|
||||
//
|
||||
|
||||
#ifndef BASE_TVFM_H
|
||||
#define BASE_TVFM_H
|
||||
|
||||
class Tv;
|
||||
|
||||
class Remote{
|
||||
public:
|
||||
enum State{
|
||||
OFF, ON
|
||||
};
|
||||
enum {
|
||||
MinVal, MaxVal = 20
|
||||
};
|
||||
enum {
|
||||
Antenna, Cable
|
||||
};
|
||||
enum {
|
||||
TV, DVD
|
||||
};
|
||||
private:
|
||||
int mode;
|
||||
public:
|
||||
Remote(int m = TV):mode(m){}
|
||||
bool volup(Tv & t);
|
||||
bool voldown(Tv & t);
|
||||
void onoff(Tv & t);
|
||||
|
||||
bool chanup(Tv & t);
|
||||
void chandown(Tv & t);
|
||||
void set_chan(Tv & t,int c);
|
||||
void set_mode(Tv & t);
|
||||
void set_input(Tv & t);
|
||||
};
|
||||
|
||||
|
||||
class Tv {
|
||||
|
||||
public:
|
||||
friend void Remote::set_chan(Tv &t, int c);
|
||||
enum State{
|
||||
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;
|
||||
};
|
||||
|
||||
inline bool Remote::volup(Tv & t){
|
||||
return t.volup();
|
||||
};
|
||||
inline bool Remote::voldown(Tv & t){
|
||||
return t.voldown();
|
||||
};
|
||||
inline void Remote::onoff(Tv & t) { t.onoff(); }
|
||||
|
||||
inline bool Remote::chanup(Tv & t){
|
||||
t.chanup();
|
||||
};
|
||||
inline void Remote::chandown(Tv & t){
|
||||
t.chandown();
|
||||
};
|
||||
inline void Remote::set_chan(Tv & t,int c){
|
||||
t.channel = c;
|
||||
}
|
||||
inline void Remote::set_mode(Tv & t){
|
||||
t.set_mode();
|
||||
}
|
||||
inline void Remote::set_input(Tv & t){
|
||||
t.set_input();
|
||||
}
|
||||
|
||||
|
||||
#endif //BASE_TVFM_H
|
33
base/friend/use_tv.cpp
Normal file
33
base/friend/use_tv.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
//
|
||||
// Created by zhuyi on 2021/9/3.
|
||||
//
|
||||
#include <iostream>
|
||||
#include "tv.h"
|
||||
|
||||
int main(){
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
Tv s42;
|
||||
cout <<"Initial settings for 42\" TV:\n";
|
||||
s42.settings();
|
||||
s42.onoff();
|
||||
s42.chanup();
|
||||
cout <<"\nAdjusted settings for 42\" TV\n";
|
||||
s42.chanup();
|
||||
cout <<"\nAdjusted settings for 42\" TV:\n";
|
||||
s42.settings();
|
||||
Remote grey;
|
||||
grey.set_chan(s42,10);
|
||||
grey.volup(s42);
|
||||
grey.volup(s42);
|
||||
cout <<"\n42\" settings after usinfg remote:\n";
|
||||
s42.settings();
|
||||
|
||||
Tv s58(Tv::ON);
|
||||
s58.set_mode();
|
||||
grey.set_chan(s58,28);
|
||||
cout <<"\n58\" settings:\n";
|
||||
s58.settings();
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user