PARVEEN (2)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 18

PRACTICAL FILE OF

OOPs

SUBMITTED TO: SUBMITTED BY:


MS. GARIMA PARVEEN
Department of C.S.E BCA 3rd Semester

INDEX
S.NO EXPERIMENT NAME D.O.P D.O.C

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.

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.

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

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.
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 .

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.
7 Write a program to count number objects created
for particular class using constructor.

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.

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

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

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;

cout<<"enter coordinates for P1";


cin>>p1.x>>p1.y;
cout<<"enter coordinates for P2";
cin>>p2.x>>p2.y; p3 =
add_point(p1,p2);
cout<<"coordinates for P1+P2 are :"<<p3.x<<","<<p3.y<<endl; }

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;

class Time { private:


int hours;
int minutes;
int seconds;

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; }

void Time::addTime(Time T1, Time T2)


{

this->seconds = T1.seconds + T2.seconds;


this->minutes = T1.minutes + T2.minutes + this->seconds / 60;
;
this->hours = T1.hours + T2.hours + (this->minutes / 60);
this->minutes %= 60; this->seconds %= 60;
}

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>

class ObjectCounter { private:


static int count;

public:
ObjectCounter() {
count++;
}

static int getCount() {


return count;
}
};

// Initialize the static count variable int


ObjectCounter::count = 0;

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;
}

// Constructor with one argument


Person(string name)
{ personName = name;
nationality = "Unknown"; // Default nationality
}

void displayInfo() { cout << "Name: " <<


personName << endl; cout << "Nationality: "
<< nationality << endl;
}
};
int main() {
Person person1("John", "American");
Person person2("Emma");

cout << "Person 1 Info:" << endl;


person1.displayInfo();

cout << "\nPerson 2 Info:" << endl;


person2.displayInfo();

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 ClassB; // Forward declaration

class ClassA {
private:
int numA;

public:
ClassA(int num) : numA(num) {}

friend int sumDataMembers(ClassA objA, ClassB objB); // Declare ClassB as a friend };

class ClassB {
private:
int numB;

public:
ClassB(int num) : numB(num) {}

friend int sumDataMembers(ClassA objA, ClassB objB); // Declare ClassA as a friend };

int sumDataMembers(ClassA objA, ClassB objB) {


return objA.numA + objB.numB;
}

int main()
{ ClassA
objA(5);
ClassB objB(10);

int sum = sumDataMembers(objA, objB);

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;
}

ComplexNumber operator+(const ComplexNumber& other) {


double sumReal = real + other.real; double
sumImaginary = imaginary + other.imaginary; return
ComplexNumber(sumReal, sumImaginary);
}

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();

ComplexNumber sum = num1 + num2;

cout << "Sum of the two complex numbers: ";


sum.display();

return 0; }

OUTPUT: 10

You might also like