Heres how you do that this line tells c that your

This preview shows page 89 - 95 out of 105 pages.

Here’s how you do that: This line tells C++ that your class exists, but doesn’t actually define all the gory details. Since none of this code actually uses the “guts” of the class (as the code did on the previous slide), this is all C++ needs to know!
Image of page 89

Subscribe to view the full document.

Last Topic: Knowing WHEN to Include .H Files Wow – so confusing! class SecondClass { public: private: }; B.h A.h Why not just always use #include to avoid the confusion? 1. If a .h file is large (thousands of lines), #including it when you don’t strictly need to slows down your compilation! #include “A.h” There are two reasons: 2. If two classes refer to each other, and both classes try to #include the other’s .H file, you’ll get a compile error. class FirstClass { public: void someFunc() { ... } }; class FirstClass … // thousands of lines … // thousands of lines // really slow!
Image of page 90
Students and Classrooms Every student knows what class he/she is in… CS 32 Math 31b Every class has a roster of its students… Kerry Cyril Lydia Hal Kerry Cyril Lydia Hal These type of cyclical relationships cause #include problems!
Image of page 91

Subscribe to view the full document.

And here we have the Student class. Note that each Student knows which classroom he or she is in… Since our ClassRoom class refers to the Student class it includes Student.h Since our Student class refers to the classroom class, it includes ClassRoom.h Here we have a ClassRoom class that holds a bunch of Students Last Topic: Self-referential Classes #include “ ClassRoom.h class Student { public: ... private: ClassRoom *m_myRoom; }; Student.h #include “ Student.h class ClassRoom { public: ... private: Student m_studs[100]; }; ClassRoom.h #include “ Student.h void Student::printMyClassRoom() { cout <<“I’m in Boelter #” << m_myRoom->getRmNum(); } Student.cpp #include “ ClassRoom.h void ClassRoom::printRoster() { for (int i=0;i<100;i++) cout << m_studs[i].getName (); } ClassRoom.cpp Do you see the problem?
Image of page 92
Last Topic: Self-referential Classes #include “ ClassRoom.h class Student { ClassRoom *m_myRoom; Student.h Student.h class ClassRoom { public: ... private: Student m_studs[100]; }; main.cpp #include “ Student.h class Student { ClassRoom *m_myRoom; #include “ ClassRoom.h class ClassRoom { public: private: }; The compiler will keep going forever!!!!! (oh, and adding Include Guards doesn’t solve the underlying problem either!)
Image of page 93

Subscribe to view the full document.

So how do we solve this cyclical nightmare? Step #1: Look at the two class definitions in your .h files. At least one of them should NOT need the full #include. Question: Which of our two classes doesn’t need the full #include? Why? #include “ ClassRoom.h class Student { public: ... private: ClassRoom *m_myRoom; }; Student.h #include “ Student.h class ClassRoom { public: ... private: Student m_studs[100]; }; ClassRoom.h This class defines actual Student variables … It therefore requires the full class definition to work! This class JUST defines a pointer to a ClassRoom. It does NOT hold a full ClassRoom variable and therefore doesn’t require the full class definition here!!!
Image of page 94
class Student { public: ...
Image of page 95
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern