# c 1 introduction to programming and the c language

DisciplinaFísica Básica I1.120 materiais10.866 seguidores
Pré-visualização50 páginas
```which is not necessary, since they are used as actual out parameters. A method
can have only one return value and out parameters solves a problem, where it is desirable that a method
must return multiple values. For example the method Points2() returns two values, which is not possible,
and the problem can then be solved by giving the method two out parameters.
Default parameters
It is also possible to set default values for the parameters. The following method has three parameters:
static double Calculate(double price, int units = 1, double discount = 5)
{
return price * units * (100 - discount) / 100;
}
C# 1 Introduction to programming and the C# language
98
Methods
The method calculates the price of goods excl. discount. The number of units is by default set to 1, and
discount rates are as default 5 percent. This means that the method can be called without specifying
values for these parameters:
static void Test5()
{
Console.WriteLine(Calculate(20));
Console.WriteLine(Calculate(20, 5));
Console.WriteLine(Calculate(20, 10));
}
and if you don\u2019t, the default values are used. In principle, all parameters can have a default value, but
the parameters that have a default value must be last.
Variable number of parameters
As a last remark concerning parameters, I will show how it is possible to have a method with a variable
number of parameters. The following method has an array of the type string as a parameter, and the
method returns a string consisting of all strings in the array separated by spaces:
static string Concat(params string[] text)
{
if (text.Length == 0) return &quot;&quot;;
string temp = text[0];
for (int i = 1; i < text.Length; ++i) temp += &quot; &quot; + text[i];
return temp;
}
What is important is the word params, which means that one can set a variable number of actual
parameters. For example the method can be called as follows:
static void Test6()
{
Console.WriteLine(Concat(&quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;));
Console.WriteLine(Concat(&quot;One&quot;));
Console.WriteLine(Concat());
Console.WriteLine(Concat(&quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;, &quot;Four&quot;, &quot;Five&quot;));
}
C# 1 Introduction to programming and the C# language
99
Methods
Exam26
Methods parameters
All of the above test methods about methods parameters are combined in the example Exam26 and if
you run this example, you get the following results:
to change
the world
Here at Ericsson we have a deep rooted belief that
the innovations we make on a daily basis can have a
profound effect on making the world a better place
In Germany we are especially looking for graduates
as Integration Engineers for
\u2022	 Radio Access and IP Networks
\u2022	 IMS and IPTV
We are looking forward to getting your application!
To apply and for all current job openings please visit
our web page: www.ericsson.com/careers
C# 1 Introduction to programming and the C# language
100
Inheritance
11 Inheritance
A class must, as mentioned several times represent a thing in the program\u2019s problem domain, and in
the design you decide which properties a class must have. When a class is first completed, tested and
put into operation one must be careful to open it again and make changes when there is a significant
risk that changes may have unintended consequences for programs that use the class. But no matter
how careful you are under design, there is a high probability that it is necessary to extend a class with
new properties, and this is where inheritance comes into play as a technique to extend a class without
changing the existing class. Another thing that inheritance must address is the situation where you have
two classes that are similar but also have differences. You can sometimes put what they have shared in
a base class that the other must inherit
Exam31
Points
This example is merely to show the syntax, but is not an example of where inheritance is used in practice.
The following class defines a point in a coordinate system:
public class Point
{
private int x;
private int y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
public int X
{
get { return x; }
set { x = value; }
}
public int Y
{
get { return y; }
set { y = value; }
}
public override string ToString()
{
return string.Format(&quot;({0}, {1})&quot;, x, y);
}
}
C# 1 Introduction to programming and the C# language
101
Inheritance
The class is simple and requires no further explanation. I will now write a class that inherits Point, and
one can think of it as a class that extends the class Point with new methods or properties \u2013 in this case
only a single method:
public class NewPoint : Point
{
public NewPoint(int x, int y) : base(x, y)
{
}
{
X += p.X;
Y += p.Y;
}
}
The syntax for inheritance is:
public class NewPoint : Point
which means that NewPoint inherits or extends Point. The result is that a NewPoint object has the pubilc
properties (properties and methods) as NewPoint defines, together with the public characteristics the
Point class defines. The idea is that if a class needs some new features, then you inherit rather than
modify the existing class. That way you avoid changing the classes that are already created and in use
and that you know works.
When you create a NewPoint object, it must transfer values to the class that it inherited from, what is
done with base after the constructor. In fact, it corresponds to that the constructor of the class Point is
performed.
Below is a program which uses the NewPoint class:
static void Main(string[] args)
{
NewPoint p1 = new NewPoint(2, 3);
NewPoint p2 = new NewPoint(1, 4);
p1.Add(p2);	 //	metode	defineret	i	NewPoint
p1.X	=	p1.X	*	3;	 //	property	defineret	i	Point
Console.WriteLine(p1); // ToString() metoden fra Point
}
C# 1 Introduction to programming and the C# language
102
Inheritance
Exam27
Persons
As an example, I will start with a class that represents a person by a first and last name:
public class Person
{
private	string	firstName;
private string lastName;
public	Person(string	firstName,	string	lastName)
{
this.firstName	=	firstName;
this.lastName = lastName;
}
public string FirstName
{
get	{	return	firstName;	}
}
public string LastName
{
get { return lastName; }
}
public override string ToString()
{
return	firstName	+	&quot;	&quot;	+	lastName;
}
}
The class is very simple and does not require many comments.
I will then define a class that represents an employee (in a company) when an employee is a person with
two additional properties in the form of a title and a salary. The class Employee can be written in several
ways, and you can for example think of it as an extension of the class Person with two new properties.
The solution is not just to extend the Person class, as you can imagine this class is used in contexts in
which concepts such as title and salary are not meaningful. Instead, you can write a class that inherits
the Person class:
public class Employee : Person
{
private string position;
private int monthly;
public	Employee(string	firstName,	string	lastName,	string	position,	int	monthly)
:	base(firstName,	lastName)
{
this.position = position;
this.monthly = monthly;
}
public string Position
{