0% found this document useful (0 votes)
4 views

OOP UE Lecture 2

This document provides an overview of basic C++ programming concepts, including the structure of a C++ program, the main function, and examples of simple programs for arithmetic operations. It also discusses key programming principles such as call by value vs call by reference, variable scoping, and passing arrays to functions. Additionally, it lists references for further reading on C++ programming.

Uploaded by

hmtahirhassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

OOP UE Lecture 2

This document provides an overview of basic C++ programming concepts, including the structure of a C++ program, the main function, and examples of simple programs for arithmetic operations. It also discusses key programming principles such as call by value vs call by reference, variable scoping, and passing arrays to functions. Additionally, it lists references for further reading on C++ programming.

Uploaded by

hmtahirhassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Object-Oriented Programming

Lecture No. 2
(Recall - Basic programming concepts)

Division of science and Technology


University of Education, Lahore
Basic structure of C++ program
• Preprocessor directive
• main() function
• Program body (C++ statements)
main () function
• Execution of C++ program starts from main()
• Each program must contains a main() function
• Syntax of main()

void main ()
{
// body of main
}
First C++ program
#include<iostream>

main()
{
cout<<“Hello World!”;
}
Pre-requisite recall ( Practice problem)

• C++ programs to add two integers with and without functions


//addition of two integers //addition of two integers using function
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main() int sum(int x ,int y);
{ int x, y, z; int main()
cout<<"enter first number: "; { int x, y;
cin>> x; cout<<"enter first number: ";
cout<<"enter second number: "; cin>> x;
cin>>y; cout<<"enter second number: ";
z=x+y; cin>>y;
cout<<"Sum of these two :"<<z; cout<<"Sum of these two :"<<sum(x,y);
return 0; return 0;
} }
int sum(int a, int b)
{
int c = a+b;
return c;
}
/*C++ Program that defines a function which performs void mathop(int a, int b, char op)
arithmetic operation on two numbers on the basis of {
the operator and operand entered by user and passed switch (op)
to function as parameters.*/ {
case '+':
void mathop (int, int, char); cout<<a << op << b <<"+" << a+b;
int main() break;
{ case '-':
int a,b; cout<<a << op << b <<"-" << a-b;
char op; break;
cout<<"Enter First Number:"; case '*':
cin>>a; cout<<a << op << b<<"*" << a+b;
cout<<"Enter Second Number:"; break;
cin>>b; case '/':
cout<<"Enter Any Arithmetic Operator: "; cout<<a << op << b<<"/" << a/b;
cin>>op; break;
mathop(a,b,op); default:
return 0; cout<<"Wrong Arithmetic Operator Entered";
} break;
}
}
Call by reference vs Call by value
• The call by reference method of passing arguments to a function
copies the reference of an argument into the formal parameter. Inside
the function, the reference is used to access the actual argument
used in the call. This means that changes made to the parameter
affect the passed argument.

• The call by value method of passing arguments to a function copies


the actual value of an argument into the formal parameter of the
function. In this case, changes made to the parameter inside the
function have no effect on the argument
/*C++ program to demonstrate call by value vs call by
reference.*/ int main () {

#include <iostream> int x = 100;


using namespace std; int y = 200;
cout << "Before func call, value of x is " << x << endl;
void func(int &x, int y); cout << "Before func call, value of y is " << y << endl;
func(x, y);
void func(int &x, int y) { cout << "After func call, value of x is " << x << endl;
x=x+1; cout << "After func call, value of y is " << y << endl;
y=y+1;
cout<<"value of x is "<<x<<endl; return 0;
cout<<"value of y is "<<y<<endl; }
return;
}
Output:
Before func call, value of x is 100
Before func call, value of y is 200
value of x is 101
value of y is 201
After func call, value of x is 101
After func call, value of y is 200
Actual parameters vs Formal parameters
• Actual Parameters: The values/variables passed while calling a
function are called actual parameters.

• Formal Parameters: These are the variables written/declared in


function definition/prototype, and receive their values when a call to
that function is made.
Variable scoping
• Variable is visible within the scope in which it is declared.
• Variable with same name cannot be declared within same scope;
however, this can be done if scope is different.
• Formal parameters have functional scope.
main()
{
int a=0;
int a=2; //error
}
Variable scoping –example 1
#include<iostream>
using namespace std;
//global variable
int x=4;
main()
{
int x=2; //local variable
cout<<x;
}
Output? How to access global variable?
Variable scoping –example 1
#include<iostream>
using namespace std;
//global variable
int x=4;
main()
{
int x=2; //local variable
cout<<x;
cout<<::x;

}
Output:
24
Variable scoping – example 2
#include<iostream> void func(int a)
using namespace std; {
int b=2;
void func(int); cout<<a;
cout<<b;
main() {
{ cout<<a;
int x=5; int a=6;
cout<<x; int b=3;
func(x); cout<<a;
cout<<b;
}
} }
Output:
52563
Array
#include <iostream>
using namespace std;
int main()
{
int arr[] = {11, 22, 33, 44, 55};
int n=0;
while(n<=4)
{
cout<<arr[n]<<endl;
n++;
}
return 0;
}
Passing array to function
/*C++ program that declares and initializes an array of
integer of size 5, pass it to function which calculates the
double getAverage(int arr[], int size)
average and displays the result in main*/
{
#include <iostream> int i, sum = 0;
using namespace std; double avg;
double getAverage(int arr[], int size);
int main () for (i = 0; i < size; ++i)
{ {
int balance[5] = {1000, 2, 3, 17, 50}; sum += arr[i];
double avg; }
avg = getAverage( balance, 5 ) ; avg = double(sum) / size;
cout << "Average value is: " << avg << endl; return avg;
return 0; }
}
References
• C++ How to Program
By Deitel & Deitel

• The C++ Programming Language


By Bjarne Stroustrup

• Object oriented programming using C++ by Tasleem Mustafa, Imran Saeed, Tariq Mehmood, Ahsan Raza

• https://www.tutorialspoint.com

• http://ecomputernotes.com

• http://www.cplusplus.com

You might also like