Skip to content

Commit c0ceced

Browse files
committed
Improving notes on inheriting and modifiers.
1 parent 87653eb commit c0ceced

10 files changed

Lines changed: 108 additions & 55 deletions

File tree

source/code/projects/FileCountProgram/FileCountProgram/Program.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@ class Program
55
{
66
static void Main()
77
{
8-
98
string filePath = Path.Combine(
109
new DirectoryInfo(
1110
Directory.GetCurrentDirectory()
1211
).ToString(),
1312
"Program.cs"
1413
);
15-
14+
1615
/*
17-
* On older versions, you may need to use the following instead,
16+
* On older versions, you may need to use the following instead,
1817
* as the program is executed in /bin/Debug, we need to go
1918
* two folders up, where Program.cs is located: note the ".Parent"
2019
* bits in the code that follows.
2120
*/
22-
21+
2322
/*
2423
2524
string filePath = Path.Combine(
@@ -30,7 +29,7 @@ static void Main()
3029
);
3130
3231
*/
33-
32+
3433
if (!File.Exists(filePath))
3534
{
3635
Console.WriteLine(

source/code/projects/FileLineCount/FileLineCount/Program.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ static void Main(string[] args)
99
string directoryPath = AppDomain
1010
.CurrentDomain
1111
.BaseDirectory;
12-
string filePath = Path.Combine(directoryPath, "dummy.txt");
12+
string filePath = Path.Combine(
13+
directoryPath,
14+
"dummy.txt"
15+
);
1316
// Creating a generator for random numbers.
1417
Random gen = new Random();
1518
int linePopulated = gen.Next(20, 100);
@@ -37,7 +40,7 @@ static void Main(string[] args)
3740
line = sr.ReadLine();
3841
while (line != null)
3942
{
40-
count++;
43+
count++;
4144
line = sr.ReadLine();
4245
}
4346
sr.Close();
@@ -48,11 +51,15 @@ static void Main(string[] args)
4851
}
4952
// We can do a simple check to make sure
5053
// we populated as many lines as we counted.
51-
if(count != linePopulated){
54+
if (count != linePopulated)
55+
{
5256
throw new Exception("Something was wrong!");
5357
}
54-
else{
55-
Console.WriteLine($"The file located at {filePath} has {count} lines.");
58+
else
59+
{
60+
Console.WriteLine(
61+
$"The file located at {filePath} has {count} lines."
62+
);
5663
}
5764
}
5865
}
Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1-
class FMember : Person{
2-
public string Relationship;
3-
public FMember(string relationP, string nameP){
4-
// This implicitely calls
5-
// Person()
6-
// which exists to create first
7-
// a person object.
8-
Relationship = relationP;
9-
// the following
10-
// name = nameP;
11-
// gives
12-
// InheritanceAndAccessModifiers/FMember.cs(5,9): error CS0122: 'Person.name' is inaccessible due to its protection level
13-
// so we have to use the public method instead:
14-
SetName(nameP);
1+
class FMember : Person
2+
{
3+
public string Relationship;
154

16-
}
5+
public FMember(string relationP, string nameP)
6+
{
7+
// This implicitely calls
8+
// Person()
9+
// which exists to create first
10+
// a person object.
11+
Relationship = relationP;
12+
// the following
13+
// name = nameP;
14+
// gives
15+
// error CS0122: 'Person.name' is inaccessible due to its protection level
16+
// so we have to use the public method instead:
17+
SetName(nameP);
18+
// the following
19+
// NewName(nameP);
20+
// gives
21+
// error CS0122: 'Person.NewName(string)' is inaccessible due to its protection level
22+
}
1723
}

source/code/projects/InheritanceAndAccessModifiers/InheritanceAndAccessModifiers/Person.cs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,28 @@
1-
public class Person{
2-
private string name;
3-
public string GetName(){return name;}
4-
public Person(){}
5-
public void SetName(string nameP){name = nameP;}
1+
public class Person
2+
{
3+
private string name;
4+
5+
public string GetName()
6+
{
7+
return name;
8+
}
9+
10+
public Person() { }
11+
12+
public void SetName(string nameP)
13+
{
14+
if (name == null)
15+
{
16+
name = nameP;
17+
}
18+
else
19+
{
20+
NewName(nameP);
21+
}
22+
}
23+
24+
private void NewName(string newnameP)
25+
{
26+
name = newnameP + $" (previous name: {name})";
27+
}
628
}
7-
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
class Program{
2-
public static void Main(){
3-
FMember test = new FMember("Cousin", "Samuel");
4-
}
1+
using System;
2+
3+
class Program
4+
{
5+
public static void Main()
6+
{
7+
Person test1 = new Person();
8+
test1.SetName("Patricia");
9+
Console.WriteLine(test1.GetName());
10+
test1.SetName("Patty");
11+
Console.WriteLine(test1.GetName());
12+
13+
FMember test = new FMember("Cousin", "Samuel");
14+
}
515
}

source/code/projects/Properties_errors/Properties_errors/Derived.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,20 @@ public string Attr1
3030
}
3131
3232
*/
33-
33+
3434
// A similar issue is to have a setter that does nothing:
35-
36-
public string Attr2 {get{return attr1;} set{}}
37-
38-
public string GetAttr2(){
35+
36+
public string Attr2
37+
{
38+
get { return attr1; }
39+
set { }
40+
}
41+
42+
public string GetAttr2()
43+
{
3944
return attr1;
4045
}
41-
42-
46+
4347
// Misconception #2
4448
public void Test(string argP)
4549
{

source/code/projects/Properties_errors/Properties_errors/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ static void Main()
99

1010
test1.Test(null);
1111
test1.Test();
12-
12+
1313
test1.Attr2 = "This is the parameter.";
1414
Console.WriteLine(test1.GetAttr2());
1515

source/code/projects/Species/Species/Bat.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
class Bat : Mammal
44
{
55
private double flightSpeed;
6-
/*
7-
* Having a property for the
8-
* flightSpeed attribute was
9-
* not required per the UML
10-
* diagram, but it was not
11-
* disallowed either.
12-
* We use this approach here
13-
* to make the code more
14-
* modular.
15-
*/
6+
7+
/*
8+
* Having a property for the
9+
* flightSpeed attribute was
10+
* not required per the UML
11+
* diagram, but it was not
12+
* disallowed either.
13+
* We use this approach here
14+
* to make the code more
15+
* modular.
16+
*/
1617
public double FlightSpeed
1718
{
1819
get { return flightSpeed; }

source/code/projects/Species/Species/Program.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,13 @@ static void Main()
136136
}
137137
try
138138
{
139-
Bat test4 = new Bat("LC", 10, "https://en.wikipedia.org/wiki/whatever", 2, -10);
139+
Bat test4 = new Bat(
140+
"LC",
141+
10,
142+
"https://en.wikipedia.org/wiki/whatever",
143+
2,
144+
-10
145+
);
140146
}
141147
catch (Exception e)
142148
{

source/lectures/oop/inheritance.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ If there was a `SetNOW` method in both the `Bike` and the `Vehicle` method, we c
7979

8080
The following example tries to make it clear that `private` attributes *cannot* be accessed from classes inheriting.
8181

82-
8382
```{download="./code/projects/InheritanceAndAccessModifiers.zip"}
8483
!include code/projects/InheritanceAndAccessModifiers/InheritanceAndAccessModifiers/Person.cs
8584
```

0 commit comments

Comments
 (0)