oops_file
oops_file
Signature
___________________
Program – 1
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 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 Code :
#include<iostream>
using namespace std;
double power (double n, int p=2)
{
double a = 1;
for(int i=1;i<=p;i++){
a=a*n;
}
return a;
}
int main()
{
int p;
double n,result;
cout<< "ENTER BASE NUMBER : " <<endl;
cin>>n;
cout<< "ENTER THE POWER : " <<endl;
cin>>p;
result=power(n,p);
cout<<"Result is : "<<result<<endl;
result=power(n);
cout<<"Result with Default power (2) is : "<<result<<endl;
return 0;
}
Output :
Program – 3
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.
Source Code :
#include<iostream>
using namespace std;
int main()
{
float n1,n2;
char op,ch;
do{
cout << "Enter first operand,operator and second operand"<< endl;
cin >> n1 >> op >> n2;
switch(op){
case '+':
cout<<"Result of "<<n1<<" + "<<n2<<" is "<< n1+n2 << endl;
break;
case '-':
cout<<"Result of "<<n1<<" - "<<n2<<" is "<<n1-n2<< endl;
break;
case '*':
cout<<"Result of "<<n1<<" * "<<n2<<" is "<<n1*n2<< endl;
break;
case '/':
cout<<"Result of "<<n1<<" / "<<n2<<" is "<<n1/n2<< endl;
}
cout<<"Do you want to run the program again? (y/n)"<< endl;
cin >> ch;
}
while(ch!='n');
return 0;
}
Output :
Program – 5
Create two classes DM and DB which stores the value of distances. DM stores
distances in meters and centimeters and DB in feet and inches. write a
program that can read values for the class object and add one abject of DM in
another object of DB. Use a friend function to carry out the additions
operation, the object that stores the result maybe a DM object or DB object,
depending on the units in which the results are required. the display should be
in format of feet and inches or meters and centimeters depending one the
object on display.
Source Code :
#include <iostream>
using namespace std;
class DB;
class DM {
private:
int meters;
int centimeters;
public:
DM(int m=0, int cm=0) : meters(m), centimeters(cm) {
}
friend DM addDistance(const DM &dm, const DB &db, bool inMeters);
void display() const {
cout << meters <<" meters and "<< centimeters <<" centimeters"<< endl;
}
};
class DB {
private:
int feet;
int inches;
public:
DB(int f = 0, int in = 0) : feet(f), inches(in) {}
friend DM addDistance(const DM &dm, const DB &db, bool inMeters);
void display() const {
cout << feet << " feet and " << inches << " inches" << endl;
}
};
int main() {
DM distance1(3, 50);
DB distance2(5, 11);
DM resultInMeters = addDistance(distance1, distance2, true);
cout << "Result in meters and centimeters: ";
resultInMeters.display();
return 0;
}
Output:
Program – 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 variable of type
phone. initialize one, and have the user input a number for the other one. then
display both numbers. the interchanges might look like :
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>
using namespace std;
struct Phone {
int areaCode;
int exchange;
int number;
};
int main() {
Phone myNumber = {459, 111, 5786};
Phone yourNumber;
cout << "Enter your area code, exchange, and number (e.g., 415 555 1212): ";
cin>> yourNumber.areaCode >> yourNumber.exchange >> yourNumber.number;
cout << "My number is (" << myNumber.areaCode << ") " << myNumber.exchange << "-" <<
myNumber.number << endl;
cout << "Your number is (" << yourNumber.areaCode << ") " << yourNumber.exchange
<< "-" << yourNumber.number << endl;
return 0;
}
Output:
Program-6
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 argument(default)
constructor with two arguments.
void reduce () that reduce 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>
#include <cmath>
using namespace std;
class Rational {
private:
double numerator;
double denominator;
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
public:
Rational() : numerator(0), denominator(1) {
}
if (denominator < 0) {
numerator = -numerator;
denominator = -denominator;
}
}
Rational operator+(const Rational& other) const {
double newNumerator = numerator * other.denominator + other.numerator * denominator;
double newDenominator = denominator * other.denominator;
return Rational(newNumerator, newDenominator);
}
friend istream& operator>>(istream& in, Rational& r) {
cout << "Enter the numerator: ";
in >> r.numerator;
cout << "Enter the denominator: ";
in >> r.denominator;
if (r.denominator == 0) {
cout << "Denominator cannot be zero. Setting denominator to 1." << endl;
r.denominator = 1;
}
r.reduce();
return in;
}
friend ostream& operator<<(ostream& out, const Rational& r) {
out << r.numerator << "/" << r.denominator;
return out;
}
};
int main() {
Rational r1, r2;
cout << "Enter the first rational number:" << endl;
cin >> r1;
cout << "Enter the second rational number:" << endl;
cin >> r2;
cout << "First rational number: " << r1 << endl;
cout << "Second rational number: " << r2 << endl;
Rational result = r1 + r2;
cout << "Sum of the two rational numbers: " << result << endl;
return 0;
}
Output:
Program – 2
Write a C++ program that illustrates the order of execution of constructors
and destructors when new class is derived from more than one base class. b)
Write a Program to Invoking Derived Class Member Through Base Class
Pointer.
Source Code:
Part-A
#include <iostream>
using namespace std;
class Base1 {
public:
Base1() {
cout << "Base1 Constructor called" << endl;
}
~Base1() {
cout << "Base1 Destructor called" << endl;
}
};
class Base2 {
public:
Base2() {
cout << "Base2 Constructor called" << endl;
}
~Base2() {
cout << "Base2 Destructor called" << endl;
}
};
class Derived : public Base1, public Base2 {
public:
Derived() {
cout << "Derived Constructor called" << endl;
}
~Derived() {
cout << "Derived Destructor called" << endl;
}
};
int main() {
// Creating an object of Derived class
Derived obj;
return 0;
};
Output:
Part-B
#include <iostream>
using namespace std;
class Base {
public:
void show() {
cout << "Base class show() function" << endl;
}
};
return 0;
}
Output: