0% found this document useful (0 votes)
96 views6 pages

Oop Lab 1

This document contains a lab assignment submitted by Zikra Zahid to Sir Rehan Ahmad Siddiqui for the course Object Oriented Programming. The lab contains 4 tasks - the first asks students to write C++ function prototypes and declarations, the second asks students to write the output of code snippets containing functions, the third asks students to compile a program with a function to check if a number is prime, and the fourth asks students to write a program with a function to calculate the volume of a cube that accepts arguments with default values.

Uploaded by

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

Oop Lab 1

This document contains a lab assignment submitted by Zikra Zahid to Sir Rehan Ahmad Siddiqui for the course Object Oriented Programming. The lab contains 4 tasks - the first asks students to write C++ function prototypes and declarations, the second asks students to write the output of code snippets containing functions, the third asks students to compile a program with a function to check if a number is prime, and the fourth asks students to write a program with a function to calculate the volume of a cube that accepts arguments with default values.

Uploaded by

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

Fatima Jinnah Women University

Department Of Software Engineering

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);

3. Which of these are valid function declarations:


a. void function();
b. void function(void);
c. void function(int);
d. function(int); e. int function();
Ans. All of these are valid function declarations except d.

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);

5. What are inline functions?


Ans. Inline function is a function that is expanded in line when it is called. When the
inline function is called whole code of the inline function gets inserted or substituted at
the point of inline function call. This substitution is performed by the C++ compiler at
compile time. Inline function may increase efficiency if it is small. C++ provides an
inline functions to reduce the function call overhead.

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:

You might also like