Archive

Archive for May, 2023

Making Waves.. C# or C++?

I often write small apps to be used either for radio and electronics calculations – antennas dimensions, inductance, filters, etc or for small business apps – text editors, small databases, etc. Mostly I have used C# from its early inception to its modern format with .NET and .NET Core. I have, in the past, used other languages including BASIC (in its original format not the VB stuff) and C, which I used for programming MS-DOS computers (before there was Windows). I mostly missed out on the C++ introduction when, at the time, a friend described it as an abomination of C because it was originally released as C with classes/objects. This is why all future versions of C++ have been backwardly compatible with C-type commands by the way..

Anyway, moving on, I decided to give C++ another try and did a couple of courses to bring myself up to speed with it. Having spent years using C# – which is also an abomination of C with objects – C++ turned out to be rather difficult at first because of all the shortcuts I had learned with C#.

C# has classes, you create a class with members then, elsewhere in the program, you create a new object of the class to have its own values. Suppose we have a car, many cars are made, each with different specs, so the class would be the car and the object created from that class would have its own specs relating to the class members.

class Car
{
	public string Make {get; set;}
	public string Model {get; set;}
	public double engineSize {get; set;}
	int yearOfManufacture = {get; set;}
}

//access it elsewhere

static void Main(string[] args)
{
	Car c = new Car();  //create a new object of the class.
	c.Make = "Ford";
	c.Model = "Escort";
	c.engineSize = 1.3;
	c.yearOfManufacture = 1970;
	
	string M = c.Make;
	string Mo = c.Model;
	double eS = c.engineSize;
	int y = yearOfManufacture;
	
	Console.WriteLine("Make: " + M + " Model: " + Mo + " Engine: " + eS + "litres, Year of Manufacture: " + y + "\n");

C++, on the other hand, would create two extra files for the car class. These would be Car.cpp and Car.h. Car.cpp would contain a call to Car.h and Car.h would contain a reference to each of the members of the class. Of course, also in C++, a class can be created as a struct, just to make it a little more confusing. So as to avoid pages of code being listed here, I am giving a couple of links below to github repos that I have created, one in C++ and one in C#. They both do the same thing but demonstrate the differences between the two languages. They are both public repos so can be accessed by anyone.

https://github.com/m0cvo/Person https://github.com/m0cvo/OtherPerson

So, back to the original question, which should I use. Well, they both have their advantages – C++ based programs tend to run faster once compiled but the continued use of some old C-type commands and variables can make them prone to bugs. Also, C++ is a more complex language to work in which may be a barrier to some. C# based programs are more strongly typed and therefore less prone to bugs – not totally free of them though. With the onset of the new .NET platform for C# they are also better for cross platform apps now and the language is less complex. So horses for courses really I suppose. Having use of both languages means that the best one for the job can be used depending on what the target app is going to do.

Until next time…..

Categories: Uncategorized Tags: , , ,