Oop Lab 1
Oop Lab 1
LAB#01
Course Title
OBJECT ORIENTED PROGRAMMING
Submitted To
SIR REHAN AHEMD SIDDIQUI
Submitted By
Zikra Zahid
Section: A (Semester#2)
Registration No: 2021-BSE-035
Date of Submission
April 5, 2022
TASK #1:
1. Write the prototype of a function named fnct() which accepts an int by value, a float
by reference and returns a char.
Ans. Char fnct(int a, float &b);
2. Using three variables a,b & c, call the function: double power(double, double);
Ans. c=power(a,b);
4. A function needs to compute the average as well as the sum of three integers passed
to it and return the answers in the main(). Suggest the prototype for this function.
Ans. void avg_sum (int &a, int &b, int &c,int &sum, float &avg);
Task #2:
Write the output of the following code fragments.
1)
int cube(int);
int main()
{
for(int i=0;i<10;i+=3)
cout << cube(i) << endl;
return 0;
}
int cube(int a)
{
return a*a*a;
}
Output:
2)
int larger(int,int);
int main()
{
int x=10,y=5;
int m = larger(x,y);
cout<<m<<endl;
return 0;
}
int larger(int a,int b)
{
if (a>b)
return a;
else
return b;
Output:
3)
void decrement(int);
int main()
{
int x=10;
cout<< x <<endl;
decrement(x);
cout<< x <<endl;
return 0;
}
void decrement(int x)
{
x--;
cout<< x <<endl;
x--;
Output:
TASK #3:
Compile a program with a function isPrime() which takes an integer as an
argument and returns true if the argument is a prime number.
#include<iostream>
using namespace std;
bool isprime(int num);
int _tmain(int argc, _TCHAR* argv[])
{
int num;
cout<<"Enter num=";
cin>>num;
if(isprime(num)==true)
cout<<num<<" is a prime number"<<endl;
else
cout<<num<<" is not a prime number"<<endl;
system("pause");
return 0;
}
bool isprime(int num)
{
bool isprime=true;
for(int i=2;i<num;i++)
{
if(num%i==0)
{
isprime=false;
break;
}
}
return isprime;
}
OUTPUT:
TASK #4:
Write a program with a function volume() which accepts three sides of a cube and returns
its volume. Provide a default value of 1 for all the three sides of a cube. Call this function
with zero, one, two and three arguments and display the volume returned in the main().
#include "stdafx.h"
#include<iostream>
using namespace std;
int volume(int a=1,int b=1,int c=1)
{
return a*b*c;
}
int _tmain(int argc, _TCHAR* argv[])
{
int a=4,b=8,c=5;
cout<<"With zero argument"<<endl;
cout<<"Volume="<<volume()<<endl;
cout<<"With one argument"<<endl;
cout<<"Volume="<<volume(a)<<endl;
cout<<"With two argument"<<endl;
cout<<"Volume="<<volume(a,b)<<endl;
cout<<"With three argument"<<endl;
cout<<"Volume="<<volume(a,b,c)<<endl;
system("pause");
return 0;
}
Output: