oops practical file 2322813
oops practical file 2322813
of
“OBJECT ORIENTED PROGRAMMING LAB”
SUBJECT CODE : PC-CS-AIML-205A
SUBMITTED BY:
Mahima Gupta
2322813
BRANCH: Artificial Intelligence and Machine Learning
INDEX
PRACTICAL-1
AIM-1 :Raising a number to a power p is the same as multiplying n by
itself p times. Write a function called power that takes two arguments, a
double value for n and an int value for p, and return the result as double
value. Use default argument of 2 for p, so that if this argument is omitted
the number will be squared. Write the main function that gets value from
the user to test function.
Procedure:
1. Make a function to calculate power such that:
a. Define two parameters , one as number and other as power
with default valueas 2.
b. In function set new variable x with value 1.
c. Start for loop with variable I initialize with zero value up to less than p.
d. Now x=x*n; until for loop.
e. Return value of x.
2. Continue main function.
3. Enter the value of number and enter power.
4. Call power function with argument passing.
5. Now print result with power passing and without power passing(Default
power is 2).
SOURCE CODE:
#include<iostream>
using namespace std;
double power(double,int=2);
int main()
{
int p;
double n,r;
cout <<"Enter number : ";
cin >> n;
cout <<"Enter exponent : ";
cin >> p;
r = power(n,p);
cout <<"Result is "<< r;
r = power(n);
2322813
}
double power(double a, int b)
{
double x = 1;
for(int i = 1; i <= b; i++)
x = x * a;
return x;
}
OUTPUT:
Enter number : 9
Enter exponent : 4
Result is 6561
Result without passing exponent is 81
RESULT:
2322813
PRACTICAL-2
AIM-2: A point on the two-dimensional plane can be represented by two numbers: an x
coordinate and a y
coordinate. For example, (4,5) represents a point 4 units to the right of the vertical axis, and 5
units up from the horizontal axis. The sum of two points can be defined as a new point whose
x coordinate is the sum of the x coordinates of the two points, and whose y coordinate is the
sum of the y coordinates. Write a program that uses a structure called point to model a point.
Define three points,
and have the user input values to two of them. Then set the third point equal to the sum of the
other two, and display the value of the new point. Interaction with the program might look
like this:
SOURCE CODE:
#include <iostream>
using namespace std;
struct point
{
int x Co ; //X coordinate
int y Co ; //Y
coordinate
};
int main ()
2322813
{
point p1, p2, p3; //define 3 points
cout << "\n Enter coordinates for p1: "; //get 2 points
cin >> p1.xCo >> p1.yCo; //from user
cout << "Enter coordinates for p2: ";
cin >> p2.xCo >> p2.yCo;
p3.xCo = p1.xCo + p2.xCo; //find sum of
p3.yCo = p1.yCo + p2.yCo; //p1 and p2
cout << "Coordinates of p1 + p2 are : " //display the sum
<< p3.xCo << ", " << p3.yCo << endl;
return 0;
}
OUTPUT:
Enter coordinates for p1: 4,6
Enter coordinates for p2: 8,2
Coordinates of p1 + p2 are : 12,8
Date of performing
2322813
13-10-23
PRACTICAL-3
AIM-3 : Create the equivalent of a four-function calculator. The program should ask the user
to enter a number, an operator, and another number. (Use floating point.) It should then carry
out the specified arithmetical operation: adding, subtracting, multiplying, or dividing the two
numbers. Use a switch statement to select the operation. Finally, display the result. When it
finishes the calculation, the program should ask whether the user wants to do another
calculation. The response can be ‘y’ or ‘n’.
SOURCE CODE:
#include <iostream>
using namespace std;
int main()
{
double n1, n2, ans;
char oper, ch;
do {
cout << "\nEnter first number, operator, second number : ";
cin >> n1 >> oper >> n2;
switch (oper)
{
case '+': ans = n1 + n2; break;
case '-': ans = n1 - n2; break;
case '*': ans = n1 * n2; break;
case '/': ans = n1 / n2; break;
default: ans = 0;
}
cout << "Answer = " << ans;
cout << "\nDo another(Enter ‘Y’ or ‘N’) ? ";
2322813
OUTPUT:
Enter first number, operator, second number : 25-12
Answer = 13
Do another(Enter Y or N) ? N
2322813
Date of
performing 13-10-
23
PRACTICAL-4
AIM-4 : A phone number, such as (212) 767-8900, can be thought of as having three parts: the
area code
(212), the exchange (767) and the number (8900). Write a program that uses a structure to
store these three parts of a phone number separately. Call the structure phone. Create
two structure variables of type phone. Initialize one, and have the user input a number
for the other one. Then display both numbers. The interchange might look like this:
Enter your area code, exchange, and number: 415 555 1212
My number is (212) 767-8900
• Your number is (415) 555-1212
SOURCE CODE:
#include <iostream>
2322813
using namespace
std; struct Phone
{
int
area_code;
int
exchange;
int number;
};
int main()
{
Phone phone1 = {212, 767,
8900}; Phone phone2;
cout << "Enter a phone number (area code exchange
number): "; cin >> phone2.areaCode >>
phone2.exchange >> phone2.number;
cout << "Phone 1: (" << phone1.areaCode << ") " << phone1.exchange << "-"
<< phone1.number << std::endl;
cout << "Phone 2: (" << phone2.areaCode << ") " << phone2.exchange << "-"
<< phone2.number << std::endl;
return 0;
}
OUTPUT: