310 likes | 451 Vues
C++ is a powerful, compiled, object-oriented programming language developed as the successor to C by Bjarne Stroustrup in the early 1980s. While rooted in C, C++ introduces advanced features like declarations, statements, variables, and functions. Users and programmers interact through executables, and the program structure includes components such as headers, main functions, and comments. This brief tutorial covers simple C++ program syntax, data types, assignment operations, constants, and the importance of documentation in coding.
E N D
Introduction to C++ Computer Science
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
Simple C++ Program #include <iostream.h> int main() { // Declarations // Statements return 0; } • Declarations and statements • Main body of function (or main part) • “//” represents the start of a comment
Simple C++ Program #include <iostream.h> int main() { // Declarations // Statements return 0; } • Return statement • specifies the value the function returns • All (almost) declarations and statements end with a semi-colon “;”
Simple C++ Program #include <iostream> int main() { // Declarations // Statements return 0; } • This program doesn’t do anything!
Sample C++ Program #include <iostream> int main() { int number; cout << “Enter a number” << endl; cin >> number; cout << “You entered: “ << number << endl; return 0; } • Variable declaration • The identifier number is declared as being of data type int, or integer
Sample C++ Program #include <iostream.h> int main() { int number; cout << “Enter a number” << endl; cin >> number; cout << “You entered: “ << number << endl; return 0; } • coutthe output statement for C++ • Note the direction of “<<“ • endl represents an end-of-line
Sample C++ Program #include <iostream.h> int main() { int number; cout << “Enter a number” << endl; cin >> number; cout << “You entered: “ << number << endl; return 0; } • cinthe input statement for C++ • Note the direction of “>>”
Sample C++ Program #include <iostream.h> int main() { int number; cout << “Enter a number” << endl; cin >> number; cout << “You entered: “ << number << endl; return 0; }
Assignment • Assignment is an 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 - 2float - 4double - 8char, bool - 1
Assignment • When a variable is assigned a value, the value is placed into the variable’s memory location 10 Total = 2 + 3 + 5; Total
constants • Constants are expressions with a fixed value • Two types of constants • Declared Constants • Defined constants (#define)
Declared Constants • These are also called as typed constants • Syntax of declared constant is • const int numberOfAlphabets = 26; • const float PI = 3.14354;
Defined Constants • These constants are defined in the declaration part of the program • Syntax of defined constants is • #define numberOfAlphabets 26; • #define PI 3.14354;
Comments • Comments are used to document your code • Comments are not the part of your code • Its always a good practice to write comments • This class requires to the code to be well commented
Types of Comments • Two types of comments • Single line comments • Syntax: // • Example: //This is a single line comment • Block comments • Syntax: /* */ • Example: /* This is a Block Comment */