0% found this document useful (0 votes)
26 views13 pages

1106 Note

Uploaded by

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

1106 Note

Uploaded by

palarnab87
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2025

Programming with C++

1106 - PROGRAMMING WITH C++


ARNAB CHANDRA PAUL

Department of Statistics, RU
History of C++
The History of C++
• 1979: Bjarne Stroustrup, a Danish computer scientist, begins work on "C
with Classes" while at Bell Labs. He wanted a language that combined the
efficiency of C with the features of Simula, particularly object-oriented
programming.
• Late 1980s - Early 1990s: C++ gains widespread adoption, especially in
systems programming, game development, and high-performance
computing. Compilers from various vendors (e.g., Borland, Microsoft)
become available.
• 1983: The name "C with Classes" is changed to C++. The "++" is a common
operator in C that increments a variable, symbolizing the language as an
extension of C.
• 1985: The first commercial release of C++ is made. The C++ Programming
Language by Stroustrup is also published, becoming the foundational text
for the language.
• 1998: The first C++ standard, C++98, is ratified by the International
Organization for Standardization (ISO). This standard formalizes the
language and its features, including the Standard Template Library (STL).
• 2003: A minor revision to the standard, C++03, is released, primarily to fix
bugs and provide clarifications.
• 2011: A major update, C++11, is released. It introduces many new
features, including lambda expressions, auto keyword, move semantics,
and a new concurrency library. This release revitalized the language and
its popularity.
• 2014, 2017, 2020: Subsequent standards, C++14, C++17, and C++20, are
released. Each adds more features and improvements, such as generic
lambdas (C++14), filesystem library (C++17), and concepts (C++20).
• Today: C++ remains a dominant language for performance-critical
applications. It is used in a wide range of fields, including finance,
scientific computing, embedded systems, and machine learning, while
continuing to evolve with new standards.

1
Boilerplate code in C++
In C++ programming, "Boilerplate code" refers to code that is almost
identical at the beginning of every program and whose main purpose is to
provide a basis for running or compiling the program.

#include <iostream>

int main() {
// Write your code here

return 0;
}

Base code in C++


Below is a basic C++ code. This code is a simple program that prints the text
"Hello World!"

#include <iostream>

int main() {
input
std::cout << "Hello World!" << std::endl;
return 0;
}
output Hello World!

• #include <iostream>: This is a header file that is required to use the input-
output functions. cout is defined in this file.

• int main() { ... }: This is the main function of the program. Every C++
program has a main function, because this is where the program
execution starts.

• std::cout << "Hello World!": std::cout is an object that is used to output


data to the console. The << operator sends the data ("Hello World!") to
cout.

2
Another way:
using namespace std; is a directive that is often used in C++. Using it
saves you from having to write std:: before every standard library
component (e.g. cout, cin, endl).
#include <iostream>
using namespace std;
int main() {
input
cout << "Hello World!" << endl;
return 0;
}
output Hello World!

• << std::endl; : This adds a newline character, which moves the cursor to
the next line. It works the same as \n.
Another way:
std::endl vs "\n"
Another common way to create a newline in C++ is std::endl. Although
these two may look similar, there is an important difference between
them.

• "\n": This simply adds a newline character. It works relatively quickly.


• std::endl: This not only creates a newline, but also performs a "flush"
operation. Flushing means that the data stored in the buffer (i.e.
output) is immediately printed to the screen.
#include <iostream>
using namespace std;
int main() {
input
cout << "Hello World!" << “\n”;
return 0;
}
output Hello World!

• return 0; : This is used to indicate that the program has ended


successfully.

3
Multiple lines in C++
There are some easy ways to write multiple lines in C++. These are
discussed in detail below:

1) \n
"\n" is a newline character. Using it, you can create multiple lines within the
same cout statement.
#include <iostream>
using namespace std;
int main() {
input
cout << "This is the first line.\nThis is the second line.";
return 0;
}
This is the first line.
output
This is the second line.

2) Using endl
endl (end line) is a C++ library function that adds a newline to the cout
statement and flushes the buffer.
#include <iostream>
using namespace std;
int main() {
input cout << "This is the first line." << endl;
cout << "This is the second line." << endl;
return 0;
}
This is the first line.
output
This is the second line.

3) Many line in one line


#include <iostream>
using namespace std;

input int main() {


cout << "This is the first line." << "This is the second line." << endl;
return 0;
}
output This is the first line. This is the second line.

4
Comment in C++
When writing code in C++, there are two main ways to add comments to the
code. Comments make the code more readable, which helps the
programmer and other developers understand the functionality of the code.
The compiler ignores comments, meaning they do not affect the
functionality of the program.

The two main ways to write comments in C++ are given below:

1) Single-line Comment
#include <iostream>
using namespace std;
int main() {
input // This is a single-line comment.
cout << "Hello World!" << endl; // This is a after-line comment.
return 0;
}
output Hello World!

2) Multi-line Comment
#include <iostream>
using namespace std;
int main() {
/*
This code is a simple C++ program.
input It prints "Hello World!"
This is a multi-line comment.
*/
cout << "Hello World!" << endl;
return 0;
}
output Hello World!

Why are comments important?


• Increase readability: Comments make your code easier to understand for
other programmers.
• Documentation: It provides information about the functionality, purpose,
and data structures of the code.

5
• Debugging: You can temporarily disable a block of code (by commenting
it out) so that it doesn't affect the functionality of the program, which can
be helpful when debugging.
• For the future: When you look back at your own code in the future,
comments will give you a quick reminder of what the code does.

Variables in C++
Suppose you have a box of chocolates. We will treat the box as a container
and the chocolates inside the box as variables.

In C++, a variable is the name of a memory location where you can store data
or information. Each variable has a specific data type (e.g. integer,
character, decimal number), which determines what kind of data can be
stored in the variable. A variable must be declared before it can be used.

#include <iostream>
using namespace std;
int main() {
// Variable Declaration
int number;
char letter;

input number = 10;


letter = 'A';

// Variable Initialization (Assigning a value)


cout << "The number is: " << number << endl;
cout << "The letter is: " << letter << endl;
return 0;
}
The number is: 10
output
The letter is: A

Variable Naming Rules


There are some specific rules for naming variables:

• The name must start with a letter (a-z, A-Z) or an underscore (_). Numbers
(0-9) cannot be used.

6
• No spaces or special characters (such as !, @, #, $) can be used in the
name.
• No reserved keywords in C++ (such as int, return, for, while) can be used
as variable names.
• Names are case-sensitive. For example, myVar and myvar are two
different variables.

Data type in C++


A data type is a classification that determines what kind of data can be
stored in a variable. It tells the compiler how much memory it will take up
and what operations can be performed on that data.

Basic data types:


Data Type Description Example
int Integer int age = 25;
char Single character char letter = 'A';
Single-precision float price = 10.50f; or,
float
decimal number float price = 10.50F;
Double-precision
double double pi = 3.14159;
decimal number
Boolean value (true or
bool bool is_valid = true;
false)
Multiple characters or string name = "John
string
words Doe";

Here is all example:


#include <iostream>
using namespace std;
int main() {
// Variable Declaration
int number;
char letter;
input float price;
double pi;
bool is_valid;
string name;

7
number = 10;
letter = 'A';
price = 10.50f;
pi = 3.14159;
is_valid = true;
name = "Arnab Chandra Paul";

// Variable Initialization (Assigning a value)


cout << "The number is: " << number << endl;
cout << "The letter is: " << letter << endl;
cout << "The price is: " << price << endl;
cout << "The pi is: " << pi << endl;
cout << "The logic is: " << is_valid << endl;
cout << "The name is: " << name << endl;

return 0;
}
The number is: 10
The letter is: A
The price is: 10.5
output
The pi is: 3.14159
The logic is: 1
The name is: Arnab Chandra Paul

Another way:

#include <iostream>
using namespace std;
int main() {
// Variable Declaration
int number = 10;
char letter = 'A';
float price = 10.50F;
input
double pi= 3.14159;
bool is_valid = true;
string name = "Arnab Chandra Paul";

// Variable Initialization (Assigning a value)


cout << "The number is: " << number << endl;
cout << "The letter is: " << letter << endl;

8
cout << "The price is: " << price << endl;
cout << "The pi is: " << pi << endl;
cout << "The logic is: " << is_valid << endl;
cout << "The name is: " << name << endl;

return 0;
}
The number is: 10
The letter is: A
The price is: 10.5
output
The pi is: 3.14159
The logic is: 1
The name is: Arnab Chandra Paul

float vs double:
In C++, both float and double are used to store floating-point numbers, but
the main difference between them is their memory size and precision.
float double
Memory size A variable of type float uses A variable of type double
4 bytes (32-bit) of memory. uses 8 bytes (64-bit) of
memory, which is twice as
much as float.
Precision It is relatively less precise It is more precise than float
and usually provides and typically provides
accuracy up to 6-7 decimal precision up to 15-16
places. decimal places.

❖ When to use which?

float: If memory is a concern and you don't need a lot of precision, float can
be used. For example, in game development or any application where fast
calculations are essential.

double: This is usually the default choice. Double is used for most
mathematical and scientific calculations, as it provides greater precision. If
you are not sure which to use, it is safer to use double.

9
Type conversation and casting in C++
Data conversion and casting in C++ is the process of converting data from
one data type to another. This is especially necessary when performing
mathematical operations using variables of different data types.

1. Data Conversion
This is when a smaller data value needs to be converted to a larger data type
when the computer can handle it. This is called implicit conversion. No data
is lost.

Example:
#include <iostream>
using namespace std;
int main() {
int int_number = 100;
double double_number = 25.5;
input // Implicit conversion: int to double
double result = int_number + double_number; // 100 + 25.5 =
125.5
cout << "Result: " << result << endl;
return 0;
}
output Result: 125.5

2. Type Casting
This is done manually, when the programmer wants to convert one data type
to another. It is also known as Explicit Conversion. There may be a possibility
of losing some information through this.

Example:
#include <iostream>
using namespace std;
int main() {
double main_price = 25.5;
input
int round_price = (int)main_price;
cout << "Result: " << round_price << endl;
return 0;
}
output Result: 25

10
Input in C++
The cin object is mainly used to take input from the user in C++. It comes
from the C++ standard library (part of the <iostream> header file).

1) cin
cin is an input stream object, which is used to read data from the keyboard.
The input stream operator (>>) is used to take data input.

Example:
#include <iostream>
#include <string>
using namespace std;

int main() {
string full_name;
Input
(Mr. Arnab) cout << "What is your name: ";
cin >> full_name;

cout << "Hello, Sir! Your name is : " << full_name << endl;

return 0;
}
What is your name: Mr. Arnab
output
Hello, Sir! Your name is : Mr.

Some important points:


• #include <string>: If you want to take string input, it is necessary to
include this header file.
• Input with spaces: The cin >> operator stops taking input when it
encounters a space or newline character. That is, if you input "John
Doe", it will only take "John". The getline() function is used to take a full
line of input.
• Data type: The data type of the variable must be correct when taking
input. If you input any character into an int variable, the program may
behave unexpectedly.

2) getline()

11
If you want to take the entire line of input including spaces, then use the
getline() function.

Example:
#include <iostream>
#include <string>
using namespace std;

int main() {
string full_name;
Input
(Mr. Arnab) cout << "What is your name: ";
getline(cin, full_name);

cout << "Hello, Sir! Your name is : " << full_name << endl;

return 0;
}
What is your name: Mr. Arnab
output
Hello, Sir! Your name is : Mr. Arnab

12

You might also like