ssrs ean 13 COMPOSITION AND INHERITANCE in Software

Creator QR in Software COMPOSITION AND INHERITANCE

COMPOSITION AND INHERITANCE
Scanning Denso QR Bar Code In None
Using Barcode Control SDK for Software Control to generate, create, read, scan barcode image in Software applications.
Printing QR Code ISO/IEC18004 In None
Using Barcode creator for Software Control to generate, create Denso QR Bar Code image in Software applications.
The test driver tests the default constructor, the se tDa t e ( > function, the overloaded insertion operator < <, and the overloaded extraction operator > >.
Decoding QR Code ISO/IEC18004 In None
Using Barcode decoder for Software Control to read, scan read, scan image in Software applications.
Denso QR Bar Code Printer In Visual C#.NET
Using Barcode encoder for Visual Studio .NET Control to generate, create Quick Response Code image in Visual Studio .NET applications.
Now we can use the Date class inside the Person class to store a person s date of birth and date of death:
Generating QR Code 2d Barcode In .NET
Using Barcode creation for ASP.NET Control to generate, create QR Code JIS X 0510 image in ASP.NET applications.
Draw QR Code ISO/IEC18004 In .NET
Using Barcode drawer for .NET framework Control to generate, create Denso QR Bar Code image in VS .NET applications.
EXAMPLE 11.3 Composing the Date Class with the Person Class #include '5tring.h" #include "Date.h" class Person { public: Person(char* n="", int s=O, char* nat="U.S.A.") : name(n), sex(s), nationality(nat) { > void setDOB(int m, int d, int y) { dob.setDate(m, d, y); > void setDOD(int m, int d, int y) { dod.setDate(m, d, y); > void printName { tout -CC name; > void printNationality { tout CC nationality; > void printDOB() { tout << dob; > void printDOD() { tout << dod; > private: String name, nationality; Date dob, dod; // date of birth, date of death int sex; // 0 = female, 1 = male 1; main0 -C Person author("Thomas Jefferson", 1); author.setDOB(4,13,1743); author.setDOD(7,4,1826); tout << 'The author of the Declaration of Independence was "; author.printName(); tout << ".\nHe was born on "; author.printDOB(); tout << ' and died on "; author.printDOD(); tout << ".\n";
Quick Response Code Drawer In VB.NET
Using Barcode creation for .NET Control to generate, create Quick Response Code image in .NET framework applications.
Painting Data Matrix ECC200 In None
Using Barcode creator for Software Control to generate, create Data Matrix 2d barcode image in Software applications.
Notice again that we have used a member function of one class to define member functions of the composed class: the se tDa t e ( ) function is used to define the se tDOB ( > and se tDOD ( > functions. Composition is often referred to as a has-a relationship because the objects of the compos-
Code 128B Drawer In None
Using Barcode generation for Software Control to generate, create Code 128 Code Set A image in Software applications.
Barcode Creation In None
Using Barcode creation for Software Control to generate, create bar code image in Software applications.
ite class have objects of the composed class as members. Each object of the Person class has a name and a nationality .which are String objects. Composition is one way of reusing existing software to create new software.
Code 39 Drawer In None
Using Barcode creator for Software Control to generate, create ANSI/AIM Code 39 image in Software applications.
Encoding UCC.EAN - 128 In None
Using Barcode generator for Software Control to generate, create EAN128 image in Software applications.
COMPOSITION AND INHERITANCE
Code 2 Of 7 Creator In None
Using Barcode generation for Software Control to generate, create NW-7 image in Software applications.
Bar Code Creator In .NET
Using Barcode printer for Reporting Service Control to generate, create barcode image in Reporting Service applications.
[CHAP. 11
Scanning Code 39 Extended In .NET Framework
Using Barcode reader for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
Recognize UPC Code In VB.NET
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
11.3 INHERITANCE
UPC Code Generator In Java
Using Barcode printer for Android Control to generate, create Universal Product Code version A image in Android applications.
Generate USS-128 In None
Using Barcode printer for Office Excel Control to generate, create UCC - 12 image in Excel applications.
Another way to reuse existing software to create new software is by means of inheritance (also called specialization or derivation). This is often referred to as an is-a relationship because every object of the class being defined is also an object of the inherited class. The common syntax folr deriving a class Y from a class x is
Bar Code Recognizer In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
Generating GTIN - 12 In VB.NET
Using Barcode encoder for VS .NET Control to generate, create Universal Product Code version A image in .NET applications.
class Y : public X { // . . .
Here x ;s called the base cZass (or superclass) and Y is called the derived cZass (or subclass). The keyword pub1 ic after the colon specifies public inheritance, which means that pub1 ic members of the base class -become pub1 ic members of the derived class.
EXAMPLE 11.4 Deriving a Student Class from the Person Class
Students are people. So it is natural to use the People class to derive a Student class:
#include "Person.h"
class Student : p u b l i c P e r s o n { public: Student(char* n, int s=O, char* i="") : Person(n, s), id(i), credits(O) { } void setDOM(int m, int d, int y) { dom.setDate(m, d, y); } void printDOM() { tout << dom; } private: String id; // student identification number Date dom; // date of matriculation int credits; // course credits float gpa; // grade-point average
The Student class inherits all the pub1 ic functionality of the Person class, including the Person ( ) constructor which it uses in its constructor to initialize name in the Person class. Note that this is a private member of the Person class, so it could not be accessed directly. Here is a test driver for the Student class:
#include "Student.h"
main0 -t Student x("Ann Jones", "219360061"); x.setDOB(5, 13, 1977); x.setDOM(8, 29, 1995); x.printName() ; x.printDOB tout CC "\n\t Born: "; > ; tout << "\n\tMatriculated: '; x.printDOM > ; )
tout CC endl;
CHAP. 1 l]
COMPOSITION AND INHERITANCE
11.4 protected CLASS MEMBERS The Student class in Section 11.3 has a significant problem: it cannot directly access the private data members of its Person superclass: name, nationality, DOB, DOD, and sex. The lack of access on the first four of these is not serious because these can be written and read through the Person class s constructor and public access functions. However, there is no way to write or read a student's sex. One way to overcome this problem would be to make sex a data member of the Student class. But that is unnatural: sex is an attribute that all Person objects have, not just Students. A better solution is to change the private access specifier to protected in the Person class. That will allow access to these data members from derived classes.
EXAMPLE 11.5 The Person Class with protected Data Members These are the same class definitions that were given in Example 11.3 and Example 11.4 except that the private access specifier has been changed to pro tee ted, and we have added the access function printSex to the Student class: #include "String.h" #include "Date.h" class Person { public: Person(char* n="", int s=O, char* nat=YJ.S.A.") : name(n), sex(s), nationality(nat) { > void setDOB(int m, int d, int y) { dob.setDate(m, d, y); > void setDOD(int m, int d, int y) { dod.setDate(m, d, y); > void printName { tout << name; > void printNationality { tout << nationality; > void printDOB() { tout << dob; > void printDOD() { tout << dod; } protected: String name, nationality; // date of birth, date of death Date dob, dod; //o= female, 1 = male * int sex; >; class Student : public Person { public: Student(char* n, int s=O, char* i=""> : Person(n, s), id(i), credits(O) { } void setDOM(int m, int d, int y) { dom.setDate(m, d, y); > void printDOM() { tout << dom; } v o i d printSex { tout CC ( s e x male : f e m a l e ) ; } protected: // student identification number String id; // date of matriculation Date dom; // course credits int credits; // grade-point average float gpa; >;
Copyright © OnBarcode.com . All rights reserved.