KEMBAR78
Intorduction to programming fundamentals | PDF
Object Oriented Programming
Lecturer: Mr. Muhammad Amjad Raza
Email: ch.amjadraza@gmail.com
Amjad.raza@riphahsahiwal.edu.pk
Call/WhatsApp: 03456762672
ْ‫ح‬َّ‫الر‬ ِ‫هللا‬ ِ‫م‬ْ‫س‬ِ‫ب‬
ْ‫ي‬ ِ‫ح‬َّ‫الر‬ ِ‫ن‬ ٰ‫م‬
ِ‫م‬
Lecture# 1-2
Programming Concepts Revision
Programming in C++
C++ Code Output
Compiler
Error Messages
First Program
•Programs in C++ are written in blocks
•Every block has name
•The name of monitor block is main
•Every statement is ended with semi-colon(;)
•Our first program will display a message on
output screen
Simple Program
• #include<iostream>
• #include<conio>
• int main()
• {
• cout<<"Every age has a language of its ownn";
• return 0;
• }
Simple Program
• Now this program will pass through two phases
1. Compile →checking for errors
2. Run →The output of program
Simple Program
• #include<iostream>
• #include<conio>
• int main(){
• cout<<"Every age has a language of its ownn";
• getch();
• return 0;
• }
Comments
• #include<iostream>
• #include<conio>//Preprocessor Directive
• int main(){ //start of the function
• cout<<"Every age has a language of its ownn";
• getch();
• return 0; //Return Control to OS
• }
• #include<iostream>
• Using namespace std;
• int main()
• {
• int base, power, result=1;
• cout<<"Plz enter base --> ";
• cin>>base;
• cout<<"Plz enter power --> ";
• cin>>power;
• for(int i=0;i<power;i++){
• result=result*base;
• cout<<"nThe result is: "<<result;}
• return 0;
• }
Nested Loops
•We can use loops inside loop body
•Lot of care is needed in this type of
structure
•First inner loop complete its iteration
and then control shift to outer one,
this process continues till end
• #include<conio>
• #include<iostream>
• int main(){
• for(int i=0;i<5;i++){
• for(int j=0;j<5;j++){
• cout<<"*";
• }
• cout<<"n";
• }
• getch();
• return 0;
• }
#include<conio>
#include<iostream>
int main(){
for(int i=1;i<=10;i++){
for(int j=1;j<=i;j++)
cout<<"*";
cout<<endl;
}
getch();
return 0;}
#include<conio>
#include<iostream>
int main(){
for(int i=10;i>=0;i--){
for(int j=1;j<=i;j++)
cout<<"*";
cout<<endl;
}
getch();
return 0;
}
Functions
Intro to Functions
• A function is a block of code which only runs when it is
called.
• You can pass data, known as parameters, into a function.
• Functions are used to perform certain actions, and they are
important for reusing code: Define the code once and use it
many times.
Why functions are important?
• The program may need to repeat the same piece of code at
various places, or it may require repeating same task again
and again.
• The program may become very large and messy if we do not
divide it into sub programs (functions).
• The piece of code that is executed repeatedly is stored in a
separate function and is called when and where it is
required.
Types of function
•There are two types of functions
•Library Functions
•User defined Functions
Types of function
1.Library Functions:
– Library functions are the built-in functions in C++.
– Programmers can use library function by invoking function directly.
– They don’t need to write it by themselves.
2.User defined Functions
– C++ allows programmers to define their own functions.
– A user defined function group code to perform a specific task and that group
of code given a name (identifier).
– When the function is invoked from any part of the program, it all executes
the code defined in the body of function.
Library functions
• #include<iostream>
• #include<math.h>
• using namespace std;
• int main()
• {
• int n ;
• cout<<" Enter your number you want square";
• cin>> n;
• cout<<"Sqrt: " <<sqrt(n);
• cout<<"Pow: " <<pow(2, n);
• }
User-defined Functions
return-value-type function-name(parameter-list)
{
declarations and statements
}
• Return-Type: No(void) return or type
• Function-name: Any(identifier rules)
• Parameter: No(void) or no. of parameters with
types
Simple Function
void line(){
for(int i=0;i<45;i++)
cout<<“*”;
cout<<endl;
}
No Return Name No Parameter
Using Function
• Three important thing should be in min when
we use functions in main function
1. Function Definition:
It is just body of function which is below the
main
2. Function declaration
3. Function call
Using Function
Function Definition
Function Call
Function Declaration
Example code Return and with
return value
• #include<iostream>
• #include<math.h>
• using namespace std;
• // Function Prototyping
• // Function Declaration
• void sum(int a, int b);
• int sum1(int c, int d);
• int main()
• {
• sum(10,20); // Function
Calling
• cout<<"n";
• cout<<sum1(100,200); //
Function Calling
• }
// Function Definations
void sum(int a, int b)
{
int e;
e = a+b;
cout<<" sum is : " << e ;
}
int sum1(int c, int d)
{
return c+d;
}
Example
• #include<iostream>
• void line();
• void hash();
• void dash();
• int main(){
• dash();
• for(int i=0;i<5;i++)
• line();
• dash();
• for(int i=0;i<5;i++)
• hash();
• getch();
• return 0;
• }
• void line(){
• for(int i=0;i<45;i++)
• cout<<"*";
• cout<<endl;
• }
• void hash(){
• for(int i=0;i<45;i++)
• cout<<"#";
• cout<<endl;
• }
• void dash(){
• for(int i=0;i<45;i++)
• cout<<"-";
• cout<<endl;
• }
Passing Values To functions
• We can pass any type of value to function
• We have to define the data type of the values
• During function call we send on those values
which a function can accept
Passing Values To functions
Case Study
• This case study aims to develop a C++ program that calculates the
factorial of a given number using a separate function. The program will
take user input, pass it to a function that computes the factorial, and
display the result.
• Problem Definition
• Factorial is a mathematical operation represented as n!, which means
multiplying all positive integers from 1 to n. It is defined as:
• n!=n×(n−1)×(n−2)×...×
• For example:
• 5!=5×4×3×2×1=120
• The goal of this study is to implement a modular approach where a
dedicated function will compute the factorial, ensuring better code
organization, reusability, and readability.
Solution
#include<iostream>
#include<math.h>
using namespace std;
// Function Prototyping // Function Declaration
void fac(int a);
int main()
{
int f;
cout<<" Enter the number you want factorial: ";
cin>>f;
fac(f); // function calling
}
// Function Defination
void fac(int a)
{
int sum = 1;
for(int i=1; i<=a; i++)
{
sum = sum * i;
}
cout<<" Factorial is: " << sum <<endl;
}
Case Study
• Given three integers, the task is to identify the smallest among
them. Mathematically, for three numbers a, b, and c, the
smallest number is:
min=min(a,b,c)
• For example:
• If the inputs are 7, 3, 9, the smallest number is 3.
• If the inputs are 5, 2, 2, the smallest number is 2.
• To ensure a modular and structured approach, a
separate function will handle the comparison logic, making
the code more readable and reusable.
• #include<conio>
• #include<iostream>
• void small(int,int,int );
• int main(){
• small(123,135,140);
• getch();
• return 0;
• }
• void small(int a,int b,int c){
• int small=a;
• if(b<small)
• small=b;
• if(c<small)
• small=c;
• cout<<"Samll is:"<<small<<endl;
• }
Functions With Return Value
• Function can return a value to the calling
function
• Use keyword return for this purpose
• We have to define the data type of value
returned by the function
• Function can return only one value
• #include<conio>
• #include<iostream>
• int add(int,int,int,int);
• int main(){
• add(2,2,2,2);
• int c;
• c=add(2,2,2,2);
• cout<<"Add is:"<<c<<endl;
• cout<<"Add is:"<<add(3,3,3,3);
• getch();
• return 0;
• }
• int add(int a,int b,int c,int d){
• int sum=a+b+c+d;
• return sum;
• }
Scope of Variable
• Scope mean visibility
• A variable declared inside a block has visibility
within that block only
• Variables defined within the function has a scope
that is function wide
Visibility of Identifiers
• Global Scope Anything identified or declared
outside of any function is visible to all functions
in that file
• Function level scope Declaring variables
inside a function can be used in the whole
function
• Block level scope Variables or integers
declared inside block are used inside block
Global Variable
Local Variable
Default Arguments
• Function call commonly pass a particular value
of an argument
• Programmer can provide default value for that
argument
• When that argument is omitted in program the
compiler use the default value
• #include<conio>
• #include<iostream>
• int add(int a,int b,int c=0,int d=0,int e=0){
• return a+b+c+d+e;
• }
• int main(){
• cout<<"nCall with two arguments:"<<add(4,5);
• cout<<"nCall with three arguments:"<<add(4,5,6);
• cout<<"nCall with four arguments:"<<add(2,3,4,5);
• cout<<"nCall with five arguments:"<<add(1,3,4,3,3);
• getch();
• return 0;
• }
Case Study
The objective of this case study is to develop a C++ program
that calculates the sum of three variables using a function
with default arguments. The function should accept three
parameters and store the sum in another variable.
Additionally, the function should be called with one, two,
and three arguments to demonstrate the flexibility of
default parameters.
Problem Definition
The sum of three numbers is defined as:
sum=a+b+c
However, the function should be able to handle cases where fewer than
three arguments are provided by using default values.
For example:
• If called with one argument → sum(5), it should compute 5 + 0 + 0 = 5.
• If called with two arguments → sum(5, 7), it should compute 5 + 7 + 0 =
12.
• If called with three arguments → sum(5, 7, 2), it should compute 5 + 7 + 2
= 14.
This ensures that the function is flexible and can be used with varying
numbers of arguments.
Function Overloading
• Defining function with one name and different
arguments is called function overloading
• Function is identified with its name ,no of
arguments and their type
• Compiler calls appropriate function according
to the function call
• #include<conio>
• #include<iostream>
• int add(int,int);
• float add(float,float);
• int add(int,int,int);
• int main(){
• float x=3.5,y=5.6;
• cout<<"nCall with integer:"<<add(4,5);
• cout<<"nCall with floats:"<<add(x,y);
• cout<<"nCall with 3 arguments:"<<add(2,3,4);
• getch();
• return 0;
• }
• int add(int a,int b){ return a+b; }
• float add(float a,float b){ return a+b; }
• int add(int a,int b,int c){ return a+b+c; }
Call by Value in C++
Call by Value
Problem: Write a function which take two value
as argument and then swap the values.
• #include<conio>
• #include<iostream>
• void swap(int,int);
• int main(){
• int x=7,y=9;
• swap(x,y);
• cout<<"n x is:"<<x<<" y is:"<<y;
• getch();
• return 0;
• }
• void swap(int a,int b){
• int temp=a;
• a=b;
• b=temp;
• }
Call by reference
• We also can pass the value by reference
• In this case we pass the address of the variable
rather than value
• We use & operator for this purpose
• To call by reference we cannot pass value, we
have to pass memory address of variable
Swap Function with reference
#include<iostream>
using namespace std;
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
int main()
{
int a,b;
cout<<"Enter the first and second digit: " <<endl;
cin>>a>>b;
cout<<" Before swapping " << a << " " << b <<endl;
swap(&a,&b);
cout<<" After swapping " << a << " " << b <<endl;
}
Pass an array into function
#include<iostream>
using namespace std;
function(int adata[5])
{
int sum =0;
for(int i=0; i<5; i++)
{
sum = sum +
adata[i];
}
cout<<sum;
}
int main()
{
int arrayprint[5];
for(int i=0; i<5; i++)
{
cout<<" Enter numebrs: ";
cin>> arrayprint[i];
cout<<"n";
}
for(int i=0; i<5; i++)
{
cout<<" Your numebrs: ";
cout<< arrayprint[i];
cout<<"n";
}
function(arrayprint);
}
Pass an multi dimension
array into function
• #include<iostream>
• using namespace std;
•
function(int adata[2][2], int adata2[2][2])
• {
• int adata3[2][2];
•
int sum =0;
• for(int i=0; i<2; i++)
• {
• for(int j=0; j<2; j++)
• {
• adata3[i][j] = adata[i][j] + adata2[i][j];
•
cout<< " " << adata3[i][j] << " ";
• }
• cout<<"n";
• }
• cout<<sum;
•
• }
•
• int main()
• {
• int arrayprint[2][2], arrayprint2[2][2];
•
for(int i=0; i<2; i++)
• {
• for(int j=0; j<2; j++)
• {
• cout<<" Enter numebrs: ";
• cin>> arrayprint[i][j];
• }
• cout<<"n";
• }
•
for(int i=0; i<2; i++)
• {
• for(int j=0; j<2; j++)
• {
• cout<<" Enter numebrs: ";
• cin>> arrayprint2[i][j];
• }
• cout<<"n";
• }
•
function(arrayprint, arrayprint2);
• }
Function Template
• A template is a simple yet very powerful tool
in C++.
• The simple idea is to pass data type as a
parameter so that we don’t need to write the
same code for different data types
Example
• #include<iostream>
• using namespace std;
•
// Function Template. template <typename T>
• T sum(T x,T y)
• {
• return x+y;
• }
int main()
• {
• int res = sum <int> (10,20);
• float res2 = sum <float> (10.10,20.20);
• cout<< res;
• cout<<endl;
• cout<< res2;
• }
Header File
• #include <iostream.h>
• Header file consist of function prototypes and some
other directives
• Function prototypes like:
• int add(int,int);
• int multiply(int,int);
main.cpp
• #include<iostream>
• #include “b.h"
• using namespace std;
•
// Function Template.
•
int main()
• {
• cout<< sum(10,20);
•
• }
b.h
• int sum(int a, int b)
• {
• return a+b;
• }
Inline Functions
Introduction
• C++ inline function is a powerful concept that is commonly used
with classes. 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.
How to make function inline:
• To make any function as inline, start its definitions with the
keyword “inline”.
inline int add(int a, int b)
{
return (a + b);
};
Inline Functions
• #include <iostream>
•
• using namespace std;
•
• inline int Max(int x, int y) {
• if(x>y)
• return x;
• else if(y>x)
• return 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;
• }
•
Inline Functions
When the above code is compiled and executed, it produces
the following result −
Max (20,10): 20
Max (0,200): 200
Max (100,1010): 1010
#define
• double const pi=3.1415926; can be
written as:
• #define pi 3.1415926
• #define COUNT 10
• Name can be used inside a program
exactly like a variable
• It cannot be used as a variable
What is OOP (Object Oriented
Programming)?
• Object-oriented programming (OOP) is defined as a programming
paradigm (and not a specific language) built on the concept of
objects, i.e., a set of data contained in fields, and code, indicating
procedures – instead of the usual logic-based system.
OOP
• Object-oriented programming is an approach or
a programming pattern where the programs are
structured around objects rather than functions
• It makes the data partitioned into two memory
areas, i.e., data and functions, and helps make the
code flexible and modular.
Why we need to study OOP
• Re-usability
• Code Maintenance
• Security
• Problem-solving
Difference between classes and
structures
• Technically speaking, structs and classes are almost equivalent
• The major difference like class provides the flexibility of combining data and
methods (functions ) and it provides the re-usability called inheritance.
• A class has all members private by default. A struct is a class where members
are public by default

Intorduction to programming fundamentals

  • 1.
    Object Oriented Programming Lecturer:Mr. Muhammad Amjad Raza Email: ch.amjadraza@gmail.com Amjad.raza@riphahsahiwal.edu.pk Call/WhatsApp: 03456762672 ْ‫ح‬َّ‫الر‬ ِ‫هللا‬ ِ‫م‬ْ‫س‬ِ‫ب‬ ْ‫ي‬ ِ‫ح‬َّ‫الر‬ ِ‫ن‬ ٰ‫م‬ ِ‫م‬
  • 2.
  • 3.
    Programming in C++ C++Code Output Compiler Error Messages
  • 4.
    First Program •Programs inC++ are written in blocks •Every block has name •The name of monitor block is main •Every statement is ended with semi-colon(;) •Our first program will display a message on output screen
  • 5.
    Simple Program • #include<iostream> •#include<conio> • int main() • { • cout<<"Every age has a language of its ownn"; • return 0; • }
  • 6.
    Simple Program • Nowthis program will pass through two phases 1. Compile →checking for errors 2. Run →The output of program
  • 7.
    Simple Program • #include<iostream> •#include<conio> • int main(){ • cout<<"Every age has a language of its ownn"; • getch(); • return 0; • }
  • 10.
    Comments • #include<iostream> • #include<conio>//PreprocessorDirective • int main(){ //start of the function • cout<<"Every age has a language of its ownn"; • getch(); • return 0; //Return Control to OS • }
  • 11.
    • #include<iostream> • Usingnamespace std; • int main() • { • int base, power, result=1; • cout<<"Plz enter base --> "; • cin>>base; • cout<<"Plz enter power --> "; • cin>>power; • for(int i=0;i<power;i++){ • result=result*base; • cout<<"nThe result is: "<<result;} • return 0; • }
  • 13.
    Nested Loops •We canuse loops inside loop body •Lot of care is needed in this type of structure •First inner loop complete its iteration and then control shift to outer one, this process continues till end
  • 14.
    • #include<conio> • #include<iostream> •int main(){ • for(int i=0;i<5;i++){ • for(int j=0;j<5;j++){ • cout<<"*"; • } • cout<<"n"; • } • getch(); • return 0; • }
  • 15.
    #include<conio> #include<iostream> int main(){ for(int i=1;i<=10;i++){ for(intj=1;j<=i;j++) cout<<"*"; cout<<endl; } getch(); return 0;}
  • 16.
    #include<conio> #include<iostream> int main(){ for(int i=10;i>=0;i--){ for(intj=1;j<=i;j++) cout<<"*"; cout<<endl; } getch(); return 0; }
  • 17.
  • 18.
    Intro to Functions •A function is a block of code which only runs when it is called. • You can pass data, known as parameters, into a function. • Functions are used to perform certain actions, and they are important for reusing code: Define the code once and use it many times.
  • 19.
    Why functions areimportant? • The program may need to repeat the same piece of code at various places, or it may require repeating same task again and again. • The program may become very large and messy if we do not divide it into sub programs (functions). • The piece of code that is executed repeatedly is stored in a separate function and is called when and where it is required.
  • 20.
    Types of function •Thereare two types of functions •Library Functions •User defined Functions
  • 21.
    Types of function 1.LibraryFunctions: – Library functions are the built-in functions in C++. – Programmers can use library function by invoking function directly. – They don’t need to write it by themselves. 2.User defined Functions – C++ allows programmers to define their own functions. – A user defined function group code to perform a specific task and that group of code given a name (identifier). – When the function is invoked from any part of the program, it all executes the code defined in the body of function.
  • 22.
    Library functions • #include<iostream> •#include<math.h> • using namespace std; • int main() • { • int n ; • cout<<" Enter your number you want square"; • cin>> n; • cout<<"Sqrt: " <<sqrt(n); • cout<<"Pow: " <<pow(2, n); • }
  • 23.
    User-defined Functions return-value-type function-name(parameter-list) { declarationsand statements } • Return-Type: No(void) return or type • Function-name: Any(identifier rules) • Parameter: No(void) or no. of parameters with types
  • 24.
    Simple Function void line(){ for(inti=0;i<45;i++) cout<<“*”; cout<<endl; } No Return Name No Parameter
  • 25.
    Using Function • Threeimportant thing should be in min when we use functions in main function 1. Function Definition: It is just body of function which is below the main 2. Function declaration 3. Function call
  • 26.
  • 27.
    Example code Returnand with return value • #include<iostream> • #include<math.h> • using namespace std; • // Function Prototyping • // Function Declaration • void sum(int a, int b); • int sum1(int c, int d); • int main() • { • sum(10,20); // Function Calling • cout<<"n"; • cout<<sum1(100,200); // Function Calling • } // Function Definations void sum(int a, int b) { int e; e = a+b; cout<<" sum is : " << e ; } int sum1(int c, int d) { return c+d; }
  • 29.
    Example • #include<iostream> • voidline(); • void hash(); • void dash(); • int main(){ • dash(); • for(int i=0;i<5;i++) • line(); • dash(); • for(int i=0;i<5;i++) • hash(); • getch(); • return 0; • }
  • 30.
    • void line(){ •for(int i=0;i<45;i++) • cout<<"*"; • cout<<endl; • } • void hash(){ • for(int i=0;i<45;i++) • cout<<"#"; • cout<<endl; • } • void dash(){ • for(int i=0;i<45;i++) • cout<<"-"; • cout<<endl; • }
  • 32.
    Passing Values Tofunctions • We can pass any type of value to function • We have to define the data type of the values • During function call we send on those values which a function can accept
  • 33.
  • 34.
    Case Study • Thiscase study aims to develop a C++ program that calculates the factorial of a given number using a separate function. The program will take user input, pass it to a function that computes the factorial, and display the result. • Problem Definition • Factorial is a mathematical operation represented as n!, which means multiplying all positive integers from 1 to n. It is defined as: • n!=n×(n−1)×(n−2)×...× • For example: • 5!=5×4×3×2×1=120 • The goal of this study is to implement a modular approach where a dedicated function will compute the factorial, ensuring better code organization, reusability, and readability.
  • 35.
    Solution #include<iostream> #include<math.h> using namespace std; //Function Prototyping // Function Declaration void fac(int a); int main() { int f; cout<<" Enter the number you want factorial: "; cin>>f; fac(f); // function calling } // Function Defination void fac(int a) { int sum = 1; for(int i=1; i<=a; i++) { sum = sum * i; } cout<<" Factorial is: " << sum <<endl; }
  • 36.
    Case Study • Giventhree integers, the task is to identify the smallest among them. Mathematically, for three numbers a, b, and c, the smallest number is: min=min(a,b,c) • For example: • If the inputs are 7, 3, 9, the smallest number is 3. • If the inputs are 5, 2, 2, the smallest number is 2. • To ensure a modular and structured approach, a separate function will handle the comparison logic, making the code more readable and reusable.
  • 37.
    • #include<conio> • #include<iostream> •void small(int,int,int ); • int main(){ • small(123,135,140); • getch(); • return 0; • } • void small(int a,int b,int c){ • int small=a; • if(b<small) • small=b; • if(c<small) • small=c; • cout<<"Samll is:"<<small<<endl; • }
  • 38.
    Functions With ReturnValue • Function can return a value to the calling function • Use keyword return for this purpose • We have to define the data type of value returned by the function • Function can return only one value
  • 39.
    • #include<conio> • #include<iostream> •int add(int,int,int,int); • int main(){ • add(2,2,2,2); • int c; • c=add(2,2,2,2); • cout<<"Add is:"<<c<<endl; • cout<<"Add is:"<<add(3,3,3,3); • getch(); • return 0; • } • int add(int a,int b,int c,int d){ • int sum=a+b+c+d; • return sum; • }
  • 40.
    Scope of Variable •Scope mean visibility • A variable declared inside a block has visibility within that block only • Variables defined within the function has a scope that is function wide
  • 41.
    Visibility of Identifiers •Global Scope Anything identified or declared outside of any function is visible to all functions in that file • Function level scope Declaring variables inside a function can be used in the whole function • Block level scope Variables or integers declared inside block are used inside block
  • 42.
  • 43.
    Default Arguments • Functioncall commonly pass a particular value of an argument • Programmer can provide default value for that argument • When that argument is omitted in program the compiler use the default value
  • 44.
    • #include<conio> • #include<iostream> •int add(int a,int b,int c=0,int d=0,int e=0){ • return a+b+c+d+e; • } • int main(){ • cout<<"nCall with two arguments:"<<add(4,5); • cout<<"nCall with three arguments:"<<add(4,5,6); • cout<<"nCall with four arguments:"<<add(2,3,4,5); • cout<<"nCall with five arguments:"<<add(1,3,4,3,3); • getch(); • return 0; • }
  • 45.
    Case Study The objectiveof this case study is to develop a C++ program that calculates the sum of three variables using a function with default arguments. The function should accept three parameters and store the sum in another variable. Additionally, the function should be called with one, two, and three arguments to demonstrate the flexibility of default parameters.
  • 46.
    Problem Definition The sumof three numbers is defined as: sum=a+b+c However, the function should be able to handle cases where fewer than three arguments are provided by using default values. For example: • If called with one argument → sum(5), it should compute 5 + 0 + 0 = 5. • If called with two arguments → sum(5, 7), it should compute 5 + 7 + 0 = 12. • If called with three arguments → sum(5, 7, 2), it should compute 5 + 7 + 2 = 14. This ensures that the function is flexible and can be used with varying numbers of arguments.
  • 47.
    Function Overloading • Definingfunction with one name and different arguments is called function overloading • Function is identified with its name ,no of arguments and their type • Compiler calls appropriate function according to the function call
  • 48.
    • #include<conio> • #include<iostream> •int add(int,int); • float add(float,float); • int add(int,int,int); • int main(){ • float x=3.5,y=5.6; • cout<<"nCall with integer:"<<add(4,5); • cout<<"nCall with floats:"<<add(x,y); • cout<<"nCall with 3 arguments:"<<add(2,3,4); • getch(); • return 0; • } • int add(int a,int b){ return a+b; } • float add(float a,float b){ return a+b; } • int add(int a,int b,int c){ return a+b+c; }
  • 49.
  • 50.
    Call by Value Problem:Write a function which take two value as argument and then swap the values.
  • 51.
    • #include<conio> • #include<iostream> •void swap(int,int); • int main(){ • int x=7,y=9; • swap(x,y); • cout<<"n x is:"<<x<<" y is:"<<y; • getch(); • return 0; • } • void swap(int a,int b){ • int temp=a; • a=b; • b=temp; • }
  • 52.
    Call by reference •We also can pass the value by reference • In this case we pass the address of the variable rather than value • We use & operator for this purpose • To call by reference we cannot pass value, we have to pass memory address of variable
  • 53.
    Swap Function withreference #include<iostream> using namespace std; void swap(int *x, int *y) { int temp; temp = *x; *x = *y; *y = temp; } int main() { int a,b; cout<<"Enter the first and second digit: " <<endl; cin>>a>>b; cout<<" Before swapping " << a << " " << b <<endl; swap(&a,&b); cout<<" After swapping " << a << " " << b <<endl; }
  • 54.
    Pass an arrayinto function #include<iostream> using namespace std; function(int adata[5]) { int sum =0; for(int i=0; i<5; i++) { sum = sum + adata[i]; } cout<<sum; } int main() { int arrayprint[5]; for(int i=0; i<5; i++) { cout<<" Enter numebrs: "; cin>> arrayprint[i]; cout<<"n"; } for(int i=0; i<5; i++) { cout<<" Your numebrs: "; cout<< arrayprint[i]; cout<<"n"; } function(arrayprint); }
  • 55.
    Pass an multidimension array into function • #include<iostream> • using namespace std; • function(int adata[2][2], int adata2[2][2]) • { • int adata3[2][2]; • int sum =0; • for(int i=0; i<2; i++) • { • for(int j=0; j<2; j++) • { • adata3[i][j] = adata[i][j] + adata2[i][j]; • cout<< " " << adata3[i][j] << " "; • } • cout<<"n"; • } • cout<<sum; • • } • • int main() • { • int arrayprint[2][2], arrayprint2[2][2]; • for(int i=0; i<2; i++) • { • for(int j=0; j<2; j++) • { • cout<<" Enter numebrs: "; • cin>> arrayprint[i][j]; • } • cout<<"n"; • } • for(int i=0; i<2; i++) • { • for(int j=0; j<2; j++) • { • cout<<" Enter numebrs: "; • cin>> arrayprint2[i][j]; • } • cout<<"n"; • } • function(arrayprint, arrayprint2); • }
  • 56.
    Function Template • Atemplate is a simple yet very powerful tool in C++. • The simple idea is to pass data type as a parameter so that we don’t need to write the same code for different data types
  • 57.
    Example • #include<iostream> • usingnamespace std; • // Function Template. template <typename T> • T sum(T x,T y) • { • return x+y; • } int main() • { • int res = sum <int> (10,20); • float res2 = sum <float> (10.10,20.20); • cout<< res; • cout<<endl; • cout<< res2; • }
  • 58.
    Header File • #include<iostream.h> • Header file consist of function prototypes and some other directives • Function prototypes like: • int add(int,int); • int multiply(int,int);
  • 59.
    main.cpp • #include<iostream> • #include“b.h" • using namespace std; • // Function Template. • int main() • { • cout<< sum(10,20); • • }
  • 60.
    b.h • int sum(inta, int b) • { • return a+b; • }
  • 61.
  • 62.
    Introduction • C++ inlinefunction is a powerful concept that is commonly used with classes. 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.
  • 63.
    How to makefunction inline: • To make any function as inline, start its definitions with the keyword “inline”. inline int add(int a, int b) { return (a + b); };
  • 64.
    Inline Functions • #include<iostream> • • using namespace std; • • inline int Max(int x, int y) { • if(x>y) • return x; • else if(y>x) • return 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; • } •
  • 65.
    Inline Functions When theabove code is compiled and executed, it produces the following result − Max (20,10): 20 Max (0,200): 200 Max (100,1010): 1010
  • 66.
    #define • double constpi=3.1415926; can be written as: • #define pi 3.1415926 • #define COUNT 10 • Name can be used inside a program exactly like a variable • It cannot be used as a variable
  • 67.
    What is OOP(Object Oriented Programming)? • Object-oriented programming (OOP) is defined as a programming paradigm (and not a specific language) built on the concept of objects, i.e., a set of data contained in fields, and code, indicating procedures – instead of the usual logic-based system.
  • 69.
    OOP • Object-oriented programmingis an approach or a programming pattern where the programs are structured around objects rather than functions • It makes the data partitioned into two memory areas, i.e., data and functions, and helps make the code flexible and modular.
  • 70.
    Why we needto study OOP • Re-usability • Code Maintenance • Security • Problem-solving
  • 71.
    Difference between classesand structures • Technically speaking, structs and classes are almost equivalent • The major difference like class provides the flexibility of combining data and methods (functions ) and it provides the re-usability called inheritance. • A class has all members private by default. A struct is a class where members are public by default