OOP/CPP class to add Polar coordinates

#include<iostream>
using namespace std;
#include<conio.h>
#include<math.h>
#include<iomanip>
class polar
{
  float r,a;
  public:
void setpolar(float i, float j){r=i; a=j;}
void showpolar()
{
  cout<<“Radius = “<<setprecision(4)<<r<<” Angle = “<<setprecision(4)<<a;
}
void getresult(){}
polar operator + (polar ob);
};
polar polar :: operator + (polar ob)
{
  float x1,x2,y1,y2,x,y,rad,angle;
  float angle1,angle2,ang;
  polar tempob;
  angle1 = (22 * a) /float(7 * 180);
  angle2 = (22 * ob.a)/ float (7 * 180);
  x1 = r*cos(angle1);
  y1 = r*sin(angle1);
  x2 = ob.r * cos(angle2);
  y2 = ob.r * sin(angle2);
  x = x1 + x2;
  y = y1 + y2;
  rad = sqrt((x*x)+(y*y));
  ang = atan(y/float(x));
  angle = (180*7*ang)/22;
  tempob.setpolar(rad,angle);
  return tempob;
}
int main()
{
   float r,a;
   int i;
   polar ob[2],sumob;
   cout<<“A Class for POLAR that determine polar coordinates\n”;
   cout<<“===========================================\n\n\n\n\n”;
   for(i=0;i<2;i++)
   {
     cout<<“\nEnter value for r : “<<i+1<< ” : “;
     cin>>r;
     cout<<“\nEnter value for a : “<<i+1<< ” : “;
     cin>>a;
     ob[i].setpolar(r,a);
   }
   cout<<“\n”;
   for(i=0;i<2;i++)
   {
     cout<<“\n\n\n\n”;
     cout<<i+1<<”   number polar coordinate : “;
     cout<<“\n\n”;
     ob[i].showpolar();
     cout<<“\n\n\n\n”;
   }
   cout<<“\n\n\n\nResult\n\n”;
   sumob = ob[0] + ob[1];
   sumob.showpolar();
   getch();
}

OOP/CPP program to demonstrate inheritance in area of shapes

#include<iostream>
using namespace std;
#include<conio.h>
class area_cl
{
    public:
double height;
double width;
};
class rectangle : public area_cl
{
    public:
rectangle(double h,double w){height=h;width=w;}
double area(){return height*width;}
};
class isosceles : public area_cl
{
  public:
isosceles(double h,double w){height=h;width=w;}
double area(){return 0.5*height*width;}
};
class cylinder : public area_cl
{
  public:
float r;
cylinder(double h,float x){height=h;r=x;}
double area(){return 2*3.141569*r*r + 2*3.141569*r*height;}
};
int main()
{
  cout<<“Calculate the area\n=====================\n\n\n”;
  float r;
  cout<<“\nEnter Radius = “;
  cin>>r;
  double h,w;
  cout<<“\nEnter Height = “;
  cin>>h;
  cout<<“\nEnter Width = “;
  cin>>w;
  rectangle  ob1(h,w);
  isosceles  ob2(h,w);
  cylinder   ob3(h,r);
  cout<<“\n\n\nArea  of rectangle = “<<ob1.area();
  cout<<“\nArea of isosceles  = “<<ob2.area();
  cout<<“\nArea of cylinder   = “<<ob3.area();
}

OOP/CPP Class for all Arithmetic Operator overloading

#include<iostream>
using namespace std;
class Float
{
  float f1,f2;
  public:
void set(float x,float y)
        {f1=x;f2=y;}
Float operator + (Float ob);
Float operator – (Float ob);
Float operator * (Float ob);
Float operator / (Float ob);
void display()
{
     cout<<“f1 = “<<f1<<”      “<<“f2 = “<<f2<<“\n”;
     }
};
Float Float :: operator + (Float ob)
{
  Float temp;
  temp.f1 = f1 + ob.f1;
  temp.f2 = f2 + ob.f2;
  return temp;
}
Float Float :: operator – (Float ob)
{
  Float temp;
  temp.f1 = f1 – ob.f1;
  temp.f2 = f2 – ob.f2;
  return temp;
}
Float Float :: operator * (Float ob)
{
  Float temp;
  temp.f1 = f1 * ob.f1;
  temp.f2 = f2 * ob.f2;
  return temp;
}
Float Float :: operator / (Float ob)
{
  Float temp;
  temp.f1 = f1 / ob.f1;
  temp.f2 = f2 / ob.f2;
  return temp;
}
main()
{
  Float ob1,ob2,ob3,ob4,ob5,ob6;
  ob1.set(30.5,40.6);
  ob2.set(20.8,30.4);
  ob3=ob1+ob2;
  ob4=ob1-ob2;
  ob5=ob1*ob2;
  ob6=ob1/ob2;
  ob1.display();
  ob2.display();
  cout<<“After Operator Overloading……….”<<“\n\n”;
  ob3.display();
  ob4.display();
  ob5.display();
  ob6.display();
}

OOP/CPP Complex class to demonstrate operator overloading

/*7. Create a class called COMPLEX that has two private data
called real and imaginary. Include member function
to add two complex objects using operator overloading.
*/
#include <iostream>
 using namespace std;
class complex
{ float real; // real part
float imag;// imaginary part
public:
complex( ) { }// constructor 1
complex(float x, float y)// constructor 2
{real=x; imag=y; }
complex operator+(complex);
void display(void);
};
complex complex::operator+(complex c)
{
complex temp;
temp.real = real + c.real;
temp.imag = imag+ c.imag;
return(temp);
}
void complex::display(void)
{
cout<< real<< “+ j” << imag << “\n”;
}
int main( )
{
complex C1, C2, C3;// invokes constructor 1
C1 = complex(2.5, 3.5);// invokes constructor 2
C2 = complex(1.6, 2.7);
C3 = C1 + C2;
cout << “C1 = “; C1.display( );
cout << “C2 = “; C2.display( );
cout << “C3 = “; C3.display( );
return 0;
}

OOP/CPP String class to add two string and initialize using constructors

/*14. Define a class string that could work as a user defined string type.
 Include constructors dynamically that will enable
 to input a string from keyboard and to initialize
 an object with a string constant at the time of creation.
 Also include a member function that adds two strings to make a third string.
Write a complete program to implement this class.
*/
#include <iostream>
using namespace std;
#include<string.h>
class String
{ char *name;
int length;
public:
String( )// constructor-1
{
length = 0;
name= new char[length + 1];
}
String(char *s)// constructor-2
{
length = strlen(s);
name = new char[length + 1];// one additional
// character for \0
strcpy(name, s);
}
void display()
{
    cout<<name<<endl;
}
void join(String &a, String &b);
};
void String::join(String &a, String &b)
{
length = a.length + b.length;
delete name;
name = new char[length + 1];// dynamic allocation
strcpy(name, a.name);
strcat(name, b.name);
};
int main ( )
{
char *first;
cout<<“Enter the first name: “;
cin>>first;
String name1(first), name2(” Louis “), name3(” Lagrange “), s1, s2;
s1.join(name1, name2);
s2.join(s1, name3);
name1.display();
name2.display();
name3.display();
s1.display();
s2.display();
      return 0;
}

OOP/CPP Bank class to deposit, withdraw and display balance of accounts

#include<iostream>
using namespace std;
class bank
{
 char d_n[45];
 int a_n;
 char t_a[45];
 int  m;
 public:
  void getdata();
  int deposit(int b,char a[],int c);
  int withdraw(int b,char a[],int c);
  void display();
};
void bank::getdata()
{
cout<<“\n\n Enter a depositor’s name = “;
cin>> d_n;
cout<<“\n Enter the account number = “;
cin>>a_n;
cout<<“\n Type of the account = “;
cin>>t_a;
cout<<“\n Enter amount in the account = “;
cin>>m;
}
int bank::deposit(int b, char a[],int c)
{
  char *q,*p;
    q=a;
    p=d_n;
 if(*q==*p && b==a_n)
  {
     m = m + c ;
     cout<<“\n\n Now, Your bank balance is = “<<m;
     return 1;
  }
 return 0;
}
int bank::withdraw(int b, char a[],int c)
{
  char *q,*p;
    q=a;
    p=d_n;
   if(*q==*p && b==a_n)
   {
if(c<=m)
{
  cout<<“\n\n Your check is acecpted. “;
  m=m-c;
cout<<“\n Now, \n  The amount of money remain in your account is = “<<m;
}
else
{
   cout<<“\n\n Your check is not acecpted. “;
   cout<<“\n Your amount in the account is = “<<m;
}
return 1;
    }
   return 0;
}
void bank::display()
{
 cout<<“\n Name = “<<d_n;
 cout<<“\n Account number = “<<a_n;
 cout<<“\n Type of account = “<<t_a;
 cout<<“\n The amount of money in the account = “<<m;
}
int main(void)
{
int n,i,b,c,d=0,p;
char a[45];
bank x[45];
cout<<“\n\n  How many depositor’s information do you want to enter = “;
cin>>n;
for(i=0;i<n;i++)
   {
cout<<“\n Information of depositor “<<i+1;
cout<<“\n——————————-“;
x[i].getdata();
   }
do{
  cout<<“\n\n You can do the following things :”;
  cout<<“\n\n [1]. Deposite an amount of money. “;
  cout<< “\n [2]. Withdraw an amount of money.”;
  cout<<“\n [3]. Display the name and the balance of the depositor.”;
  cout<<“\n [4]. Quit.”;
  cout<<“\n\n Enter your choose = “;
  cin>>p;
 switch(p)
  {
     case 1 :
cout<<“\n\n Enter the following information :”;
cout<<“\n\n Your name = “;
cin>>a;
cout<<“\n Account number = “;
cin>>b;
cout<<“\n The amount of money  = “;
cin>>c;
for(i=0;i<n;i++)
{
d= x[i].deposit(b,a,c);
if(d==1) break;
}
if(d==0)
{
  cout<<“\n\n You have enter incorrect name or accont number.”;
  cout<<“\n\n Please, enter the correct information.”;
}
break;
     case 2:
cout<<“\n\n Enter the following information :”;
cout<<“\n\n Your name = “;
cin>>a;
cout<<“\n Account number = “;
cin>>b;
cout<<“\n The amount of your check = “;
cin>>c;
for(i=0;i<n;i++)
   {
d=x[i].withdraw(b,a,c);
if (d==1)
  break;
    }
if(d==0)
{
cout<<“\n\n You have enter incorrect name or account number.”;
cout<<“\n\n Please, enter the correct information.”;
}
break;
   case 3:
cout<<“\n\n  The list of the depositors in the bank are shown below:”;
for(i=0;i<n;i++)
{
cout<<“\n\n Depositor #”<<i+1<<“\n”;
cout<<“—————“;
x[i].display();
}
break;
  case 4: break;
   default:
cout<<“\n\n Wrong choice.”;
cout<<“\n\n Please, choose any of the above option and try again.”;
break;
    }
 }  while(p!=4);
}

OOP/CPP Distance class to Convert feet & inches into meter & centimeter and vice versa

#include <iostream>
using namespace std;
#include<stdlib.h>
//1 inch= 2.54cm, 1 feet=30.48cm =12*2.54cm,1 cm=0.40 inches.
class DB;    // forward declaration
class DM
{
   int met,cm;
   public:
  void initialize(){met=0;cm=0;}
  void set_distance(int i,int j){met=i;cm=j;}
  void get_distance()
  {
    cout<<“\n\n\n\n\tIN METER CENTEMETER……\n\n\n”;
    cout<<“\n\tMETER = “<<met<<endl;
    cout<<“\n\tCENTIMETER = “<<cm;
  }
  friend DM addMet(DM dmob,DB dbob);
  friend DB addFeet(DM dmob,DB dbob);
};
class DB
{
   int feet,inch;
   public:
  void initialize(){feet=0;inch=0;}
  void set_distance(int i,int j){feet=i;inch=j;}
  void get_distance()
  {
    cout<<“\n\n\n\n\tIN FEET & INCH….\n\n\n”;
    cout<<“\n\tFEET = “<<feet<<endl;
    cout<<“\n\tINCH = “<<inch;
  }
  friend DM addMet(DM dmob,DB dbob);
  friend DB addFeet(DM dmob,DB dbob);
};
DM addMet(DM dmob,DB dbob)
{
  float met1,totinch;
  int cem1,totcem;
  totinch=(dbob.feet*12)+dbob.inch;
  totcem=totinch*2.54;
  met1=(dmob.cm+totcem)/100+dmob.met;
  cem1=(dmob.cm+totcem)%100;
  dmob.met=met1;
  dmob.cm=cem1;
  return dmob;
}
DB addFeet(DM dmob,DB dbob)
{
  float feet1,totcem;
  int inch1,totinch;
  totcem=(dmob.met*100)+dmob.cm;
  totinch=totcem * 0.40;
  feet1=(dbob.inch+totinch)/12+dbob.feet;
  inch1=(dbob.inch+totinch)%12;
  dbob.feet=feet1;
  dbob.inch=inch1;
  return dbob;
}
int main()
{
  DM dm_ob;
  DB db_ob;
  float meter,cem;
  float ft,inc;
  char chk;
  while(1)
   {
     cout<<“\tConvert feet & inches into meter & centimeter and vice versa\n”;
     cout<<“\t=============================================\n\n\n”;
     dm_ob.initialize();
     db_ob.initialize();
     cout<<“\n\tEnter distance in METER & CENTEMETER …. \n”;
     cout<<“\n\tMeter =  “;
     cin>>meter;
     cout<<“\n\tCentermeter =   “;
     cin>>cem;
     cout<<“\n\n\n\n\tEnter distance in feet & inch ….\n”;
     cout<<“\n\tFeet = “;
     cin>>ft;
     cout<<“\n\tInches = “;
     cin>>inc;
     dm_ob.set_distance(meter,cem);
     db_ob.set_distance(ft,inc);
     cout<<“\n\tPress ‘m’ to see result in METER & CENTEMETER \n\t”;
     cout<<“Otherwise press ‘f’ to see result in FEET & INCH “;
     cin>>chk;
     if(chk==’m’)
     {
       dm_ob = addMet(dm_ob,db_ob);
       dm_ob.get_distance();
     }
     if(chk==’f’)
     {
       db_ob = addFeet(dm_ob,db_ob);
       db_ob.get_distance();
     }
     cout<<“\n\n\n\n\tDo you want to EXIT ? [Y/N] : “;
     cin>>chk;
     if((chk==’y’)||(chk==’Y’))
     exit(0);
   }
}

OOP/CPP Vector class to create, add, display vector

/*Using class write a program that would be able to do the following task:
a) To create the vector.
b) To add the value of a two vector
c) To display the vector in the form (ai+bj+ck)
*/
#include <iostream>
using namespace std;
#include<stdlib.h>
class vector
{ float i,j,k;
  int n;
  public:
void create();
void show();
friend void add(vector,vector);
};
void vector::create()
{
    cout<<“Enter the value for i,j,k: “;
    cin>>i>>j>>k;
}
void vector::show()
{cout<<“The vector is: “<<i<<“i+”<<j<<“j+”<<k<<“k”<<endl;
}
void add(vector a,vector b)
{
    vector c;
    c.i=a.i+b.i;
    c.j=a.j+b.j;
    c.k=a.k+b.k;
    cout<<“After summation “;
    c.show();
}
int main()
{  vector p,q;
p.create();
p.show();
q.create();
q.show();
add(p,q);
}

OOP/CPP demonstrate how friend functions work

/*A C++ program to demonstrate how friend functions work
as a bridge between the classes.
Write a program to find the largest and smallest number
between two numbers of different classes.
*/
#include <iostream>
using namespace std;
class ABC; //forward declaration
class XYZ
{int x;
public:
void setvalue(int i) {x = i;}
friend void max(XYZ, ABC);
friend void min(XYZ, ABC);
};
class ABC
{int a;
public:
void setvalue(int i) {a = i;}
friend void max(XYZ, ABC);
friend void min(XYZ, ABC);
};
void max(XYZ m, ABC n) //definition of friend
{if(m.x >= n.a)
cout << “largest number is:”<<m.x<<endl;
else
cout << “largest number is:”<<n.a<<endl;
}
void min(XYZ m, ABC n) //definition of friend
{if(m.x <= n.a)
cout << “smallest number is:”<<m.x;
else
cout << “smallest number is:”<<n.a;
}
//————————————————————————–//
int main()
{
ABC abc;
abc.setvalue(10);
XYZ xyz;
xyz.setvalue(20);
max(xyz, abc);
min(xyz,abc);
}

OOP/CPP Complex class to add and subtract complex numbers

/* 3. Create a class called COMPLEX
that has two private data called real and imaginary.
Include member function input() to input
real & imaginary values, show() to display complex numbers.
Write a program to add and subtract two complex numbers.*/
#include <iostream>
using namespace std;
class complex // x+iy form
{float x; //real part
float y; //imaginary part
public:
void input (float real, float imag)
{x= real; y = imag;}
friend complex sum(complex, complex);
void subtract(complex, complex);
void show();
};
complex sum(complex c1, complex c2)
{complex c3; //objects c3 is created
c3.x = c1.x + c2.x;
c3.y = c1.y + c2.y;
return(c3); //returns object c3
}
void complex::subtract(complex c1, complex c2)
{x = c1.x – c2.x;
y = c1.y – c2.y;
}
void complex :: show()
{
cout << x << “+j”<< y << endl;
}

int main()
{
complex A,B,C,D;
A.input(3.1, 5.65);
B.input(2.75, 1.2);

C = sum(A, B);
D.subtract(A,B); //C = A + B
cout << “A = “; A.show();
cout << “B = “; B.show();
cout << “C = “; C.show();
cout << “D = “; D.show();

}