c 1 introduction to programming and the c language

c 1 introduction to programming and the c language


DisciplinaFísica Básica I826 materiais10.228 seguidores
Pré-visualização50 páginas
You will be surprised where it can take you.
Send us your CV on
www.employerforlife.com
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
107 
Inheritance
If a member of a class is protected, it can be referenced by derived classes, as it was public while it from 
objects of this class looks as private. protected is a visibility between private and public, which allows 
derived classes to refer to members in the base class, while the base class maintains protection against 
other classes. Stated differently, then variable monthly may be referenced by subclasses of Employee, but 
not from classes that do not inherit Employee. A class should not make all of its variables protected, but 
only those variables that one must expect that the derived classes need to refer to. When you make a 
member protected you also are opening up the protection in relation to derived classes.
In this case, protected is only included to explain the concept, the problem could be solved in another 
way:
public override int Monthly
{
get { return base.Monthly + (int)(sale * commission / 100); }
}
where you with base refers to the property Monthly, which is defined in the base class.
There is now a definition of the property Monthly in both Employee and Consultant, and the meaning is 
that in Consultant shall override the property in Employee \u2013 give it a different meaning. For that to be 
possible, you must in Employee open up for it by declaring the property virtual:
public virtual int Monthly
{
get { return monthly; }
}
When that is the case, a derived class \u2013 here Consultant \u2013 can choose to override the property by entering 
the keyword override. If you do not, you get a warning that it hides the base class version.
It was the class Consultant, and below is shown a method that uses the class:
static void Test2()
{
Consultant c = new Consultant("Gudrun", "Madsen", 2000, 10);
c.Sale = 30000;
Print(c);
}
If you run the method, you get the result:
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
108 
Inheritance
It is not entirely obvious. Note that the property that is executed for Monthly, is the one in the class 
Consultant, even though the parameter to the Print() method is of the type Employee. This means that the 
system \u201cremembers\u201d the type of the current object, even if the object in Print() is known as an Employee. 
It is an extremely important option known as polymorphism.
When you write a class, you have no guarantee that there not in the future is one that inherits the class and 
extends it with new features \u2013 and that is exactly also the idea of inheritance. However, there may be situations 
where you do not want this option and you can then declare the class sealed, meaning that it can\u2019t be inherited. 
If for example you do not want it to be possible to inherit the class Director, you can define it as follows:
public sealed class Director : Employee
{
public	Director(string	firstName,	string	lastName,	int	monthly)
:	base(firstName,	lastName,	"Director",	monthly)
{
}
}
Comment
Inheritance is not an especially difficult concept \u2013 at least not when you\u2019ve seen it a few times \u2013 but there 
are certain things one must be aware of:
\u2022	 A class \u2013 for example Employee \u2013 may have one or more derived classes, but a class can 
inherit only one class.
\u2022	 Polymorphism \u2013 that the runtime system remembers the specific type of an object (the type 
which is used when the new object is created \u2013 is one of the most important concepts of 
object oriented programming.
Moreover, there are some names associated with inheritance, that it is important to understand:
\u2022	 base
\u2022	 protected
\u2022	 virtual
\u2022	 override
You can in the above examples to see how these words are used.
Download free eBooks at bookboon.com
Click on the ad to read more
C# 1 Introduction to programming and the C# language 
109 
The class Object
12 The class Object
I have above seen on some classes such as Coin, Dice, Cup etc. and these classes have not used 
inheritance \u2013 apparently, but actually, they have indirectly inherits a class called Object. If you do not 
write anything, then any class automatically inherit this class, and thus all classes without exception, 
directly or indirectly inherit Object. It is actually more than that, for any type whether it is a value type 
or reference type inherits Object.
C# defines an alias object of class System.Object, which is a reserved word, exactly the same way as the 
string is an alias for the class System.String.
The class Object does not contain much, and its primary purpose is to be a common base class for all 
types. The class defines a few methods that I will mention below. First I will mention ToString() which 
returns the value of an object as a string. Consider the following class:
class ZipCode
{
private string code;
private string name;
© Agilent Technologies, Inc. 2012 u.s. 1-800-829-4444 canada: 1-877-894-4414
Budget-Friendly. Knowledge-Rich.
The Agilent In\ufb01niiVision X-Series and 
1000 Series offer affordable oscilloscopes 
for your labs. Plus resources such as 
lab guides, experiments, and more, 
to help enrich your curriculum 
and make your job easier.
See what Agilent can do for you.
www.agilent.com/\ufb01nd/EducationKit
Scan for free 
Agilent iPhone 
Apps or visit 
qrs.ly/po2Opli
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
110 
The class Object
public ZipCode(string code, string name)
{
this.code = code;
this.name = name;
}
public string Code
{
get { return code; }
}
public string Name
{
get { return name; }
}
}
When not specified otherwise, the class ZipCode inherits the class Object. Consider the following method:
static void Test1()
{
ZipCode z = new ZipCode("7800", "Skive");
Console.WriteLine(z);
}
which created an object of type ZipCode, and this object is printed on the screen. Note that when the 
object is printed with WriteLine (), then it is the result of the object\u2019s ToString() method that is printed. 
In this case there is no ToString() method in class ZipCode, but the program can be translated and run, 
and the result is the following:
The ToString() method that is carried out, comes from the class Object, and print the full name of the 
object\u2019s type, here is the class\u2019s namespace and class name. One can thus state that all objects without 
exception has a ToString() method, but it is the one who writes the object\u2019s class, which is responsible 
for override the method so that it returns a meaningful result. As a class always inherits Object, that is 
why I\u2019ve written override in all the classes that have defined a ToString() method \u2013 ToString() is a virtual 
method in the class Object.
Object also defines a method called Equals() that have an object as a parameter. It is a method that 
returns true if the current object and the parameter are the same. If you execute the following method
static void Test2()
{
ZipCode z1 = new ZipCode("7800", "Skive");
ZipCode z2 = new ZipCode("7800", "Skive");
Console.WriteLine(z1.Equals(z2));
}
Download free eBooks at bookboon.com
C# 1 Introduction to programming and the C# language 
111 
The class Object
it will write False on the screen, and it was not what one would expect. The two objects z1 and z2 have 
the same value, but they are two different objects, each of which refers to their own object on the heap. 
The method Equals() as defined in the class Object, compares the object references, and as z1 and z2 
are two different objects will Equals() returns false, even if the two objects have the same value. Should 
it be otherwise, it is up to the programmer to override the method Equals(),