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

oop lab 1

The document outlines a lab assignment for Object Oriented Programming, including tasks related to function prototypes, valid function declarations, inline functions, and coding exercises. It includes specific questions and code snippets that require students to demonstrate their understanding of function usage and implementation in C++. Additionally, it features tasks that involve writing functions to compute values such as averages and prime number checks.

Uploaded by

hashirhaider78
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

oop lab 1

The document outlines a lab assignment for Object Oriented Programming, including tasks related to function prototypes, valid function declarations, inline functions, and coding exercises. It includes specific questions and code snippets that require students to demonstrate their understanding of function usage and implementation in C++. Additionally, it features tasks that involve writing functions to compute values such as averages and prime number checks.

Uploaded by

hashirhaider78
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

NAME:EMAN HUSSAIN

REG NO.FA24B1-SE-081
COURSE TITLE: OBJECT ORIENTED
PROGRAMMING
SUBMITTED TO: SIR REHAN
BSSE 1-A
LAB 01
Lab 01

Task 1 :

Give answers to the following.

Q1. Write the prototype of a function named fnct() which accepts an int by value, a float by

reference and returns a char.

Q2.Using three variables a,b & c, call the function: double power(double, double);

Q3.Which of these are valid function declarations:


a. Void function();
b. Void function(void);
c. Void function(int);
d. Function(int);
e. Int function();

Q4.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: We can solve this problems in 3 ways i.e using structures, using pointers or passing value by
reference.
Q5.What are inline functions?
Ans:Inline functions are functions whose body is substituted directly into the code where they’re called,
potentially improving performance by reducing function call overhead.

Task 2 :

Write the output of the following code fragments.

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:

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:

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 "stdafx.h"
#include<iostream>
using namespace std;

bool isPrime(int num) {


if (num < 2) return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return false;
}
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{int number;
cout << "Enter a number: ";
cin >> number;

if (isPrime(number))
cout << number << " is a prime number." << endl;
else
cout << number << " is not a prime number." << endl;

system("pause");
return 0;
}

Task 4:
Write a program having a inline function average() which
takes 5 float values as argument and return the average
of them.
#include "stdafx.h"
#include<iostream>
using namespace std;
inline float average(float a, float b, float c, float d, float e) {
return (a + b + c + d + e) / 5.0;
}

int _tmain(int argc, _TCHAR* argv[])


{ float num1, num2, num3, num4, num5;

cout << "Enter five float numbers: ";


cin >> num1 >> num2 >> num3 >> num4 >> num5;

cout << "The average is: " << average(num1, num2, num3, num4, num5) << endl;

system("pause");
return 0;
}

//

You might also like