Object Oriented Programming Practical 6

Question 1
Write a C++ program to calculate area of Rectangle using Inline function.
rectangle.cpp
C
				#include <iostream.h>
#include <conio.h>

inline int calculateArea(int length, int width) 
{
    return length * width;
}

void main() 
{
    int length, width;

    cout << "Enter the length of the rectangle: ";
    cin >> length;

    cout << "Enter the width of the rectangle: ";
    cin >> width;

    int area = calculateArea(length, width);

    cout << "The area of the rectangle is: " << area << endl;

    getch();
}

			
Output
Question 2
Write a C++ program to create a class "Number" having data members n1 and n2 and perform mathematical operations like addition, subtraction, multiplication and division on two numbers using inline functions.
number.cpp
C
				#include <iostream>
#include <conio.h>

class Number 
{
	private:
		int n1, n2;

	public:
		void accept()
		{
			cout<<"Enter number 1: ";
			cin>>n1;
			cout<<"Enter number 2: ";
			cin>>n2;
		}
		
		inline int add() 
		{
			return n1 + n2;
		}

		inline int subtract() 
		{
			return n1 - n2;
		}

		inline int multiply() 
		{
			return n1 * n2;
		}

		inline float divide() 
		{
			if (n2 != 0) 
			{
				return (float)n1/n2;
			} 
			else 
			{
				cout << "Error: Division by zero is not allowed." << endl;
				return 0;
			}
		}
};

int main() {
    int num1, num2;
    Number num;
	num.accept();
    cout << "Addition: " << num.add() << endl;
    cout << "Subtraction: " << num.subtract() << endl;
    cout << "Multiplication: " << num.multiply() << endl;
    cout << "Division: " << num.divide() << endl;
	getch();
    return 0;
}
			
Output

Leave a Comment

Your email address will not be published. Required fields are marked *