Saturday, 13 January 2018

CPP-Slip12




Q.1) Write a C++ program to accept length and width of a rectangle. Calculate and display perimeter as well as area of a rectangle by using Inline function.  
#include<iostream.h>
#include<conio.h>
class rectangle
{
                float l,w;
                public:
                void getdata()
                {
                                cout<<"\nEnter length of rectangle : ";
                                cin>>l;
                                cout<<"\nEnter width of rectangle : ";
                                cin>>w;
                        }
                        inline void Peri()
                        {
                                                cout<<"\nPerimeter of rectangle = "<<2*(l+w);
                        }
                        inline void Area()
                        {
                                                cout<<"\nArea of rectangle = "<<l*w;
                        }
};
void main()
{
                        clrscr();
                        rectangle obj;
                        obj.getdata();
                        obj.Peri();
                        obj.Area();
                        getch();
}




Q.2)     Create a class MyString which contains a character pointer (using new operator). Write a    C++ program to overload following operators:    >    to compare length of two strings    !=           to check equality of two strings



#include<conio.h>
#include<iostream.h>
#include<string.h>
class Mystring
{
char *str;
int len;
public:
Mystring()
{}

Mystring(char s[])
{
len=strlen(s);
str=new char[len+1];
strcpy(str,s);
}

void display()
{
cout<<endl<<"string : "<<str;
}

void operator>(Mystring ob)
{
int len1,len2;
len1=strlen(str);
len2=strlen(ob.str);
if(len1>len2)
cout<<endl<<str<<" is max length ";
else
cout<<endl<<ob.str<<" is max length ";
}

void operator!=(Mystring ob)
{
if(strcmp(str,ob.str)==0)
cout<<endl<<"strings are same";
else
cout<<endl<<"strings are not same";
}

Mystring operator+(Mystring ob)
{
Mystring a;
int len1,len2;
len1=strlen(str);
len2=strlen(ob.str);
a.str=new char[len1+len2+1];
strcpy(a.str,str);
strcat(a.str,ob.str);
return a;
}

};

void main()
{
char s1[10],s2[20];
clrscr();
cout<<"\n Enter string : ";
cin>>s1;
cout<<"\n Enter 2nd string : ";
cin>>s2;
Mystring m1(s1),m2(s2),m3;
m1.display();
m2.display();
m1>m2;
m1!=m2;
m3=m1+m2;
m3.display();
getch();
}

Another way



#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
class string
{

char *name;
char *n;
int length;
public:
string()
{
length=0;
name = new char[length+1];
}


void get()
{
cout<<"\n Enter a name:\t";
cin>>n;
length = strlen(n);
name = new char[length+1];
strcpy(name,n);
}

void display()
{
cout<<"\t"<<name;
}

friend int operator<(string &a,string &b);
friend int operator!=(string &a,string &b);

};


int operator<(string &a,string &b)
{
if(a.length<b.length)
{
return 1;
}
else
{
return 0;
}
}

int operator!=(string &a,string &b)
{
if(a.length!=b.length)
return 1;
else
return 0;
}


void main()
{

string s1,s2,s3,s4;
clrscr();
s1.get();
s2.get();
           

if(s1!=s2)
{
cout<<"\n Both string length are not equal\t";
s1.display();
s2.display();
}else{
cout<<"\n Both string length are equal\t";
s1.display();
s2.display();
}
s3.get();
s4.get();

if(s3<s4)
{
s3.display();
cout<<" smaller than ";
s4.display();
}
else
{
s4.display();
cout<<" smaller than ";
s3.display();
}
getch();
}