Skip to content

Commit e398c54

Browse files
committed
Started working on the methods section of chapter 4, changed readme to reflect being on github.
1 parent 7ded4dc commit e398c54

File tree

4 files changed

+132
-2
lines changed

4 files changed

+132
-2
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
Classes \emph{AND} Objects?
2+
What is the difference?
3+
\\
4+
5+
Well I am glad you asked.
6+
A class is the definition or blueprint of an object.
7+
A class tells a program what to expect when coming across an object of the given class.
8+
What methods and properties to expect and even how to create and destroy objects.
9+
\par
10+
11+
\emph{An object refers to a single instance of a class}
12+
\\
13+
Objects are refered to as being instances of a class.
14+
When you deine a class you are not creating a usable object that you can then call methods on
15+
or access properties of.
16+
You must then create an instance of that class (object) to be able to use it throughout your program.
17+
18+
\subsection{Classes}
19+
Ok, so as I mentioned before we need to first define a class before we can start creating objects
20+
and using them in our program.
21+
How do we do this?
22+
\par
23+
24+
\begin{lstlisting}[caption={Class Definition}]
25+
class Person
26+
\end{lstlisting}
27+
28+
Ok...?
29+
That seems too easy?
30+
\\
31+
Yes creating classes is usually fairly easy, just make sure to check how to create a class in your language of choice.
32+
33+
\subsection{Objects}
34+
Ok, so we have our class definition from above, but how do we create an instance of this class so we can use it in our program?
35+
\par
36+
37+
\begin{lstlisting}[caption={Object Declaration}]
38+
class Person
39+
40+
p = new Person()
41+
\end{lstlisting}
42+
43+
That is it.
44+
We can create an instance of our \pigVar{Person} class by using the \pigVar{new} keyword and calling \pigVar{Person()}.
45+
We can assign this instance to a variable, \pigVar{p}, and then use \pigVar{p} as an alias for our object throughout
46+
our program.
47+
\par
48+
49+
Can we only have one object?
50+
No, you can have as many instances as your would like.
51+
\par
52+
53+
\begin{lstlisting}[caption={Multiple Object Instances}]
54+
class Person
55+
56+
p1 = new Person()
57+
p2 = new Person()
58+
p3 = new Person()
59+
\end{lstlisting}
60+
61+
This then allows us to act on each of these instances as though they are separate.
62+
What does that mean?
63+
It means that if we were to modify a property of \pigVar{p1} then it would not have any effect on
64+
the same properties in \pigVar{p2} and \pigVar{p3}.
65+
66+
\subsection{Properties}
67+
We are able to store variables inside of a class, these are called properties.
68+
To define a property we must define its name, access modifier and default value (if any).
69+
\par
70+
71+
An access modifier can either be \emph{public}, \emph{private} or \emph{protected} (some languages do not support
72+
access modifiers).
73+
The \emph{public} modifier means that anyone who has access to the object can read and modify that property.
74+
The \emph{private} modifier means that no one outside of the object can read and modify the property, meaning that
75+
only the object itself has acess to the given property.
76+
The \emph{protected} modifier means that the given object and its children (we will get to this later in the chapter)
77+
will have access to read and modify the property. Lets look at an example.
78+
\par
79+
80+
\begin{lstlisting}[caption={Class Properties}]
81+
class Person
82+
public name
83+
private age = 22
84+
85+
p = new Person()
86+
p.name = ``Brett Langdon''
87+
88+
p.age = 23 //this will cause an error
89+
90+
\end{lstlisting}
91+
92+
In this example we are creating a class with two properties, one is public (\pigVar{name}) and the other is private (\pigVar{age}).
93+
We then create a new instance of our class assigning it to the variable \pigVar{p}.
94+
Then we set the public property \pigVar{name} to \pigVal{``Brett Langdon''}.
95+
In line 8 there is the comment ``this will cause an error'' this is because the property \pigVar{age} is private and cannot be accessed
96+
from outside of the class.
97+
98+
99+
\subsection{Methods}
100+
So what is a Method?
101+
A method, simply put, is a function that belongs to a class.
102+
We use methods for the same reasons that we use functions for, to provide code reuse within our applications.
103+
Ok, so we know how to use functions, but how do we use them from within a class?
104+
\par
105+
106+
\begin{lstlisting}[caption={Class Methods}]
107+
class Person
108+
public name
109+
private age
110+
111+
function printName()
112+
print this.name
113+
114+
p = new Person()
115+
p.name = ``brett''
116+
p.printName()
117+
\end{lstlisting}
118+
119+
The output of this code would be \pigOut{brett}.
120+
121+
\subsection{Special Methods}
122+

Programming In General.pdf

19.4 KB
Binary file not shown.

Programming In General.tex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ \section{Classes and Objects}
163163
\input{"./4 - Object Oriented Programming/1.1 - Classes and Objects"}
164164
\vfill
165165
\pagebreak
166+
\section{Classes and Objects~}
167+
\input{"./4 - Object Oriented Programming/1.1 - Classes and Objects~"}
168+
\vfill
169+
\pagebreak
166170
\section{Inheritence}
167171
\input{"./4 - Object Oriented Programming/1.2 - Inheritence"}
168172
\vfill

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ the examples to which ever programming language they would like.
99

1010
Enjoy.
1111

12+
## PDF
13+
14+
I will always try to keep the lastest pdf version of the book provided in the repository, but if I forgot then you can build the pdf from source.
15+
1216
## Build PDF
1317

1418
To build from source you must have latex and python installed.
@@ -17,8 +21,8 @@ from the chapter subdirectories. Then it compiles the .pdf version from that
1721

1822
### Instructions:
1923

20-
git clone git@git.blangdon.com:programming_in_general.git
21-
cd "programming in general"
24+
git clone git://github.com/brettlangdon/programming-in-general.git
25+
cd "programming-in-general"
2226
./build
2327

2428
Output will be:

0 commit comments

Comments
 (0)