0% found this document useful (0 votes)
11 views29 pages

Unit-1 C++

C++ is a general-purpose, object-oriented programming language that extends C with features like classes and inheritance. It supports both procedural and object-oriented programming, making it versatile for various applications such as system programming, game development, and AI tools. The document outlines the structure of a C++ program, data types, variables, constants, input/output operations, operators, control structures, functions, and variable scope and lifetime.

Uploaded by

soroutsanjeet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views29 pages

Unit-1 C++

C++ is a general-purpose, object-oriented programming language that extends C with features like classes and inheritance. It supports both procedural and object-oriented programming, making it versatile for various applications such as system programming, game development, and AI tools. The document outlines the structure of a C++ program, data types, variables, constants, input/output operations, operators, control structures, functions, and variable scope and lifetime.

Uploaded by

soroutsanjeet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Basics of Programming in

C++
Introduction to
C++
•C++ is a general-purpose, object-oriented programming
language developed by Bjarne Stroustrup in 1979 at Bell Labs.

•It is an extension of C language with additional features like


classes, objects, inheritance, polymorphism, abstraction, and
encapsulation.

•C++ is often called "C with classes" because it adds object-


oriented features to the C language.

•It supports both procedural programming (like C) and object-


oriented programming, so it is known as a multi-paradigm
language.
Key Features of
C++
•Simple & Fast – Similar to C, but with more features.

•Object-Oriented – Uses concepts like class, object, inheritance,


polymorphism, etc.

•Compiled Language – Programs are compiled into machine code


for faster execution.

•Portable – Can run on different platforms (Windows, Linux, Mac).

•Rich Library Support – Has built-in functions for various operations.


Uses of C++
•System Programming (Operating Systems, Compilers, Drivers)

•Game Development

•Database Software (like MySQL)

•High-performance Applications (like Banking and Real-time systems)


• Embedded Systems

• Artificial Intelligence & Machine Learning Tools


Structure of a C++
Program
C++ program has a fixed structure.

It can be divided into 7 main parts:

• Documentation Section
• Preprocessor Directives
• Namespace Declaration
• Global Declarations
• main() Function
• Variable Declarations & Statements
• Return Statement
Part 1 – Documentation
Section
Definition: Contains comments that describe the program.
• Helps others understand the code.
• Not executed by compiler.

Example:
// Program to display a number
/* Author: Priyanshi */
Part 2 – Preprocessor
Directives
Definition: Instructions to the compiler to include libraries.

Example:

#include <iostream>
Part 3 – Namespace
Declaration
Definition: Provides scope to identifiers and avoids conflicts.
Example:

using namespace std;


Part 4 – Global Declaration
Section
Definition: Variables, constants, or functions declared here
can be used anywhere.
Example:

int x = 10; // Global variable


Part 5 – main() Function
Definition: Entry point of every C++ program. Execution
starts here.
Example: int main() {
// program body
return 0;
}
Part 6 – Variables &
Statements
Definition: Inside main(), variables are declared and logic is
written.
Example:
int number;
Cin >> number;
Cout << number
Part 7 – Return Statement
Definition: Ends the program and returns control to the
OS.
return
Example:
0;

Complete Example Program


// Documentation Section
// Program to display a number
/* Author: Priyanshi */

#include <iostream> // Preprocessor Directive


using namespace std; // Namespace Declaration

int x = 5; // Global Declaration

int main() { // main() Function


int number; // Variable Declaration
cout << "Enter a number: ";
cin >> number;
cout << "You entered: " << number << endl;

return 0; // Return Statement


}
Data Types in C++
Definition:
A data type specifies the type of data that a variable can
hold (like integer, decimal, character, etc.) and the amount of
memory required to store it.

Types of Data Types:


1. Basic Data Types

• int → Integer values (e.g., 10, -25 )


• float → Single precision decimal values (e.g., 3.14)
• double → Double precision decimal values (e.g., 12.345678)
• char → Single character (e.g., 'A')
• bool → Boolean values (true or false)
Data Types in C++
Types of Data Types:

2.Derived Data Types


•Array, Pointer, Function

3.User-defined Data Types


•Structure (struct), Class, Enum
Variables in C++
Definition:
A variable is a named memory location used to store data, and its
value can change during program execution.

Rules for Naming Variables:


•Must start with a letter or underscore _
•Cannot start with a digit
•Cannot use reserved keywords (int, for, etc.)
•Case-sensitive (Age and age are different
Constants in C++
Definition:
A constant is a variable whose value cannot be changed
during program execution
Ways to Declare Constants:
1. Using const keyword
2. Using #define preprocessor

Summary:
• Data Types → Defines the type of data (int, float, char, bool, etc.)
•Variable → Named memory storage whose value can change
•Constant → Fixed value whose value cannot change
Input/ Output operations
In C++, input and output operations are used to take data
from the user (input) and display results (output). These
operations are handled by streams

1. What is a Stream?

A stream is a flow of data:

Input Stream → Data flows from input device


(keyboard) to the computer program.
Output Stream → Data flows from program to an
output device (screen/monitor)
)

Input/ Output operations


2. Header File for Input/Output
To use input/output in C++, we include . Ex- #include <iostream>

3. Output in C++
We use cout (character output) along with the insertion operator (<<). .Ex- cout << "Hello,
Welcome to C++!"

4. Input in C++
We use cin (character input) along with the extraction operator (>>).

5. Multiple Inputs and Outputs


We can take multiple inputs or print multiple outputs in a single statement

6. Summary
•cin → Input from user
•cout → Output to screen
•>> → Extraction operator (used with cin)
•<< → Insertion operator (used with cout)
)

Operators and Expressions


Operators in C++
Operators are symbols that tell the computer what
action to perform. Operators = symbols (+, -, *, etc.)

Expressions in C++
An expression is a combination of numbers,
variables, and operators that gives a result.
Expressions = combination of operators and values
Operators and Expressions
Types of Basic
Operators

1. Arithmetic Operators 2.Relational Operators


Used in math. Used for comparison (result is
•+ → Addition
true/false).
•- → Subtraction •== → Equal to
•* → Multiplication •!= → Not equal
•/ → Division •> → Greater than
•% → Remainder •< → Less than
Operators and Expressions
Types of Basic
Operators 4. Assignment
Operators
3. Logical Operators Used to give
5. Increment / Decrement
values. •++ → Increase by 1
Used for conditions. •= → Assign •-- → Decrease by
•&& → AND •+= → Add and
•|| → OR assign
•! → NOT •-= → Subtract
and assign
Control Structures in C++
Control structures determine the flow of execution of a program.
They allow us to make decisions and repeat tasks.
Types of control structures:
•Decision Making → if, if-else, nested if, switch
•Looping → for, while, do-while
•Jump Statements → break, continue, goto

a) if-else Statement
• Executes a block of code only if a condition is true.
• If the condition is false, another block may be executed
Control Structures
• b) switch Statement
in C++
• Used when we have multiple choices based on one
expression.
• More readable than multiple if-else statements.

(a)
if (marks >= 50)
cout << "Pass";
else
cout << "Fail";
Loops in C++
Loops allow execution of a block of code multiple times

• a) for loop
• Used when the number of iterations is known.
• Contains initialization, condition, and increment/decrement

• b) while loop
• Used when the number of iterations is not known in
advance.
• Condition is checked before execution.
• c) do-while loop
• Similar to while loop, but executes the block at least once
Loops in C++
int i=1;
int i=1;
do {
for (int i=1; i<=5; while(i<=5) {
cout << i << "
i++) cout << i << " ";
";
cout << i << " "; i++;
i++;
}
} while(i<=5);
Functions in C++
A function is a block of code that performs a
specific task.
It helps in code reusability and modularity
Types of functions:
1.Library Functions → pre-defined, e.g., sqrt(),
pow().
2.User-defined Functions → created by the
programmer

✔ Functions may or may not return a value.


✔ Functions may or may not take parameters
Scope of Variables
Scope means where a variable can be accessed in
a program.
•Local Scope: Declared inside a function/block,
accessible only there.
•Global Scope: Declared outside all functions,
accessible everywhere in the program.
•Block Scope: Variables inside { } exist only
within that block.
•Function Scope: Parameters of a function are local
to that function
Lifetime of Variables
Lifetime means how long a variable exists in memory.
•Automatic (Local) Variables
•Created when function starts, destroyed when function ends.
•Example: normal variables inside a function.
•Global Variables
•Created at the beginning of the program, destroyed at the end.
•Can be accessed by all functions.
•Static Variables
•Retain their value between function calls.
•Declared using the keyword static.
•Dynamic Variables
•Created at runtime using new.
•Destroyed manually using delete

You might also like