0% found this document useful (0 votes)
2 views

AKSH Cpp Assignemnt

Uploaded by

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

AKSH Cpp Assignemnt

Uploaded by

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

Akshatha s b

01fe22bar048

// FOR SUBTRACTING TWO COMPLEX NUMBERS USING FRIEND CLASS

#include <iostream>

using namespace std;

class Complex {

private:

double real;

double img;

public:

Complex(double r, double i) : real(r), img(i) {}

void display() {

if (img < 0)

cout << real << " - " << -img << "i";

else

cout << real << " + " << img << "i";

friend Complex operator-(const Complex& o1, const Complex& o2);

};

Complex operator-(const Complex& o1, const Complex& o2) {

double newReal = o1.real - o2.real;

double newImg = o1.img - o2.img;

return Complex(newReal, newImg);


}

int main() {

Complex myComplex1(7.0, 5.65

);

Complex myComplex2(2.54, 8.0);

cout << "myComplex1 = ";

myComplex1.display();

cout << endl;

cout << "myComplex2 = ";

myComplex2.display();

cout << endl;

Complex result = myComplex1 - myComplex2;

cout << "myComplex1 - myComplex2 = ";

result.display();

cout << endl;

return 0;

//OUTPUT
//code to find total balance from current and savings account using friend class

#include <iostream>

using namespace std;

class SavingsAccount;

class CurrentAccount;

void printTotalBalance(const SavingsAccount& savings, const CurrentAccount& current);

class SavingsAccount {

private:

float balance;

public:

SavingsAccount(float balance) : balance(balance) {}


friend void printTotalBalance(const SavingsAccount& savings, const CurrentAccount& current);

};

class CurrentAccount {

private:

float balance;

public:

CurrentAccount(float balance) : balance(balance) {}

friend void printTotalBalance(const SavingsAccount& savings, const CurrentAccount& current);

};

// Friend function definition

void printTotalBalance(const SavingsAccount& savings, const CurrentAccount& current) {

float totalBalance = savings.balance + current.balance;

cout << "Total Balance in both accounts: " << totalBalance << endl;

int main() {

SavingsAccount savings(1000.50);

CurrentAccount current(2500.75);

printTotalBalance(savings, current);

return 0;

}
//OUTPUT

You might also like