Xem mẫu

different, and why C++ in particular is different, concepts of OOP methodologies, and finally the kinds of issues you will encounter when moving your own company to OOP and C++. OOP and C++ may not be for everyone. It’s important to evaluate your own needs and decide whether C++ will optimally satisfy those needs, or if you might be better off with another programming system (including the one you’re currently using). If you know that your needs will be very specialized for the foreseeable future and if you have specific constraints that may not be satisfied by C++, then you owe it to yourself to investigate the alternatives21. Even if you eventually choose C++ as your language, you’ll at least understand what the options were and have a clear vision of why you took that direction. You know what a procedural program looks like: data definitions and function calls. To find the meaning of such a program you have to work a little, looking through the function calls and low-level concepts to create a model in your mind. This is the reason we need intermediate representations when designing procedural programs – by themselves, these programs tend to be confusing because the terms of expression are oriented more toward the computer than to the problem you’re solving. Because C++ adds many new concepts to the C language, your natural assumption may be that the main( ) in a C++ program will be far more complicated than for the equivalent C program. Here, you’ll be pleasantly surprised: A well-written C++ program is generally far simpler and much easier to understand than the equivalent C program. What you’ll see are the definitions of the objects that represent concepts in your problem space (rather than the issues of the computer representation) and messages sent to those objects to represent the activities in that space. One of the 21 In particular, I recommend looking at Java (http://java.sun.com) and Python (http://www.Python.org). 80 Thinking in C++ www.BruceEckel.com delights of object-oriented programming is that, with a well-designed program, it’s easy to understand the code by reading it. Usually there’s a lot less code, as well, because many of your problems will be solved by reusing existing library code. 1: Introduction to Objects 81 2: Making & Using Objects This chapter will introduce enough C++ syntax and program construction concepts to allow you to write and run some simple object-oriented programs. In the subsequent chapter we will cover the basic syntax of C and C++ in detail. 83 By reading this chapter first, you’ll get the basic flavor of what it is like to program with objects in C++, and you’ll also discover some of the reasons for the enthusiasm surrounding this language. This should be enough to carry you through Chapter 3, which can be a bit exhausting since it contains most of the details of the C language. The user-defined data type, or class, is what distinguishes C++ from traditional procedural languages. A class is a new data type that you or someone else creates to solve a particular kind of problem. Once a class is created, anyone can use it without knowing the specifics of how it works, or even how classes are built. This chapter treats classes as if they are just another built-in data type available for use in programs. Classes that someone else has created are typically packaged into a library. This chapter uses several of the class libraries that come with all C++ implementations. An especially important standard library is iostreams, which (among other things) allow you to read from files and the keyboard, and to write to files and the display. You’ll also see the very handy stringclass, and the vectorcontainer from the Standard C++ Library. By the end of the chapter, you’ll see how easy it is to use a pre-defined library of classes. In order to create your first program you must understand the tools used to build applications. The process of language translation All computer languages are translated from something that tends to be easy for a human to understand (source code) into something that is executed on a computer (machine instructions). Traditionally, translators fall into two classes: interpreters and compilers. 84 Thinking in C++ www.BruceEckel.com ... - tailieumienphi.vn