lec-1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 38

Object Oriented

Lecture
Programing
1

Muhammad Jawad Fareed


Programming Concepts Revision

Programming in C++

C++ Code Compiler Output

Error Messages
• 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
First Program
#include<iostream>
using namespace std;
int main() //start of the function
{
cout<<“Hello Word \n";
return 0; //Return Control to OS
}
Simple Program

• Now this program will pass through two


phases
1. Compile checking for errors
2. Run The output of program

output
#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

Examples:
1- #include<iostream>
int main()
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
cout<<"*";
}
cout<<"\n";
}
return 0;
}
2- #include<iostream>
int main()
{
for(int i=1;i<=10;i++)
{
for(int j=1;j<=i;j++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}
•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

1)Library Functions
2)User defined Functions
 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.
 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)

• Return-Type: No return (void) or type (int , char , …)


• Function-name: Any (identifier rules)
• Parameter: No () / Number of parameters with types
(int num)
{
Declarations and statements
}
Simple Function

Name
No Return No Parameter

void line ()
{
for(int i=0;i<45;i++)
{
cout<<“*”;
}
cout<<endl;
}
Using Function

Three important thing should be in mind when we use


functions in main Program

Function Declaration
Function Definition
It is a body of function which is below or before the main
Function call
Function Declaration

Function Call

Function Definition
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
Problem
Write a function which takes three integers as parameters
and display the smallest integer
#include<iostream>
void small(int,int,int );
int main() {
small(123,135,140);
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<iostream>
Using namespace std;
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);
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<iostream>
Using namespace std;
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);
return 0;
}
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<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; }
Function Calling in C++
 Call by Value
Write a function which take two value as argument and then swap values.
#include<iostream>
void swap(int,int);
int main(){
int x=7,y=9;
swap(x,y);
cout<<"\n x is:"<<x<<" y is:"<<y;
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
 Call by 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]); function(int adata[5])
int main() { {
int sum =0;
int arrayprint[5]; for(int i=0; i<5; i++)
for(int i=0; i<5; i++)
{
{
cout<<" Enter numebrs: "; sum = sum + adata[i];
}
cin>> arrayprint[i]; cout<<sum;
} }
for(int i=0; i<5; i++)
{
cout<<" Your numebrs: " << arrayprint[i];
}
function(arrayprint);
return 0;
}
Inline Functions

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”.
#include <iostream>
using namespace std;
inline int Max(int x, int y) {
return (x > y)? x : y;
}
// Main function for the program
OUTPUT
int main() {
cout << "Max (20,10): " << Max(20,10) << endl; Max (20,10): 20
Max (0,200): 200
cout << "Max (0,200): " << Max(0,200) << endl; Max (100,1010):
1010
cout << "Max (100,1010): " << Max(100,1010) << endl;
return 0; }
#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
OOP

OOP, or 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
Links .

https://pediaa.com/what-is-the-difference-between-staructured-and-object-oriented-
programming/#:~:text=The%20main%20difference%20between%20structured,of
%20objects%20and%20their%20interactions.

You might also like