Get 20M+ Full-Text Papers For Less Than $1.50/day. Start a 14-Day Trial for You or Your Team.

Learn More →

A Programmer’s Introduction to C# 2.0Base Classes and Inheritance

A Programmer’s Introduction to C# 2.0: Base Classes and Inheritance CHAPTER 6 ■ ■ ■ As discussed in Chapter 1, it makes sense to derive one class from another if the derived class is an example of the base class. The Engineer Class The following class implements Engineer and methods to handle billing for Engineer: using System; class Engineer // constructor public Engineer(string name, float billingRate) this.name = name; this.billingRate = billingRate; // figure out the charge based on engineer's rate public float CalculateCharge(float hours) return(hours * billingRate); // return the name of this type public string TypeName() return("Engineer"); private string name; protected float billingRate; 39 40 CH APTER 6 ■ BASE CLA SSES AND INHERITA NC E class Test public static void Main() Engineer engineer = new Engineer("Hank", 21.20F); Console.WriteLine("Name is: {0}", engineer.TypeName()); Engineer will serve as a base class for this scenario. It contains the private field name and the protected field billingRate. The protected modifier grants the same access as private; however, classes that are derived from this class also have access to the field. Protected is therefore used to give classes that derive from this class access to a field. Protected access allows other classes to depend upon the internal implementation of the class and http://www.deepdyve.com/assets/images/DeepDyve-Logo-lg.png

A Programmer’s Introduction to C# 2.0Base Classes and Inheritance

Editors: Gunnerson, Eric; Wienholt, Nick
Springer Journals — Jan 1, 2005

Loading next page...
 
/lp/springer-journals/a-programmer-s-introduction-to-c-2-0-base-classes-and-inheritance-XZrzWkh2ON
Publisher
Apress
Copyright
© Apress 2005
ISBN
978-1-59059-501-5
Pages
39 –51
DOI
10.1007/978-1-4302-0035-2_6
Publisher site
See Chapter on Publisher Site

Abstract

CHAPTER 6 ■ ■ ■ As discussed in Chapter 1, it makes sense to derive one class from another if the derived class is an example of the base class. The Engineer Class The following class implements Engineer and methods to handle billing for Engineer: using System; class Engineer // constructor public Engineer(string name, float billingRate) this.name = name; this.billingRate = billingRate; // figure out the charge based on engineer's rate public float CalculateCharge(float hours) return(hours * billingRate); // return the name of this type public string TypeName() return("Engineer"); private string name; protected float billingRate; 39 40 CH APTER 6 ■ BASE CLA SSES AND INHERITA NC E class Test public static void Main() Engineer engineer = new Engineer("Hank", 21.20F); Console.WriteLine("Name is: {0}", engineer.TypeName()); Engineer will serve as a base class for this scenario. It contains the private field name and the protected field billingRate. The protected modifier grants the same access as private; however, classes that are derived from this class also have access to the field. Protected is therefore used to give classes that derive from this class access to a field. Protected access allows other classes to depend upon the internal implementation of the class and

Published: Jan 1, 2005

Keywords: Base Class; Correct Function; Abstract Function; Abstract Class; Real Type

There are no references for this article.