
















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

















































Chapter 1: Introductio n to C++
C++ is an evolution of the C language, developed to add object-oriented features and more robust programming tools. It has become a powerful language used in diverse fields like systems development, gaming, and real-time simulations. Developed by Bjarne Stroustrup at Bell Labs (1979) Originally called “C with Classes” Released commercially in 1985 Standardized as C++98 by ISO Later versions: C++03, C++11, C++14, C++17, C++20, C++ Blends low-level programming with high-level abstraction
C++ is a high-performance, general-purpose programming language. It supports procedural, object-oriented, and generic programming paradigms, making it versatile and powerful. Statically typed and compiled language Extends C with object-oriented features Allows for low-level memory management Cross-platform and portable
STL and templates available in C++ Exception handling available in C++ C++ uses new and delete for memory management; C uses malloc and free
A C++ program follows a defined structure including headers, the main function, and statements. Understanding this structure is key to writing compilable and executable programs. Header files (#include
C++ source code needs to be compiled into machine code before execution. This process involves several stages from writing code to getting the final output. Write code in .cpp file Use compiler (e.g., g++, clang++) to compile Syntax check during compilation
Link object files to create executable Run executable file to produce output Errors handled at compile time or runtime
C++ syntax must be followed strictly as it is case-sensitive. It uses specific rules and reserved keywords to define logic and behavior. Statements end with semicolons Code blocks enclosed in {} braces Case-sensitive: int ≠ Int Commenting: // for single-line, /* */ for multi-line Reserved keywords: int, char, float, if, else, return, while, class, public
C++ uses cin and cout for input and output operations. These are part of the standard I/O stream library and are widely used in C++ programs. cin for input from the user cout for output to the screen Use of >> and << operators Requires inclusion of
Disadvantages: Steep learning curve Manual memory management prone to errors Complex syntax for beginners Lack of modern safety features compared to newer languages
Chapter 2: C++ Basics
o int: Specifies that the main function will return an integer value to the operating system upon completion. { } (Curly Braces): Delimit the body of the main function (the block of code to be executed). std::cout : The standard output stream object in C++. Used to display output on the console. << (Insertion Operator): Used to send data to the output stream (std::cout). "Hello, World!" : A string literal - a sequence of characters enclosed in double quotes. This is the data being sent to the output stream. std::endl : A manipulator that inserts a newline character into the output stream and flushes the stream (forces the output to be displayed). return 0; : A statement that indicates that the main function has completed successfully. A non-zero return value typically indicates an error. 2.2 Comments Purpose of Comments: Used to add explanatory notes to the code. Comments are ignored by the compiler and are for human readers to understand the code better. Types of Comments:
o Single-line comments: Start with // and continue until the end of the line. C++ o // This is a single-line comment.std::cout << "This line will be executed."; // This is another comment. o o Multi-line comments: Start with /* and end with /. Everything in between is considered a comment, even across multiple lines. C++ o / This is a multi-line comment. It can span across several lines of code. */std::cout << "This line will also be executed.";
int main(){std::cout<<"Hello";}return 0 ; C++ int main(){ std::cout << "Hello"; return 0 ; } 2.4 Statements and Semicolons What is a Statement? A complete instruction in C++ that performs some action. Semicolon as a Statement Terminator: Most statements in C++ must end with a semicolon ;. The semicolon tells the compiler where one statement ends and the next begins. Examples of Statements: C++
int x; // Declaration statement x = 5 ; // Assignment statementstd::cout << x << std::endl; // Output statementreturn 0 ; // Return statement Exceptions: Some constructs, like preprocessor directives (#include), function definitions, and control flow structures (like if, for, while) do not end with a semicolon (although the statements within their blocks do). 2.5 Keywords What are Keywords? Reserved words in the C++ language that have special meanings to the compiler. These words cannot be used as identifiers (names for variables, functions, etc.). Examples of Keywords: int, double, char, bool, if, else, for, while, return, class, struct, const, etc. Compiler Recognition: The compiler will flag an error if you try to use a keyword as an identifier. 2.6 Identifiers What are Identifiers? Names given to entities in a program, such as variables, functions, classes, etc. Rules for Naming Identifiers:
o Arithmetic Operators: +, - , *, /, % (addition, subtraction, multiplication, division, modulo). o Assig nment Operator: = (assigns a value to a variable). o Comparison Operators: ==, !=, >, <, >=, <= (equal to, not equal to, greater than, less than, greater than or equal to, less than or equal to). Example: C++ int a = 10 ;int b = 5 ;int sum = a + b; // '+' is the addition operatorbool isEqual = (a == b); // '==' is the equality operator 2.8 Variables What is a Variable? A named storage location in the computer's memory that can hold a value. Think of it as a labeled box that can store data. Declaration: Creating a variable by specifying its data type and name.
o Syntax: data_type variable_name; (e.g., int age;, double price;) Initialization: Assigning an initial value to a variable when it is declared. o Syntax: data_type variable_name = initial_value; (e.g., int age = 30;, double price = 99.99;) Assignment: Changing the value of an existing variable using the assignment operator =. o Syntax: variable_name = new_value; (e.g., age = 31;, price = 105.50;) Data Types (Introduced briefly, covered in detail in the next chapter): Specify the kind of data a variable can hold (e.g., integers, floating-point numbers, characters, boolean values). 2.9 Literals What are Literals? Fixed values that appear directly in the code. Types of Literals: o Integer Literals: Whole numbers (e.g., 10 , -5, 0 ).