Wednesday, October 6, 2010

PROGRAM USING IMPLEMENTATION OF TYPE CONVERSION

Ex No.2b PROGRAM USING IMPLEMENTATION OF TYPE CONVERSION
18-8-10

AIM:
To write a c++ program to perform type conversion from basic type to class type and class type to basic type.

ALGORTHIM:
Step 1: Start the program.
Step 2: Use constructor to convert basic type to class type.
Step 3: Use conversion function to convert class type to basic type.
Step 4: Display the result.
Step 5: Stop the program.

PROGRAM:
#include<iostream.h>
#include<conio.h>
class complex
{
int x,y;
double a,b;
public:
complex(int real,int imag)
{
x=real;y=imag;
cout&lt;&lt;"\n int to class type:"&lt;<x<<"+j"<<y;
}
complex(double real1,double imag1)
{
a=real1;b=imag1;
cout&lt;&lt;"\n class type to double:"&lt;<a<<"+j"<<b;
}
operator double()
{
cout&lt;&lt;"\n class type to double:"&lt;<a+b;
return 0;
}
};
void main()
{
clrscr();
complex c(3.5);
complex c1(5.5,6.3);
double num =c1;
getch();
}

RESULT:
Thus a c++ program for performing type conversion is wrritten,executed and output verified successfully.

Tuesday, October 5, 2010

PROGRAM USING OPERATOR OVERLOADING

Ex No.2a PROGRAM USING OPERATOR OVERLOADING
7-8-10

AIM:
To write a c++ program to perform arithmetic operations like addition, subraction, multiplication and division of two complex no.s using operator overloading,
ALGORTHIM:
Step 1:Start the program.
Step 2:In order to perform the basic arithmetic operation like addition, subraction, multiplication and division of two complex no.s using operator overloading function.
Step 3:Call the operator overloading function '+','-','*','/' operators to perform addition,subraction,multiplication and division of two complex no.s and display the results respectively.
Step 4:Stop the program.

PROGRAM:
#include<iostream.h>
#include<conio.h>
class complex
{
float x;float y;
public:
complex(){}
complex(float real,float imag)
{x=real;y=imag}
complex operator+(complex c);
complex operator-(complex c);
complex operator*(complex c2);
complex operator/(complex c2);
void display(void);
};
void complex::display(void)
{
cout&lt;<x<<"+j"<<y;
};
complex complex::operator+(complex c)
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return temp;
}
complex complex::operator-(complex c)
{
complex temp;
temp.x=x-c.x;
temp.y=y-c.y;
return temp;
}
complex complex::operator*(complex c2)
{
complex temp;
temp.x=x*2.x-y*c2.y;
temp.y=x*2.y+y*c2.x;
return temp;
}
complex complex::operator/(complex c2)
{
complex temp;
float d;
d=c2.x*c2.x+c2.y*c2.y;
temp.x=(x*c2.y+y*c2.y)/d;
temp.y=(y*c2.x-x*c2.y)/d;
return temp;
}
int main()
{
complex c1,c2,c3;
c1=complex(3.1,5.1);
c2=complex(6.2,7.1);
clrscr();
cout&lt;&lt;"c1:";c1.display();
cout&lt;&lt;"c2:";c2.display();
c3=c1+c2;cout&lt;&lt;"\n sum:"c3.display();
c3=c1-c2;cout&lt;&lt;"\n difference:";c3.display();
c3=c1*c2;cout&lt;&lt;"\n product:";c3.display();
c3=c1/c2;cout&lt;&lt;"\n division:";c3.display();
getch();
return 0;
}

RESULT:Thus a c++ program for performing arithmetic operation of two complex no. using operator overloading is executed and verified successfully.

PROGRAM USING FRIEND FUNCTION

Ex No.1b PROGRAM USING FRIEND FUNCTION
4-8-10

AIM:To add two complex numbers using c++.

ALGORTHIM:
Step 1: Start the program.
Step 2: The operation of sum is declared as friend function.
Step 3: Call the functions from the main functions.
Step 4: As the function has arguments, the value are given in main function itself.
Step 5: The sum of two complex no.is calculated.
Step 6: End the program.


PROGRAM:
#include<iostream.h>
#include<conio.h>.
class complex
{
float x; float y;
public:
void input(float real,float image)
{
x=read;y=image;
}
friend complex sum(complex,complex);
void show(complex);
};
complex sum(complex c1,complex c2)
{
complex c3;
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.y;
return c3;
}
void complex::show(complex c)
{
cout&lt;<c.x<<"+j"<<c.y;
}
int main()
{
clrscr();
complex A,B,C;
A.input(3.1,5.5);
B.input(2.7,1.2);
C=sum(A,B);
cout&lt;&lt;"\n A=";A.show(A);
cout&lt;&lt;"\n B=";B.show(B);
cout&lt;&lt;"\n C=";C.show(C);
getch();
return 0;
}


RESULT:
Thus the c++ program to find the sum of two complex number using friend function us executed and output is verified successfully.

DISPLAYING OF STUDENT DETAILS USING STATIC FUNCTION

Ex N0.:1c              DISPLAYING OF STUDENT DETAILS USING STATIC FUNCTIONS
4-8-10
AIM:
To display a no.of student details using static functions.
ALGORTHIM:
Step 1: Start the program.
Step 2: Get the no. of students from the user.
Step 3: Get the name and roll no.
Step 4: Enter the no. of subjects required.
Step 5: Get the marks.
Step 6: The total and average are displayed
Step 7: The step 3 to step 6 is repeated for the given no. of students.
Step 8: End the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class stud
{
int i,n,roll no.;
char a[20];
int marks[50],total;
float avg;
public:
int k;
int j;
static int count;
void getdetails()
{
cout<<”\nEnter the name and roll no,”;
cin>>a;
cin>>roll no.;
cout<<”\n Enter the number of subjects”;
cin>>n;
total=0;
for(i=0;i<n;i++)
{
cin>>marks[i];
cout<<endl;
total+=marks[i];
}
avg=(total n);
}
void display()
{
cout<<”STUDENT”<<cout<<endl;
cout<<”NAME”<<a<<endl<<”ROLL NO.”<<roll no<<endl<<endl;
for(i=0;i<n;i++)
{
cout<<”MARK”<<i+1<<”:”<<marks[i]<<endl;
}
cout<<”TOTAL”<<total;
cout<<”AVERAGE”<<avg;
}
static void showcount(void)
{
count++;
cout<<endl<<”      STUDENT     “<<count<<end;
}
};
int student::count;
void main()
{
clrscr();
student 5;
cout<<”\nEnter the number of students”;
cin>>s.j;
for(s.k=0;s.k<s.j;s.k++)
{
student::showcount();
s.getdetails();
s.display();
}
getch();
}

RESULT:
Thus a no. of student details are obtained using static members and the output is verified successfully.
Ex No.1                                  PROGRAM USING DEFAULT ARGUMENTS
28-7-10
   

AIM:
To write a program using c++ to find the amount for tax percentage using default arguments.

ALGORTHIM:
Step 1:Start the program.

Step 2:Read the amount to calculate the tax percent.

Step 3:Read the tax percent

Step 4: Calculate tax amount for default tax percent

Step 5:Display the value

Step 6:Calculate tax amount for given tax amount

Step 7:Display the value

Step 8:Stop the program


PROGRAM:
 
#include<iostream.h>
#include<conio.h>
class tax
{
int t;
public:
int cal(int a,int b=2)
{
t=(a*b)/100;
cout<<t<<endl;
return t;
}
};
void main();
{
int  x,y;
clrscr();
tax s;
cout<<”\n Enter the amount to calculate tax amount <<”endl;
cin>>x;
cout<<”\nEnter the percentage of tax for the given amount “<<endl;
cin>>y;
cout<<”\nTax amount for default percent 2”<<s.cal(x)<<endl;
cout<<”\nTax amount for given amount of the given percent”<<s.cal(x,y);
getch();
}



RESULT:
Thus the c++ program to calculate tax amount using default argument has been executed and the output is verified successfully.