1 / 24

Introduction to C++: Overview of Basics, Syntax, and Data Types

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.

sema
Télécharger la présentation

Introduction to C++: Overview of Basics, Syntax, and Data Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to C++ Computer Science

  2. 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++

  3. People & Programs • User: an individual who runs, or executes, a program • Programmer: an individual who creates, or writes, a program

  4. 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

  5. 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

  6. 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

  7. Simple C++ Program #include <iostream.h> int main() { // Declarations // Statements return 0; } • Main function

  8. 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)

  9. 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

  10. 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

  11. 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 “;”

  12. Simple C++ Program #include <iostream> int main() { // Declarations // Statements return 0; } • This program doesn’t do anything!

  13. 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

  14. 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

  15. 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 “>>”

  16. Sample C++ Program #include <iostream.h> int main() { int number; cout << “Enter a number” << endl; cin >> number; cout << “You entered: “ << number << endl; return 0; }

  17. 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”

  18. 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

  19. Assignment • When a variable is assigned a value, the value is placed into the variable’s memory location 10 Total = 2 + 3 + 5; Total

  20. constants • Constants are expressions with a fixed value • Two types of constants • Declared Constants • Defined constants (#define)

  21. Declared Constants • These are also called as typed constants • Syntax of declared constant is • const int numberOfAlphabets = 26; • const float PI = 3.14354;

  22. 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;

  23. 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

  24. 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 */

More Related