C++ Controls, Ptrs
C++ Controls, Ptrs
cout<<setfill('*')<<setw(10);
cout<<15<<endl;
cout<<setfill('#')<<setw(5);
cout<<5<<endl;
getch();
}
Output
Value of str is : Welcome to SYBCA Class
The << operator is overloaded to output data items of built-in types integer, float,
double, strings and pointer values.
The insertion operator << may be used more than once in a single statement as
shown above and endl is used to add a new-line at the end of the line.
Output
Please enter your name: PoojaPatil
PoojaPatil
Output
GoodMorning
End of line
Examples:
#include <iostream.h>
#include<conio.h>
void main() {
cout<< "Size of char : " <<sizeof(char) <<endl;
cout<< "Size of int : " <<sizeof(int) <<endl;
cout<< "Size of short int : " <<sizeof(short int) <<endl;
cout<< "Size of long int : " <<sizeof(long int) <<endl;
cout<< "Size of float : " <<sizeof(float) <<endl;
cout<< "Size of double : " <<sizeof(double) <<endl;
cout<< "Size of wchar_t : " <<sizeof(wchar_t) <<endl;
Prof. Paurnima N. Patil. Page 4
getch();
}
Output
Size of char : 1
Size of int : 4
Size of short int : 2
Size of long int : 8
Size of float : 4
Size of double : 8
Size of wchar_t : 4
#include <iostream.h>
#include<conio.h>
#include <string.h>
int main() {
// Integer data types
int a = 10;
short b = 20;
long c = 30;
long long d = 40;
cout<< "Integer data types: " <<endl;
cout<< "int: " << a <<endl;
cout<< "short: " << b <<endl;
cout<< "long: " << c <<endl;
cout<< "long long: " << d <<endl;
Output
int: 10
short: 20
long: 30
long long: 40
Floating-point data types:
float: 3.14
double: 3.14159
long double: 3.14159
Character data types:
char: a
Output
m is 20
Types of Functions
There are two types of functions in C++ programming:
1) Library Functions: are the functions which are declared in the C++ header
files such as ceil(x), cos(x), exp(x), etc.
2) User-defined functions: are the functions which are created by the C++
programmer, so that he/she can use it many times. It reduces complexity of a
big program and optimizes the code.
1) Call By Value
The call by value method of passing arguments to a function copies the
actual value of an argument into the formal parameter of the function. In this case,
changes made to the parameter inside the function have no effect on the argument.
Example:
// function declaration
void swap(int x, int y);
void main ()
{
int a = 100;
int b = 200;
getch();
}
Output:
Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :100
After swap, value of b :200
2) Call By Reference
The call by reference method of passing arguments to a function copies the
reference of an argument into the formal parameter. Inside the function, the
reference is used to access the actual argument used in the call. This means that
changes made to the parameter affect the passed argument.
Example
#include<iostream.h>
#include<conio.h>
// function declaration
void swap(int &x, int &y);
void main ()
{
swap(a, b);
getch();
}
Output:
Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :200
After swap, value of b :100
Array
An array is a collection of items stored at continuous memory locations. The
idea of array is to represent many instances in one variable.
Syntax:
DataType ArrayName[size_of_array];
Example
#include <iostream>
using namespace std;
int main()
{
arr[3] = arr[0];
return 0;
}
Output:
5 2 -10 5
Output
Address of number variable is:0x7ffccc8724c4
Address of p variable is:0x7ffccc8724c4
Value of p variable is:30
Pointer Arithmetic
Pointer is an address which is a numeric value; therefore, you can perform
arithmetic operations on a pointer just as you can a numeric value. There are four
arithmetic operators that can be used on pointers: ++, --, +, and -.
Incrementing a Pointer
We prefer using a pointer in our program instead of an array because the
variable pointer can be incremented, unlike the array name which cannot be
incremented because it is a constant pointer.
Decrementing a Pointer
The same considerations apply to decrementing a pointer, which decreases
its value by the number of bytes
Example:
Program To Demonstrate The Incrementy And Decrement
#include <iostream.h>
#include<conio.h>
void main()
{
int num = 27;
Pointer to an array
It is also known as a locator or indicator that points to an address of a variable or
memory block. Pointers can store the address of a single variable, as well as it can
also store the address of indexes of an array.
Example:
#include<iostream.h>
#include<conio.h>
void main()
{
int arr[5] = { 1, 2, 3, 4, 5 };
int *ptr = arr;
cout <<"\n"<< ptr;
getch();
}
Output:
0x7fff16b7b6d0
ptr = &var;
pptr = &ptr;
Syntax:
return_type (*FuncPtr) (parameter type, ....);
Example:
#include<iostream.h>
#include<conio.h>
int multiply(int a, int b)
{
return a * b;
}
void main()
{
int (*func)(int, int);
func = multiply;
int prod = func(15, 2);
cout << "The value of the product is: " << prod << endl;
getch();
}
Output:
The value of the product is: 30
Output:
Empid10
Salary42000
Examples
#include <iostream.h>
#include <conio.h>
Output
Day 4
Output
Day 2
Syntax:
Output
Roll Number: 1
Name: Harry
Marks: 91.5
2) Protected:
The protected keyword is used to create protected members (data and
function). The protected members can be accessed within the class and from
the derived class
Output
Area of side 4 is49
Area of side 10 is100
Output
49
Structure: A structure creates a data type that can be used to group items of
possibly different types into a single type.
Syntax:
struct address {
char name[50];
char street[100];
char city[50];
char state[20];
int pin;
};
Examples:
struct Books {
char title[50];
char author[50];
char subject[100];
intbook_id;
};
void main()
{
struct Books Book1; // Declare Book1 of type Book
// book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
getch();
}
Output
Book 1 title : Learn C++ Programming
Book 1 author : Chand Miyan
Book 1 subject : C++ Programming
Book 1 id : 6495407
Unions: A union is a type of structure that can be used where the amount of
memory used is a key factor. Similarly to the structure, the union can contain
different types of data types.Each time a new variable is initialized from the
union it overwrites the previous in C language but in C++ we also don‟t
Where:
Name of the union – One can use any name as the union‟s name. After
writing the union, name the union according to the requirement.
Define the members − Here, the coder has to define the member variables.
Union objects − Here, the coder can write the objects of the union.
Example
#include<iostream.h>
#include<conio.h>
union product
{
intproductid;
char name[20];
float price;
};
void main()
{
union product obj;
cout<< "Enter product-id: ";
cin>>obj.productid;
cout<< "Enter name of product: ";
cin>> obj.name;
cout<< "Enter price of product: ";
cin>>obj.price;
cout<< "Product-id is: " <<obj.productid<<endl;
cout<< "Product name is: " << obj.name <<endl;
Output:
Enter product-id: 12
Enter name of product: Phone
Enter price of product: 1200.89
Product-id is: 1150688379
Product name is: {ûDe
Product price is: 1200.89
Above program show a union can have many members but only one of its
members will have a value at any given time. Here, we stored the last value in the
price. So, only the price variable will have the correct value.
Operators
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
1) Arithmetic Operators
There are following arithmetic operators supported by C++ language −
Assume variable A holds 10 and variable B holds 20, then −
Operator Description Example
+ Adds two operands A + B will give 30
- Subtracts second operand from the first A - B will give -10
* Multiplies both operands A * B will give 200
/ Divides numerator by de-numerator B / A will give 2
% Modulus Operator and remainder of after an B % A will give 0
integer division
++ Increment operator, increases integer value by one A++ will give 11
-- Decrement operator, decreases integer value by A-- will give 9
one
#include <iostream.h>
#include <conio.h>
void main() {
int a = 21;
int b = 10;
int c ;
c = a + b;
cout<< "addition is :" << c <<endl ;
c = a - b;
cout<< "substraction is :" << c <<endl
;
c = a * b;
cout<< "multiplication is :" << c <<endl ;
c = a / b;
cout<< "division is :" << c <<endl ;
c = a % b;
cout<< "modulo is :" << c <<endl ;
c = a++;
cout<< "increment operator is :" << c <<endl ;
c = a--;
cout<< "decrement operator is :" << c <<endl ;
getch();
}
Output
addition is :31
2) Relational Operators
There are following relational operators supported by C++ language
Assume variable A holds 10 and variable B holds 20, then −
Operator Description Example
== Checks if the values of two operands are (A == B) is not true.
equal or not, if yes then condition becomes
true.
!= Checks if the values of two operands are (A != B) is true.
equal or not, if values are not equal then
condition becomes true.
> Checks if the value of left operand is greater (A > B) is not true.
than the value of right operand, if yes then
condition becomes true.
< Checks if the value of left operand is less (A < B) is true.
than the value of right operand, if yes then
condition becomes true.
>= Checks if the value of left operand is greater (A >= B) is not true.
than or equal to the value of right operand,
if yes then condition becomes true.
<= Checks if the value of left operand is less (A <= B) is true.
than or equal to the value of right operand,
if yes then condition becomes true.
Example:
#include <iostream.h>
#include <conio.h>
void main() {
int a = 21;
int b = 10;
int c ;
Prof. Paurnima N. Patil. Page 27
if( a == b ) {
cout<< "a is equal to b" <<endl ;
} else {
cout<< "a is not equal to b" <<endl ;
}
if( a < b ) {
cout<< "a is less than b" <<endl ;
} else {
cout<< "a is not less than b" <<endl ;
}
if( a > b ) {
cout<< "a is greater than b" <<endl ;
} else {
cout<< "a is not greater than b" <<endl ;
}
if( b >= a ) {
cout<< "b is either greater than \ or equal to b" <<endl ;
}
getch();
}
Output
a is not equal to b
a is not less than b
a is greater than b
a is either less than or equal to b
b is either greater than or equal to b
Example:
#include <iostream.h>
#include <conio.h>
void main() {
int a = 5;
int b = 20;
int c ;
if(a && b) {
cout<< "Condition is true"<<endl ;
}
if(a || b) {
cout<< "Condition is true"<<endl ;
}
if(a && b) {
cout<< "Condition is true"<<endl ;
} else {
Prof. Paurnima N. Patil. Page 29
cout<< "Condition is not true"<<endl ;
}
getch();
}
Output
Condition is true
Condition is true
Condition is not true
Condition is true
4) Bitwise Operators
Bitwise operator works on bits and perform bit-by-bit operation. The truth tables
for &, |, and ^ are as follows −
p q p&q p|q p^q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Assume if A = 60; and B = 13; now in binary format they will be as follows −
A = 0011 1100
B = 0000 1101
-----------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
Example:
#include <iostream.h>
#include <conio.h>
void main() {
unsigned int a = 60; // 60 = 0011 1100
unsigned int b = 13; // 13 = 0000 1101
int c = 0;
c = a | b; // 61 = 0011 1101
cout<< "Value of c is: " << c <<endl ;
c = a ^ b; // 49 = 0011 0001
cout<< "Value of c is: " << c <<endl ;
getch();
}
Output
Value of c is : 12
Value of c is: 61
Value of c is: 49
Value of c is: -61
Value of c is: 240
Value of c is: 15
5) Assignment Operators
There are following assignment operators supported by C++ language −
Operator Description Example
= Simple assignment operator, Assigns C = A + B will assign value of A
values from right side operands to left + B into C
side operand.
+= Add AND assignment operator, It adds C += A is equivalent to C = C +
right operand to the left operand and A
assign the result to left operand.
-= Subtract AND assignment operator, It C -= A is equivalent to C = C - A
subtracts right operand from the left
operand and assign the result to left
operand.
*= Multiply AND assignment operator, It C *= A is equivalent to C = C * A
multiplies right operand with the left
operand and assign the result to left
operand.
/= Divide AND assignment operator, It C /= A is equivalent to C = C / A
divides left operand with the right
operand and assign the result to left
operand.
%= Modulus AND assignment operator, It C %= A is equivalent to C = C %
takes modulus using two operands and A
Example:
#include <iostream.h>
#include <conio.h>
void main() {
int a = 21;
int c ;
c = a;
cout<< "= Operator, Value of c = : " <<c<<endl ;
c += a;
cout<< "+= Operator, Value of c = : " <<c<<endl ;
c -= a;
cout<< "-= Operator, Value of c = : " <<c<<endl ;
c *= a;
cout<< "*= Operator, Value of c = : " <<c<<endl ;
c /= a;
cout<< "/= Operator, Value of c = : " <<c<<endl ;
c = 200;
c %= a;
cout<< "%= Operator, Value of c = : " <<c<<endl ;
c <<= 2;
cout<< "<<= Operator, Value of c = : " <<c<<endl ;
c >>= 2;
cout<< ">>= Operator, Value of c = : " <<c<<endl ;
Prof. Paurnima N. Patil. Page 33
c &= 2;
cout<< "&= Operator, Value of c = : " <<c<<endl ;
c ^= 2;
cout<< "^= Operator, Value of c = : " <<c<<endl ;
c |= 2;
cout<< "|= Operator, Value of c = : " <<c<<endl ;
getch();
}
Output
= Operator, Value of c = : 21
+= Operator, Value of c = : 42
-= Operator, Value of c = : 21
*= Operator, Value of c = : 441
/= Operator, Value of c = : 21
%= Operator, Value of c = : 11
<<= Operator, Value of c = : 44
>>= Operator, Value of c = : 11
&= Operator, Value of c = : 2
^= Operator, Value of c = : 0
|= Operator, Value of c = : 2
6) Misc Operators
The following table lists some other operators that C++ supports.
Sr.No Operator & Description
1. sizeof
sizeof operator returns the size of a variable. For example,sizeof(a), where „a‟
is integer, and will return 4.
2. Condition ? X : Y
Conditional operator (?). If Condition is true then it returns value of X
otherwise returns value of Y.
3. Cast
Casting operators convert one data type to another. For example,int(2.2000)
would return 2.
getch();
}
Output
Size of char : 1
Size of int : 4
Size of short int : 2
Size of long int : 4
Size of float : 4
Size of double : 8
Size of wchar_t : 4
c = (int) a;
cout<< "Value of (int)a is :" << c <<endl ;
c = (int) b;
cout<< "Value of (int)b is :" << c <<endl ;
getch();
}
Output:
Value of (int)a is :21
Value of (int)b is :10
1) if statement
The if statement is the most simple decision-making statement. It is used to
decide whether a certain statement or block of statements will be executed or not
i.e if a certain condition is true then a block of statements is executed otherwise
not.
Syntax
if(condition)
{
// Statements to execute
if
// condition is true
}
Here, the condition after evaluation will be either true or false. C if statement
accepts boolean values – if the value is true then it will execute the block of
statements below it otherwise not. If we do not provide the curly braces „{„ and „}‟
after if(condition) then by default if statement will consider the first immediately
below statement to be inside its block.
Example
#include <iostream.h>
#include <conio.h>
void main () {
int number = 10;
if (number % 2 == 0)
{
cout<< "The Number you have Enter it is Even";
}
getch();
}
Output
The Number you have Enter it is Even
Output
The Number you have Enter it is Odd
3) Nested if-else
A nested if in C is an if statement that is the target of another if statement.
Nested if statements mean an if statement inside another if statement. Yes, both C
if (i == 10) {
// First if statement
if (i < 15)
cout<< "i is smaller than 15\n";
if (i < 12)
cout<< "i is smaller than 12
too\n";
else
cout<< "i is greater than 15";
}
getch();
}
Output
i is smaller than 15
i is smaller than 12 too
#include <iostream.h>
#include <conio.h>
voidmain(){
int number;
cout<<"To Check Grade Enter a Number:";
cin>> number;
if(number <0|| number >100)
{
cout<<"wrong No";
}
elseif(number >=0&& number <40)
{
cout<<"Fail";
}
elseif(number >=40&& number <59)
{
cout<<"D Grade";
}
elseif(number >=60&& number <70)
{
cout<<" C Grade";
}
Prof. Paurnima N. Patil. Page 40
elseif(number >=71&& number <79)
{
cout<<"B Grade";
}
elseif(number >=80&& number <89)
{
cout<<"A Grade";
}
elseif(number >=90&& number <=100)
{
cout<<"A+ Grade";
}
getch();
}
Output
To Check Grade Enter a Number:40
D Grade
5) Switch Statement
The switch case statement is an alternative to the if else if ladder that can be
used to execute the conditional code based on the value of the variable specified in
the switch statement. The switch block consists of cases to be executed based on
the value of the switch variable.
Syntax of switch
switch (expression) {
case value1:
statements;
case value2:
statements;
....
....
....
default:
statements;}
Example
#include<iostream.h>
#include<conio.h>
void main () {
int number;
Prof. Paurnima N. Patil. Page 41
cout<< "To check the grade enter a number:";
cin>> number;
switch (number)
{
case 2: cout<< "It is 2"; break;
case 3: cout<< "It is 3"; break;
case 4: cout<< "It is 4"; break;
default: cout<< "Not 2, 3 or 4"; break;
}
getch();
}
Output
To check the grade enter a number:3
It is 3
Iteration Statements
1) While Loop
In C++, the loop is used several times for the iteration of a part of the
program. If the iteration number is not set, it is advisable to use the loop rather than
the loop.
Syntax
While(condition0
{
//code should be executed;
}
Example
#include<iostream.h>
#include<conio.h>
void main() {
int i = 5;
while(i <= 10)
{
cout<< i << "\t";
i++;
}
getch();
Output
5 6 7 8 9 10
2) Do while loop
C++ is used many times to iterate a part of the software. It is advised that
you use a do-while loop, if the number of iteration is not known and the loop must
be performed at least once.
Syntax
do
{
//code should be executed;
}
While(condition);
Example
#include<iostream.h>
#include<conio.h>
void main() {
int j = 2;
do{
cout<< j << "\t";
j++;
} while (j <= 10) ;
getch();
}
Output
2 3 4 5 6 7 8 9 10
3) For Loop
The C++ loop is used multiple times to iterate a part of the program. It is
recommended that you use for loops when the iteration number is set. For loops, it
is recommended.
Syntax
For(initialization; condition;
Example
#include <iostream.h>
#include <conio.h>
void main() {
for(int i = 3; i <= 10; i++){
cout<< i << "\t";
}
getch();
}
3 4 5 6 7 8 9
Jump Statements
1) Break Statement
The break C++ is used for loop breakage or statement switching. It breaks
the program‟s current flow in the given state. In the case of an inner loop, only an
internal loop splits.
Syntax
Jump-statement;
break;
Example
#include<iostream.h>
#include<conio.h>
void main() {
for (int j = 1; j <= 20; j++)
{
if (j == 15)
{
break;
}
cout<< j << "\t";
}
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14
2) Continue Statement
The declaration C++ is used for the continuation of the loop. The current
program flow continues and the remaining code is omitted at a specified state. If
there is an inner loop, only an inner loop continues.
Syntax
Jump-statement;
Continue;
Example
#include<iostream.h>
#include<conio.h>
void main()
{
if (i == 6)
continue;
else
cout<< i << " ";
}
getch();
}
Output
1 2 3 4 5 7 8 9 10
void main()
{
printNumbers();
getch();
}
Output
1 2 3 4 5 6 7 8 9 10
Recursive Function
A function that calls itself is called a recursive function. When a recursive
function is called, it executes a set of instructions and then calls itself to execute
the same set of instructions with a smaller input. This process continues until a
base case is reached, which is a condition that stops the recursion and returns a
value.
Example
#include<iostream.h>
#include<conio.h>
int factorial(int);
result = factorial(n);
cout << "Factorial of " << n << " = " << result;
getch();
}
int factorial(int n)
{
if (n > 1)
{
return n * factorial(n - 1);
}
else
{
return 1;
}
}
Output:
Enter a non-negative number: 4
Factorial of 4 = 24
Inline Function
1) If a function is inline, the compiler places a copy of the code of that function at
each point where the function is called at compile time.
2) Any change to an inline function could require all clients of the function to be
recompiled because compiler would need to replace all the code once again
otherwise it will continue with old functionality.
3) To inline a function, place the keyword inline before the function name and
define the function before any calls are made to the function. The compiler can
ignore the inline qualifier in case defined function is more than a line.
OR
Function Overloading
Function overloading is a feature of object-oriented programming where two
or more functions can have the same name but different parameters. When a
function name is overloaded with different jobs it is called Function Overloading.
In Function Overloading “Function” name should be the same and the arguments
should be different.
Suppose you have to perform addition of the given numbers but there can be
any number of arguments, if you write the function such as a(int,int) for two
parameters, and b(int,int,int) for three parameters then it may be difficult for you to
understand the behavior of the function because its name differs.
Declaration
add(int a, int b)
add(double a, double b)