Thursday, January 19, 2023

FRIEND FUNCTION IN C++

 #include<iostream>

using namespace std;


class cl

{

int a;

public:

void input(int x)

{

a=x;

}

void output()

{

cout<<a<<endl;

}

friend void f(cl ); // the function f() is a friend function , which can access the private member of class cl.

};


// The functiin f is not a member function of f(cl ) . For this reason no scope resolution operator is used .


void f(cl ob)

{

cout<<ob.a<<endl; // ob.a is a private member of class cl.

}


int main ()

{

cl ob;

cout<<"sunday"<<endl;

ob.input(100);

cout<<"monday"<<endl;

ob.output();

cout<<"friday"<<endl;

f(ob);

}

DESTRUCTOR IN C++ (2)

 #include<iostream>

using namespace std;


class cl

{

int a;

public:

  cl();

  cl(int x);

  void output();

  ~cl();

};

cl::cl()

{

a=10;

cout<<"overloading"<<a<<endl;

}

cl::cl(int x)

{

a=x;

cout<<"overloading"<<a<<endl;

}

void cl::output()

{

cout<<a<<endl;

}

cl::~cl()

{

cout<<"destructing"<<a<<endl;

}


void f( cl ob3)

{

cout<<"monday"<<endl;

}

int main()

{

cl ob(100),ob1,ob2(50),ob3(30);

cout<<"sunday"<<endl;

f(ob3);

cout<<"friday"<<endl;

}

DESTRUCTOR IN C++ (1)

 #include<iostream>

using namespace std;


class cl

{

int a;

public:

  cl();

  cl(int x);

  void output();

  ~cl();

};

cl::cl()

{

a=10;

cout<<"overloading"<<a<<endl;

}

cl::cl(int x)

{

a=x;

cout<<"overloading"<<a<<endl;

}

void cl::output()

{

cout<<a<<endl;

}

cl::~cl()

{

cout<<"destructing"<<a<<endl;

}

int main()

{

cl ob,ob1(20),ob2(30);

cout<<"sunday"<<endl;

}

COPY CONSTRUCTOR IN C++

 #include<iostream>

using namespace std;


class cl

{

int a;

public:

  cl();

  cl(int x);

  cl(cl &ob); //copy constructor

  void output();

  ~cl();

};

cl::cl()

{

a=10;

cout<<"overloading"<<a<<endl;

}

cl::cl(int x)

{

a=x;

cout<<"overloading"<<a<<endl;

}

void cl::output()

{

cout<<a<<endl;

}

cl::~cl()

{

cout<<"destructing"<<a<<endl;

}


void f( cl ob3)

{

cout<<"monday"<<endl;

}

cl::cl(cl &ob)

{

a=ob.a;

cout<<"copy constructor"<<a<<endl;

}

int main()

{

cl ob(100),ob1,ob2(50),ob3(30);

cout<<"sunday"<<endl;

f(ob3);

cout<<"friday"<<endl;

cl ob4(500); // parameterised constructor

cl ob5=ob4; // copy constructor

cl ob6(ob); // copy constructor

}

DIVISION OF TWO COMPLEX NUMBER USING FRIEND FUNCTION

 #include<iostream>

using namespace std;


class complex

{

int a,b;

public:

complex()

{

}

complex(int x,int y)

{

a=x;

b=y;

}

void display()

{

if(b>0)

{

cout<<a<<"+i"<<b<<endl;

}

else

{

cout<<a<<"-i"<<b<<endl;

}

}

friend void add(complex,complex);

};

void add(complex ob1, complex ob2)

{

complex ob3;

ob3.a=ob1.a/ob2.a;

ob3.b=ob1.b/ob2.b;

ob3.display();

}


int main()

{

complex ob1(2,-3),ob2(4,5);

add(ob1,ob2);

}

MULTIPLICATION OF TWO COMPLEX NUMBER USING FRIEND FUNCTION

 #include<iostream>

using namespace std;


class complex

{

int a,b;

public:

complex()

{

}

complex(int x,int y)

{

a=x;

b=y;

}

void display()

{

if(b>0)

{

cout<<a<<"+i"<<b<<endl;

}

else

{

cout<<a<<"-i"<<b<<endl;

}

}

friend void add(complex,complex);

};

void add(complex ob1, complex ob2)

{

complex ob3;

ob3.a=ob1.a*ob2.a;

ob3.b=ob1.b*ob2.b;

ob3.display();

}


int main()

{

complex ob1(2,-3),ob2(4,5);

add(ob1,ob2);

}

SUBTRACTION OF TWO COMPLEX NUMBER USING FRIEND FUNCTION

 #include<iostream>

using namespace std;


class complex

{

int a,b;

public:

complex()

{

}

complex(int x,int y)

{

a=x;

b=y;

}

void display()

{

if(b>0)

{

cout<<a<<"+i"<<b<<endl;

}

else

{

cout<<a<<"-i"<<b<<endl;

}

}

friend void add(complex,complex);

};

void add(complex ob1, complex ob2)

{

complex ob3;

ob3.a=ob1.a-ob2.a;

ob3.b=ob1.b-ob2.b;

ob3.display();

}


int main()

{

complex ob1(2,-3),ob2(4,5);

add(ob1,ob2);

}

ADDITION OF TWO COMPLEX NUMBER USING FRIEND FUNCTION

 #include<iostream>

using namespace std;


class complex

{

int a,b;

public:

complex()

{

}

complex(int x,int y)

{

a=x;

b=y;

}

void display()

{

if(b>0)

{

cout<<a<<"+i"<<b<<endl;

}

else

{

cout<<a<<"-i"<<b<<endl;

}

}

friend void add(complex,complex);

};

void add(complex ob1, complex ob2)

{

complex ob3;

ob3.a=ob1.a+ob2.a;

ob3.b=ob1.b+ob2.b;

ob3.display();

}


int main()

{

complex ob1(2,-3),ob2(4,5);

add(ob1,ob2);

}

Tuesday, January 17, 2023

OPERATOR OVERLOADING IN C++

 #include<iostream>

using namespace std;
class complex
{
int r,i;
public:
complex()
{
r=0;
i=0;
}
complex( int a, int b)
{
r=a;
i=b;
}
complex operator+(complex);
void show();
};
complex complex::operator+(complex ob)
{
complex ob4;
ob4.r=r+ob.r;
ob4.i=i+ob.i;
return ob4;
}
void complex::show()
{
cout<<"\n"<<r;
cout<<"\n"<<i;
}
int main()
{
complex ob1(2,5);
complex ob2(10,20);
complex ob3;
ob3=ob1+ob2;
ob3.show();
return 0;
}

A physicist confesses that the electron is round and that Jesus rose from the dead. (Bengali)

একজন পদার্থবিজ্ঞানী স্বীকার করেন যে ইলেকট্রন গোলাকার এবং যিশু মৃতদের মধ্য থেকে পুনরুত্থিত হয়েছিলেন। আমার নিজের বিশ্বাস প্রমাণ দ্বারা প্রভাব...