Pre-referential declaration considerations for classes in C + +

Source: Internet
Author: User

//Perhaps, Friend is vc++6.0 heart forever pain, for this bug I always very mind.
Note: This procedure does not work in the vc++6.0, in the VS2008 is possible.
#include <iostream>#include<string>using namespacestd;classStudent;//Advance Citation Statement//declaring the teacher classclassTeacher { Public: Teacher () {} Teacher (intIstringSCharCintt=0): num (i), name (s), sex (c), Theachyears (t) {} Teacher (Student); Friend Ostream&operator<< (ostream& out,teacher&te);Private: intnum; stringname; CharSex//f/m intTheachyears;};//declaring the Student classclassStudent { Public: Student () {} Student (intIstringSCharCintg=0): num (i), name (s), sex (c), Grade (g) {} friend Teacher::teacher (Student); Friend Ostream&operator<< (ostream& out,student&St);Private: intnum; stringname; CharSex//f/m intgrade;};//Note: According to the Convention, we prefer to write the implementation of the member function directly before the declaration of the class.//However, the implementation of the constructor cannot be written before the student class declaration,
Because it uses members of the student class, the advance reference declaration will not work here Teacher::teacher (Student st) {num=St.num; Name=St.name; Sex=St.sex; Theachyears=0; }//<< of heavy Duty teacher class
Note: The implementation of this constructor cannot be written before the student class declaration for the same reason asostream&operator<< (ostream& out,teacher&te) { out<<"num="<<te.num<<","<<"name="<<te.name<<","<<"sex="<<te.sex<<","<<"theachyears="<<te.theachYears<<Endl; return out;}//<< of heavy duty student classostream&operator<< (ostream& out,student&St) { out<<"num="<<st.num<<","<<"name="<<st.name<<","<<"sex="<<st.sex<<","<<"grade="<<st.grade<<Endl; return out;}intMain () {Student s (1,"Xiaoer",'F',2); cout<<s; Teacher T=Teacher (s); cout<<T; return 0;}

Pre-referential declaration considerations for classes in C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.