CIn ++, youyuan includes youyuan functions and youyuan classes. Youyuan functions are already known in the usage of classes in the previous chapter. Now we mainly discuss youyuan functions.
1.Youyuan class:
Previously, we used the youyuan function in the extended interface of the class. The class is not only a youyuan function, but can also be used as a youyuan class.
All member functions of the youyuan class are the youyuan functions of the other class and can access the hidden information (including private and protected members) in the other class ).
When you want a class to be able to access the private members of another class, you can declare this class as another type of membership class. The statement format for defining a friend Meta class is as follows:
Friend class name;
Among them: Friend and class are keywords, and the class name must beProgramA defined class in.
For example, the following statement indicates that Class B is a friend class of Class:
Class
{
...
Public:
Friend Class B;
...
};
After the preceding descriptions, all member functions of Class B are membership functions of Class A. They can access private and protected members of Class.
Note:
(1) Friendship cannot be inherited.
(2) The relationship between friends and friends is unidirectional and not interchangeable. If Class B is a friend of Class A, Class A is not necessarily a friend of class B. It depends on whether there is a corresponding declaration in the class.
(3) The relationship between friends and friends is not transmitted. If Class B is a friend of Class A, Class C is a friend of Class B, and class C is not necessarily a friend of Class A, it also depends on whether there is a corresponding statement in the class
The following is a simple example program for simulating TV sets and remote controls:
Header file TV. h:
# Pragma once
# Include <iostream>
Using namespace STD;
// TV
Class TV
{
PRIVATE:
Int state; // ON or OFF
Int volume; // volume
Int maxchannel; // maximum number of maxchannel
Int channel; // current channel
Int mode; // broadcast or cable
Int input; // TV or VCR
Public:
Friend class remote; // remote control class
Enum {off, on };
Enum {minval, maxval = 20 };
Enum {broadcast, cable };
Enum {TV, VCR };
TV (INT S = OFF, int MC = 100): 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 ();
Void chanup ();
Void chandown ();
Void setmode () {mode = (mode = broadcast )? Cable: broadcast ;}
Void setinput () {input = (input = TV )? VCR: TV ;}
Void settings () const; // display all settings
};
// Remote control
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 ();}
Void chanup (TV & T) {T. chanup ();}
Void chandown (TV & T) {T. chandown ();}
Void setmode (TV & T) {T. setmode ();}
Void setinput (TV & T) {T. setinput ();}
Void setchan (TV & T, int c) {T. Channel = C ;}
};
Implementation file TV. cpp:
# Include <iostream>
# Include "TV. H"
Using namespace STD;
Bool TV: volup ()
{
If (volume <maxval)
{
Volume ++;
Return true;
}
Else
Return false;
}
Bool TV: voldown ()
{
If (volume> 0)
{
Volume --;
Return true;
}
Else
Return false;
}
Void TV: chanup ()
{
If (Channel <maxchannel)
{
Channel ++;
}
Else
Channel = 1;
}
Void TV: chandown ()
{
If (Channel> 0)
{
Channel --;
}
Else
Channel = maxchannel;
}
Void TV: settings () const
{
Cout <"TV is" <(State = off? "Off": "On") <Endl;
If (State = on)
{
Cout <"volume setting =" <volume <Endl;
Cout <"channel setting =" <channel <Endl;
Cout <"mode =" <mode <Endl;
Cout <"input =" <input <Endl;
}
}
Test File mian. cpp:
# Include <iostream>
# Include "TV. H"
Using namespace STD;
Int _ tmain (INT argc, _ tchar * argv [])
{
TV TCL;
Cout <"Initial settings for TCL:" <Endl;
TCL. settings ();
TCL. Onoff ();
TCL. chanup ();
Cout <"adjusted settings for TCL:" <Endl;
TCL. settings ();
Remote RMT;
RMT. setchan (TCL, 24 );
RMT. volup (TCL );
Cout <"settings after by remote:" <Endl;
TCL. settings ();
Return 0;
}
Youyuan member function: if you do not need a public interface for all methods of a class, you can choose to make a specific class member a friend of another class, instead of making the entire class A Friend. In this way, note the Declaration and definition order.
As shown in the above program, the method for making remote: setchan () a friend of the TV class is to declare it as a friend in the TV class declaration:
Class TV
{
Public:
Friend void remote: setchan (TV & T, int C );
};
However, to enable the compiler to understand the definition of remote, you must put the definition of remote class before the definition of TV. However, the setchan method has the TV parameter, so the TV definition should be placed before the remote definition. To avoid this loop, the forward declaration should be adopted:
Class TV;
Class Remote {...};
Class TV {....};
Can you think of the following order?
Class Remote;
Class TV {...};
Class Remote {..};
The answer is no. (You Need To Know why ?)
2.Nested class:
A nested class, as its name implies, is a class nested in another class. In C ++, class declarations can be placed in another class. Classes declared in another class are called Nested classes ). It avoids name confusion by providing new type class scopes. A member function that contains a class can create and use objects of the nested class. Only when the declaration is in the public part can the nested class be used outside the class, And the scope parsing operator must be used.