0% found this document useful (0 votes)
10 views5 pages

Oops Exp5,6

OOPs lab experiments

Uploaded by

garimasagar10428
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)
10 views5 pages

Oops Exp5,6

OOPs lab experiments

Uploaded by

garimasagar10428
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/ 5

Experiment-5

1. Aim
To write a program that demonstrates the use of inline functions in C++, which help optimize performance by
reducing function call overhead.

2. Introduction
Inline functions are used in C++ to suggest to the compiler that the function code should be inserted at each call
point, rather than using a traditional function call. This can reduce the overhead associated with function calls,
especially in cases where the function is small and called frequently. However, it is up to the compiler to decide
whether to inline a function or not.

3. Algorithm
1. Define an inline function using the inline keyword.

2. Implement a small, frequently called function, such as one that calculates the square of a number or the
sum of two numbers.

3. Call the inline function from the main() function.

4. Observe that the function is executed without the typical function call overhead.

4. Program Code
#include <iostream>

using namespace std;

// Inline function to calculate the square of a number

inline int square(int x) {

return x * x;

// Inline function to add two numbers

inline int add(int a, int b) {

return a + b;

int main() {

int num1, num2;

cout << "Enter a number to find its square: ";

cin >> num1;


cout << "Square of " << num1 << " is: " << square(num1) << endl;

cout << "Enter two numbers to find their sum: ";

cin >> num1 >> num2;

cout << "Sum of " << num1 << " and " << num2 << " is: " << add(num1, num2) << endl;

return 0;

5. Output

6. Result
The program successfully implemented inline functions to calculate the square of a number and the sum of two
numbers, demonstrating how inline functions can reduce function call overhead for small, frequently used
functions.

7. Learning from Experiment


This experiment provided insight into the use of inline functions in C++, illustrating how they can optimize
performance by eliminating function call overhead. It also highlighted that the decision to inline a function is
ultimately made by the compiler, and that inline functions are best suited for small, frequently called functions.
Experiment-6
1. Aim
To write a program that calculates the mean of two numbers from different classes using a friend function in C++.

2. Introduction
A friend function in C++ is a special function that has access to the private and protected members of a class. By
declaring a function as a friend, the class allows this function to access its private data. This is particularly useful
when you need to perform operations involving data from different classes. In this experiment, a friend function
will be used to calculate the mean of two numbers stored in different classes.

3. Algorithm
1. Define two classes, each with a private member to hold a number.

2. Declare a friend function that takes objects of both classes as arguments.

3. The friend function should access the private members of both classes, calculate the mean, and return it.

4. Implement the main() function to create objects of both classes, input values, and call the friend function
to compute the mean.

4. Program Code
#include <iostream>

using namespace std;

class ClassA; // Forward declaration

class ClassB {

private:

int numB;

public:

void setNumber(int n) {

numB = n;

friend float findMean(ClassA, ClassB); // Friend function declaration

};

class ClassA {

private:

int numA;

public:
void setNumber(int n) {

numA = n;

friend float findMean(ClassA, ClassB); // Friend function declaration

};

// Friend function definition

float findMean(ClassA objA, ClassB objB) {

return (objA.numA + objB.numB) / 2.0;

int main() {

ClassA objA;

ClassB objB;

int num1, num2;

cout << "Enter a number for ClassA: ";

cin >> num1;

objA.setNumber(num1);

cout << "Enter a number for ClassB: ";

cin >> num2;

objB.setNumber(num2);

cout << "Mean of the two numbers is: " << findMean(objA, objB) << endl;

return 0;

}
5. Output

6. Result
The program successfully implemented a friend function to calculate the mean of two numbers from different
classes, demonstrating how friend functions can access private members of different classes.

7. Learning from Experiment


This experiment demonstrated the use of friend functions in C++. It showed how friend functions can access and
manipulate private data from different classes, allowing for operations like calculating the mean of values stored
in separate classes. The experiment also emphasized the importance of encapsulation and controlled access to
class members in object-oriented programming.

You might also like