Thursday, 2 November 2017

Constructors in C#

C# (C-Sharp) is an object oriented programming language or OOP for short. Many modern language such as C++, Objective-C, Java, Ruby and Python are also object oriented. You can think of an object as a sort of package that wraps up data – that’s variables, such as strings and numbers – and behaviour – that’s functions or methods that do something, often by manipulating an object’s internal data.

The definition of an object is called its class. You can think of a class as a blueprint for an object. It’s just like the blueprint for a car. The blueprint defines all the fundamental features of a car but you can’t actually drive the blueprint. To use a car you have to create one based on the blueprint’s definition. That’s what you do in programming too. You write a class and you create usable objects from it. In fact, just as you can create many cars based on a single blueprint, so you can create many objects based on a single class.

When you create a new object from a class, you can call that class’s constructor to initialize the object. A constructor is a method that has the same name as the class itself. This class is called MyClass, so the constructor method will also be called MyClass.

public MyClass( ) {
}

The constructor has to be public – that is, it has to be visible to code outside the class itself – because clearly I need to call the constructor method from other parts of my program, whenever I want to create a new MyClass object.

A constructor doesn’t have to have any parameters. However, when an object has some internal fields, it is quite common for the constructor to define a list of parameters to initialize those fields. In that case, when you invoke the constructor, to create a new object, you would need to pass the appropriate arguments to initialize those fields. When an object descends from another object and the object from which it descends has a constructor that initializes some fields, the descendent class’s constructor can call the ancestor class’s constructor, passing to it any arguments required by the ancestor constructor to initialize its fields. It does that by putting a colon after the parameter list of the constructor, then the keyword base, then a comma-separated list of arguments between parentheses.

This short video (taken from my course ‘Learn C# Programming (in ten easy steps)’ gives a few more details about C# constructors.


This course was first created in 2012 but now, in 2017, it has been completely remade. Every single video has been re-done and no less than 86 new video lessons have been added. What’s more, students can also download the entire version 1 of the course (almost 4 more hours of video) as an added bonus. This is what the course contains:

  • 117 video lessons
  • 7 hours, 7 minutes of video
  • 32 sample projects
  • 10 quizzes
  • 89-pages of pdf documentation
  • PLUS: The entire version 1 course as a free download!

The regular price for this course is $145. But use the link below to sign up for just $35 (valid only until the end of 2017, so don’t wait!)

https://www.udemy.com/learn-c-sharp-programming-in-ten-easy-steps/?couponCode=35DOLLARDEAL

Wednesday, 1 November 2017

C Programming - Arrays, addresses and pointers

#include <stdio.h>

int main(int argc, char **argv) {
char str1[] = "Hello";
char *str2 = "Goodbye";
// str2 = &str1;
// str2 = str1;
printf("%d %d %s\n", &str1, str1, str1);
printf("%d %d %s\n", &str2, str2, str2);
return 0;
}

Let’s now look at how pointers and addresses work in C – and why arrays are special. In the code shown above I declare two string variables, str1 and str2. You can create string variables using either syntax but you need to understand that these two variables are by no means the same as one another. The first, str1, declared with a pair of square brackets is an array. The second, str2, declared using the star operator, *, is a pointer. As we know (see previous article), an array and an address are equivalent. So str1 is the address at which the array of characters in the string “Hello” are stored. But str2 is a pointer whose value is not the string “Goodbye” but the address of that string. So str2 'points to' the address at which the characters “Goodbye” are stored.

This should be clearer when you run the code.

When displayed as an integer value, the address of str1 (returned by the & address-of operator) is the same as the value of the variable str1 itself. That’s because str1 is an array - and an array is, in effect, an address.

But the address of str2 (returned by the & address-of operator) is different from the value of the str2 variable. Here the address gives us the location in memory of the pointer variable, str2. But the value of that variable is the address of the array of chars to which it points.

str1 is the location of the start of this array of characters, “Hello”, in computer memory. The address of str1 is the location where that string of chars begins.

But the value of the pointer variable str2 is a number that gives the location of the start of the array of characters “Goodbye” in computer memory. The address of str2 is the location where the str2 pointer variable is stored – and if we just want to get at data in the array, the address of the pointer variable itself is of no interest to us. For now, I’m only interested in the address of the array to which this pointer points. The value of str2 is the address of that array: the address of the first character in the string “Goodbye”.

The video below is taken from my online course, Advanced C Programming: Pointers. You can subscribe to this course, for a limited period, and save 63% by clicking this link. This lets you join up for $35 (regular price is $95): https://www.udemy.com/advanced-c-programming-pointers/?couponCode=CPOINTERS2017