Five Tips for Effective Object-oriented Programming in C++

November 8, 2006

Spurgeon’s Law says that 90% of everything is crap. This certainly holds true in the world of software development, and especially when it comes to object-orientation and C++ programming. This is largely due to the pervasiveness and complexity of C++; it’s one of the most commonly used object-oriented languages, and yet few people know how to use it effectively. Couple this with the fact that few programmers truly grasp the object-oriented programming philosophy and you have an instant recipe for sloppy code. Volumes can be—and have been—written on how to develop proper object-oriented software designs using C++. Obviously, there won’t be space in this article for a truly thorough discussion; however, I would like to present a few quick, simple and effective tips for developing robust C++ software.

1. Work on the class designs first before deciding on the precise sequence of operations. Many programmers fancy themselves as doing object-oriented programming, but in reality, they use structured programming with some external object-oriented trappings. Ideally, one should first select a set of software objects that provide a logical abstraction of the software, then work on the sequence of operations. (This is by no means an absolute rule though, as the process of developing this sequence often reveals ways in which the object design can be refined.)

2. Think in terms of design patterns. Design patterns allow someone to draw on years of problem-solving experience within the computer science community. Instead of reinventing the wheel, why not just pick one off the shelf?

3. Use ‘const’ objects and ‘const’ functions whenever possible. If you know that an object’s data is never supposed to be modified, then declare the object to be ‘const’. This will prevent you—or worse, a naïve colleague–from accidentally modifying its state later on. Of course, one should also declare the appropriate member functions to be ‘const’ as well, to properly enforce this rule.

4. Avoid using public ‘get’ and ‘set’ member functions. Getters and setters are not inherently bad; however, they are often a sign of poor software abstraction. ‘Get’ and ‘set’ functions implicitly force the user to think in terms of the object’s internal data (i.e. whatever these functions are getting or setting), and such details should ideally be hidden from the object’s user. Getters and setters have their place, but please use them sparingly.

5. Avoid double indirection. C programmers are forced to work with pointers, and so they are used to having pointers to pointers throughout their code. This can often lead to confusing code, due to the all-too-familiar problems of NULL references and pointer arithmetic. In contrast, C++ programmers are under no such constraints, due to the built-in support for variable references in C++; that is, one can always use references to pointers instead, which makes the code much easier to understand. (Hint: If a C++ programmer uses double indirection, that’s a clear sign that he’s still thinking like an inexperienced C hack!)

About the author:

V. B. Velasco Jr., Ph.D. has worked as an electrical and software engineer for more than a decade. He currently works for a biotech firm that provides cryopreserved PBMCs, ELISPOT analyzers and ELISPOT expertise.

Good Book: C++ How to Program (5th Edition) (How to Program)


C++ Tutorial 1, Introduction to C++

November 6, 2006

Introduction to C++

Why Learn C++?

C++ may at first seem like a boring, confusing programming language that you can only program command prompt applications with. Well, that is what it is like in the [b]begining[/b]. And you are going to need to learn the basics like this with any programming language. As you get better and progress in your C++ skills, you will begin to learn that you can start making some cool applications easier than you thought you could. The main reason to learn C++ though, is that it teaches all the basics of programming and you will learn concepts that will be used in other types of programming. Many other programming languages are like C++ or use C++. For example, PHP, a powerful web development language uses almost the same syntaxes as C++. DirectX, a common game development graphics library primarily uses C++.

Setting up C++

Ill assume that most people are not going to want to spend the money for a compiler, thats fine, there are many great free compilers. I use Dev-C++ because it has some nice features and is fast, and best of all, its FREE!

http://www.bloodshed.net/dev/devcpp.html

You can download it here.Download it and install it, this step is pretty easy. During instillation, select all the deafult values during instillation.

Create a New Project:
Go to the top menu: File -> New -> Source File, or just press Ctrl+N

You should now have a text document. Then, write the following text in the document:

#include

using namespace std;

int main() {cout<<”Hello World!”;

cin.get(); }

I would suggest typing it instead of copying it, you will get alot more out of actually typing it rather than copying it, because you get used to the syntaxes and development.Run the Application:
Now that you have your first C++ Application, [b]Press F9 [/b] to compile and run it. If you did everything right, it should open a command prompt like application and say ‘Hello World’ or whatever you told it to say. If it doesnt, look over the code and make sure everything is correct.

Code Analysis

#include
Tells the compiler that we are going to be using the iostream library, which includes information on things we will be using in the code.

using namespace std;
Saves time by telling the compiler that we are going to be using the ’std::’ functions, which stands for ’standard’.

int main()
Starts the main function, this is were the heart of the program is. I will get into what functions are later on.

{ Starts the main function.

cout<<”Hello World!”;[/code] Writes "Hello World!" to the screen. cout stands for 'console output.'

cin.get();[/code] Gets a variable from the user, we wont really be using this, but it is what will keep our application from being closed really quick.

} Closes our main function.

Analysis:
Now that we have made a simple C++ Application, we will start getting into more advanced topics so you can start making useful applications.

Chris Silop - http://www.syschat.com Computer Forum

 

Good Book:

 

Bundle of Algorithms in C++, Parts 1-5: Fundamentals, Data Structures, Sorting, Searching, and Graph Algorithms (3rd Edition)