PRACTICAL FILE OF OBJECT ORIENTED AND PROGRAMING UNIVERSITY INSTIUTE OF ENGINEERING AND TECHNOLOGY, KURUKSHETRA UNIVERSITY, (SESSION : 2021 – 2025) Submitted To: Ms. Sonia Asst. Professor UIET KUK Submitted By: Harsh Dev Singh 252102060 CSE – A (3rd Sem) PROGRAM NO. 1 Q1. Raising a number n to a power p is the same as multiplying n by itself p times. Write a function called power ( ) that takes a double value for n and an int value for p, and returns the result as double value. Use a default argument of 2 for p, so that if this argument is omitted, the number will be squared. Write a main ( ) function that gets values from the user to test this function SOURCE PROGRAM: #include <iostream> using namespace std; double power(double a, int b = 2) { double x = 1; for (int i = 0; i < b; i++) { x = x * a; } return x; }; int main() { double n, res; int p; cout << "Enter the number:"; cin >> n; cout<<"Enter the power:"; cin>>p; cout << "Your output is "; //if the power entered by the user is negative then the function would use the default arguments if (p >= 0) { res = power(n, p); cout << res << endl; } else { res = power(n); cout << res << endl; } return 0; } OUTPUT: PROGRAM NO. 2 Q2. 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 origin along the X axis and 5 units up the Y 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 points and whose Y coordinate is the sum of their 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. SOURCE PROGRAM: #include <iostream> using namespace std; struct point { int x, y ; }; int main() { point P1, P2, ans; cout << "Enter the coordinates of the P1: "; cin >> P1.x >> P1.y; cout << "Enter the coordinates of the P2: "; cin >> P2.x >> P2.y; ans.x = P1.x + P2.x; ans.y = P1.y + P2.y; cout << "The coordinates of P1 + P2 are: " << ans.x << "," << ans.y << endl; return 0; } OUTPUT: PROGRAM NO. 3 Q3. Create the equivalent of a four function calculator. The program should request the user to enter a number, an operator, and another number. It should then carry out the specified arithmetical operation: adding, subtracting, multiplying, or dividing the two numbers. (It should use a switch statement to select the operation). Finally it should display the result. When it finishes the calculation, the program should ask if the user wants to do another calculation. The response can be ‘Y’ or ‘N’. SOURCE CODE: #include <iostream> using namespace std; class cal { float a, b, result; char c; public: void display() { cout << "Enter the first number: "; cin >> a; cout << "Enter the second number: "; cin >> b; cout << "Enter the operator: "; cin >> c; switch (c) { case '+': result = a + b; cout << "result = " << result << endl; break; case '-': result = a - b; cout << "result = " << result << endl; break; case '*': result = a * b; cout << "result = " << result << endl; break; case '/': result = a / b; cout << "result = " << result << endl; break; default: cout << "Not a vaild operator!" << endl; } } }; int main() { char c; do { cal obj; obj.display(); cout << "Do another(Y/N)?"; cin >> c; } while (c == 'Y'); If (c == ‘N’){ cout << “Program is finished!”; return 0; } OUTPUT: PROGRAM NO. 4 Q4. 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. SOURCE CODE: #include <iostream> using namespace std; struct phone { int area_code, exchange, number; }; int main() { phone a, b; a.area_code = 313; a.exchange = 786; a.number = 605; cout << "Enter your area code, exchange and number: "; cin >> b.area_code >> b.exchange >> b.number; cout << "My number is (" << a.area_code << ") " << a.exchange << "-" << a.number << endl; cout << "Your number is (" << b.area_code << ") " << b.exchange << "-" << b.number << endl; return 0; } OUTPUT: PROGRAM NO. 5 Q5. Create two classes DM and DB which store the value of distances. DM stores distances in metres and centimeters and DB in feet and inches. Write a program that can read values for the class objects and add one object of DM with another object of DB. Use a friend function to carry out the addition operation. The object that stores the results maybe a DM object or DB objects, depending on the units in which the results are required. The display should be in the format of feet and inches or metres and cenitmetres depending on the object on display. SOURCE CODE: #include <iostream> using namespace std; class DB; class DM { int m, cm; public: void getdata() { cout << "Enter the the distance in meter: "; cin >> m; cout << "Enter the distance in centimeter: "; cin >> cm; } friend void add(DM b, DB a); }; class DB { int feet, inches; public: void getdata() { cout << "Enter the the distance in feet: "; cin >> feet; cout << "Enter the distance in inches: "; cin >> inches; } friend void add(DM b, DB a); }; void add(DM M, DB F) { float meter = M.m + F.feet * 0.305; float centimeter = M.cm + F.inches * 2.54; cout << "Total distance is:" << endl << "METER: " << meter << endl << "CENTIMETER: " << centimeter; } int main() { DM X; DB Y; X.getdata(); Y.getdata(); add(X,Y); return 0; } OUTPUT: PROGRAM NO. 6 Q. Create a class rational which represents a numerical value by two double values- NUMERATOR and DENOMINATOR. Include the following public member Functions: • constructor with no arguments (default). • constructor with two arguments. • void reduce( ) that reduces the rational number by eliminating the highest common factor between the numerator and denominator. • Overload + operator to add two rational number. • Overload >> operator to enable input through cin. • Overload << operator to enable output through cout. Write a main ( ) to test all the functions in the class. SOURCE CODE: #include <iostream> using namespace std; class rational { double NUMERATOR, DENOMINATOR; public: rational(){}; rational(double a, double b) { NUMERATOR = a; DENOMINATOR = b; } friend ostream &operator<<(ostream &out, rational &r) { out << "NUMERATOR: " << r.NUMERATOR << endl; out << "DENOMINATOR: " << r.DENOMINATOR << endl; return out; } friend istream &operator>>(istream &in, rational &r) { in >> r.NUMERATOR; in >> r.DENOMINATOR; return in; } rational operator+(rational &r) { rational temp; temp.NUMERATOR = NUMERATOR + r.NUMERATOR; temp.DENOMINATOR = DENOMINATOR + r.DENOMINATOR; return temp; } friend void reduce(rational &r) { int a = r.NUMERATOR; int b = r.DENOMINATOR; int num = 0; for(int i = 0;i<=min(a,b);i++){ if(a%i == 0 && b%i == 0){ num = i; } } r.NUMERATOR = r.NUMERATOR - num; r.DENOMINATOR = r.DENOMINATOR - num; } }; int main() { rational r1(3.1, 7.8); rational r2; cout << "Enter the numerator and denominator of R2: "; cin >> r2; cout << "The sum of two rational number is :\n"; rational r3 = r1 + r2; cout << r3; reduce(r3); cout << "After reduction , R3 would be:\n" << r3; return 0; } OUTPUT: PROGRAM NO. 7 Q7. Consider the following class definition class father { protected : int age; public: father (int x) {age = x;} virtual void iam ( ) { cout < < “I AM THE FATHER, my age is : ”<< age<< end1:} }; Derive the two classes son and daughter from the above class and for each, define iam ( ) to write our similar but appropriate messages. You should also define suitable constructors for these classes. Now, write a main ( ) that creates objects of the three classes and then calls iam ( ) for them. Declare pointer to father. Successively, assign addresses of objects of the two derived classes to this pointer and in each case, call iam ( ) through the pointer to demonstrate polymorphism in action. SOURCE CODE: #include<iostream> using namespace std; class father{ protected: int age; public: father(){}; father(int x){ age = x; } virtual void iam(){ cout<<"I AM THE FATHER, my age is "<<age<<endl; } }; class son:public father{ public: son(){}; son(int x){ age = x; } void iam(){ cout<<"I AM THE SON, my age is "<<age<<endl; } }; class daughter:public father{ public: daughter(){}; daughter(int x){ age = x; } void iam(){ cout<<"I AM THE DAUGHTER, my age is "<<age<<endl; } }; int main(){ father f(50); son s(21); daughter d(23); father *bptr; f.iam(); bptr = &s; bptr->iam(); bptr = &d; bptr->iam(); return 0;} OUTPUT: