Example: bankruptcy

A Transition Guide: Python to C++ - Denison University

A Transition Guide: Python to C++Michael H. GoldwasserDavid LetscherThis is a supplement to the book,Object-Oriented programming in Python ,Prentice-Hall, 2007. ISBN-13 rights Purpose of This GuidePython is a wonderful programming language and we expect that readers of this book will find many oppor-tunities to use it. That said, there are many different programming languages used by software language has its own strengths and weaknesses, and professionals must become accustomed to program-ming in different languages. Fortunately, once you have a solid foundation in one language it becomes easierto Transition to another language. This guide is designed for readers choosing C++ as a second ++ is among the most widely used languages in industry. As object-oriented languages, they have a greatdeal in common with Python . Yet there exist significant differences between the two Transition guide is not meant to serve as a complete self-contained reference for C++.

Python is a wonderful programming language and we expect that readers of this book will find many oppor- tunities to use it. That said, there are many different programming languages used …

Tags:

  Programming, Python

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of A Transition Guide: Python to C++ - Denison University

1 A Transition Guide: Python to C++Michael H. GoldwasserDavid LetscherThis is a supplement to the book,Object-Oriented programming in Python ,Prentice-Hall, 2007. ISBN-13 rights Purpose of This GuidePython is a wonderful programming language and we expect that readers of this book will find many oppor-tunities to use it. That said, there are many different programming languages used by software language has its own strengths and weaknesses, and professionals must become accustomed to program-ming in different languages. Fortunately, once you have a solid foundation in one language it becomes easierto Transition to another language. This guide is designed for readers choosing C++ as a second ++ is among the most widely used languages in industry. As object-oriented languages, they have a greatdeal in common with Python . Yet there exist significant differences between the two Transition guide is not meant to serve as a complete self-contained reference for C++.

2 Our goalis to provide an initial bridge, built upon the knowledge andterminology that we have gained in begin in Section 1 by providing a high-level discussion about programming languages and the moresignificant differences between Python , and C++. Section 2 provides our first direct comparison betweenPython source code and C++ code. As much as possible, we rely upon earlier examples in Python and thentranslate these to C++. From there we will go into specific syntax and usage of C++. In Section 3 we discussthe major features of C++. Writing classes are discussed in Section 4 with coverage of Python and C++ sobject models and memory management in Section 5. Storage ofC++ objects in containers is discussed inSection??. Good software practices and working with larger programs,including the Mastermind case studyis in Section??. And writing template based classes is in Section??. And in Section?

3 ?we discuss how tointegrate the use of Python and C++ in a single program. The full source code for all of the programs iscontained in an that said, let the tour begin..1 Contents1 High-Level programming Convenience versus Efficiency .. Interpreter versus Compiler .. Dynamic versus Static Typing .. Why C++ .. 52 A First Glance at C++63 C++ Data Types and Operators .. Input and Output .. Control Structures .. Defining a Function .. Managing a Complete Program .. 164 Classes in C++ Using Classes .. Defining a Class .. Inheritance .. 225 Assignments and the Object Model241 High-Level programming LanguagesTo begin, we recommend that you reread Section of the book, where we first describe the distinctionbetweenlow-levelandhigh-level programming languages. At its core, a computing architecture supportsan extremely limited set of data types and operations.

4 For this reason, we describe a CPU s machinelanguage as alow-levelprogramming language. It is possible to develop software directly for that machinelanguage. In fact, this is often done for specialized applications where execution speed is of utmost , it is extremely inconvenient to develop complex software systems in a low-level language. High-levelprogramming languages were conceived to better support a programmer s expressiveness, thereby reducingthe development time of software systems, providing greater opportunity for code reuse, and improving theoverall reliability and maintainability of Convenience versus EfficiencyIn effect, high-level languages offer convenience. They support a greater range of data types and a richersyntax for expressing a series of operations. Yet this additional support is somewhat artificial. In the end,the software must be translated back to the CPU s machine language in order to be executed on a high-level languages, this translation has been automated in the form of a compiler or interpreter.

5 As aresult, software written in a high-level language is no morepowerful than equivalent software that could havebeen written directly in the low-level language (given enough time and expertise). The convenience affordedby a high-level language often comes at the expense of some slight inefficiencies in the performance of theresulting software. The automated translation from high level to low level has been carefully optimized, butstill the generated low-level code is not always as streamlined as code crafted directly by an expert in as a society, we simply cannot afford to have each and everypiece of software hand-crafted in a low-level language. While there are slight discrepancies in efficiency, those are quickly negated by improvementsin hardware, networks, and other aspects of a computing environment. A more significant concern is thesoftware development time, that is, the time it takes to carry an idea from the initial inspiration to the2final software for a consumer.

6 The design and development of quality software applications is extremelylabor-intensive, and can take months or years depending on the project. The single biggest factor in the costof a software project is employing the developers. So there is great benefit in use of a high-level languagethat can better support abstractions and thereby reduce theoverall development than a thousand high-level languages have been developed over time, with perhaps a hundred thatare still actively used for program development. What makeseach language unique is the way in whichconcepts are abstracted and expressed. No single language is perfect, and each strikes its own balance intrying to support the development of efficient, maintainable, and reusable software. This guide is limitedto the examination of three specificobject-orientedlanguages, yet the object-oriented paradigm is just oneexample of an abstraction for program development.

7 Even within the object-oriented framework, there aredifferences between languages. In the remainder of this section, we discuss the most significant ways in whichPython and C++ Interpreter versus CompilerAn important aspect of any high-level language is the process by which it is translated back to the low-levelmachine code to be executed. Python is an example of aninterpretedlanguage. We run a typical Pythonprogram by feeding its source code as input to another piece of software known as the Python Python interpreter is the software that is actually executing on the CPU. It adapts its outward behaviorto match the semantics indicated by the given source code. Ineffect, the translation from the high-level codeto low-level operations is performed on-the-fly, each time the program is contrast, C++ is an example of acompiledlanguage. Progressing from the original source code to arunning program is a two-step process.

8 During the first phase( compile-time ), the source code is fed asinput to a special piece of software known as acompiler. That compiler analyzes the source code based on thesyntax of the language. If there are syntax errors, they are reported and the compilation fails. Otherwise, thecompiler translates the high-level code into machine code for the computing system, generating another fileknown as an executable. During the second phase (the run-time ), the executable is independently startedby the user; the compiler is no longer needed unless a new executable must be generated, for example whena change is made to the original source greatest advantage of the compilation model is execution speed. In essence, the more that can behandled at compile-time, the less work there is to be done at run-time. By performing the full translationto machine code in advance, the execution of the software is streamlined so as to perform only those compu-tations that are a direct part of the software application.

9 Asecond advantage is that the executable can bedistributed to customers as free-standing software. So long as it was designed for the particular machine codeof their system, it can be executed by those users without anyfurther requirement of software installations( , an interpreter). A consequence of this model is that the machine code can be distributed by a companywithout exposing the original source code that was used to generate it (although some companies choose to open source their software).In contrast, there is no distinction between compile-time and run-time for a purely interpreted interpreter bears the burden of translating the original source code as part of the run-time , distributing the source code is only useful toa customer who has a compatible interpreterinstalled on his or her system. The primary advantage of an interpreted language is greater platform-independence.

10 The same source code can be distributed for use on different computing platforms, so long aseach platform has a valid interpreter. In contrast, a compiled executable is catered to one particular machinelanguage; different versions of the executable must be distributed for use on different computing software developers, the debugging cycle varies a greatdeal when working with an interpreter languageversus a compiled language. For example, we have readily used Python s interpreter not just as a means forrunning a final version of a program, but to provide useful feedback and interaction when problems compiler can be helpful in detecting purely syntacticalerrors at compile-time, but it is no longer of usewhen run-time errors Dynamic versus Static TypingFor compiled languages, there is an advantage in doing as much work as possible at compile-time, so as tostreamline the run-time process.


Related search queries