Yale CPSC 427 - Chapter 2: Issues and Overview
School name Yale University
Pages 12

Unformatted text preview:

2 Issues and Overview2.1 Why did C need a ++?2.1.1 Design Goals for C2.1.2 C++ Extends C.2.1.3 Modeling.2.2 Object Oriented Principles.2.3 Important Differences2.4 Generic Insertion Sort2.4.1 Main Program2.4.2 The DataPack Header File2.4.3 The Data Pack FunctionsChapter 2: Issues and Overview2.1 Why did C need a ++?For application modeling. Shakespeare once explained it for houses, but it should be true of programs as well:When we mean to build, we first survey the plot then draw the model.. . . Shakespeare, King Henry IV.2.1.1 Design Goals for CC was designed to write Unix. C is sometimes called a “low level” language. It was created by DennisRitchie so that he and Kenneth Thompson could write a new operating system that they named Unix. Thenew language was designed to control the machine hardware (clock, registers, memory, devices) and implementinput and output conversion. Thus, it was essential for C to be able to work efficiently and easily at a low level.Ritchie and Thompson worked with small, slow machines, so they put great emphasis on creating an simplelanguage that could be easily compiled into efficient object code. There is a direct and transparent relationshipbetween C source code and the machine code produced by a C compiler.Because C is a simple language, a C compiler can be much simpler than a compiler for C++ or Java. Asa result, a good C compiler produces simple error comments tied to specific lines of code. Compilers for full-featured modern languages such as C++ and Java are the opposite: error comments can be hopelessly wordyand also vague. Often, they do not correctly pinpoint the erroneous line.Ritchie never imagined that his language would leave their lab and become a dominant force in the worldand the ancestor of three powerful modern languages, C++, C#, and Java. Thus, he did not worry aboutreadability, portability, and reusability. Because of that, readability is only achieved in C by using self-disciplineand adhering to strict rules of style. However, because of the clean design, C became the most portable andreusable language of its time.In 1978, Brian Kernighan and Dennis Ritchie published “The C Programming Language”, which served asthe only language specification for eleven years. During that time, C and Unix became popular and widespread,and different implementations had subtle and troublesome differences. The ANSI C standard (1989) addressedthis by providing a clear definition of the syntax and the meaning of C. The result was a low-level languagethat provides unlimited opportunity for expressing algorithms and excellent support for modular program con-struction. However, it provides little or no support for expressing higher-level abstractions. We can write manydifferent efficient programs for implementing a queue in C, but we cannot express the abstraction “queue” in aclear, simple, coherent manner.2.1.2 C++ Extends C.C++ is an extension and adaptation of C. The designer, Bjarne. Stroustrup, originally implemented C++ as aset of macros that were translated by a preprocessor into ordinary C code. His intent was to retain efficiency andtransparency and simultaneously improve the ability of the language to model abstractions. The full C languageremains as a subset of C++ in the sense that anything that was legal in C is still legal in C++ (although somethings have a slightly different meaning). In addition, many things that were considered “errors” in C are nowlegal and meaningful in C++.Readability. C++ was no more readable than C, because C was retained as the basic vehicle for coding inC++ and is a proper subset of C++ . However, an application program, as a whole, may be much more readablein C++ than in C because of the new support for application modeling.78 CHAPTER 2. ISSUES AND OVERVIEWPortability. A portable program can be “brought up” on different kinds of computers and produce uniformresults across platforms. By definition, if a language is fully portable, it does not exploit the special featuresof any hardware platform or operating system. It cannot rely on any particular bit-level representation of anyobject, operation, or device; therefore, it cannot manipulate such things. A compromise between portabilityand flexibility is important for real systems.A program in C or C++ can be very portable if the programmer designs it with portability in mind andfollows strict guidelines about segregating sections of code that are not portable. Skillful use of the prepro-cessor and conditional compilation can compensate for differences in hardware and in the system environment.However, programs written by naive programmers are usually not portable because C’s most basic type, int,is partially undefined. Programs written for the 4-byte integer model often malfunction when compiled under2-byte compilers and vice versa. C++ does nothing to improve this situation.Reusability. Code that is filled with details about a particular application is not very reusable. In C, thetypedef and #define commands do provide a little bit of support for creating generic code that can be tailoredto a particular situation as a last step before compilation. The C libraries even include two generic functions(quicksort() and bsearch() that can be used to process an array of any base type. C++ provides muchbroader support for this technique and provides new type definition and type conversion facilities that makegeneric code easier to write.Teamwork potential. C++ supports highly modular design and implementation and reusable components.This is ideal for team projects. The most skilled members of the group can design the project and implementany non-routine portions. Lesser-skilled programmers can implement the routine modules using the expert’sclasses, classes from the standard template library, and proprietary class libraries. All these people can worksimultaneously, guided by defined class interfaces, to produce a complete application.2.1.3 Modeling.The problems of modeling are the same in C and C++. In both cases the questions are, what data objects doyou need to define, how should each object work, and how do they relate to each other? A good C programmerwould put the code for each type of object or activity in a different file and could use type modifiers externand static to control visibility. A poor C programmer, however, would throw it all into one file, producingan unreadable and incomprehensible mess. Skill and style made a huge difference.


View Full Document

Yale CPSC 427 - Chapter 2: Issues and Overview

Download Chapter 2: Issues and Overview
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 2: Issues and Overview and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 2: Issues and Overview 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?