Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 99 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
99
Dung lượng
536 KB
Nội dung
Introduction to C++ Computer Science I Quote “Language is the only instrument of science….” Samuel Johnson Q: What is C++ C++ is a compiled, object-oriented language It is the “successor” to C, a procedural language (the “++” is called the successor operator in C++) C was derived from a language called B which was in turn derived from BCPL C was developed in the 1970’s by Dennis Ritchie of AT&T Bell Labs C++ was developed in the early 1980’s by Bjarne Stroustrup of AT&T Bell Labs. Most of C is a subset of C++ People & Programs User : an individual who runs, or executes, a program Programmer : an individual who creates, or writes, a program C++ Program Consists of… Declarations Define the use of various identifiers, thus creating the elements used by the program (computer) Statements Or executable statements, representing actions the computer will take on the user’s behalf Identifiers Names for various entities used in a program; used for Variables : values that can change frequently Constants : values that never changes Functions : programming units that represents complex operations Parameters : values that change infrequently Simple C++ Program #include <iostream.h> int main() { // Declarations // Statements return 0; } Compiler directive: Tells the compiler what to do before compiling This one includes source code from another file Simple C++ Program #include <iostream.h> int main() { // Declarations // Statements return 0; } Main function Simple C++ Program #include <iostream.h> int main() { // Declarations // Statements return 0; } Header for main function States… data type for the return value identifier for function list of arguments between parenthesis (none for this function) Simple C++ Program #include <iostream.h> int main() { // Declarations // Statements return 0; } Braces enclose the body of the function They represent the start and end of the function [...]... operation that assigns the value of an expression to a variable Ex Total = 2 + 3 + 5 First, the expresssion “2 + 3 + 5” is evaluated Then, this value is assigned to the variable “Total” Assignment When a variable is declared, space is allocated in the computer’s memory for the variable Each data type requires a different number of bytes in memory for storing a variable int - 2 float - 4 double -... assigned a value, the value is placed into the variable’s memory location Total 10 Total = 2 + 3 + 5; Arithmetic Operations Addition: Subtraction: Multiplication: Division: 2 + 3 5 - 2 10 * 4 12 / 3 Order of Operations Arithmetic expressions are evaluated according to the following order of operations At each level, operations are evaluated left to right (1) Parenthesis, Functions (2)... evaluated left to right (1) Parenthesis, Functions (2) Multiplication, Division (3) Addition, Subtraction Parenthesis Parenthesis are used to alter the order with which operations are evaluated Ex 4 + 5 * 2 equals 14 (4 + 5) * 2 equals 18 Here we go! Problem: To determine the average of three numbers Task: Request, from the user, three numbers, compute the average and the three numbers, and print... programming unit Similar to mathematical functions Example: f(x) = x2 + 5x + 7 For x = 2, f(2) = (2)2 =5(2) + 7 = 21 Q: What is a function? It has arguments a name (identifier) a value it returns a body int foo(int x) { int result; result = x*x + 5*x + 7; return result; } Procedural Abstraction Think “Black Box” ! When using a function, you only need to be concerned with what... 5*x + 7; return result; } Procedural Abstraction Think “Black Box” ! When using a function, you only need to be concerned with what it does, not how it does it When writing a function, you need to be concerned with the how Example: Cube it! int cubeIt(int x) { int result; result = x*x*x; return result; } int cubeIt(int x) { int result; result = x; result = x*result; result = x*result; return . the value of an expression to a variable Ex. Total = 2 + 3 + 5 First, the expresssion “2 + 3 + 5” is evaluated Then, this value is assigned to the variable “Total” Assignment When a. a compiled, object-oriented language It is the “successor” to C, a procedural language (the “++” is called the successor operator in C++) C was derived from a language called B which was. Introduction to C++ Computer Science I Quote “Language is the only instrument of science….” Samuel