Archive for the 'C++ / C#' Category

Objects (Classes) Explained In Very Simple Terms

One of the most common questions on Yahoo! Answers is how classes or objects, or the parts of a class / object, such as properties, methods and events, work.

Here’s a gross simplification of how classes and objects work. Most of this is lifted from a previous blog entry, Interpreting A C# Class Code Structure.

Classes / Objects

A class or object is a way of combining all sorts of data that relates to a single thing in one place, and a way of associating functions with that data. (For most intents and purposes, classes and objects are the same thing. Whenever I use the term class, I also mean object; whenever I use object, I also mean class. More on that in a moment.)

For example, you can think of a human as a class.

In the human class, you are an instance. For example, you might think that God decided to run this code when you were born:

Dim you As New HumanBeing()

What He did was create a variable, named you, and set the value of that variable to be an instance of the HumanBeing object. An instance is the actual use of an object. The object is the mold; the instance is the actual thing you made from the mold.

(Most programmers use the term class when they are speaking about an instance, and object when they are talking in general terms, so that’s the real difference between class and object. However, you can generally use the two terms interchangeably when discussing a specific programming issue.)

Read the rest of this entry »

Variable Scope Made Simple

Recently asked on Yahoo! Answers:

What does this statement mean? “Local variables are local to a function or a procedure.”?

What it means, in a nutshell, is if you declare a variable inside a function or procedure, you cannot get the value of that variable from outside that function or procedure.

Read the rest of this entry »

Interpreting A C# Class Code Structure

Recently asked on Yahoo! Answers:

C# Code Explanation?
using System;
class Base {

public Base() {
Console.WriteLine(”Printing the Base constructor 1!”);
}

public Base(int x) {
Console.WriteLine(”Printing the Base constructor 2!”);
}
}

class Derived : Base {
// implicitly call the Base(int x)
public Derived() : base(10) {
Console.WriteLine(”Printing the Derived constructor!”);
}
}

class MyClass {
public static void Main() {
// Displays the Base constructor 2 followed by Derived Constructor
Derived d1 = new Derived();
Console.ReadLine();
}
}

This is clearly a sample some professor has put together to illustrate some basic principles behind classes, which are also often called “objects.”

First, let’s get a really rudimentary understanding of what a class is.

A class is a way of combining all sorts of data that relates to a single thing in one place, and a way of associating functions with that data.

For example, you can think of a human as a class.

Humans have certain properties — skin color, height, weight, age, etc. We also have methods, or ways of going about doing things or changing our properties: eating, sleeping, dieting, for example. Finally, we have triggers, or events, that tell us when to employ a method or change properties: hunger, cold, illness, marriage, etc.

Read the rest of this entry »