words per minute
5
AkshayYadav1
00:00
Speed
#include <iostream>
using namespace std;
// Forward declaration
class Complex;
class Calculator
{
public:
int add(int a, int b)
{
return (a + b);
}
int sumRealComplex(Complex, Complex);
int sumCompComplex(Complex, Complex);
};
class Complex
{
int a, b;
// Individually declaring functions as friends
// friend int Calculator ::sumRealComplex(Complex, Complex);
// friend int Calculator ::sumCompComplex(Complex, Complex);
// Aliter: Declaring the entire calculator class as friend
friend class Calculator;
public:
void setNumber(int n1, int n2)
{
a = n1;
b = n2;
}
void printNumber()
{
cout << "Your number is " << a << " + " << b << "i" << endl;
}
};
int Calculator ::sumRealComplex(Complex o1, Complex o2)
{
return (o1.a + o2.a);
}
int Calculator ::sumCompComplex(Complex o1, Complex o2)
{
return (o1.b + o2.b);
}
int main()
{
Complex o1, o2;
o1.setNumber(1, 4);
o2.setNumber(5, 7);
Calculator calc;
int res = calc.sumRealComplex(o1, o2);
cout << "The sum of real part of o1 and o2 is " << res << endl;
int resc = calc.sumCompComplex(o1, o2);
cout << "The sum of complex part of o1 and o2 is " << resc << endl;
return 0;
}
#include<iostream>
using namespace std;
class Y;
class X{
int data;
public:
void setValue(int value){
data = value;
}
friend void add(X, Y);
};
class Y{
int num;
public:
void setValue(int value){
num = value;
}
friend void add(X, Y);
};
void add(X o1, Y o2){
cout<<"Summing data of X and Y objects gives me "<< o1.data + o2.num;
}
int main(){
X a1;
a1.setValue(3);
Y b1;
b1.setValue(15);
add(a1, b1);
return 0;
}
#include<iostream>
using namespace std;
class c2;
class c1{
int val1;
friend void exchange(c1 & , c2 &);
public:
void indata(int a){
val1 = a;
}
void display(void){
cout<< val1 <<endl;
}
};
class c2{
int val2;
friend void exchange(c1 &, c2 &);
public:
void indata(int a){
val2 = a;
}
void display(void){
cout<< val2 <<endl;
}
};
/*
Trick to swap 2 numbers a and b:
temp = a;
a = b;
b = temp;
*/
void exchange(c1 &x, c2 &y){
int tmp = x.val1;
x.val1 = y.val2;
y.val2 = tmp;
}
int main(){
c1 oc1;
c2 oc2;
oc1.indata(34);
oc2.indata(67);
exchange(oc1, oc2);
cout<<"The value of c1 after exchanging becomes: ";
oc1.display();
cout<<"The value of c2 after exchanging becomes: ";
oc2.display();
return 0;
}
#include <iostream>
using namespace std;
class Complex
{
int a, b;
public:
// Creating a Constructor
// Constructor is a special member function with the same name as of the class.
//It is used to initialize the objects of its class
//It is automatically invoked whenever an object is created
Complex(void); // Constructor declaration
void printNumber()
{
cout << "Your number is " << a << " + " << b << "i" << endl;
}
};
Complex ::Complex(void) // ----> This is a default constructor as it takes no parameters
{
a = 0;
b = 0;
// cout<<"Hello world";
}
int main()
{
Complex c1, c2, c3;
c1.printNumber();
c2.printNumber();
c3.printNumber();
return 0;
}