C++ : program to concanate two string


#include<iostream>
#include<string.h>
#include<conio.h>
using namespace std;

class String
{
private:
    int length;
    char *str;
public:
    String()
    {
        length=0;
        str= new char[length+1];
    }
    String(char *s)
    {

        length=strlen(s);
        str=new char[length+1];
        strcpy(str,s);
    }
    friend String operator +(String &s1, String &s2);
    friend void show(String &s);
};

void show(String &s)
{
    cout<<s.str<<endl;
}

String operator +(String &s1, String &s2)
{
    String temp;
    temp.length=s1.length+s2.length;
    delete temp.str;
    temp.str=new char[temp.length+1];
    strcpy(temp.str,s1.str);
    strcat(temp.str,s2.str);
    return temp;
}

int main()
{
    String s1("Ram"), s2("Kumar");
    String s3;
    s3=s1+s2;
    cout<<"s3 is:";
    show(s3);
    getch();
    return 0;

}

C++ : comparing two distances using operator overloading


#include<iostream>
using namespace std;

class dist
{
private:
    int feet;
    float inches;
public:
    dist():feet(0),inches(0){}
    dist(int f, float in): feet(f),inches(in){}
    void getdist()
    {
        cout<<"feet=";cin>>feet;
        cout<<"inches=";cin>>inches;
    }
    friend bool operator <(dist d1, dist d2);
};

bool operator <(dist d1,dist d2)
{

    float dd1=d1.feet+d1.inches/12;
    float dd2=d2.feet+d2.inches/12;
    return dd1<dd2?true:false;
}

int main()
{

    dist d1(7,6.25);
    dist d2;
    d2.getdist();
    if(d1<d2)
        cout<<"d1 is smaller than d2"<<endl;
    else
        cout<<"d2 is smaller than d1"<<endl;
    return 0;
}

C++ : find the value of given base to the given power


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

using namespace std;

int main()
{
float y,a,b;
cout<<"Enter the base (a) = ";cin>>a;
cout<<"Enter the power (b) = ";cin>>b;
y=pow(a,b);
cout<<"\n"<<a<<" to the power "<<b<<" is "<<y;
getch();
return(0);

}

C++ : generate the number as


                123454321
  1234321
    12321
      121
        1

==================================================


#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
int i,j,k,l;

for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
cout<<j;
}
for(k=i-1;k>=1;k--)
{
cout<<k;
}
cout<<endl;
for(l=0;l<=5-i;l++)
{
cout<<" ";
}
}
getch();
return(0);
}

C++ : generate the number as


                123454321
  1234321
    12321
      121
        1

==================================================


#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
int i,j,k,l;

for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
cout<<j;
}
for(k=i-1;k>=1;k--)
{
cout<<k;
}
cout<<endl;
for(l=0;l<=5-i;l++)
{
cout<<" ";
}
}
getch();
return(0);
}

C++ : generate the number as


                123454321
  1234321
    12321
      121
        1

==================================================


#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
int i,j,k,l;

for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
cout<<j;
}
for(k=i-1;k>=1;k--)
{
cout<<k;
}
cout<<endl;
for(l=0;l<=5-i;l++)
{
cout<<" ";
}
}
getch();
return(0);
}

C++ : generate the number as


                123454321
  1234321
    12321
      121
        1

==================================================


#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
int i,j,k,l;

for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
cout<<j;
}
for(k=i-1;k>=1;k--)
{
cout<<k;
}
cout<<endl;
for(l=0;l<=5-i;l++)
{
cout<<" ";
}
}
getch();
return(0);
}