C++-Unit_1_2 Notes
C++-Unit_1_2 Notes
PROBLEM-SOLVING
• Problem-solving is the process of using computational-methods and logical-
reasoning to find solutions to complex issues or tasks.
• This process is fundamental to developing software.
1) Problem Definition
• This stage involves defining the problem that the software aims to solve.
• Key-Points
- Clearly identify and state the problem.
- Gather requirements from stakeholders to understand what they expect.
- Define the scope of the software-project, including its goals and objectives.
- Document the problem and requirements for later reference.
2) Problem Analysis
• In this stage, the problem defined earlier is analyzed to understand its nature, scope, and specific
requirements.
• Key-Points
- Break down the problem into smaller parts or modules.
- Analyze user needs to ensure the solution meets requirements.
- Consider time, budget, and technical limits.
- Assess if the problem can feasibly be solved with available resources.
Object orientation contributes the solution of many problems associated with the development and
quality of softwareproducts.
The new technology promises greater productivity better quality of software and lesser
maintainacecost.
Object oriented system can be easily upgraded from small to large system.
The principle of data hiding helps the programmer to build secure program that cannot be
accessed by other part of the program.
Though inheritance we can eliminate redundancy and extend the use of exisiting class.
APPLICATIONS OF OOPS:
Real Time System
Stimulation and Modeling
Object Oriented Data Base
AI and Expert Systems
Neural Network and Parallel Processing
CIM/CAD/CAM system
Hypertext, Hypermedia and expert text
#include <iostream>
using namespace std;
int main() {
cout << "Welcome to C++" << endl;
return 0;
}
Output:
Welcome to C++
1) Preprocessing
• Purpose: Before compilation, the preprocessor scans the source-code for
preprocessor-directives (`#include`, `#define`, etc.).
• Actions:
- Header inclusion: `#include` directives pull in contents of header-files.
- Macro substitution: `#define` macros are replaced with their defined-values.
2) Compilation
• Purpose: The preprocessed source-code is translated into machine-readable instructions.
• Actions:
- Syntax analysis: Checks syntax for correctness, identifies tokens (keywords, identifiers, literals).
- Semantic analysis: Validates semantics (type checking, function-calls, etc.).
- Code generation: Translates validated-code into assembly- or machine-code.
Note:
HOW TO LEARN C++ LANGUAGE?
• English is a universal language used to communicate with others.
• In the same way, C++ is a language used to communicate with computer. In other words, C++ is used
to instruct computer to perform particular task.
• The task can be
→ simple task like adding 2 numbers or
→ complex task like building a railway reservation system
• Before you play the game, you should learn rules of the game. So that you can play better and win easily.
In the same way, to write C++ programs, you should learn rules of C++ language.
STEPS TO LEARN C++ LANGUAGE
Step 1: Before speaking any language, you should first learn alphabets. In the same way, to learn C++
language, you should first learn alphabets in C++.
Step 2: Then, you should learn how to group alphabets in particular sequence to form a meaningful word.
In the same way, in C++ language, you should learn tokens (i.e. words).
Step 3: Then, you should learn how to group the words in particular sequence to form a meaningful
sentence. In the same way, in C++ language, you should learn instruction (i.e. sentence).
Step 4: Then, you should learn how to group the sentences in particular sequence to form a meaningful
paragraph. In the same way, in C++ language, you should learn program (i.e. paragraph).
2.5 TOKENS
• A token is a smallest element of a C++ program.
• One or more characters are grouped in sequence to form meaningful words. These meaningful words are
called tokens.
• The tokens are broadly classified as follows
→ Keywords ex: if, for, while
→ Identifiers ex: sum, length
→ Constants ex: 10, 10.5, 'a', "sri"
→ Operators ex: + - * /
→ Special symbols ex: [], (), {}
2.5.2 IDENTIFIER
• As the name indicates, identifier is used to identify various entities of program such as variables,
constants, functions etc.
• In other words, an identifier is a word consisting of sequence of
→ Letters
Digits or
→ "_"(underscore)
• For ex:
area, length, breadth
2) Floating-point-constant
• The floating-point-constant is a real-number.
• The floating-point-constants can be represented using 2 forms as listed in below table:
3) Character-constant
• A symbol enclosed within a pair of single-quotes(') is called a character-constant.
• Each character is associated with a unique-value called an ASCII (American Standard Code for
Information Interchange) code.
• For ex:
'9', 'a', '\n'
4) String-constant
• A sequence of characters enclosed within a pair of double-quotes(“) is called a
string-constant.
• The string always ends with NULL (denoted by \0) character.
• For ex:
"9" "a" "gcwmaddur" "\n"
5) Escape Sequence Characters
• An escape sequence character begins with a backslash and is followed by one character.
• A backslash (\) along with some characters give rise to special print effects by changing
(escaping) the meaning of some characters.
2.6.2 QUALIFIERS
• Qualifiers alter the meaning of primary data-types to yield a new data-type.
1) Size Qualifiers
• Size-qualifiers alter the size of primary data-type.
• The keywords long and short are two size-qualifiers. For
example:
long int i; //The size of int is 4 bytes but, when long keyword is
//used, that variable will be of 8 bytes
short int i; //The size of int is 4 bytes but, when short keyword is
//used, that variable will be of 2 byte
#include <iostream>
using namespace std;
int main() {
// Variables to store user input
string name;
int age;
return 0;
}
Output:
Enter your name: Rama
Enter your age: 30
Hello, Rama! You are 30 years old.
3.1 OPERATOR
3.1.1 ARITHMETIC OPERATORS
3.1.2 RELATIONAL OPERATORS
3.1.3 LOGICAL OPERATORS
3.1.4 ASSIGNMENT OPERATOR
3.1.5 SHORTHAND OPERATORS
3.1.6 CONDITIONAL OPERATOR
3.1.7 BITWISE OPERATORS
3.1.8 COMMA OPERATOR
3.1.9 sizeof OPERATOR
3.1.10 INCREMENT/DECREMENT OPERATORS
3.1.10.1 INCREMENT OPERATOR
3.1.10.2 DECREMENT OPERATOR
3.1.11 SCOPE RESOLUTION OPERATOR
3.2 EXPRESSION
3.2.1 ARITHMETIC EXPRESSIONS
3.2.2 RELATIONAL-EXPRESSIONS
3.2.3 LOGICAL-EXPRESSIONS
3.3 PRECEDENCE AND ASSOCIATIVITY
3.3.1 PRECEDENCE OF OPERATORS
3.3.2 ASSOCIATIVITY OF OPERATORS
3.3.2.1 LEFT-ASSOCIATIVE OPERATORS
3.3.2.2 RIGHT-ASSOCIATIVE OPERATORS
3.4 TYPE CONVERSION
3.4.1 TYPE CONVERSION (IMPLICIT CONVERSION)
3.4.2 TYPE CASTING (EXPLICIT CONVERSION)
3.1 OPERATOR
• An operator specifies what operation need to be performed on the data.
• For ex:
+ indicates add operation
* indicates multiplication operation
Operand
• An operand can be a constant or a variable.
Expression
• An expression is combination of operands and operator that reduces to a single- value.
• For ex:
Consider the following expression a+b here a and
b are operands
+ is an operator
CLASSIFICATION OF OPERATORS
c = a + b;
cout << "a+b=" << c << endl;
c = a - b;
cout << "a-b=" << c << endl;
c = a * b;
cout << "a*b=" << c << endl;
c = a / b;
cout << "a/b=" << c << endl;
c = a % b;
cout << "Remainder when a divided by b=" << c << endl;
return 0;
}
Output:
a+b=13
a-b=5
a*b=36
a/b=2
Remainder when a divided by b=1
• For ex:
Condition Return values
2>1 1 (or true)
2>3 0 (or false)
3+2<6 1 (or true)
• Program to illustrate the use of relational operators.
int main() {
cout << "4>5 : " << (4 > 5) << endl;
cout << "4>=5 : " << (4 >= 5) << endl;
cout << "4<5 : " << (4 < 5) << endl;
cout << "4<=5 : " << (4 <= 5) << endl;
cout << "4==5 : " << (4 == 5) << endl;
cout << "4!=5 : " << (4 != 5) << endl;
return 0;
}
Output:
4>5 : 0
4>=5 : 0
4<5 : 1
4<=5 :1
4==5 : 0
4!=5 : 1
int main() {
return 0;
}
Output:
7 && 0 : 1
7 || 0 : 1
!0 : 1
int main() {
int a = 9, b = 4, c;
c = a + b;
cout << "a+b=" << c << endl;
return 0;
}
Output:
a+b=13
int main() {
int num = 10;
cout << "Initial value of num: " << num << endl;
// Shorthand operators
num += 5; // Equivalent to num =
num + 5; cout << "After num += 5: " << num << endl;
return 0;
}
Output:
Initial value of num: 10
After num += 5: 15
int main() {
int a, b, max;
cout << "Enter 2 distinct numbers: " << endl;
cin >> a >> b;
max = (a > b) ? a : b;
cout << "Largest number = " << max << endl;
return 0;
}
Output:
enter 2 distinct numbers
34
largest number = 4
int main() {
// Variables for demonstration
unsigned int num1 = 13; // Binary: 0000 1101
unsigned int num2 = 6; // Binary: 0000 0110
unsigned int result;
// Bitwise AND
result = num1 & num2;
cout << "Bitwise AND (num1 & num2): " << result << endl;
// Bitwise OR
result = num1 | num2;
cout << "Bitwise OR (num1 | num2): " << result << endl;
// Bitwise XOR
result = num1 ^ num2;
cout << "Bitwise XOR (num1 ^ num2): " << result << endl;
// Bitwise NOT
result = ~num1;
cout << "Bitwise NOT (~num1): " << result << endl;
// Left shift
result = num1 << 1;
cout << "Left shift (num1 << 1): " << result << endl;
// Right shift
result = num1 >> 1;
cout << "Right shift (num1 >> 1): " << result << endl;
return 0;
}
Output:
Bitwise AND (num1 & num2): 4 Bitwise OR
(num1 | num2): 15 Bitwise XOR (num1 ^
num2): 11 Bitwise NOT (~num1):
4294967282 Left shift (num1 << 1): 26
Right shift (num1 >> 1): 6
int main() {
int a = 1, b = 2, c = 3;
// Output results
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
cout << "Result = " << result << endl;
return 0;
}
Output:
a=6
b = 12
c = 18
Result = 18
int main() {
int intSize = sizeof(int);
double doubleSize = sizeof(double); char
charSize = sizeof(char);
bool boolSize = sizeof(bool);
cout << "Size of int: " << intSize << " bytes" << endl;
cout << "Size of double: " << doubleSize << " bytes" << endl; cout << "Size
of char: " << charSize << " byte" << endl;
cout << "Size of bool: " << boolSize << " byte" << endl; return 0;
}
Output:
Size of int: 4 bytes Size of
double: 8 bytes Size of
char: 1 byte Size of bool: 1
byte
int main() {
int x = 10, y = 10, z;
z = x++;
cout << "z = " << z << " x = " << x << endl;
z = ++y;
cout << "z = " << z << " y = " << y << endl;
return 0;
}
Output:
z=10 x=11
z=11 y=11
int main() {
int x = 10, y = 10, z;
z = x--;
cout << "z = " << z << " x = " << x << endl;
z = --y;
cout << "z = " << z << " y = " << y << endl;
return 0;
}
Output:
z=10 x=9
z=9 y=9
int main() {
int localVar = 50; // Local variable
return 0;
}
Output:
Global-variable: 100
Local variable: 50
Accessing globalVar: 100
int main() {
int a = 9, b = 4, c;
c = a + b;
cout << "a+b=" << c << endl;
c = a - b;
cout << "a-b=" << c << endl;
return 0;
}
Output:
a+b=13
a-b=5
3.2.2 RELATIONAL-EXPRESSIONS
• Relational-expressions compare two values and return a Boolean result (`true` or
`false`).
• They are used in decision-making statements like `if`, `while`, and `for` loops:
• Program to illustrate the use of Relational-expressions.
#include <iostream>
using namespace std;
int main() {
cout << "4>5 : " << (4 > 5) << endl;
cout << "4>=5 : " << (4 >= 5) << endl;
return 0;
}
Output:
4>5 : 0
4>=5 : 0
4<5 : 1
int main() {
cout << "7 && 0 : " << (7 && 0) << endl;
cout << "7 || 0 : " << (7 || 0) << endl;
return 0;
}
Output:
7 && 0 : 1
7 || 0 : 1
Precedence Table
}
Output:
Result of a + b * c: 20
int main() {
Output:
Result: 8
int main() {
int a = 5, b = 10, c = 15;
cout << "a: " << a << ", b: " << b << ", c: " << c << endl;
return 0;
}
Output:
a: 15, b: 15, c: 15
int main()
{ int a =
5;
double b = 10.5;
return 0;
}
Output:
b = 11
c = 22
d = 0.5
****
****
****
***
****
***
int main()
{ int n;
cout << "Enter any non zero integer: " << endl;
cin >> n;
if (n > 0)
cout << "Number is a positive number" << endl;
if (n < 0)
cout << "Number is a negative number" << endl;
return 0;
}
Output:
Enter any non zero integer:
7
Number is positive number
****
***
int main()
{ int n;
cout << "Enter any non-zero integer: " << endl;
cin >> n;
if (n > 0)
cout << "Number is a positive number" << endl;
else
cout << "Number is a negative number" << endl;
return 0;
}
Output:
Enter any non-zero integer:
7
Number is positive number
****
4.1.3 THE nested if STATEMENT
• An if-else statement within another if-else statement is called nested if statement.
• This is used when an action has to be performed based on many decisions. Hence, it is called as multi-
way decision-statement.
• Syntax:
if(expr1)
{
if(expr2)
statement1
else
} statement2
else
{
if(expr3)
statement3
else
statement4
}
• Here, firstly expr1 is evaluated to true or false.
If the expr1 is evaluated to true, then expr2 is evaluated to true or false.
If the expr2 is evaluated to true, then statement1 is executed. If the expr2 is
evaluated to false, then statement2 is executed.
If the expr1 is evaluated to false, then expr3 is evaluated to true or false.
If the expr3 is evaluated to true, then statement3 is executed. If the expr3 is
evaluated to false, then statement4 is executed.
• The flow diagram is shown below:
• Program to select and print the largest of the 3 numbers using nested “if-else”
statements.
#include <iostream>
using namespace std;
int main()
{ int a, b, c;
cout << "Enter Three Values: " << endl;
cin >> a >> b >> c;
return 0;
}
Output:
Enter Three Values:
786
Largest Value is: 8
4.1.4 THE else if LADDER STATEMENT
• This is basically a “multi-way” decision-statement.
• This is used when we must choose among many alternatives.
• Syntax:
if(expression1)
statement1; else
if(expression2)
statement2;
else if(expression3)
statement3 else
if(expression4)
statement4
else
default statement5
• The expressions are evaluated in order (i.e. top to bottom).
• If an expression is evaluated to true, then
→ statement associated with the expression is executed &
→ control comes out of the entire else if ladder
• For ex, if exprression1 is evaluated to true, then statement1 is executed.
If all the expressions are evaluated to false, the last statement4 (default case) is executed.
• Program to illustrate the use of else if ladder statement.
#include <iostream>
using namespace std;
int main() {
char grade; // local variable definition
cout << "Enter grade A to D: " << endl;
cin >> grade;
switch (grade) {
case 'A':
cout << "Excellent!" << endl;
break;
case 'B':
cout << "Well done" << endl;
break;
case 'C':
cout << "You passed" << endl;
break;
#include <iostream>
using namespace std;
int main()
{ int n;
cout << "Enter any integer: ";
cin >> n;
if (n > 0)
cout << "Number is Positive" << endl;
else if (n < 0)
cout << "Number is Negative" << endl;
else if (n == 0)
cout << "Number is Zero" << endl;
else
cout << "Invalid input" << endl;
return 0;
}
Output:
Enter any integer: 7
Number is Positive
4.1.5 THE switch-statement
• This is basically a “multi-way” decision-statement.
• This is used when we must choose among many alternatives.
• The syntax & flow diagram is shown below:
int main() {
cout << "Welcome to C++ language" << endl;
cout << "Welcome to C++ language" << endl;
cout << "Welcome to C++ language" << endl;
cout << "Welcome to C++ language" << endl;
cout << "Welcome to C++ language" << endl;
return 0;
}
Output:
Welcome to C++ language
Welcome to C++ language
Welcome to C++ language
Welcome to C++ language
Welcome to C++ language
4.2 BASIC CONCEPT OF LOOP
• Let's consider a situation when you want to write a message “Welcome to C++ language” five times.
Here is a simple C++ program to do the same:
case 'D':
cout << "Better try again" << endl;
break;
default:
cout << "Invalid grade" << endl;
return 0;
}
cout << "Your grade is " << grade << endl;
return 0;
}
Output:
enter grade A to D:
B
Well done
Your grade is B
#include <iostream>
using namespace std;
int main() {
int i = 1;
while (i <= 5) {
cout << "Welcome to C++ language" << endl;
i = i + 1;
}
return 0;
}
Output:
Welcome to C++ language
Welcome to C++ language
Welcome to C++ language
Welcome to C++ language
Welcome to C++ language
• Program to display a message 5 times using while statement.
4.2.2 THE for LOOP
• A for loop-statement can be used to execute s set of statements repeatedly as long as a given condition
is true.
• Syntax:
for(expr1;expr2;expr3)
{
statement1;
}
• Here, expr1 contains initialization statement
expr2 contains limit test expression expr3 contains
updating expression
• Firstly, expr1 is evaluated. It is executed only once.
• Then, expr2 is evaluated to true or false.
If expr2 is evaluated to false, the control comes out of the loop without executing the
body of the loop.
If expr2 is evaluated to true, the body of the loop (i.e. statement1) is executed.
• After executing the body of the loop, expr3 is evaluated.
• Then expr2 is again evaluated to true or false. This cycle continues until expression
becomes false.
• The flow diagram is shown below:
Program to display a message 5 times using for statement.
int main() {
for (int i = 1; i <= 5; i++) {
cout << "Welcome to C++ language" << endl;
}
return 0;
}
Output:
Welcome to C++ language
Welcome to C++ language
Welcome to C++ language
Welcome to C++ language
Welcome to C++ language
4.2.3 THE do while STATEMENT
• When we do not know exactly how many times a set of statements have to be repeated, do-while
statement can be used.
• Syntax:
do
{
statement1;
}while(expression);
• Firstly, the body of the loop is executed. i.e. the body of the loop is executed at
least once.
• Then, the expression is evaluated to true or false.
• If the expression is evaluated to true, the body of the loop (i.e. statement1) is executed
• After executing the body of the loop, the expression is again evaluated to true or false. This cycle
continues until expression becomes false.
• The flow diagram is shown below:
#include <iostream>
using namespace std;
int main() {
int i = 1;
do {
cout << "Welcome to C++ language" << endl;
i = i + 1;
} while (i <= 5);
return 0;
}
Output:
Welcome to C++ language
Welcome to C++ language
Welcome to C++ language
Welcome to C++ language
Welcome to C++ language
• Program to display a message 5 times using do while statement
WHILE LOOP VS. DO-WHILE LOOP
4.2.4 NESTED LOOPS
• Nested loops refer to placing one loop inside another loop.
• Syntax:
for (initialization; condition; update) {
// Outer loop code
for (initialization; condition; update) {
// Inner loop code
}
}
while (outer_condition) {
// Outer loop code
while (inner_condition) {
// Inner loop code
}
}
do {
// Outer loop code
do {
// Inner loop code
} while (inner_condition);
} while (outer_condition);
#include <iostream>
int main() {
for (int i = 1; i <= 3; ++i) { for
(int j = 1; j <= 3; ++j) {
cout << "(" << i << "," << j << ") ";
}
cout << endl;
}
return 0;
}
Output:
(1,1) (1,2) (1,3)
(2,1) (2,2) (2,3)
(3,1) (3,2) (3,3)
4.3 break AND continue STATEMENTS
4.3.1 THE break STATEMENT
• The break statement is jump-statement which can be used in switch-statement and loops.
• The break statement works as shown below:
1) If break is executed in a switch-block, the control comes out of the switch-block and the statement
following the switch-block will be executed.
2) If break is executed in a loop, the control comes out of the loop and the statement following the loop
will be executed.
• Syntax:
#include <iostream>
int main() {
for (int i = 1; i <= 10; ++i)
{ if (i == 5) {
cout << "Breaking the loop at i = 5" << endl;
break;
}
cout << i << " ";
}
return 0;
}
Output:
1 2 3 4 Breaking the loop at i = 5
PROBLEM SOLVING AND PROGRAMMING USING
C++
4.3.2 THE continue STATEMENT
• During execution of a loop, it may be necessary to skip a part of the loop based on some condition. In
such cases, we use continue statement.
• The continue statement is used only in the loop to terminate the current iteration.
• Syntax:
• Program to read and add only positive numbers using continue statement. #include
<iostream>
using namespace std; int
main() {
int i = 1, num, sum = 0; for (i =
0; i < 5; i++) {
cout << "Enter an integer: "; cin >>
num;
if (num < 0) {
cout << "You have entered a negative number" << endl; continue; //
skip the remaining part of loop
}
sum = sum + num;
}
cout << "The sum of the positive integers entered = " << sum << endl; return 0;
}
Output:
Enter an integer: 10 Enter
an integer:-15
You have entered a negative number Enter an
integer: 15
Enter an integer: -100
You have entered a negative number Enter an
integer: 30
The sum of the positive integers entered = 55