Sunday, November 13, 2011

CPP Practs......

1. Write a program to implement inline function.


#include

#include

inline int mul(int x,int y)

{

return(x*y);

}

inline int raiseto(int m,int n)

{

int num=1;

for(int i=1;i<=n;i++)

num*=m;

return(num);

}

void main()

{

int a,b,c,d;

clrscr();

cout<<"\n enter the value of a,b&d:-";

cin>>a>>b>>d;

c=mul(a,b);

cout<

cin>>a>>b;

cout<<"Before swaping a = "<

}

float area(float length, float width)

{

return(length * width);

}

float area(float radius)

{

return(3.142 * radius * radius);

}



5.

6. Inbuilt string class program.

#include

#include

#include

class string

{ int len;

char str1[15],str2[30];

public:

void read()

{ cout<<"Enter the first string:-";

cin>>str1;

cout<<"Enter the second string:-";

cin>>str2;

}

void compute()

{ char temp[30];

int f;

cout<<"First string lenth="<

cout<<"Second string lenth="<

strcpy(temp,str1);

cout<<"Reverse of first string="<

strcpy(temp,str2);

strcat(temp,str1);

cout<<"concatination of strings="<

f=strcmp(str1,str2);

if(f>0)

{cout<<"first string is greater than second string"<

}

if(f<0)

{cout<<"seccond string is greater than first string"<

}

if(f==0)

{cout<<"both string are equal"<

} }

};

void main()

{ clrscr();

string s;

s.read();

s.compute();

getch(); }



7. Write a program to implement inheritance using graphics figure.

#include

#include

#include

class line1

{

public:

void draw()

{

line(200,300,400,300);

}

void draw1()

{

line(200,300,300,200);

}

};

class Line:public line1

{

public:



void draw2()

{

line(300,200,400,300);

}

};

void main()

{

int gd,gm;

gd=DETECT;

initgraph(&gd,&gm,"C:\\BC45");

cout<<"Creating a line object.....\n";

Line l;

l.draw();

l.draw1();

l.draw2();

getch();

closegraph();

}

8. Write a program to implement this pointer.

#include

#include

class example

{

private :

int i;

public :

void setdata(int ii)

{

i = ii;



}

void showdata( example e)

{

cout<

cout << "B : " << b << "\n";

}

};

void main(void)

{

clrscr();

sample A;

A.print_data();

getch();

}

12. Write a program to implement parameterized Constructor with implicit call.

#include

#include

class sample

{

int a,b;

public :

sample(int x,int y)

{

a=x;

b =y;

}

void print_data()

{

cout << "A: " << a << "\n";

cout << "B : " << b << "\n";

}

};

void main(void)

{

clrscr();

sample A(10,20);

A.print_data();

getch();

}

13. Write a program to implement call by value.

#include

#include

void swap(int a,int b)

{

int t;

t=a;

a=b;

b=t;

cout<<"After swaping a = "<

cin>>a>>b;

cout<<"Before swaping a = "<

cin>>n;

cout<<"enter elements of array"<

for(i=0;i

{

cin>>a[i];

}



}

void insert()

{

int t;

cout<<"\nInsert number of element do you want to enter";

cin>>t;



cout<<"Enter the numbers"<

for(i=n;i

{

cin>>a[i];

}

n=n+t;

}

void del()

{

int p;

cout<<"\nEnter position of element of you want delete"<

cin>>p;

for(i=p-1;i

{

a[i]=a[i+1];

}

n=n-1;

}

void sort()

{

for(i=0;i

{

for(j=0;j

{

if(a[i]

{ Temp=a[i]; a[i]=a[j]; a[j]=Temp;

} }

} }

void display()

{



cout<<"\nsorted Order of array"<

for(i=0;i

{

cout<<" "<

} }

void rev()

{

cout<<"\nReverse Order of array"<

for(i=n-1;i>=0;i--)

{

cout<<" "<

}

}

};

void main()

{

class arrays a;

clrscr();

a.read();

a.sort();

a.display();

a.insert();

a.sort();

a.display();

a.del();

a.display();

a.rev();

getch();

}

15. Write a program to add two complex numbers using constructors.

#include

#include

#include

class complex

{

float r,i;

public: complex()

{}

complex(float real,float img)

{

r=real;

i=img;

}

complex operator+(complex);

void display();

};

complex complex :: operator+(complex c)

{

complex temp;

temp.r=r+c.r;

temp.i=i+c.i;

return (temp);

}



void complex :: display()

{ cout<<"\n\n The result of complex no is: "<

if(i>0)

cout<

else

cout<

}

void main()

{

float a,b,c,d;

clrscr();

complex p1,p2,p3;

cout<<"\n\n Enter 1st no: ";

cin>>a>>b;

p1=complex(a,b);

cout<<"\n\n Enter 2nd no: ";

cin>>c>>d;

p2=complex(c,d);

//complex p3;

p3=p1+p2;

p3.display();

getch();

}

























For marks 15 onlyyyyyyyyyy

1. Write a program to append, read, and write in binary mode.

#include

#include

#include

#include

class a

{

char name[20];

int sal,code;

public :

void get()

{

cout<<"Enter the name:\n";

cin>>name;

cout<<"enter the sallary:";

cin>>sal;

cout<<"enter the code:";

cin>>code;

}

void disp()

{

cout<

}

};

void main()

{

a ca;

fstream in;

in.open("xyz.txt",ios::ate
ios::out
ios::in
ios::binary);

in.seekg(0,ios::beg);

cout<<"content of item\n";

while(in.read((char *) & ca,sizeof ca))

{

ca.disp();

}

in.clear();

ca.get();

char ch;

cin.get(ch);

in.write((char *) & ca,sizeof ca);

in.seekg(0);

cout<<"\n\n display appended file\n";

while(in.read((char *) & ca, sizeof ca))

{

ca.disp();

}

in.close();

getch();

}



2. Write a program to copy one, file into another file.

#include

#include

void main () {

char ch;

ifstream infile;

ofstream outfile;

filebuf *inbuf, *outbuf;



infile.open ("xyz.txt", ios::out
ios::app
ios::binary);

outfile.open ("abc.txt");



inbuf=infile.rdbuf();

outbuf=outfile.rdbuf();



ch = inbuf->sgetc();

while ( ch != EOF)

{

outbuf->sputc (ch);

ch= inbuf->snextc();

}



outfile.close();

infile.close();



cout << "Copied from xyz.txt to abc.txt!" << endl << endl;



}



3. Write a program to implement exception handling.

#include

#include



void divide(int x,int y,int z)

{

cout<<"\n We are inside the function\n";

if((x-y)!=0)

{

int R= (z/(x-y));

cout<<"Result="<

}

else

{

throw(x-y);

}

}



void main()

{

try

{

cout<<"We are inside the try block\n";

divide(10,20,30);

divide(10,10,30);

}

catch(int i)

{

cout<<"Caught the exception\n";

}

}



4. Write a program to implement friend function

#include

class Point

{

friend void ChangePrivate( Point & );

public:

Point( void ) : m_i(0) {}

void PrintPrivate( void ){cout << m_i << endl; }



private:

int m_i;

};

void ChangePrivate ( Point &i ) { i.m_i++; }

void main()

{

Point sPoint;

sPoint.PrintPrivate();

ChangePrivate(sPoint);

sPoint.PrintPrivate();

}



5. Define a class string which can store a string. Write member function strlen, strcpy, strcmp, strcat, reverse & palindrome.



#include

#include

class str

{

char s[80];

public:void accept();

void display(str);

int strlen();

void strcp(str);

void strcat(str);

int strcmp(str);

void reverse();

int palind(str);

};



void str::accept()

{

cout<<"\n Enter the string : ";

cin>>s;

}



void str::display(str o)

{

cout<

}



int str::strlen()

{

int i,c=0;

for(i=0;s[i]!='\0';i++)

c++;



return c;

}



void str::strcp(str o)

{

int i;

for(i=0;s[i]!='\0';i++)

s[i]=o.s[i];

s[i]='\0';

}



void str::strcat(str o)

{

int i,j;

i=strlen();

for(j=0;s[j]!='\0';i++,j++)

s[i]=o.s[j];

s[i]='\0';

}



int str::strcmp(str o)

{

int i,j;

for(i=0,j=0;s[i]!='\0' && o.s[j]!='\0';i++,j++)

{

if(s[i]

return -1;

else if(s[i]>o.s[j])

return 1;

}

if(s[i]!='\0')

return 1;

else if(o.s[j]!='\0')

return -1;



else

return 0;

}



void str::reverse()

{

int i,j,l,temp;

l=strlen();

j=l;

for(i=0;i

{

temp=s[i];

s[i]=s[j-1];

s[j-1]=temp;

j=j-1;

}

}



int str::palind(str o)

{

str r;

r.strcp(o);

r.reverse();

if(r.strcmp(o)==0)

return 0;

else

return 1;

}











void main()

{

int x,y,z;

str s1,s2,s3,s4,s5,s6;

clrscr();



s1.accept();

s2.accept();

x=s1.strlen();

cout<<"\n Length of first string is : "<

cout<<"\n\n";

cout<<"\n\n\n---------------Copying the strings---------------";

cout<<"\n\n";

s1.strcp(s2);

cout<<"\n Copying second string to the first : ";

s1.display(s1);

cout<<"\n\n\n---------------Concatenating the string---------------";

cout<<"\n\n";

s4.accept();

s2.strcat(s4);

cout<<"\n Concatenating new string to the second string :";

s2.display(s2);

cout<<"\n\n\n---------------Comparing strings---------------";

cout<<"\n\n";

s5.accept();

y=s1.strcmp(s5);

if(y==0)

cout<<"\n Both are same.";

else

cout<<"\n Both are different.";

cout<<"\n\n\n---------------Reversing the string---------------";

cout<<"\n\n";

s3.accept();

s3.reverse();

cout<<"\n The reverse string is : ";

s3.display(s3);

cout<<"\n\n\n---------------String is palindrome or not---------------";

cout<<"\n\n";

s6.accept();

z=s6.palind(s6);

if(z==0)

cout<<"\n The string is Palindrome.";

else

cout<<"\n The string is not a Palindrome.";

getch();

}

6. Define class matrix having function to accept, display subtract & multiply.

#include

#include

class matrix

{

int a[3][3],b[3][3],c[3][3],i,j,k,sum,temp;

public: void accept()

{

cout<<"enter the first matrix row 3 and colum 3\n";

for(j=0;j<3;j++){

for(i=0;i<3;i++)

{

cout<<"enter the ["<

cin>>a[j][i];

} }

cout<<"enter the second matrix row 3 and colum 3\n";

for(j=0;j<3;j++){

for(i=0;i<3;i++)

{

cout<<"enter the ["<

cin>>b[j][i];

} } }



void display()

{

cout<<"first matrix\n";

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{ cout<

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{ cout<

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

for(k=0;k<3;k++)

{ temp=a[i][k]*b[k][j];

sum=sum+temp; }

c[i][j]=sum;

sum=0;

cout<

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

temp=a[i][j]-b[i][j];

cout<

for(i=0;i<5;i++)

{

cin>>str[i];

}

}

void sorting()

{

char temp[25];

for(i=0;i<5;i++)

{

for(j=0;j<5;j++)

{

if(strcmp(str[i],str[j])<0)

{

strcpy(temp,str[i]);

strcpy(str[i],str[j]);

strcpy(str[j],temp);

}

}

}

}

void display()

{

cout<<"\nsorted string"<

for(i=0;i<5;i++)

{

cout<

}

}



};

void main()

{

sort s;

clrscr();

s.read();

s.sorting();

s.display();

}



8. Write a program to implement operator overloading.

#include

#include

#include

class complex

{

float r,i;

public: complex()

{}

complex(float real,float img)

{

r=real;

i=img;

}

complex operator+(complex);

void display();

};

complex complex :: operator+(complex c)

{

complex temp;

temp.r=r+c.r;

temp.i=i+c.i;

return (temp);

}



void complex :: display()

{ cout<<"\n\n The result of complex no is: "<

if(i>0)

cout<

else

cout<

}

void main()

{

float a,b,c,d;

clrscr();

complex p1,p2,p3;

cout<<"\n\n Enter 1st no: ";

cin>>a>>b;

p1=complex(a,b);

cout<<"\n\n Enter 2nd no: ";

cin>>c>>d;

p2=complex(c,d);

//complex p3;

p3=p1+p2;

p3.display();

getch();

}

9. Write a program to implement template function.

#include

#include

template

void swap(T &x,T &y)

{

T temp=x;

x=y;

y= temp;

}

void main()

{

int m=10,n=20;

float a=30.30,b=40.40;

cout<<"m and n before swap:"<

}

~alpha()

{

cout<<"\n no of object destroyed:"<

count--;

}

};

void main()

{

clrscr();

alpha a1,a2,a3,a4;

{

cout<<"\n enter block:";

alpha a5;

}

{

cout<<"\n enter block:";

alpha a6;

}

getch();

}



12. Write a program to implement Polymorphism.

#include

float area (float, float);

float area (float);

void main( )

{

cout << "Area of a rectangle = " << area(1.5, 3.6);

cout << "\nArea of a circle = " << area(2.5) << "\n";

}

float area(float length, float width)

{

return(length * width);

}

float area(float radius)

{

return(3.142 * radius * radius);

}

13. Write a program to implement Static Binding.

#include

float area (float, float);

float area (float);

void main( )

{

cout << "Area of a rectangle = " << area(1.5, 3.6);

cout << "\nArea of a circle = " << area(2.5) << "\n";

}

float area(float length, float width)

{

return(length * width);

}

float area(float radius)

{

return(3.142 * radius * radius);

}

14. Write a program to implement dynamic Binding

#include

#include

class Base

{

public :

void display( )

{

cout<<"\n Display Base ";

}

virtual void show( )

{

cout<<"\n Show Base ";

}

};

class Derived : public Base

{

public :

void display( )

{

cout<<"\n Display Derived ";

}

void show( )

{

cout<<"\n Show Derived ";

}

};

void main( )

{

Base B;

Derived D;

Base *bptr;

cout<<"\n bptr points to Base ";

bptr = &B;

bptr -> display( );

bptr -> show( );

cout<<" \n\n bptr points to Derived ";

bptr = &D;

bptr -> display( );

bptr -> show( );

getch( );

}

15. Write a program to overload + operator for addition of two complex numbers.

#include

#include

#include

class complex

{

float r,i;

public: complex()

{}

complex(float real,float img)

{

r=real;

i=img;

}

complex operator+(complex);

void display();

};

complex complex :: operator+(complex c)

{

complex temp;

temp.r=r+c.r;

temp.i=i+c.i;

return (temp);

}



void complex :: display()

{ cout<<"\n\n The result of complex no is: "<

if(i>0)

cout<

else

cout<

}

void main()

{

float a,b,c,d;

clrscr();

complex p1,p2,p3;

cout<<"\n\n Enter 1st no: ";

cin>>a>>b;

p1=complex(a,b);

cout<<"\n\n Enter 2nd no: ";

cin>>c>>d;

p2=complex(c,d);

//complex p3;

p3=p1+p2;

p3.display();

getch();

}