Part III: Introduction to Classes in Visual Studio .NET
Part III: Introduction to Classes PDF417 Creator In VS .NET Using Barcode creation for Visual Studio .NET Control to generate, create PDF 417 image in VS .NET applications. A class member can also be protected by declaring it private In this book, I use the protected keyword exclusively since it expresses the more generic concept Decode PDF417 In .NET Framework Using Barcode decoder for .NET Control to read, scan read, scan image in .NET applications. Making an Argument for Using
Bar Code Encoder In .NET Framework Using Barcode generation for VS .NET Control to generate, create barcode image in VS .NET applications. Protected Members
Recognize Bar Code In Visual Studio .NET Using Barcode decoder for .NET framework Control to read, scan read, scan image in .NET framework applications. Now that you know a little more about how to use protected members in an actual class, I can replay the arguments for using protected members Making PDF 417 In Visual C# Using Barcode drawer for .NET Control to generate, create PDF417 image in Visual Studio .NET applications. Protecting the internal state of the class
PDF-417 2d Barcode Creator In VB.NET Using Barcode maker for Visual Studio .NET Control to generate, create PDF417 image in .NET applications. Making the gpa member protected precludes the application from setting the grade point average to some arbitrary value The application can add courses, but it can t change the grade point average If the application has a legitimate need to set the grade point average directly, the class can provide a member function for that purpose, as follows: Draw Data Matrix ECC200 In Visual Studio .NET Using Barcode generator for Visual Studio .NET Control to generate, create DataMatrix image in Visual Studio .NET applications. class Student { public: // same as before float grade() { return gpa; } // here we allow the grade to be changed float grade(float newGPA) { float oldGPA = gpa; // only if the new value is valid if (newGPA > 0 && newGPA <= 40) { gpa = newGPA; } return oldGPA; } // other stuff is the same including the data members: protected: int semesterHours; // hours earned toward graduation float gpa; }; Barcode Generator In .NET Framework Using Barcode generation for VS .NET Control to generate, create bar code image in .NET framework applications. TEAM LinG - Live, Informative, Non-cost and Genuine! Encode GS1-128 In .NET Framework Using Barcode maker for Visual Studio .NET Control to generate, create EAN128 image in .NET framework applications. 15: Protecting Members: Do Not Disturb
Creating Code 128A In VS .NET Using Barcode printer for Visual Studio .NET Control to generate, create USS Code 128 image in VS .NET applications. The addition of the member function grade(float) allows the application to set the gpa Notice, however, that the class still hasn t given up control completely The application can t set gpa to any old value; only a gpa in the legal range of values (from 0 through 40) is accepted Thus, Student class has provided access to an internal data member without abdicating its responsibility to make sure that the internal state of the class is valid ISSN Creator In Visual Studio .NET Using Barcode maker for .NET framework Control to generate, create ISSN - 10 image in .NET applications. Using a class with a limited interface
Making GS1-128 In Visual C# Using Barcode generator for .NET framework Control to generate, create GS1 128 image in .NET applications. A class provides a limited interface To use a class, all you need (or want) to know are its public members, what they do, and what their arguments are This can drastically reduce the number of things you need to master and remember to use the class As conditions change or as bugs are found, you want to be able to change the internal workings of a class Changes to those details are less likely to require changes in the external application code if you can hide the internal workings of the class A second, perhaps more important reason, lies in the limited ability of humans (I can t speak for dogs and cats) to keep a large number of things in their minds at any given instant Using a strictly defined class interface allows the pro grammer to forget the details that go on behind it Likewise, a programmer building the class need not concentrate to quite the same degree on exactly how each of the functions is being used Code 128 Creator In Visual C# Using Barcode encoder for .NET framework Control to generate, create Code-128 image in VS .NET applications. Giving Non-Member Functions
Code-39 Creation In Visual Basic .NET Using Barcode maker for .NET framework Control to generate, create Code 3/9 image in .NET framework applications. Access to Protected Members
Barcode Creation In VS .NET Using Barcode maker for ASP.NET Control to generate, create bar code image in ASP.NET applications. Occasionally, you want a non-member function to have access to the pro tected members of a class You do so by declaring the function to be a friend of the class by using the keyword friend Sometimes, an external function can use direct access to a data member I know this appears to break the strictly defined, well-sealed-off class interface position that I ve been advocating, but just consider the following First, including a friend function is, in effect, adding that function to the interface (that s why a class shouldn t have too many friends) You re okay as long as you attempt to treat this function as a normal function that, oh yeah, happens to have direct access Second, providing a public access method that acts as Recognizing Barcode In Visual Studio .NET Using Barcode scanner for .NET framework Control to read, scan read, scan image in VS .NET applications. TEAM LinG - Live, Informative, Non-cost and Genuine! Bar Code Printer In Java Using Barcode printer for Java Control to generate, create bar code image in Java applications. Part III: Introduction to Classes
Creating GTIN - 13 In Java Using Barcode drawer for Java Control to generate, create European Article Number 13 image in Java applications. a thin veil over a data member doesn t do anything to abstract away class details Such a thin veneer function fulfills the letter of the law, but not the spirit The friend declaration appears in the class that contains the protected member The friend declaration is like a prototype declaration in that it includes the extended name and the return type In the following example, the function initialize() can now access anything it wants in Student: Encoding Code39 In Java Using Barcode encoder for Java Control to generate, create Code 39 Extended image in Java applications. class Student { friend void initialize(Student*); public: // same public members as before protected: int semesterHours; // hours earned toward graduation float gpa; }; // the following function is a friend of Student // so it can access the protected members void initialize(Student *pS) { pS->gpa = 0; // this is now legal pS->semesterHours = 0; // when it wasn t before } Generate GTIN - 128 In Java Using Barcode creation for Java Control to generate, create UCC - 12 image in Java applications. A single function can be declared a friend of two classes at the same time Although this can be convenient, it tends to bind the two classes together This binding of classes is normally considered bad because it makes one class dependent on the other If the two classes naturally belong together, however, it s not all bad, as shown here: class Student; // forward declaration class Teacher { friend void registration(Teacher& t, Student& s); public: void assignGrades(); protected: int noStudents; Student *pList[100]; }; class Student { friend void registration(Teacher& t, Student& s); public: // same public members as before protected: Teacher *pT; int semesterHours; // hours earned toward graduation float gpa; }; TEAM LinG - Live, Informative, Non-cost and Genuine!
|
|
|
|
| ALL RIGHTS RESERVED. Business Refinery (c) 2006 - 2010. | Terms of Use | Privacy Policy |