In the process of learning C + +, also once learned Java, found that Java has a class of nesting, and read C + + books, never a C + + class can be nested, so try to see if C + + can be nested class (compiler environment vc++6.0)
The following code is then available:
1#include <iostream>2 using namespacestd;3 classA4 {5 Public:6 A () {7cout<<"This a"<<Endl;8 }9~a () {cout<<"a dead"<<Endl;}Ten classB One { A Public: -B () {cout<<"This b"<<Endl;} -~b () {cout<<"B dead"<<Endl;} the }; - b b; - }; - classC: PublicA + { - }; + intMainintargcChar*argv[]) A { at A; - return 0; -}
The result is correct, so it appears that C + + is supporting nested classes, at least vc++6.0 is supported
So can class B be used? The following code is then available:
1#include <iostream>2 using namespacestd;3 classA4 {5 Public:6 A () {7cout<<"This a"<<Endl;8 }9 voidGeta () {cout<<"I Am geta ()"<<Endl;}Ten~a () {cout<<"a dead"<<Endl;} One classB A { - Public: -B () {cout<<"This b"<<Endl;} the~b () {cout<<"B dead"<<Endl;} - voidGETB () {cout<<"I Am GETB ()"<<Endl;} - - }; + b b; - }; + intMainintargcChar*argv[]) A { at A; - A.b.getb (); - a::b B; - B.getb (); - return 0; -}
The result can be successfully called the GETB () method, which can indirectly prove that the class is correctly recognized, OK, then we see whether B can inherit other classes? The following code is then available:
1#include <iostream>2 using namespacestd;3 classA4 {5 Public:6 A () {7cout<<"This a"<<Endl;8 }9 voidGeta () {cout<<"I Am geta ()"<<Endl;}Ten~a () {cout<<"a dead"<<Endl;} One classB: PublicA A { - Public: -B () {cout<<"This b"<<Endl;} the~b () {cout<<"B dead"<<Endl;} - voidGETB () {cout<<"I Am GETB ()"<<Endl;} - - }; + b b; - }; + A intMainintargcChar*argv[]) at { - A; - A.b.getb (); - a::b B; - b.getd (); - return 0; in}
Well, it turns out that a is undefined, okay, so let's define a class, as follows:
1#include <iostream>2 using namespacestd;3 classD4 {5 Public:6 voidgetd ()7 {8cout<<"I Am getd ()"<<Endl;9 }Ten }; One classA A { - Public: - A () { thecout<<"This a"<<Endl; - } - voidGeta () {cout<<"I Am geta ()"<<Endl;} -~a () {cout<<"a dead"<<Endl;} + classB: PublicD - { + Public: AB () {cout<<"This b"<<Endl;} at~b () {cout<<"B dead"<<Endl;} - voidGETB () {cout<<"I Am GETB ()"<<Endl;} - - }; - b b; - }; in intMainintargcChar*argv[]) - { to A; + A.b.getb (); - A.geta (); the a.b.getd (); * a::b B; $ B.getb ();Panax Notoginseng b.getd (); - return 0; the}
The result is also correct, it seems that Class B can inherit other classes, there are also those inheritance relationships.
Conclusion:
We analyze that C + + can be nested in a class, and can manipulate it like a normal class, in addition to the class nested it cannot inherit, there are no other features, it is interesting to verify that the anonymous class can be allowed in C + +.
A probe into some features of C + +