PARVEEN (2)
PARVEEN (2)
PARVEEN (2)
OOPs
INDEX
S.NO EXPERIMENT NAME D.O.P D.O.C
PRACTICAL : 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 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 CODE:
#include<iostream>
#include<math.h>
using namespace std;
int main() { double
base,exponent,result;
cout<<"enter the base number:";
cin>>base;
cout<<"enter the exonent:";;
cin>>exponent; result =
pow(base,exponent);
cout<<base<<"raised to the power of "<<exponent<<" is :"<<result<<endl;
return 0; }
OUTPUT:1
PRACTICAL: 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 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.
SOURCE CODE:
#include<iostream>
using namespace std;
struct point { int
x; int y; }; int
main() {
point A,B,C;
cout<<"enter coordinates for p1 ";
cin>>A.x>>A.y;
cout<<"enter coordinates for p2 ";
cin>>B.x>>B.y; C.x=A.x+B.x;
C.y=A.y+B.y; cout<<"the sum of X
coordinates :"<<C.x <<endl; cout<<"the sum of Y
coordinates :"<<C.y<<endl;
cout<<"the new point coordinates are :"<<C.x<<","<<C.y<<endl; }
OUTPUT:2
PRACTICAL: 3
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: Enter coordinates for P1: 3 4
Enter coordinates for P2: 5 7 Coordinates of P1 + P2 are : 8, 11
SOURCE CODE:
#include<iostream>
using namespace std;
struct point { int
x; int y;
};
point add_point( point p1, point p2){
point p3; p3.x = p1.x + p2.x;
p3.y = p1.y + p2.y;
return p3;
} int
main(){
struct point p1,p2,p3;
OUTPUT: 3
PRACTICAL: 4
Create a class “Bank_Account” that contains Depositor_Name , Acc_No ,
Acc_type, Balance as its data members. Also create member functions for
account creation, deposit, withdraw and balance inquiry for class. Demonstrate
its use in main.
SOURCE CODE:
#include<iostream>
#include<string.h>
using namespace std;
class bank
{ private: int acc_no ;
string depositor__name;
string acc_type; float
balance; public:
void create_account(int account_no,string name,string account_type,float bal){
acc_no=account_no; depositor__name=name; balance=bal;
acc_type=account_type;
cout<<"account created successfully!"<<endl;
}
void deposit(double amount)
{ balance+=amount;
cout<<"deposit successful"<<endl;
}
void withdraw(double amount){
if(amount>balance){
cout<<"insufficiant balance!"<<endl;
}
else
{
balance-=amount;
cout<<"current balance"<<balance<<endl;
}
}
void balanceinquiry(){
cout<<"current balance"<<balance<<endl;
} }; int
main(){ bank
account;
account.creat
e_account(12
645,"john
doe","savings
",5000.00);
account.depo
sit(2500.00);
account.balan
ceinquiry();
account.with
draw(1000.00
);
account.balan
ceinquiry();
return 0; }
OUTPUT: 4
PRACTICAL: 5
Define a class “Time” that contains following data members and member
functions. Data members: 1. Hours 2. Minutes 3. Seconds Member Functions: 1.
To get time from user. 2. To display time on the screen 3. To calculate sum of
two time objects Write a program that can read values of Time for two objects
T1 and T2, calculate sum and display sum using defined member functions .
SOURCE CODE:
#include <iostream>
using namespace std;
public: void
getTime(void); void
putTime(void);
void addTime(Time T1, Time T2);
};
void Time::getTime(void)
{
cout << "Enter time:" << endl;
cout << "Hours -- "; cin >>
hours; cout << "Minutes -- ";
cin >> minutes; cout <<
"Seconds-- "; cin >> seconds;
}
void Time::putTime(void)
{
cout << endl;
cout << "Time after add: ";
cout << hours << ":" << minutes << ":" << seconds << endl; }
int main()
{
Time T1, T2, T3;
T1.getTime();
T2.getTime();
//add two times
T3.addTime(T1, T2);
T3.putTime();
return 0;
}
OUTPUT:5
PRACTICAL: 6
Create class “Sales” having following
data members and member functions: Data Members: 1. Name of Salesman 2.
Sales of Salesman Member functions to calculate commission : • Commission
is Rs. 10 per thousand if sales are at least Rs. 25000 or more • Commission is
Rs. 5 otherwise Write a program that calculate and print name and sales of
salesman.
SOURCE CODE:
#include <iostream >
using namespace std;
class Sales {
private:
string name;
double sales;
public:
Sales(string n, double s) {
name = n; sales = s;
}
double calculateCommission() {
if (sales >= 25000) { return
(sales / 1000) * 10;
} else {
return (sales / 1000) * 5;
}
}
void displayInfo() {
cout << "Salesman Name: " << name << endl;
cout << "Sales Amount: Rs. " << sales << endl;
cout << "Commission: Rs. " << calculateCommission() << endl;
}
};
int main() {
Sales salesman1("sahil", 30000);
Sales salesman2("rahul", 20000);
salesman1.displayInfo(); cout <<
endl;
salesman2.displayInfo();
return 0;
}
OUTPUT: 6
PRACTICAL:7
Write a program to count number objects created for particular class using
constructor.
SOURCE CODE:
#include <iostream>
public:
ObjectCounter() {
count++;
}
int main() {
ObjectCounter obj1;
ObjectCounter obj2;
ObjectCounter obj3;
ObjectCounter obj4;
std::cout << "Number of objects created: " << ObjectCounter::getCount() << std::endl;
return 0;
}
OUTPUT:7
PRACTICAL:8
Create class “Person” having a two data members as person name and
nationality. Also create two constructors for this class in which one has two
arguments and second has one argument.
SOURCE CODE:
#include <iostream>
#include <string>
using namespace std;
class Person {
private: string
personName;
string nationality;
public:
// Constructor with two arguments
Person(string name, string nat) {
personName = name; nationality
= nat;
}
return 0;
OUTPUT:8
PRACTICAL: 9
Write a program to declare two classes, each one have one int data member.
Find the sum of data members of both classes using friend function. Create
suitable objects and functions
SOURCE CODE:
#include <iostream>
class ClassA {
private:
int numA;
public:
ClassA(int num) : numA(num) {}
class ClassB {
private:
int numB;
public:
ClassB(int num) : numB(num) {}
int main()
{ ClassA
objA(5);
ClassB objB(10);
std::cout << "Sum of data members: " << sum << std::endl;
return 0;
}
OUTPUT: 9
PRACTICAL: 10
Create Class complex number having real and imaginary part as data member,
constructor and member function to read complex number. Class should
overload + operator to add to find sum of two complex numbers.
SOURCE CODE:
#include <iostream>
using namespace std;
class ComplexNumber {
private: double real;
double imaginary;
public:
ComplexNumber(double r, double i) : real(r), imaginary(i) {}
void readComplexNumber() {
cout << "Enter real part: ";
cin >> real;
cout << "Enter imaginary part: ";
cin >> imaginary;
}
void display() {
cout << real << " + " << imaginary << "i" << endl;
} };
int main() {
ComplexNumber num1(0, 0); // Initialize with default values
ComplexNumber num2(0, 0);
cout << "Enter the first complex number: " << endl;
num1.readComplexNumber();
cout << "Enter the second complex number: " << endl;
num2.readComplexNumber();
return 0; }
OUTPUT: 10