OPERATOR OVERLOADING

What is Operator Overloading?

In programming, operator overloading, sometimes termed operator ad hoc polymorphism, is a specific case of polymorphism, where different operators have different implementations depending on their arguments. Operator overloading is generally defined by a programming language, a programmer, or both.

Program for Operator Overloading.

#include<iostream>
using namespace std;
class FLOAT
{
int i;
public:
FLOAT()
{
i=0;
}
FLOAT(float a)
{
i=a;
}

void display()
{
std::cout<<i;
}

friend FLOAT operator +(FLOAT,FLOAT);
};
FLOAT operator +(FLOAT a,FLOAT b)
{
FLOAT ans;
ans.i=a.i+b.i;
return ans;
}

int main()
{
FLOAT i3;
FLOAT i1(3);
cout<<“\n Number 1:”;
i1.display();
FLOAT i2(4);
cout<<“\n Number 2:”;
i2.display();
i3=i1+i2;
cout<<“\n Addition is:”;
i3.display();
}

CONSTRUCTOR

What is Constructor?

Constructors in C++ A constructor is a member function of a class which initializes objects of a class. In C++,Constructor is automatically called when object(instance of class) create.It is special member function of the class.

Program for constructor

#include<iostream>
using namespace std;
class Student
{
public:
char name;
int roll_no;
int marks;

//Default Constructor
Student();

void display_data()
{
cout<<“Values from default constructor are:”<<endl;
cout<<“Name:”<<name<<endl;
cout<<“ROll no:”<<roll_no<<endl;
cout<<“Marks:”<<marks<<endl;
}
};

Student::Student()
{
name=’A’;
roll_no=’1′;
marks=’1′;
}

int main()
{
Student s; //Invoke Default Constructor
s.display_data();

return 0;
}

Holi

Holi is an ancient Hindu religious festival which has become popular with non-Hindus in many parts of South Asia, as well as people of other communities outside Asia. In recent years the festival has spread to parts of Europe and North America as a spring celebration of love, frolic, and colours.

HAPPY HOLI.

Secant Method

The secant method is defined by the recurrence relationship.

x_{n}=x_{n-1}-f(x_{n-1}){\frac {x_{n-1}-x_{n-2}}{f(x_{n-1})-f(x_{n-2})}}={\frac {x_{n-2}f(x_{n-1})-x_{n-1}f(x_{n-2})}{f(x_{n-1})-f(x_{n-2})}}

As can be seen from the recurrence relation, the secant method requires two initial values, x0 and x1, which should ideally be chosen to lie close to the root.

Program for secant method.

#include #include
#include
float A,B,C,D,E,x0,x1,x2,ans;
float fun(float x)
{
return((A*x*x*x*x)+(B*x*x*x)+(C*x*x)+(D*x)+E);
}
void main()
{
printf(“Please Enter Value Of Coefficient\n”);
scanf(“%f %f %f %f %f”,&A,&B,&C,&D,&E);
printf(“\n\n(%0.2f)x^4+(%0.2f)x^3+(%0.2f)x^2+(%0.2f)x+(%0.2f)\n\n”,A,B,C,D,E);

printf(“\n\nplease enter a value of x0 and x1\n\n”);
scanf(“%f %f”,&x0,&x1);

x2=(x1-((x1-x0)/(fun(x1)-fun(x0)))*fun(x1));
do
{
x0=x1;
x1=x2;
ans=x2;
x2=(x1-((x1-x0)/(fun(x1)-fun(x0)))*fun(x1));
}while(fabs(ans-x2)>0.001);
printf(“\nfinal x= %f\n”,ans);
}

Banking Program

A simple Banking Program

#include<iostream>
using namespace std;
class bank_account
{
float balance,dipositv,withdrawv;int act_no;char name[10],acc_type[10];
public:
void assignvalue()
{
cout<<“\nEnter a customer name :\n”<<endl;
cin>>name;
cout<<“\nSelect account type\n”<<endl<<“\n 1.Saving \n 2.Current \n”;
cin>>acc_type;
cout<<“\nEnter account number: \n”;
cin>>act_no;
cout<<“\nenter a balance:\n”;
cin>>balance;
}
float diposit();
float withdraw1();

void display();
};
float bank_account::diposit()
{
cout<<“\nEnter a diposit money:\n”;
cin>>dipositv;
balance=balance+dipositv;
return balance;
}
float bank_account::withdraw1()
{
vik:
cout<<“\nEnter a withdraw money: \n”;
cin>>withdrawv;
if(withdrawv>balance)
{
cout<<“\ninsufficient balance \n”;
goto vik;
}
balance=balance-withdrawv;
return balance;
}
void bank_account::display()
{
cout<<“\ncustomer name\n”<<name;
cout<<“\nbalance is :\n”<<balance;
}
int main()
{
bank_account b[1];
for(int i=0;i<1;i++)
{
b[i].assignvalue();
b[i].withdraw1();
b[i].diposit();
b[i].display();
}
return 0;
}

Banking Program

Simple banking program in C.

for more projects click the link

#include<iostream>
using namespace std;
class bank_account
{
float balance,dipositv,withdrawv;int act_no;char name[10],acc_type[10];
public:
void assignvalue()
{
cout<<“\nEnter a customer name :\n”<<endl;
cin>>name;
cout<<“\nSelect account type\n”<<endl<<“\n 1.Saving \n 2.Current \n”;
cin>>acc_type;
cout<<“\nEnter account number: \n”;
cin>>act_no;
cout<<“\nenter a balance:\n”;
cin>>balance;
}
float diposit();
float withdraw1();

void display();
};
float bank_account::diposit()
{
cout<<“\nEnter a diposit money:\n”;
cin>>dipositv;
balance=balance+dipositv;
return balance;
}
float bank_account::withdraw1()
{
vik:
cout<<“\nEnter a withdraw money: \n”;
cin>>withdrawv;
if(withdrawv>balance)
{
cout<<“\ninsufficient balance \n”;
goto vik;
}
balance=balance-withdrawv;
return balance;
}
void bank_account::display()
{
cout<<“\ncustomer name\n”<<name;
cout<<“\nbalance is :\n”<<balance;
}
int main()
{
bank_account b[1];
for(int i=0;i<1;i++)
{
b[i].assignvalue();
b[i].withdraw1();
b[i].diposit();
b[i].display();
}
return 0;
}

Bisection Method

What is Bisection Method?

The method is applicable for numerically solving the equation f(x) = 0 for the real variable x, where f is a continuous functions defined on an interval [ab] and where f(a) and f(b) have opposite signs. In this case a and b are said to bracket a root since, by the intermediate value theorem, the continuous function f must have at least one root in the interval (ab).

At each step the method divides the interval in two by computing the midpoint c = (a+b) / 2 of the interval and the value of the function f(c) at that point. Unless c is itself a root (which is very unlikely, but possible) there are now only two possibilities: either f(a) and f(c) have opposite signs and bracket a root, or f(c) and f(b) have opposite signs and bracket a root. The method selects the sub interval that is guaranteed to be a bracket as the new interval to be used in the next step. In this way an interval that contains a zero of f is reduced in width by 50% at each step. The process is continued until the interval is sufficiently small.

PROGRAM FOR BISECTION METHOD.

#include<stdio.h>
#include<conio.h>
#include<math.h>

float A,B,C,D,E;
float fun(float x)
{
return((A*x*x*x*x)+(B*x*x*x)+(C*x*x)+(D*x)+E);
}
int main()
{
float a,b,c,pre;

printf(“\nPlease Enter Values Of Coefficient\n”);
scanf(“%f %f %f %f %f”,&A,&B,&C,&D,&E);
printf(“\n\n(%0.2f)x^4+(%0.2f)x^3+(%0.2f)x^2+(%0.2f)x+(%0.2f)\n\n”,A,B,C,D,E);

do
{
printf(“\n\nplease enter a value of a and b such a way f(a) and f(b) have opposite sign\n\n”);
scanf(“%f %f”,&a,&b);
} while(fun(a)*fun(b)>0);

c=(a*fun(b)-b*fun(a))/(fun(b)-fun(a));
do
{
pre=c;
if(fun(a)*fun(c)>0)
{
a=c;
}
else
{
b=c;
}
c=(a*fun(b)-b*fun(a))/(fun(b)-fun(a));

}while(fabs(pre-c)>0.001);

printf(“\nsolution is %f\n”,c);
getch();
}