home comics writing pictures archive about

2018-04-24 - C++

C++ started out as an extension to C that soon developed into its own unique language. While C is simple yet powerful C++ is complex yet very powerful. C++ is an object oriented language which means that the language is focused around creating classes consisting of attributes and actions. These classes are used as schematics for creating instances of those classes called objects. The attributes provide information about the object and the actions define what the object can do. C++ also includes a vast standard library that provide classes for many common tasks.

C++ has grown over the years and tries to meet the needs of many different scenarios. The many features of C++ are one of its greatest strengths and also one of its greatest weaknesses. In C++ there are usually multiple ways to do the same thing and it's not always clear why one should be preferred over the others. In C the only way to cast a value from one type to another was to enclose the desired type in parenthesises before the value. In C++ you can do casts the C way which is discouraged or you can use the new dynamic_cast, static_cast, reinterpret_cast and const_cast operators. All of these different casts are meant for specific types of conversions. The problem is that now you have to think about your specific scenario in order to determine the best one to use.

C++ is by no means a bad language. It is simply a language that requires you to be very aware of what you are doing which might be the biggest thing it inherited from C. Instead of trying to provide a singular preferred path it instead went for many possible paths each meant for a different situation. Probably the best indicator of the importance of C++ is the number of languages that came after it. You can see the influences of C++ in the syntax and style of many modern object oriented languages. 

Although C++ can be frustrating to use at time it's hard to give up the many tools it provides to you.

2018-03-25 - In IL: Print the Alphabet (while)

The while loop is the simplest form of loop. It simply repeats the specified operations as long as the specified condition is true. To show off the while loop we are going to use a program that prints out the letters of the alphabet.

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsWhile
{
class Program
{
static void Main(string[] args)
{
char ch = 'A';
while(ch <= 'Z')
{
Console.Write(ch);
ch++;
}
Console.WriteLine();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

As you can see this is a very simple program. We start at the letter A and then print and increment it until it's the letter Z. This works because characters are actually just numbers and in the encoding used by C# all capital letters are continuous and in order.

Now let's compile this program and see what we get.

Main
.method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// Code size 27 (0x1b)
.maxstack 2
.locals init ([0] char ch)
IL_0000: ldc.i4.s 65
IL_0002: stloc.0
IL_0003: br.s IL_0010
IL_0005: ldloc.0
IL_0006: call void [mscorlib]System.Console::Write(char)
IL_000b: ldloc.0
IL_000c: ldc.i4.1
IL_000d: add
IL_000e: conv.u2
IL_000f: stloc.0
IL_0010: ldloc.0
IL_0011: ldc.i4.s 90
IL_0013: ble.s IL_0005
IL_0015: call void [mscorlib]System.Console::WriteLine()
IL_001a: ret
} // end of method Program::Main
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

As you can see there's no special looping instructions. Instead we just get a couple of branching instructions. We start by initializing our variable to 65 ('A') and then we jump to a point near the end of the function. There we find a second branch that tests if the local variable is less than or equal to 90 ('Z') and jumps backwards in the function if it is. There we find the innards of our loop where we print out and increment the local variable. Then we find ourselves at the second branch point again and perform the test again. If it's true we repeat or loop again and if it's false we continue on and the function ends.

Next time we will look at other kinds of loops.

2018-03-04 - The easier road

I'm currently working on converting the writings section into a hierarchy of pages instead of the simple category-page system it has now. This allows me the freedom to have as many or a few levels as I need instead of trying to force all pages into a fixed arrangement. One of the main requirements for the new version is that it should support using Open Live Writer, the program I use to write blog posts, to create and edit pages. That way I have a consistent and simple way to maintain the content on the site which should encourage me to write more.

I started down the road of duplicating the interface I had for the blog into the writing section. The plan was to use the title as the path to the page. For example I would have a page with a title of "Programming/Languages/C++" where "C++" would be the actual title of the page and "Languages" and "Programming" would be the titles of parent pages. The API would split the title into parts based on slashes and use that to identify the page being created, edited or deleted. There would also have been logic to create any missing pages in the path and delete any pages that were empty as a result of the changes. That is until I discovered an easier road.

While looking into what information Open Live Writer encodes in a post when sending it to the website I found mentions of "Supports pages" and "Supports parent" options. Pages and parents? That's exactly what I'm trying to implement. After some investigation I discovered that Open Live Writer had a second editor mode for dealing with a hierarchies of pages. All I had to do was tell it that my website supported Pages and it would enable that functionality for me.

I was very happy to find this because it made things a lot simpler. Instead of abusing the title to give more information that it was intended to give I could get that information from places specifically decided to contain that information. On the other hand I was also slightly saddened to find this. I had spent a lot of time planning out how to handle the title as a path and had already started writing code for some of the methods. Now I had to throw all of that away.

The easier road is nice but sometimes it's annoying when you don't notice it until after you've been walking down the hard road for a while.

2018-02-10 - Parts of Speech: Articles

Articles are used to indicate which entity a noun refers to. The direct article (The in English) is used to refer to a specific thing while the indirect article (A or An) is used to indicate a generic thing. A is used when the noun begins with a consonant sound while An is used when the noun begins with a vowel sound. For example it would be "A house" but "An H".

Articles are actually a type of adjective but I wanted to talk about articles specifically before talking about adjectives in general because articles allow us to be able to completely describe a simple sentence. The main parts of a sentence are the noun and the verb. The noun said what did something and the verb says what that something did. The article helps to indicate which something is being referred to and although they aren't required they do show up often.

The cat jumps
Article Noun Verb

Acardrives
ArticleNounVerb

catsjump
NounVerb

Next we will go back a bit and talk more about adjectives.

Prev page

9 10 11 12 13

Prev page