0% found this document useful (0 votes)
9 views16 pages

OOPS

The document contains multiple C++ programming examples demonstrating various concepts such as friend functions, passing objects as arguments, swapping values using different methods, and implementing a banking application using classes. Each example includes code snippets that illustrate the functionality of the concepts discussed. The examples cover topics like private variables, member functions, and user interactions for banking transactions.

Uploaded by

soninilay605
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views16 pages

OOPS

The document contains multiple C++ programming examples demonstrating various concepts such as friend functions, passing objects as arguments, swapping values using different methods, and implementing a banking application using classes. Each example includes code snippets that illustrate the functionality of the concepts discussed. The examples cover topics like private variables, member functions, and user interactions for banking transactions.

Uploaded by

soninilay605
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Exp 1:-friend function

#include<iostream>

using namespace std;

class ABC;

class XYZ

{ int x;

public:

void setvalue(int i)

{ x=i; }

friend void max(XYZ,ABC);

};

class ABC

{ int a;

public:

void setvalue(int i)

{ a=i; }

friend void max(XYZ,ABC);

int b;

public:

void setvalue()

{ a=25; b=40; }

friend float mean(ABC s);

};

float mean(ABC s)

{ return float(s.a + s.b)/2.0; }

int main()

{ ABC x;

x.setvalue();
cout<<"Mean value = "<<mean(x)<<"\n";

return 0;

Exp2:- passing object as argument

#include<iostream>

using namespace std;

class time

{ int hours; int minutes;

public:

void gettime(int h,int m)

{ hours=h; minutes=m; }

void puttime(void)

{ cout<<hours<<" hours : ";

cout<<minutes<<" minutes "<<"\n"; }

void sum(time,time);

};

void time :: sum(time t1,time t2)

{ minutes=t1.minutes + t2.minutes;

hours=minutes/60;

minutes=minutes%60;

hours=hours+t1.hours+t2.hours;

int main()
{ time t1,t2,t3;

t1.gettime(2,45);

t2.gettime(3,30);

t3.sum(t1,t2);

cout<<"T1 "; t1.puttime();

cout<<"T2 "; t2.puttime();

cout<<"T3 "; t3.puttime();

return 0;

Exp3:- swap using classes

#include<iostream>

using namespace std;

class c2;

class c1

{ int v1;

public:

void indata(int a)

{ v1=a; }

void display(void)

{ cout<<v1<<"\n"; }

friend void exchange(c1 &,c2 &);

};
class c2

{ int v2;

public:

void indata(int a)

{ v2=a; }

void display(void)

{ cout<<v2<<"\n"; }

friend void exchange(c1 &,c2 &);

};

void exchange(c1 & x,c2 & y)

{ int temp=x.v1;

x.v1=y.v2;

y.v2=temp;

int main()

{ c1 C1;

c2 C2;

C1.indata(100);

C2.indata(200);

cout<<"Values after exchange "<<"\n";

exchange(C1,C2);

C1.display();

C2.display();

return 0;
}

Exp4:- using private variable

#include<iostream>

using namespace std;

class triangle

{ private:

double side1,side2,side3;

public:

triangle(double s1,double s2,double s3): side1(s1), side2(s2),side3(s3) {}

void determineType() const

{ if(side1<=0||side2<=0||side3<=0)

{ cout<<"Invalid sides"; }

if(side1==side2&&side2==side3)

{ cout<<"Equilateral triangle"; }

else if(side1==side2||side1==side3||side2==side3)

{ cout<<"Isosceles triangle"; }

else

{ cout<<"Scalene triangle"; }

};

int main()

{ double s1,s2,s3;

cout<<"Enter the length of three sides";


cout<<"\nSide-1: ";

cin>>s1;

cout<<"Side-2: ";

cin>>s2;

cout<<"Side-3: ";

cin>>s3;

triangle triangle(s1,s2,s3);

triangle.determineType();

return 0;

Exp5:-Swapping by call by reference

#include<iostream>

using namespace std;

int swap(int *a,int *b)

{ int s=*a;

*a=*b;

*b=s;

int main()

{ int a=9,b=15;

cout<<"A before calling the function is "<<a;

cout<<"\nB before calling the function is "<<b;

swap(&a,&b);
cout<<"\nA after calling the function is "<<a;

cout<<"\nB after calling the function is "<<b;

return 0;

Exp6:- Swap using call by value

#include<iostream>

using namespace std;

int swap(int a,int b)

{ int s=a;

a=b;

b=s;

int main()

{ int a=9,b=15;

cout<<"A before calling the function is "<<a;

cout<<"\nB before calling the function is "<<b;

swap(a,b);

cout<<"\nA after calling the function is "<<a;

cout<<"\nB after calling the function is "<<b;

return 0;

}
Exp7:- Swap using reference variable

#include<iostream>

using namespace std;

int swap(int &a,int &b)

{ int s=a;

a=b;

b=s;

int main()

{ int a=9,b=15;

cout<<"A before calling the function is "<<a;

cout<<"\nB before calling the function is "<<b;

swap(a,b);

cout<<"\nA after calling the function is "<<a;

cout<<"\nB after calling the function is "<<b;

return 0;

Exp8:- Bank transaction using reference variable


#include<iostream>

using namespace std;

void withdraw(float &a)

{ float w;

cout<<"Enter the amount to withdraw ";

cin>>w;

if(w>a)

{ cout<<"Cannot withdraw funds "; }

else

{ if(a>0)

{ a=a-w; }

else

{ cout<<"Not enough funds to withdraw ";

void transfer(float &a)

{ float t;

cout<<"Enter the amount to transfer ";

cin>>t;

if(t>a)

{ cout<<"Cannot transfer funds "; }

else

{ if(a>1200)

{ a=a-t; }

else

{ cout<<"Not enough funds to transfer "; }

}
}

void deposit(float &a)

{ float d;

cout<<"Enter the amount to deposit ";

cin>>d;

a=a+d;

void show(float &a)

{ cout<<"Current balance is : $"<<a;

int main()

{ float a=8000.00; int choice;

cout<<"Enter your choice \n1 Withdraw \n2 Transfer \n3 Deposit \n4 Show Balance";

cin>>choice;

switch(choice)

{ case 1: withdraw(a);

cout<<"\nYour balance after withdrawal "<<a;

break;

case 2: transfer(a);

cout<<"\nYour balance after transferring to another account "<<a;

break;

case 3: deposit(a);

cout<<"\nYour account balance after depositing money "<<a;

break;

case 4: show(a);

break;

return 0;
}

Exp9:- Banking application using classes

#include <iostream>

using namespace std;

class BankAccount {

private:

float balance;

public:

BankAccount(float initial_balance) {

balance = initial_balance; }

void withdraw() {

float w;

cout << "Enter the amount to withdraw: ";

cin >> w;

if (w <= 0) {

cout << "Invalid amount to withdraw.\n"; }

else if (w > balance) {

cout << "Cannot withdraw funds. Insufficient balance.\n";

} else {

balance = balance - w; }
}

void transfer() {

float t;

cout << "Enter the amount to transfer: ";

cin >> t;

if (t <= 0) {

cout << "Invalid amount to transfer.\n";

else if (t > balance) {

cout << "Cannot transfer funds. Insufficient balance.\n";

} else if (balance <= 1200) {

cout << "Not enough funds to transfer (balance below $1200).\n";

} else {

balance = balance - t;

void deposit() {

float d;

cout << "Enter the amount to deposit: ";

cin >> d;

if (d <= 0) {

cout << "Invalid amount to deposit.\n";

} else {

balance = balance + d;

}
void show() const {

cout << "Current balance is: $" << balance << endl;

};

int main() {

BankAccount account(8000.00);

int choice;

do {

cout << "\nEnter your choice: \n1. Withdraw \n2. Transfer \n3. Deposit \n4. Show
Balance \n5. Exit\n";

cin >> choice;

switch (choice) {

case 1:

account.withdraw();

cout << "Your balance after withdrawal: $";

account.show();

break;

case 2:

account.transfer();

cout << "Your balance after transfer: $";

account.show();

break;

case 3:

account.deposit();
cout << "Your balance after depositing money: $";

account.show();

break;

case 4:

account.show();

break;

case 5:

cout << "Exiting program.\n";

break;

default:

cout << "Invalid choice. Please try again.\n";

} while (choice != 5);

return 0;

EXP 10:- Member function

#include <iostream>

using namespace std;


class Date {

private:

int day, month, year;

bool isLeapYear() {

return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));

bool isValidDate() {

if (month < 1 || month > 12 || day < 1) return false;

int daysInMonth[] = {31, 28 + isLeapYear(), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

return day <= daysInMonth[month - 1];

public:

void setDate(int d, int m, int y) {

day = d; month = m; year = y;

void displayDate() {

if (isValidDate()) cout<<"Valid dates "<<day<<"/"<<month<<"/"<<year<<endl;

else cout << "Invalid date!" << endl;

};

int main() {

Date date;

date.setDate(2, 2, 2024);

date.displayDate();

date.setDate(3, 1, 2024);

date.displayDate();

return 0;

You might also like