1.71k likes | 2.52k Views
Programming in C++. The Turbo C++ Environment C++ Program Structure Modular Programming with Functions C++ Control Structures Advanced Data Types Classes. Turbo C++ Environment. Windows based product Integrated Development Environment (IDE) editor compiler linker debugger.
E N D
Programming in C++ • The Turbo C++ Environment • C++ Program Structure • Modular Programming with Functions • C++ Control Structures • Advanced Data Types • Classes
Turbo C++ Environment • Windows based product • Integrated Development Environment (IDE) • editor • compiler • linker • debugger
Structure of a C++ Program preprocessor directives main function header { declare statements statements }
Using the Turbo C++ IDE • tool bars • menu • editor
Using the Turbo C++ IDE (2) • compiling • linking • executing
Developing Programs • Understand the problem • Design a solution to the problem • including test cases and the solutions to the test cases • Implement and document • Translate the solution to a programming language
Developing Programs (2) • Verify • Test and debug the solution • using test cases from design phase • Maintain
Problem Solving (1) consider a trapezoid -- 4 sided figure in which two sides are ||, the area is 1/2 the product of the height and the sum of the lengths of the two bases. b1 h b2 Area = (b1 + b2)h/2
Problem Solving -- Trapezoid Pseudocode input b1 input b2 input height bases = b1 + b2 area = bases * h /2 output area
Problem Solving (2) consider finding the area and circumference of a circle pi = 3.14159 area = pi * radius2 circumference = 2 * pi * radius
Problem Solving -- Circle Functions Pseudocode pi = 3.14159 input radius circum = 2 * pi * radius area = pi * radius*radius output area output circum
Problem Solving (3) consider converting temperatures from Centigrade to Fahrenheit (or vice versa) where c = 5/9(f-32) f = 9/5c + 32
Problem Solving --Temperature Conversion Pseudocode input temp input scale if scale = = ‘f’ newtemp = 5/9 (temp-32) else newtemp = 9/5 temp + 32 output newtemp
Problem Solving (4) consider sales commissions based upon the number of sales made during the time period $8 per sale for < 15 sales $12 per sale = 15 sales $16 per sale > 15
Problem Solving -- Commission Pseudocode • quota = 15 • input number_sales • if number_sales < quota • rate = 8 • else if number_sales == quota • rate = 12 • else rate = 16 • com = rate * number_sales • output com
Problem Solving -- Commission Pseudocode Multiple Salespeople quota = 15 input number_salespeople
Problem Solving -- Pseudocode Multiple Salespeople (2) • loop number_salespeople times • input number_sales • if number_sales < quota • rate = 8 • else if number_sales == quota • rate = 12 • else rate = 16 • com = rate * number_sales • output com
Exercise -- GO Develop a series of problems for the students to do using each of the statement types
Introduction to the C++ Language • keywords • C++ is case-sensitive • identifiers • can not be keywords • comments • enclosed in /* */ multi-line • start with // single line
Preprocessor Statements library header files -- #include < > -- system library #include <iostream.h> “ “ -- personal library #include “apstring.h”
Data Types and Declarations • declare statement • allocates memory and assigns “name” • data type name [= initial value]; • int -- 2 bytes • float -- 4 bytes • double -- 8 bytes • char -- enclosed in ‘ ‘
User Defined Data Types • class -- mechanism to establish new data types • ap classes • string • apstring.h apstring.ccp • bool • bool.h
int a; int a,b,c; float x, y; double average = 0.0; char answer = ‘Y’; bool another; bool more = false; apstring name; apstring class = “C++”; Example Declare Statements
Input and Output Statements #include <iostream.h> cout -- output << insertion character cin -- input >> extraction character
Using APSTRING Class • #include “apstring.h” • entire path • create project • place apstring and program in project • you will need a different project for each program
Input and Output Statements (2) • COUT -- control codes • way of inserting placement control • \n -- new line • \t -- tab • iomanip.h • contains more formatting methods
Arithmetic in C++ • operator precedence ( ) *, /, % (left to right) +, - (left to right) • integer arithmetic • operations involving integers yielding integer results • truncation on integer division • % -- modulo operator
Arithmetic in C++ (2) • mixed mode arithmetic • operands of different data types • hierarchy double/float/int • highest mode is used • determined on a operation by operation basis
Assignment Statements • assignment operator = • operator precedence and mixed mode arithmetic hold • combination operators +=, -=, *=, /=, %= • variable = expression;
Increment and Decrement Statements • special operators which add or subtract one from a variable • more efficient (generates inc, dec) • a++; ==> a = a+1; • a--; ==> a = a -1; • postfix (a++;) (a--;) • done after the expression is evaluated • prefix (++a;) (--a;) • done prior to evaluating the expression
Type Casting • changes the evaluation data type of the expression • does not change the data type of the variable • (data type) • (int) • (float) • (apstring)
Programming Problems • convert distance in miles to distance in kilometers and meters • 1 mile = 1.61 km, 1 km = 1000 meter • convert a temperature in Celsius to Kelvin • -273.15oC = 0oK • convert a temperature in Fahrenheit to Kelvin • -459.67oF = 0oK
Mathematical Functions (math.h) • code reuse • sqrt, pow, exp, log, log10 • abs, ceil, floor • trigonometric functions
Programming Problems • determine the volume of a sphere with an input radius • volume = (4 * pi * radius3)/3 • determine the area of a triangle when given length of two sides and the included angle in degrees • degrees = 180 * radians / pi • area = side1 * side2 * sin (radians) / 2
Programming Problems (2) • determine the distance from a point on the Cartesian plane to the origin • distance = sqrt (x2 + y2)
Exercise -- GO Implement the sequential problems developed in the first exercise
Modular Programming with Functions • designed in small segments • each segment implemented as a function • sqrt, pow, sin • self contained • requires input through parameters • sends output through name • Abstraction • know what the function does and what it needs to do its task • not how the function works
Modular Programming with Functions (2) • allows for reuse • eliminates redundancy • allows for team development • simplifies logic
Form of a Function [return data type] Function name (parameter list) { [declarations] statements [return ] }
Types of Functions • no input, no return value • void • input but no return value • both input and output • no input but returns a value
Example -- Circle Functions • calculate the area and circumference of a circle of an input radius • input radius • calculate area • calculate circumference • output results • invoke the functions • use name and parameters in an expression • functions must be defined before they can be used
Example -- Pythagorean Triples • Pythagorean Triple are the three sides of a right triangle a,b,c • a2 + b2 = c2 • given m and n, such that m>n we can generate the triples • a = m2 - n2 • b= 2mn • c = m2 + n2
Call by Value • on invocation the value of the actual parameter is copied into the formal parameter • when the function terminates the value IS NOT copied back to the actual parameter • can not change the value of a parameter within the function
Example Call by Value #include <iostream.h> int test (int n) { int i = 5; n +=i; return (n); } void main (void) { int n=1, i; i = test (n); cout << i << “ = “ << n << endl; }
Example Call by Value (2) main test 1 n 1 6 n i 6 i 5
Functions -- Pass by Reference • returns 0 or 1 value through name • need to return more than 1 • swap the values of two variables • change the values of parameters • bank deposit or check • pass the “name” of the parameter rather than its value so that the function uses the same memory location as the actual parameter
Reversing Order -- Swap if (num1 < num2) { temp = num1; num1 = num2; num2 = num1; }
Reference Parameters • Parameter which shares the memory of the actual parameter rather than declare new memory and copy the actual’s value into it • Parameter declaration int & x; • x is an alias for the actual integer parameter double & y • y is an alias for the actual double parameter
Function Swap void swap (int & num1, int & num2) { int temp; temp = num1; num1 = num2; num2 = temp; } if a > b swap (a,b); to invoke the function
Call by Value vs Reference • Use reference vs return type • all input • when need to return more than 1 value • always have return type void • Use value • all other cases • no side effects