SlideShare a Scribd company logo
1 of 113
Introduction to C ++
Problem Solving Skills
ā€¢ STEP 1: ANALYSE THE PROBLEM
ā€¢ STEP 2: PLANNING THE ALGORITHM
ā€¢ STEP 3: CHECK THE ALGORITHM
ā€¢ STEP 4: CODING THE ALGORITHM
ā€¢ STEP 5: EVALUATE AND MODIFY
Software Evolution
Procedural Oriented Programming
Object Oriented Programming
Basics Concepts of Object Oriented
Programming
Inheritance Property
Polymorphism Property
Application of OOP
Problem solving skills
ā€¢ Step 1: Understanding the Problem:
ā€¢ Here we try to understand the problem to be solved in totally. Before with the next stage
or step, we should be absolutely sure about the objectives of the given problem.
ā€¢ Step 2: Analyzing the Problem:
ā€¢ After understanding thoroughly the problem to be solved, we look at different ways of
solving the problem and evaluate each of these methods.
ā€¢ The idea here is to search for an appropriate solution to the problem under
consideration. The end result of this stage is a broad overview of the sequence of
operations that are to be carried out to solve the given problem.
ā€¢ Step 3: Developing the solution:
ā€¢ Here, the overview of the sequence of operations that was the result of the analysis
stage is expanded to form a detailed step by step solution to the problem under
consideration.
Problem solving skills
ā€¢ Step 4: Coding and Implementation:
ā€¢ The last stage of problem-solving is the conversion of the detailed sequence of operations into a language
that the computer can understand. Here, each step is converted to its equivalent instruction or instructions
in the computer language that has been chosen for the implantation.
ā€¢ The vehicle for the computer solution to a problem is a set of explicit and unambiguous instructions
expressed in a programming language. This set of instruction is called a program with problem solving
through programming in C++.
ā€¢ A program may also be thought of as an algorithm expressed in a programming language. an algorithm,
therefore, corresponds to a solution to a problem that is independent of any programming language.
ā€¢ To obtain the computer solution to a problem once we have the program we usually have to supply the
program with input or data. The program then takes this input and manipulates it according to its
instructions. Eventually produces an output which represents the computer solution to the problem.
ā€¢ The problem solving is a skill and there are no universal approaches one can take to solving problems.
Basically one must explore possible avenues to a solution one by one until she/he comes across the right
path to a solution.
ā€¢ In general, as one gains experience in solving problems, one develops oneā€™s own techniques and strategies,
though they are often intangible. Problem-solving skills are recognized as an integral component of
computer programming.
What is C++
Rules of flowchart :
Algorithm with Flowchart
Pre processor directive
ā€¢ Nothing but a program
ā€¢ It makes the C++ program portable
ā€¢ User can create own pre-processor directive
ā€¢ Can be placed before the main function
ā€¢ Cannot be terminated by semi colon since it is a special instruction
ā€¢ E.g # include ā€“ includes the header file (containing library functions)
Namespace in C++
ā€¢ What is a namespace in C++?
ā€¢ A namespace is a declarative region that provides a scope where
identifiers like variables, functions, classes etc are declared.
ā€¢ Main purpose is to prevent ambiguity that may occur when two
identifiers have same name.
ā€¢
Simple C++ Program
Operators in C++
Header files
Average of 2 numbers
If condtional statement
ā€¢ Wrt score write grade
ā€¢ Height comparison
ā€¢ Light on/off wrt 0/1
If else conditional statement
ā€¢ Greet according to time of the day
ā€¢ Check if integer is positive, negative or zero
ā€¢ Find largest number amongst 3 nos
Switch case
ā€¢ switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
ā€¢ The switch expression is evaluated once
ā€¢ The value of the expression is compared with the
values of each case
ā€¢ If there is a match, the associated block of code is
executed
ā€¢ The break and default keywords are optional
ā€¢ int day = 4;
switch (day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
}
ā€¢ Write an algorithm, draw a flowchart and write a C program to accept
the percentage and print the grade A (90 to 100), B (80 to 90), C (50
to 80) and Fail below 50.
ā€¢ Write a C++ program to check whether input character is vowel or
consonant.
ā€¢ Write a program to give a choice to user to find area of a shape ā€“
circle, rectangle, triangle, square
Loops in C++
ā€¢While
ā€¢For
ā€¢Do while
ā€¢Nested
For loop
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 5; ++i)
{ cout << i << " "; }
return 0;
}
For loop
ā€¢WAP to display a text 5 times
ā€¢ WAP to find the sum of first n Natural Numbers
ā€¢WAP to find factorial of a number
While loop
While loop
ā€¢ Print 10 numbers
ā€¢ Calculate sum of 10 integer
ā€¢ Calculate sum of odd numbers
do while loop
Do while
ā€¢ Print 10 numbers
ā€¢ Calculate sum of 10 integer
ā€¢ Calculate sum of odd numbers
Nested loop
ā€¢ for ( initialization; condition; increment )
ā€¢ {
ā€¢ for ( initialization; condition; increment )
ā€¢ {
ā€¢ // statement of inside loop } //
ā€¢ statement of outer loop
ā€¢ }
Nested loop
ā€¢ Display 7 days of 3 weeks
ā€¢ Create a pattern
ā€¢ Write a C++ program to print below patterns.
1 1
1 2 2 3
1 2 3 4 5 6
1 2 3 4 7 8 9 10
Nested loop
ā€¢ while(condition)
ā€¢ {
ā€¢ while(condition)
ā€¢ {
ā€¢ // statement of inside loop
ā€¢ }
ā€¢ // statement of outer loop
ā€¢ }
Nested loop
ā€¢ do
ā€¢ {
ā€¢ do
ā€¢ {
ā€¢ // statement of inside loop
ā€¢ }
ā€¢ while(condition);
ā€¢ // statement of outer loop
ā€¢ }
ā€¢ while(condition);
2D array
ā€¢ Write a program to take values for 2-dimensional array and to give
choice to the user for performing below operations:
ā€¢ 1. Sum of elements of each row.
ā€¢ 2. Sum of diagonal elements.
ā€¢ 3. Finding transpose of matrix
2D array
ā€¢ Develop, implement and execute a C++ program that reads two
matrices A (m x n ) and B (p x q ) and Compute the product A and B.
Read matrix A and matrix B in row major order and in column major
order respectively. Print both the input matrices and resultant matrix
with suitable headings and output should be in matrix format only.
Program must check the compatibility of orders of the matrices for
multiplication. Report appropriate message in case of incompatibility.
Functions
1. Write a function to find sum and average of upper diagonal elements
in 2-dimensional matrix. Write a main program () to take number of
rows, cols and values of matrix from the user.
2.Write a menu-based program in C++ (Use Switch case and do while)
that uses a set of functions to perform the following operations:
ā€¢ a. Addition of two complex numbers
ā€¢ b. Subtraction of two complex numbers
ā€¢ c. Multiplication of two complex numbers
Inline function example
#include <iostream>
using namespace std;
inline int cube(int s)
{
return s*s*s;
}
int main()
{
cout << "The cube of 3 is: " << cube(3) << "n";
return 0;
}
Inline function example
#include <iostream>
using namespace std;
inline int Max(int x, int y)
{ return (x > y)? x : y; }
// Main function for the program
int main() { cout << "Max (20,10): " << Max(20,10) << endl;
cout << "Max (0,200): " << Max(0,200) << endl;
cout << "Max (100,1010): " << Max(100,1010) << endl; return 0; }
Some facts about inline function
ā€¢ Compiler may not perform inlining in such circumstances like:
1) If a function contains a loop. (for, while, do-while)
2) If a function contains static variables.
3) If a function is recursive.
4) If a function return type is other than void, and the return
statement doesnā€™t exist in function body.
5) If a function contains switch or goto statement.
Some facts about inline function
ā€¢ The added variables from the inlined function consumes additional
registers, After in-lining function if variables number which are going
to use register increases than they may create overhead on register
variable resource utilization. This means that when inline function
body is substituted at the point of function call, total number of
variables used by the function also gets inserted. So the number of
register going to be used for the variables will also get increased. So if
after function inlining variable numbers increase drastically then it
would surely cause an overhead on register utilization.
Some facts about inline function
ā€¢ If you use too many inline functions then the size of the binary
executable file will be large, because of the duplication of same code.
ā€¢ Too much inlining can also reduce your instruction cache hit rate, thus
reducing the speed of instruction fetch from that of cache memory to
that of primary memory.
Structures
struct { // Structure declaration
int myNum; // Member (int variable)
string myString; // Member (string variable)
} myStructure; // Structure variable
ā€¢ Structures (also called structs) are a way to group several related variables
into one place. Each variable in the structure is known as a member of the
structure.
ā€¢ Unlike an array, a structure can contain many different data types (int,
string, bool, etc.).
Example
ā€¢ // Create a structure variable called myStructure
struct {
int myNum;
string myString;
} myStructure;
// Assign values to members of myStructure
myStructure.myNum = 1;
myStructure.myString = "Hello World!";
// Print members of myStructure
cout << myStructure.myNum << endl;
cout << myStructure.myString << endl;
Example of structure
#include <iostream>
using namespace std;
struct Person
{ char name[50];
int age; float salary; };
int main()
{ Person p1;
cout << "Enter Full name: ";
cin.get(p1.name, 50);
cout << "Enter age: ";
cin >> p1.age;
cout << "Enter salary: ";
cin >> p1.salary;
cout << "nDisplaying Information." << endl;
cout << "Name: " << p1.name << endl;
cout <<"Age: " << p1.age << endl;
cout << "Salary: " << p1.salary; return 0; }
Structure as function argument
#include<iostream>
using namespace std;
struct Person
{ int citizenship; int age; };
void func(struct Person p);
int main()
{ struct Person p; p.citizenship = 1; p.age = 27; func(p); return 0; }
void func(struct Person p) { cout << " Person citizenship: " <<
p.citizenship<<endl;
cout << " Person age: " << p.age; }
Pointers
ā€¢ Pointers store the address of variables or a memory location
ā€¢ pointer ā€œptrā€ holds the address of an integer variable or holds the
address of memory whose value(s) can be accessed as integer values
through ā€œptrā€
ā€¢ Pointers:
ā€¢ Pointers save memory space.
ā€¢ Execution time with pointers is faster because data are manipulated
with the address, that is, direct access to memory location.
ā€¢ Memory is accessed efficiently with the pointers. The pointer assigns
and releases the memory as well. Hence it can be said the Memory of
pointers is dynamically allocated.
ā€¢ Pointers are used with data structures. They are useful for
representing two-dimensional and multi-dimensional arrays.
Pointers
ā€¢ pointer is a symbolic representation of a memory address.
ā€¢ Here is an example of valid pointer declarations in C++:
ā€¢ int *x; // a pointer to integer
ā€¢ double *x; // a pointer to double
ā€¢ float *x; // a pointer to float
ā€¢ char *ch // a pointer to a character
Pointers
ā€¢ Reference operator (&) and Deference operator (*)
ā€¢ The reference operator (&) returns the variableā€™s address.
ā€¢ The dereference operator (*) helps us get the value that has been
stored in a memory address.
Example of Pointers
#include <iostream>
using namespace std;
int main()
{ int x = 27;
int *ip;
ip = &x;
cout << "Value of x is : ";
cout << x << endl;
cout << "Value of ip is : ";
cout << ip<< endl;
cout << "Value of *ip is : ";
cout << *ip << endl; return 0; }
More examples in Pointers
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{ int i = 10;
int *Ptr;
Ptr = &i;
cout << "nValue Of i :" << i;
cout << "nAddress Of i :" << i;
cout << "nValue Of Ptr :" << Ptr;
cout << "nAddress Of Ptr :" << &Ptr;
cout << "nPtr's Pointer Value:" << *Ptr;
cout << "nPtr Equal to &i :" << *(&i); }
NULL pointer
#include <iostream>
using namespace std;
int main()
{ int *ip = NULL;
cout << "Value of ip is: " << ip;
return 0; }
Array using Pointers
#include <iostream>
using namespace std;
int main()
{ int *ip;
int arr[] = { 10, 34, 13, 76, 5, 46 };
ip = arr;
for (int x = 0; x < 6; x++)
{ cout << *ip << endl; ip++; }
return 0; }
Functions in Pointers
#include <iostream>
using namespace std;
void test(int*, int*);
int main()
{ int a = 5, b = 5;
cout << "Before changing:" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
test(&a, &b);
cout << "nAfter changing" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0; }
void test(int* n1, int* n2)
{ *n1 = 10; *n2 = 11; }
Swapping numbers using pointers
#include <iostream>
#include<conio.h>
using namespace std;
// Declare Swap Function Using Pointer
void swap_numbers(int *value1, int *value2)
{ int temp;
temp = *value1;
*value1 = *value2;
*value2 = temp; }
int main()
{ // Declare Variables int number1, number2;
cout << "Simple Example Program for Swap Numbers Using Pointers In C++ā€œ<<endl;
// Read User Input
Swapping numbers using pointers
//Print Values before Swapping
cout << "Enter value of Swap Number # 1: ";
cin>>number1;
cout << "Enter value of Swap Number # 2: ";
cin>>number2;
cout << "Before Swapping : Number # 1=" << number1 << ", Number # 2=" << number2 <<
endl;
//Call Swap Function By Passing Reference
swap_numbers(&number1, &number2);
//Print Values after Swapping
cout << "After Swapping : Number # 1=" << number1 << ", Number # 2=" << number2 <<
endl;
getch();
return 0; }
Print string using pointers
#include <iostream>
#include<conio.h>
using namespace std;
int main()
{ // Declare Variables
char str[20], *pt;
cout << "Pointer Example C++ Program : Print Stringā€<<endl;
cout << "Enter Any string [below 20 chars] : ";
cin>>str;
// Assign to Pointer Variable
pt = str;
while (*pt != '0')
{ cout << *pt;
pt++; }
return 0; }
Declaring a Pointer to a pointer
ā€¢ int *ptr1 = &var; // Declaration of single pointer.
ā€¢ int **ptr2 = &ptr1; // Declaration of double pointer or pointer to
pointer in C++.
Declaring a Pointer to a pointer
#include<iostream>
using namespace std;
int main()
{ // Declaring an integer variable.
int age = 21;
// Declaring a single pointer variable, // storing address of age variable.
int *ptr1 = &age;
// Declaring a double pointer variable storing address of ptr1.
int **ptr2 = &ptr1;
// Printing value using age variable.
cout<<"Value stored in the var variable "<<var<<"n";
// Printing value using single pointer variable.
cout<<"Value accessed using single pointer "<<*ptr1<<"n";
// Printing value using double pointer variable.
cout<<"Value accessed using double pointer "<<**ptr2<<"n";
return 0; }
Examples of pointers
ā€¢ WAP to demonstrate different arithmetic operations on pointers.
ā€¢ WAP to find largest of the elements of an array using a function and
passing argument using pointer.
ā€¢ WAP to demonstrate use of a pointer to pointer.
ā€¢ WAP to demonstrate string handling using pointer.
To do Tasks
ā€¢ WAP to demonstrate different arithmetic operations on pointers.
ā€¢ WAP to swap values of two variables using pointers.
ā€¢ WAP to largest of the elements of an array using a function and
passing argument using pointer.
ā€¢ WAP to demonstrate use of a pointer to pointer.
ā€¢ WAP to demonstrate string handling using pointer.
Advantages of using Pointers
ā€¢ Pointers are variables which store the address of other variables in
C++.
ā€¢ More than one variable can be modified and returned by function
using pointers.
ā€¢ Memory can be dynamically allocated and de-allocated using
pointers.
ā€¢ Pointers help in simplifying the complexity of the program.
ā€¢ The execution speed of a program improves by using pointers.
Class Declaration
Example of Class
Example of Class
Tokens
ā€¢ Keywords
ā€¢ Identifiers
ā€¢ Constants
ā€¢ Strings
ā€¢ Operators
Keywords
Identifiers
ā€¢ Identifiers ā€“ variables, functions, arrays, classes created by
programmer.
ā€¢ Only alphabetic characters, digits and underscore
ā€¢ Name cannot start with a digit
ā€¢ Uppercase and lowercase letters are distinct
ā€¢ Declared keyword cannot be used as variable name
Constant
ā€¢ Refers to fixed values that do not change during execution of a
program
ā€¢ constant int a = 25;
Data Types
Size and range of data types
User defined datatypes : Class
User defined datatypes : Structure
User Defined Data Types
Macros
ā€¢ A macro is a piece of code in a program that is replaced by the value of the macro. Macro is defined by #define directive.
Whenever a macro name is encountered by the compiler, it replaces the name with the definition of the macro. Macro definitions
need not be terminated by a semi-colon(;)
// C++ program to illustrate macros
#include <iostream>
using namespace std;
// Macro definition
#define LIMIT 5
int main()
{
// Print the value of macro defined
cout << "The value of LIMIT"
<< " is " << LIMIT;
return 0;
}
Macros example
// C++ program to illustrate macros
#include <iostream>
using namespace std;
#define AREA(l, b) (l * b)
int main()
{
// Given lengths l1 and l2
int l1 = 10, l2 = 5, area;
// Find the area using macros
area = AREA(l1, l2);
// Print the area
cout << "Area of rectangleā€œ << " is: "<< area;
return 0;
}
Storage classes
Derived data types : Array
Derived data types : Function
Derived data types : Function
Derived Data Types : Pointers
Derived Data Types : Pointers
Declaration of Variables
Control Structures
Example of control structure: Selection
Example of control structure: If statement
Example of control structure: Switch case
Example of control structure: While
statement
Example of control structure: Do While
statement
ā€¢ A hospital management want to enhance their software in the
following manner :
ā€¢ The software should be able to display departments like medicine,
surgery, gynaecology, obstetrics, paediatrics, eye and ENT (any 4)
ā€¢ The user should be able to view the availability of respective doctors
in the respective department with their charge (fees)
ā€¢ Once user selects department he/she wants to visit , a tentative bill is
to be generated
ā€¢ Give the option the user to exit
Array
ā€¢ Declaration :
Datatype arrayName[arraySize]
Few Things to Remember:
ā€¢ The array indices start with 0. Meaning x[0] is the first element stored at
index 0.
ā€¢ If the size of an array is n, the last element is stored at index (n-1). In this
example, x[5] is the last element.
ā€¢ Elements of an array have consecutive addresses. For example, suppose
the starting address of x[0] is 2120.
Then, the address of the next element x[1] will be 2124, the address
of x[2] will be 2128, and so on.
Here, the size of each element is increased by 4. This is because the size
of int is 4 bytes.
Array Initialization
ā€¢ // declare and initialize and array
ā€¢ int x[6] = {19, 10, 8, 17, 9, 15};
Another method to initialize array during
declaration:
ā€¢ / declare and initialize an array
int x[] = {19, 10, 8, 17, 9, 15};
To store and print array
#include<iostream.h>
using namespace using std;
main()
{
int i;
int a[6] = {6,23,15,2,18,9};
for(i=0;i<6;i++)
{cout<<a[i]<<endl;}
return 0;
}
Examples
For below statements declare array variable.
a. To store integer marks of 10 students.
b. To store yearly average rating of 50 employees.
c. To store 10 characters.
Examples
For below arrays find the address of said position.
a. int marks[10]; Find the address of element stored at index 5. Base
address is 1000.
ā€¢ char c[10]; Find the address of element stored at index 7. Base
address is 1000.
45 70 80 30 67 85 98 34 25 56
A C D R Y N O P E F
Examples
Write a program to search an element from given array (linear search)
2 dimensional array
Write a program to take values for 2-dimensional array and to give
choice to the user for performing below operations:
1. Sum of elements of each row.
2. Sum of diagonal elements.
3. Finding transpose of matrix.
Declaring a function
return_type function_name( parameter list )
{ body of the function }
ā€¢ Return Type āˆ’ A function may return a value. The return_type is the data type of
the value the function returns. Some functions perform the desired operations
without returning a value. In this case, the return_type is the keyword void.
ā€¢ Function Name āˆ’ This is the actual name of the function. The function name and
the parameter list together constitute the function signature.
ā€¢ Parameters āˆ’ A parameter is like a placeholder. When a function is invoked, you
pass a value to the parameter. This value is referred to as actual parameter or
argument. The parameter list refers to the type, order, and number of the
parameters of a function. Parameters are optional; that is, a function may contain
no parameters.
ā€¢ Function Body āˆ’ The function body contains a collection of statements that
define what the function does.
Example
ā€¢ // function returning the max between two numbers
int max(int num1, int num2) { // local variable declaration
int result;
if (num1 > num2)
result = num1;
else result = num2;
return result; }
In main
// function declaration
int max(int num1, int num2);
int main ()
{
// local variable declaration:
int a = 100; int b = 200; int ret;
// calling a function to get max value.
ret = max(a, b);
cout << "Max value is : " << ret << endl;
return 0;
}
Introduction to C ++.pptx
Introduction to C ++.pptx
Introduction to C ++.pptx
Introduction to C ++.pptx
Introduction to C ++.pptx

More Related Content

Similar to Introduction to C ++.pptx

C programming language tutorial
C programming language tutorialC programming language tutorial
C programming language tutorialSURBHI SAROHA
Ā 
C programming for Computing Techniques
C programming for Computing TechniquesC programming for Computing Techniques
C programming for Computing TechniquesAppili Vamsi Krishna
Ā 
Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Mohamed El Desouki
Ā 
c++ referesher 1.pdf
c++ referesher 1.pdfc++ referesher 1.pdf
c++ referesher 1.pdfAnkurSingh656748
Ā 
Intro to javascript (5:2)
Intro to javascript (5:2)Intro to javascript (5:2)
Intro to javascript (5:2)Thinkful
Ā 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
Ā 
CPP06 - Functions
CPP06 - FunctionsCPP06 - Functions
CPP06 - FunctionsMichael Heron
Ā 
classVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxclassVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxssusere336f4
Ā 
object oriented programming part inheritance.pptx
object oriented programming part inheritance.pptxobject oriented programming part inheritance.pptx
object oriented programming part inheritance.pptxurvashipundir04
Ā 
Programming Language
Programming  LanguageProgramming  Language
Programming LanguageAdeel Hamid
Ā 
F6dc1 session6 c++
F6dc1 session6 c++F6dc1 session6 c++
F6dc1 session6 c++Mukund Trivedi
Ā 
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2ndConnex
Ā 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesInductive Automation
Ā 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesInductive Automation
Ā 
Basic elements of java
Basic elements of java Basic elements of java
Basic elements of java Ahmad Idrees
Ā 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptxjinkhatima
Ā 
0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdf0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdfssusere19c741
Ā 

Similar to Introduction to C ++.pptx (20)

C programming language tutorial
C programming language tutorialC programming language tutorial
C programming language tutorial
Ā 
C programming for Computing Techniques
C programming for Computing TechniquesC programming for Computing Techniques
C programming for Computing Techniques
Ā 
Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++Basic Programming concepts - Programming with C++
Basic Programming concepts - Programming with C++
Ā 
c++ referesher 1.pdf
c++ referesher 1.pdfc++ referesher 1.pdf
c++ referesher 1.pdf
Ā 
Aspdot
AspdotAspdot
Aspdot
Ā 
Intro to javascript (5:2)
Intro to javascript (5:2)Intro to javascript (5:2)
Intro to javascript (5:2)
Ā 
Software Engineering
Software EngineeringSoftware Engineering
Software Engineering
Ā 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Ā 
Prog1-L2.pptx
Prog1-L2.pptxProg1-L2.pptx
Prog1-L2.pptx
Ā 
CPP06 - Functions
CPP06 - FunctionsCPP06 - Functions
CPP06 - Functions
Ā 
classVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptxclassVII_Coding_Teacher_Presentation.pptx
classVII_Coding_Teacher_Presentation.pptx
Ā 
object oriented programming part inheritance.pptx
object oriented programming part inheritance.pptxobject oriented programming part inheritance.pptx
object oriented programming part inheritance.pptx
Ā 
Programming Language
Programming  LanguageProgramming  Language
Programming Language
Ā 
F6dc1 session6 c++
F6dc1 session6 c++F6dc1 session6 c++
F6dc1 session6 c++
Ā 
Presentation 2nd
Presentation 2ndPresentation 2nd
Presentation 2nd
Ā 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
Ā 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
Ā 
Basic elements of java
Basic elements of java Basic elements of java
Basic elements of java
Ā 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptx
Ā 
0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdf0-Slot05-06-07-Basic-Logics.pdf
0-Slot05-06-07-Basic-Logics.pdf
Ā 

Recently uploaded

Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
Ā 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
Ā 
Study on Air-Water & Water-Water Heat Exchange in a Finned ļ»æTube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned ļ»æTube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned ļ»æTube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned ļ»æTube ExchangerAnamika Sarkar
Ā 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture designssuser87fa0c1
Ā 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
Ā 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
Ā 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
Ā 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
Ā 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIkoyaldeepu123
Ā 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
Ā 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
Ā 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
Ā 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage examplePragyanshuParadkar1
Ā 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHC Sai Kiran
Ā 
Call Us ā‰½ 8377877756 ā‰¼ Call Girls In Shastri Nagar (Delhi)
Call Us ā‰½ 8377877756 ā‰¼ Call Girls In Shastri Nagar (Delhi)Call Us ā‰½ 8377877756 ā‰¼ Call Girls In Shastri Nagar (Delhi)
Call Us ā‰½ 8377877756 ā‰¼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
Ā 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
Ā 

Recently uploaded (20)

Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
Ā 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
Ā 
young call girls in Green ParkšŸ” 9953056974 šŸ” escort Service
young call girls in Green ParkšŸ” 9953056974 šŸ” escort Serviceyoung call girls in Green ParkšŸ” 9953056974 šŸ” escort Service
young call girls in Green ParkšŸ” 9953056974 šŸ” escort Service
Ā 
Study on Air-Water & Water-Water Heat Exchange in a Finned ļ»æTube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned ļ»æTube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned ļ»æTube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned ļ»æTube Exchanger
Ā 
pipeline in computer architecture design
pipeline in computer architecture  designpipeline in computer architecture  design
pipeline in computer architecture design
Ā 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
Ā 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
Ā 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
Ā 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
Ā 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Ā 
EduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AIEduAI - E learning Platform integrated with AI
EduAI - E learning Platform integrated with AI
Ā 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
Ā 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Ā 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
Ā 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
Ā 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage example
Ā 
Introduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECHIntroduction to Machine Learning Unit-3 for II MECH
Introduction to Machine Learning Unit-3 for II MECH
Ā 
Call Us ā‰½ 8377877756 ā‰¼ Call Girls In Shastri Nagar (Delhi)
Call Us ā‰½ 8377877756 ā‰¼ Call Girls In Shastri Nagar (Delhi)Call Us ā‰½ 8377877756 ā‰¼ Call Girls In Shastri Nagar (Delhi)
Call Us ā‰½ 8377877756 ā‰¼ Call Girls In Shastri Nagar (Delhi)
Ā 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
Ā 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
Ā 

Introduction to C ++.pptx

  • 2. Problem Solving Skills ā€¢ STEP 1: ANALYSE THE PROBLEM ā€¢ STEP 2: PLANNING THE ALGORITHM ā€¢ STEP 3: CHECK THE ALGORITHM ā€¢ STEP 4: CODING THE ALGORITHM ā€¢ STEP 5: EVALUATE AND MODIFY
  • 6. Basics Concepts of Object Oriented Programming
  • 10. Problem solving skills ā€¢ Step 1: Understanding the Problem: ā€¢ Here we try to understand the problem to be solved in totally. Before with the next stage or step, we should be absolutely sure about the objectives of the given problem. ā€¢ Step 2: Analyzing the Problem: ā€¢ After understanding thoroughly the problem to be solved, we look at different ways of solving the problem and evaluate each of these methods. ā€¢ The idea here is to search for an appropriate solution to the problem under consideration. The end result of this stage is a broad overview of the sequence of operations that are to be carried out to solve the given problem. ā€¢ Step 3: Developing the solution: ā€¢ Here, the overview of the sequence of operations that was the result of the analysis stage is expanded to form a detailed step by step solution to the problem under consideration.
  • 11. Problem solving skills ā€¢ Step 4: Coding and Implementation: ā€¢ The last stage of problem-solving is the conversion of the detailed sequence of operations into a language that the computer can understand. Here, each step is converted to its equivalent instruction or instructions in the computer language that has been chosen for the implantation. ā€¢ The vehicle for the computer solution to a problem is a set of explicit and unambiguous instructions expressed in a programming language. This set of instruction is called a program with problem solving through programming in C++. ā€¢ A program may also be thought of as an algorithm expressed in a programming language. an algorithm, therefore, corresponds to a solution to a problem that is independent of any programming language. ā€¢ To obtain the computer solution to a problem once we have the program we usually have to supply the program with input or data. The program then takes this input and manipulates it according to its instructions. Eventually produces an output which represents the computer solution to the problem. ā€¢ The problem solving is a skill and there are no universal approaches one can take to solving problems. Basically one must explore possible avenues to a solution one by one until she/he comes across the right path to a solution. ā€¢ In general, as one gains experience in solving problems, one develops oneā€™s own techniques and strategies, though they are often intangible. Problem-solving skills are recognized as an integral component of computer programming.
  • 15. Pre processor directive ā€¢ Nothing but a program ā€¢ It makes the C++ program portable ā€¢ User can create own pre-processor directive ā€¢ Can be placed before the main function ā€¢ Cannot be terminated by semi colon since it is a special instruction ā€¢ E.g # include ā€“ includes the header file (containing library functions)
  • 16. Namespace in C++ ā€¢ What is a namespace in C++? ā€¢ A namespace is a declarative region that provides a scope where identifiers like variables, functions, classes etc are declared. ā€¢ Main purpose is to prevent ambiguity that may occur when two identifiers have same name. ā€¢
  • 20. Average of 2 numbers
  • 21. If condtional statement ā€¢ Wrt score write grade ā€¢ Height comparison ā€¢ Light on/off wrt 0/1
  • 22. If else conditional statement ā€¢ Greet according to time of the day ā€¢ Check if integer is positive, negative or zero ā€¢ Find largest number amongst 3 nos
  • 23. Switch case ā€¢ switch(expression) { case x: // code block break; case y: // code block break; default: // code block }
  • 24. ā€¢ The switch expression is evaluated once ā€¢ The value of the expression is compared with the values of each case ā€¢ If there is a match, the associated block of code is executed ā€¢ The break and default keywords are optional
  • 25. ā€¢ int day = 4; switch (day) { case 1: cout << "Monday"; break; case 2: cout << "Tuesday"; break; case 3: cout << "Wednesday"; break; case 4: cout << "Thursday"; break; case 5: cout << "Friday"; break; case 6: cout << "Saturday"; break; case 7: cout << "Sunday"; break; }
  • 26. ā€¢ Write an algorithm, draw a flowchart and write a C program to accept the percentage and print the grade A (90 to 100), B (80 to 90), C (50 to 80) and Fail below 50. ā€¢ Write a C++ program to check whether input character is vowel or consonant. ā€¢ Write a program to give a choice to user to find area of a shape ā€“ circle, rectangle, triangle, square
  • 28.
  • 29. For loop #include <iostream> using namespace std; int main() { for (int i = 1; i <= 5; ++i) { cout << i << " "; } return 0; }
  • 30. For loop ā€¢WAP to display a text 5 times ā€¢ WAP to find the sum of first n Natural Numbers ā€¢WAP to find factorial of a number
  • 32. While loop ā€¢ Print 10 numbers ā€¢ Calculate sum of 10 integer ā€¢ Calculate sum of odd numbers
  • 34. Do while ā€¢ Print 10 numbers ā€¢ Calculate sum of 10 integer ā€¢ Calculate sum of odd numbers
  • 35. Nested loop ā€¢ for ( initialization; condition; increment ) ā€¢ { ā€¢ for ( initialization; condition; increment ) ā€¢ { ā€¢ // statement of inside loop } // ā€¢ statement of outer loop ā€¢ }
  • 36. Nested loop ā€¢ Display 7 days of 3 weeks ā€¢ Create a pattern ā€¢ Write a C++ program to print below patterns. 1 1 1 2 2 3 1 2 3 4 5 6 1 2 3 4 7 8 9 10
  • 37. Nested loop ā€¢ while(condition) ā€¢ { ā€¢ while(condition) ā€¢ { ā€¢ // statement of inside loop ā€¢ } ā€¢ // statement of outer loop ā€¢ }
  • 38. Nested loop ā€¢ do ā€¢ { ā€¢ do ā€¢ { ā€¢ // statement of inside loop ā€¢ } ā€¢ while(condition); ā€¢ // statement of outer loop ā€¢ } ā€¢ while(condition);
  • 39. 2D array ā€¢ Write a program to take values for 2-dimensional array and to give choice to the user for performing below operations: ā€¢ 1. Sum of elements of each row. ā€¢ 2. Sum of diagonal elements. ā€¢ 3. Finding transpose of matrix
  • 40. 2D array ā€¢ Develop, implement and execute a C++ program that reads two matrices A (m x n ) and B (p x q ) and Compute the product A and B. Read matrix A and matrix B in row major order and in column major order respectively. Print both the input matrices and resultant matrix with suitable headings and output should be in matrix format only. Program must check the compatibility of orders of the matrices for multiplication. Report appropriate message in case of incompatibility.
  • 41. Functions 1. Write a function to find sum and average of upper diagonal elements in 2-dimensional matrix. Write a main program () to take number of rows, cols and values of matrix from the user. 2.Write a menu-based program in C++ (Use Switch case and do while) that uses a set of functions to perform the following operations: ā€¢ a. Addition of two complex numbers ā€¢ b. Subtraction of two complex numbers ā€¢ c. Multiplication of two complex numbers
  • 42. Inline function example #include <iostream> using namespace std; inline int cube(int s) { return s*s*s; } int main() { cout << "The cube of 3 is: " << cube(3) << "n"; return 0; }
  • 43. Inline function example #include <iostream> using namespace std; inline int Max(int x, int y) { return (x > y)? x : y; } // Main function for the program int main() { cout << "Max (20,10): " << Max(20,10) << endl; cout << "Max (0,200): " << Max(0,200) << endl; cout << "Max (100,1010): " << Max(100,1010) << endl; return 0; }
  • 44. Some facts about inline function ā€¢ Compiler may not perform inlining in such circumstances like: 1) If a function contains a loop. (for, while, do-while) 2) If a function contains static variables. 3) If a function is recursive. 4) If a function return type is other than void, and the return statement doesnā€™t exist in function body. 5) If a function contains switch or goto statement.
  • 45. Some facts about inline function ā€¢ The added variables from the inlined function consumes additional registers, After in-lining function if variables number which are going to use register increases than they may create overhead on register variable resource utilization. This means that when inline function body is substituted at the point of function call, total number of variables used by the function also gets inserted. So the number of register going to be used for the variables will also get increased. So if after function inlining variable numbers increase drastically then it would surely cause an overhead on register utilization.
  • 46. Some facts about inline function ā€¢ If you use too many inline functions then the size of the binary executable file will be large, because of the duplication of same code. ā€¢ Too much inlining can also reduce your instruction cache hit rate, thus reducing the speed of instruction fetch from that of cache memory to that of primary memory.
  • 47. Structures struct { // Structure declaration int myNum; // Member (int variable) string myString; // Member (string variable) } myStructure; // Structure variable ā€¢ Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. ā€¢ Unlike an array, a structure can contain many different data types (int, string, bool, etc.).
  • 48. Example ā€¢ // Create a structure variable called myStructure struct { int myNum; string myString; } myStructure; // Assign values to members of myStructure myStructure.myNum = 1; myStructure.myString = "Hello World!"; // Print members of myStructure cout << myStructure.myNum << endl; cout << myStructure.myString << endl;
  • 49. Example of structure #include <iostream> using namespace std; struct Person { char name[50]; int age; float salary; }; int main() { Person p1; cout << "Enter Full name: "; cin.get(p1.name, 50); cout << "Enter age: "; cin >> p1.age; cout << "Enter salary: "; cin >> p1.salary; cout << "nDisplaying Information." << endl; cout << "Name: " << p1.name << endl; cout <<"Age: " << p1.age << endl; cout << "Salary: " << p1.salary; return 0; }
  • 50. Structure as function argument #include<iostream> using namespace std; struct Person { int citizenship; int age; }; void func(struct Person p); int main() { struct Person p; p.citizenship = 1; p.age = 27; func(p); return 0; } void func(struct Person p) { cout << " Person citizenship: " << p.citizenship<<endl; cout << " Person age: " << p.age; }
  • 51. Pointers ā€¢ Pointers store the address of variables or a memory location ā€¢ pointer ā€œptrā€ holds the address of an integer variable or holds the address of memory whose value(s) can be accessed as integer values through ā€œptrā€ ā€¢ Pointers: ā€¢ Pointers save memory space. ā€¢ Execution time with pointers is faster because data are manipulated with the address, that is, direct access to memory location. ā€¢ Memory is accessed efficiently with the pointers. The pointer assigns and releases the memory as well. Hence it can be said the Memory of pointers is dynamically allocated. ā€¢ Pointers are used with data structures. They are useful for representing two-dimensional and multi-dimensional arrays.
  • 52. Pointers ā€¢ pointer is a symbolic representation of a memory address. ā€¢ Here is an example of valid pointer declarations in C++: ā€¢ int *x; // a pointer to integer ā€¢ double *x; // a pointer to double ā€¢ float *x; // a pointer to float ā€¢ char *ch // a pointer to a character
  • 53. Pointers ā€¢ Reference operator (&) and Deference operator (*) ā€¢ The reference operator (&) returns the variableā€™s address. ā€¢ The dereference operator (*) helps us get the value that has been stored in a memory address.
  • 54. Example of Pointers #include <iostream> using namespace std; int main() { int x = 27; int *ip; ip = &x; cout << "Value of x is : "; cout << x << endl; cout << "Value of ip is : "; cout << ip<< endl; cout << "Value of *ip is : "; cout << *ip << endl; return 0; }
  • 55. More examples in Pointers #include<iostream> #include<conio.h> using namespace std; int main() { int i = 10; int *Ptr; Ptr = &i; cout << "nValue Of i :" << i; cout << "nAddress Of i :" << i; cout << "nValue Of Ptr :" << Ptr; cout << "nAddress Of Ptr :" << &Ptr; cout << "nPtr's Pointer Value:" << *Ptr; cout << "nPtr Equal to &i :" << *(&i); }
  • 56. NULL pointer #include <iostream> using namespace std; int main() { int *ip = NULL; cout << "Value of ip is: " << ip; return 0; }
  • 57. Array using Pointers #include <iostream> using namespace std; int main() { int *ip; int arr[] = { 10, 34, 13, 76, 5, 46 }; ip = arr; for (int x = 0; x < 6; x++) { cout << *ip << endl; ip++; } return 0; }
  • 58. Functions in Pointers #include <iostream> using namespace std; void test(int*, int*); int main() { int a = 5, b = 5; cout << "Before changing:" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; test(&a, &b); cout << "nAfter changing" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; return 0; } void test(int* n1, int* n2) { *n1 = 10; *n2 = 11; }
  • 59. Swapping numbers using pointers #include <iostream> #include<conio.h> using namespace std; // Declare Swap Function Using Pointer void swap_numbers(int *value1, int *value2) { int temp; temp = *value1; *value1 = *value2; *value2 = temp; } int main() { // Declare Variables int number1, number2; cout << "Simple Example Program for Swap Numbers Using Pointers In C++ā€œ<<endl; // Read User Input
  • 60. Swapping numbers using pointers //Print Values before Swapping cout << "Enter value of Swap Number # 1: "; cin>>number1; cout << "Enter value of Swap Number # 2: "; cin>>number2; cout << "Before Swapping : Number # 1=" << number1 << ", Number # 2=" << number2 << endl; //Call Swap Function By Passing Reference swap_numbers(&number1, &number2); //Print Values after Swapping cout << "After Swapping : Number # 1=" << number1 << ", Number # 2=" << number2 << endl; getch(); return 0; }
  • 61. Print string using pointers #include <iostream> #include<conio.h> using namespace std; int main() { // Declare Variables char str[20], *pt; cout << "Pointer Example C++ Program : Print Stringā€<<endl; cout << "Enter Any string [below 20 chars] : "; cin>>str; // Assign to Pointer Variable pt = str; while (*pt != '0') { cout << *pt; pt++; } return 0; }
  • 62. Declaring a Pointer to a pointer ā€¢ int *ptr1 = &var; // Declaration of single pointer. ā€¢ int **ptr2 = &ptr1; // Declaration of double pointer or pointer to pointer in C++.
  • 63. Declaring a Pointer to a pointer #include<iostream> using namespace std; int main() { // Declaring an integer variable. int age = 21; // Declaring a single pointer variable, // storing address of age variable. int *ptr1 = &age; // Declaring a double pointer variable storing address of ptr1. int **ptr2 = &ptr1; // Printing value using age variable. cout<<"Value stored in the var variable "<<var<<"n"; // Printing value using single pointer variable. cout<<"Value accessed using single pointer "<<*ptr1<<"n"; // Printing value using double pointer variable. cout<<"Value accessed using double pointer "<<**ptr2<<"n"; return 0; }
  • 64.
  • 65.
  • 66. Examples of pointers ā€¢ WAP to demonstrate different arithmetic operations on pointers. ā€¢ WAP to find largest of the elements of an array using a function and passing argument using pointer. ā€¢ WAP to demonstrate use of a pointer to pointer. ā€¢ WAP to demonstrate string handling using pointer.
  • 67.
  • 68. To do Tasks ā€¢ WAP to demonstrate different arithmetic operations on pointers. ā€¢ WAP to swap values of two variables using pointers. ā€¢ WAP to largest of the elements of an array using a function and passing argument using pointer. ā€¢ WAP to demonstrate use of a pointer to pointer. ā€¢ WAP to demonstrate string handling using pointer.
  • 69. Advantages of using Pointers ā€¢ Pointers are variables which store the address of other variables in C++. ā€¢ More than one variable can be modified and returned by function using pointers. ā€¢ Memory can be dynamically allocated and de-allocated using pointers. ā€¢ Pointers help in simplifying the complexity of the program. ā€¢ The execution speed of a program improves by using pointers.
  • 73. Tokens ā€¢ Keywords ā€¢ Identifiers ā€¢ Constants ā€¢ Strings ā€¢ Operators
  • 75. Identifiers ā€¢ Identifiers ā€“ variables, functions, arrays, classes created by programmer. ā€¢ Only alphabetic characters, digits and underscore ā€¢ Name cannot start with a digit ā€¢ Uppercase and lowercase letters are distinct ā€¢ Declared keyword cannot be used as variable name
  • 76. Constant ā€¢ Refers to fixed values that do not change during execution of a program ā€¢ constant int a = 25;
  • 78. Size and range of data types
  • 80. User defined datatypes : Structure
  • 82. Macros ā€¢ A macro is a piece of code in a program that is replaced by the value of the macro. Macro is defined by #define directive. Whenever a macro name is encountered by the compiler, it replaces the name with the definition of the macro. Macro definitions need not be terminated by a semi-colon(;) // C++ program to illustrate macros #include <iostream> using namespace std; // Macro definition #define LIMIT 5 int main() { // Print the value of macro defined cout << "The value of LIMIT" << " is " << LIMIT; return 0; }
  • 83. Macros example // C++ program to illustrate macros #include <iostream> using namespace std; #define AREA(l, b) (l * b) int main() { // Given lengths l1 and l2 int l1 = 10, l2 = 5, area; // Find the area using macros area = AREA(l1, l2); // Print the area cout << "Area of rectangleā€œ << " is: "<< area; return 0; }
  • 86. Derived data types : Function
  • 87. Derived data types : Function
  • 88. Derived Data Types : Pointers
  • 89. Derived Data Types : Pointers
  • 92. Example of control structure: Selection
  • 93. Example of control structure: If statement
  • 94. Example of control structure: Switch case
  • 95. Example of control structure: While statement
  • 96. Example of control structure: Do While statement
  • 97. ā€¢ A hospital management want to enhance their software in the following manner : ā€¢ The software should be able to display departments like medicine, surgery, gynaecology, obstetrics, paediatrics, eye and ENT (any 4) ā€¢ The user should be able to view the availability of respective doctors in the respective department with their charge (fees) ā€¢ Once user selects department he/she wants to visit , a tentative bill is to be generated ā€¢ Give the option the user to exit
  • 98. Array ā€¢ Declaration : Datatype arrayName[arraySize] Few Things to Remember: ā€¢ The array indices start with 0. Meaning x[0] is the first element stored at index 0. ā€¢ If the size of an array is n, the last element is stored at index (n-1). In this example, x[5] is the last element. ā€¢ Elements of an array have consecutive addresses. For example, suppose the starting address of x[0] is 2120. Then, the address of the next element x[1] will be 2124, the address of x[2] will be 2128, and so on. Here, the size of each element is increased by 4. This is because the size of int is 4 bytes.
  • 99. Array Initialization ā€¢ // declare and initialize and array ā€¢ int x[6] = {19, 10, 8, 17, 9, 15};
  • 100. Another method to initialize array during declaration: ā€¢ / declare and initialize an array int x[] = {19, 10, 8, 17, 9, 15};
  • 101. To store and print array #include<iostream.h> using namespace using std; main() { int i; int a[6] = {6,23,15,2,18,9}; for(i=0;i<6;i++) {cout<<a[i]<<endl;} return 0; }
  • 102. Examples For below statements declare array variable. a. To store integer marks of 10 students. b. To store yearly average rating of 50 employees. c. To store 10 characters.
  • 103. Examples For below arrays find the address of said position. a. int marks[10]; Find the address of element stored at index 5. Base address is 1000. ā€¢ char c[10]; Find the address of element stored at index 7. Base address is 1000. 45 70 80 30 67 85 98 34 25 56 A C D R Y N O P E F
  • 104. Examples Write a program to search an element from given array (linear search)
  • 105. 2 dimensional array Write a program to take values for 2-dimensional array and to give choice to the user for performing below operations: 1. Sum of elements of each row. 2. Sum of diagonal elements. 3. Finding transpose of matrix.
  • 106. Declaring a function return_type function_name( parameter list ) { body of the function } ā€¢ Return Type āˆ’ A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void. ā€¢ Function Name āˆ’ This is the actual name of the function. The function name and the parameter list together constitute the function signature. ā€¢ Parameters āˆ’ A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters. ā€¢ Function Body āˆ’ The function body contains a collection of statements that define what the function does.
  • 107. Example ā€¢ // function returning the max between two numbers int max(int num1, int num2) { // local variable declaration int result; if (num1 > num2) result = num1; else result = num2; return result; }
  • 108. In main // function declaration int max(int num1, int num2); int main () { // local variable declaration: int a = 100; int b = 200; int ret; // calling a function to get max value. ret = max(a, b); cout << "Max value is : " << ret << endl; return 0; }