[ Pobierz całość w formacie PDF ]
.There should be no change.Why Two KeywordsDo the Same ThingYou're probably wondering why two keywords do the same thing.This is an accidentof history.When C++ was developed, it was built as an extension of the C language.C has structures, although C structures don't have class methods.Bjarne Stroustrup,the creator of C++, built upon structs, but he changed the name to classto represent the new, expanded functionality.DO put your class declaration in an HPP file and your member functions ina CPP file.DO use const whenever you can.DO understand classesbefore you move on.SummaryToday you learned how to create new data types called classes.You learned howto define variables of these new types, which are called objects.A class has data members, which are variables of various types, including otherclasses.A class also includes member functions--also known as methods.You use thesemember functions to manipulate the member data and to perform other services.Class members, both data and functions, can be public or private.Public membersare accessible to any part of your program.Private members are accessible only tothe member functions of the class.It is good programming practice to isolate the interface, or declaration, of theclass in a header file.You usually do this in a file with an.HPP extension.The implementation of the class methods is written in a file with a.CPPextension.Class constructors initialize objects.Class destructors destroy objects and areoften used to free memory allocated by methods of the class.Q&AQ.How big is a class object?A.A class object's size in memory is determined by the sum of the sizes ofits member variables.Class methods don't take up room as part of the memory setaside for the object.Some compilers align variables in memory in such a way that two-byte variables actuallyconsume somewhat more than two bytes.Check your compiler manual to be sure, butat this point there is no reason to be concerned with these details.Q.If I declare a class Cat with a private member itsAge and then define two Catobjects, Frisky and Boots, can Boots access Frisky's itsAge member variable?A.No.While private data is available to the member functions of a class,different instances of the class cannot access each other's data.In other words,Frisky's member functions can access Frisky's data, but not Boots'.In fact, Frisky is a completely independent cat from Boots, andthat is just as it should be.Q.Why shouldn't I make all the member data public?A.Making member data private enables the client of the class to use the datawithout worrying about how it is stored or computed.For example, if the Catclass has a method GetAge(), clients of the Cat class can ask forthe cat's age without knowing or caring if the cat stores its age in a member variable,or computes its age on the fly.Q.If using a const function to change the class causes a compiler error, whyshouldn't I just leave out the word const and be sure to avoid errors?A.If your member function logically shouldn't change the class, using thekeyword const is a good way to enlist the compiler in helping you find sillymistakes.For example, GetAge() might have no reason to change the Catclass, but your implementation has this line:if (itsAge = 100) cout << "Hey! You're 100 years old\n";Declaring GetAge() to be const causes this code to be flaggedas an error.You meant to check whether itsAge is equal to 100, but insteadyou inadvertently assigned 100 to itsAge.Because this assignment changesthe class--and you said this method would not change the class--the compiler is ableto find the error.This kind of mistake can be hard to find just by scanning the code.The eye oftensees only what it expects to see.More importantly, the program might appear to runcorrectly, but itsAge has now been set to a bogus number.This will causeproblems sooner or later.Q.Is there ever a reason to use a structure in a C++ program?A.Many C++ programmers reserve the struct keyword for classes thathave no functions.This is a throwback to the old C structures, which could not havefunctions.Frankly, I find it confusing and poor programming practice.Today's methodlessstructure might need methods tomorrow.Then you'll be forced either to change thetype to class or to break your rule and end up with a structure with methods.WorkshopThe Workshop provides quiz questions to help you solidify your understanding ofthe material covered and exercises to provide you with experience in using what you'velearned.Try to answer the quiz and exercise questions before checking the answersin Appendix D, and make sure you understand the answers before continuing to thenext chapter.Quiz1.What is the dot operator, and what is it used for?2.Which sets aside memory--declaration or definition?3.Is the declaration of a class its interface or its implementation?4.What is the difference between public and private data members?5
[ Pobierz całość w formacie PDF ]