OOP UE Lecture 2
OOP UE Lecture 2
Lecture No. 2
(Recall - Basic programming concepts)
void main ()
{
// body of main
}
First C++ program
#include<iostream>
main()
{
cout<<“Hello World!”;
}
Pre-requisite recall ( Practice problem)
}
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
• 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