Core C++ A Software Engineering Approach phần 2 pptx

120 300 0
Core C++ A Software Engineering Approach phần 2 pptx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

file://///Administrator/General%20English%20Learning/it2002-7-6/core.htm x = 4*a = b*c; // syntax error: there is no lvalue for 4*a Simpo PDF Merge and Split Unregistered Version - http://www.simpopdf.com In addition to the traditional assignment operator, C++ has a number of variants¡Xarithmetic assignment operators Their goal is to shorten arithmetic expressions For example, instead of saying x = x + y; we can say x + = y; The result is the same These assignment operators are available for all binary operators ('+=', '-=', '*=', '/=', '%=', '&=', '|=', '^=', ' a2.number) // swap accounts to order numbers Simpo PDF Merge and Split { a3=a1; a1=a2; a2=a3; } Unregistered Version -a3 holds data temporarily // http://www.simpopdf.com The next example shows some graphical computations Since the drawing functions are not portable, I not want to draw on the screen in my examples Instead, I ask the user to enter coordinates of endpoints of two lines, AB and CD Then I compute the length of each line and the angle between each line and the x-axis Listing 5.12 shows the source code for this example that uses two data types, Point and Line The program requests input data in pixels (integers), uses the data to initialize points, uses these points to initialize the lines, and then computes the line lengths and angles Functions sqrt() and atan2() come from the math.h library header file They compute the square root and arctangent of their double parameters Because their actual arguments are defined as int, they are converted implicitly to double Since line length is also expressed in pixels as integers, the result of computing the square root is cast implicitly To avoid truncation, 0.5 is added to the length to achieve proper rounding Variable coeff converts angles from radians to degrees Figure 5-12 shows the results of the execution of the program Figure 5-12 Execution example for program in Listing 5.12 Example 5.12 Using #include directive for programmer-defined types #include #include atan2() #include "point.h" nt known to compiler #include "line.h" known to compiler using namespace std; int main () { const double coeff = 180/3.1415926536; Point p1, p2; Line line1, line2; rammer-defined types int diffX, diffY, length1, length2; double angle1, angle2; cout > p1.x >> p1.y; cout > p2.x >> p2.y; line1.start = p1; line1.end = p2; file://///Administrator/General%20English%20Learning/it2002-7-6/core.htm (235 of 1187) [8/17/2002 2:57:49 PM] file://///Administrator/General%20English%20Learning/it2002-7-6/core.htm cout > p1.y; Split Unregistered Version - http://www.simpopdf.com >> p1.x Merge and cout > p2.x >> p2.y; line2.start = p1; line2.end = p2; diffX = line1.end.x - line1.start.x; // ugly notation diffY = line1.end.y - line1.start.y; length1 = sqrt(diffX*diffX + diffY*diffY) + 0.5; angle1 = atan2(diffY,diffX) * coeff; cout

Ngày đăng: 12/08/2014, 11:20

Từ khóa liên quan

Mục lục

  • Table of Content

  • Preface

  • Part I: Introduction to Programming with C++

    • Chapter 1. Object-oriented approach: What's So Good About It?

    • Chapter 2. Getting Started Quickly: A Brief Overview of C++

    • Chapter 3. Working with C++ Data and Expressions

    • Chapter 4. C++ Control Flow

    • Chapter 5. Aggregation with Programmer-Defined Data Types

    • Chapter 6. Memory Management: the Stack and the Heap

    • Part II: Object-oriented programing with C++

      • Chapter 7. Programming With C++ Functions

      • Chapter 8. Object-Oriented Programming with Functions

      • Chapter 9. C++ Class as a Unit of Modularization

      • Chapter 10. Operator Functions: Another Good idea

      • Chapter 11. Constructors and Destructors: Potential Trouble

      • Part III: Object-Oriented Programming with Aggregation and Inheritance

        • Chapter 12. Composite Classes: Pitfalls and Advantages

        • Chapter 13. Similar Classes: How to Treat Them

        • Chapter 14. Choosing between Inheritance and Composition

        • Part IV: Advanced uses of C++

          • Chapter 15. Virtual Functions and other Advanced Uses of Inheritance

          • Chapter 16. Advanced Uses of Operator Overloading

          • Chapter 17. Templates: Yet Another Design Tool

          • Chapter 18. Programming with Exceptions

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan