#include<iostream.h>
inline float mul(float x,float y)
{
return(x*y);
}
inline double div(double p,double q)
{
return(p/q);
}
int main( )
{
float a = 12.345;
float b = 9.82;
cout<<mul(a,b)<<endl;
cout<<div(a,b)<<endl;
return 0;
}
Output:
121.228
1.25713
Click here to read about Inline Functions
OOPc using C++
With this blog I want to share my knowledge of OOPs and C++. I Hope this can quench your thirst for learning.Any suggestions and comments are welcome.
Wednesday, August 4, 2010
Program 6 -Operator Overloading illustrated by Matrix Addition,Subtraction and Multiplication Program
The following program implements Function Overloading by doing addition,subtraction and multiplication of Matrices.
#include<iostream.h>
#include<iomanio.h>
const int MAXROW=3,MAXCOL=3;
struct matrix
{
int arr[MAXROW][MAXCOL];
};
matrix operator+(matrix a,matrix b);
matrix operator-(matrix a,matrix b);
matrix operator*(matrix a,matrix b);
void mat_print(matrix p);
void main( )
{
matrix a = {
1,2,3,
4,5,6,
7,8,9,
};
matrix b = {
1,2,3,
4,5,6,
7,8,9,
};
matrix c,d,e,f;
c=a+b;
d=a*b;
e=a+b*c;
f=a-b*c+d;
mat_print(c);
mat_print(d);
mat_print(e);
mat_print(f);
}
matrix operator+(matrix a,matrix b)
{
matrix c;
int i,j;
for(i=0;i<MAXROW;i++)
{
for(j=0;j<MAXROW;j++)
c.arr[i][j] = a.arr[i][j] + b.arr[i][j];
}
return c;
}
#include<iostream.h>
#include<iomanio.h>
const int MAXROW=3,MAXCOL=3;
struct matrix
{
int arr[MAXROW][MAXCOL];
};
matrix operator+(matrix a,matrix b);
matrix operator-(matrix a,matrix b);
matrix operator*(matrix a,matrix b);
void mat_print(matrix p);
void main( )
{
matrix a = {
1,2,3,
4,5,6,
7,8,9,
};
matrix b = {
1,2,3,
4,5,6,
7,8,9,
};
matrix c,d,e,f;
c=a+b;
d=a*b;
e=a+b*c;
f=a-b*c+d;
mat_print(c);
mat_print(d);
mat_print(e);
mat_print(f);
}
matrix operator+(matrix a,matrix b)
{
matrix c;
int i,j;
for(i=0;i<MAXROW;i++)
{
for(j=0;j<MAXROW;j++)
c.arr[i][j] = a.arr[i][j] + b.arr[i][j];
}
return c;
}
matrix operator-(matrix a,matrix b)
{
matrix c;
int i,j;
for(i=0;i<MAXROW;i++)
{
for(j=0;j<MAXROW;j++)
c.arr[i][j] = a.arr[i][j] - b.arr[i][j];
}
return c;
}
matrix operator*(matrix a,matrix b)
{
matrix c;
int i,j,k;
for(i=0;i<MAXROW;i++)
{
for(j=0;j<MAXROW;j++)
{
c.arr[i][j]=0;
for(k=0;k<MAXCOL;k++)
c.arr[i][j] + = a.arr[i][k] * b.arr[k][j];
}
}
return c;
}
void mat_print(matrix p)
{
int i,j;
cout<<endl<<endl;
for(i=0;i<MAXROW;i++)
{
cout<<endl;
for(j=0;j<MAXCOL;j++)
cout<<setw(5)<<p.arr[i][j];
}
}
Program 5- Program to demonstrate Function Overloading.
The program returns the absolute value of an argument.
#include<iostream.h>
void main( )
{
int i=-25,j;
long l=-100000L,m;
double d=-12.34,e;
int abs(int);
long abs(long);
double abs(double);
j=abs(i);
m=abs(l);
e=abs(d);
cout<<endl<<j<<endl<<m<<e;
}
int abs(int ii)
{
return(ii>0?ii:ii*-1);
}
long abs(long ll)
{
return(ll>0?ll:ll*-1);
}
double abs(double dd)
{
return(dd>0?dd:dd*-1);
}
Click here to read about Function Overloading
#include<iostream.h>
void main( )
{
int i=-25,j;
long l=-100000L,m;
double d=-12.34,e;
int abs(int);
long abs(long);
double abs(double);
j=abs(i);
m=abs(l);
e=abs(d);
cout<<endl<<j<<endl<<m<<e;
}
int abs(int ii)
{
return(ii>0?ii:ii*-1);
}
long abs(long ll)
{
return(ll>0?ll:ll*-1);
}
double abs(double dd)
{
return(dd>0?dd:dd*-1);
}
Click here to read about Function Overloading
Tuesday, August 3, 2010
Program 4- Program to switch between different cases.
This program takes in the user's choice as a screen input.
Depending on the user's choice, it switches between the different cases.
The appropriate message is then outputted using the 'cout' command.
#include <iostream.h>
#include <conio.h>
int main( )
{
clrscr( );
int choice;
cout << "1. Talk" << endl;
cout << "2. Eat" << endl;
cout << "3. Play" << endl;
cout << "4. Sleep" << endl;
cout << "Enter your choice : " << endl;
cin>>choice;
switch(choice)
{
case 1 : cout << "You chose to talk...talking too much is a bad habit." << endl;
break; case 2 : cout << "You chose to eat...eating healthy foodstuff is good." << endl;
break; case 3 : cout << "You chose to play...playing too much everyday is bad." << endl;
break; case 4 : cout << "You chose to sleep...sleeping enough is a good habit." << endl;
break; default : cout << "You did not choose anything...so exit this program." << endl;
}
getch();
}
Sample Input:3
Sample Output:
You chose to play...playing too much everyday is bad.Program 3- Program to find if the number is prime or composite.
This program takes in an integer num1 as a screen input from the user.
It then determines whether the integer is prime or composite.
It finally outputs the approriate message by writing to the 'cout' stream.
#include <iostream.h>
#include <conio.h>
#include <process.h>
void main()
{
clrscr();
int num1,x;
cout << "Enter an integer : " << endl;
cin>>num1;
for(x=2;x<num1;x++)
{
if(num1%x==0) {
cout << num1 << " is a composite number." << endl;
} else
{
cout << num1 << " is a prime number." << endl;
}
}
getch();
exit(0);
}
Sample Input:21
Sample Output:21 is a composite number.
Program 2 - Program to enter the sale value and print the agent's commission
This program takes in the total sale value as a screen input from the user.
The program then calculates the agent's commission with the help of the 'IF-ELSE' command as follows :
5% if the total sale value is less than or equal to Rs10000.
10% if the total sale value is less than or equal to Rs.25000.
20% if the total sale value is greater than Rs.25000. It then outputs the agent's commission using the 'cout' command.
#include <iostream.h>
#include <conio.h>
void main( )
{
clrscr( );
long int svalue;
float commission;
cout << "Enter the total sale value : " << endl;
cin>>svalue;
if(svalue<=10000)
{
commission=svalue*5/100;
cout << "For a total sale value of $" << svalue << ", ";
cout << "the agent's commission is $" << commission;
}
else if(svalue<=25000)
{
commission=svalue*10/100;
cout << "For a total sale value of $" << svalue << ", ";
cout << "the agent's commission is $" << commission;
}
else if(svalue>25000)
{
commission=svalue*20/100;
cout << "For a total sale value of $" << svalue << ", ";
cout << "the agent's commission is $" << commission;
}
getch();
} Sample Input:
26000
Sample Output:
For a total sale value of Rs.26000,the agent's commission is Rs.5200
Program 1: a Simple program depicting the use of Class
#include<iostream.h>
class rectangle
{
private:
int len,br;
public:
void getdata( )
{
cout<<<"Enter length and breadth";
cin>>len>>br;
}
void setdata(int l,int b)
{
len=l;
br=b;
}
void displaydata()
{
cout<<<"Length = "<
cout<<<"Breadth = "<
}
void area_peri()
{
int a,p;
a=len*br;
p=2*(len+br);
cout<<<"Area = "<
cout<<<"Perimeter ="<
}
}; //End of Class
void main( )
{
rectangle r1,r2,r3; //define three objects of class rectangle
r1.setdata(10,20); //set data in elements of the object
r1.display( ); //display the data set by setdata()
r1.area_peri( ); //calculate and print area and perimeter
r2.setdata(5,8);
r2.displaydata( );
r2.area_peri( );
r3.getdata( ); //receive data from keyboard
r3.displaydata( );
r3.area_peri( );
}
class rectangle
{
private:
int len,br;
public:
void getdata( )
{
cout<
cin>>len>>br;
}
void setdata(int l,int b)
{
len=l;
br=b;
}
void displaydata()
{
cout<
cout<
}
void area_peri()
{
int a,p;
a=len*br;
p=2*(len+br);
cout<
cout<
}
}; //End of Class
void main( )
{
rectangle r1,r2,r3; //define three objects of class rectangle
r1.setdata(10,20); //set data in elements of the object
r1.display( ); //display the data set by setdata()
r1.area_peri( ); //calculate and print area and perimeter
r2.setdata(5,8);
r2.displaydata( );
r2.area_peri( );
r3.getdata( ); //receive data from keyboard
r3.displaydata( );
r3.area_peri( );
}
Subscribe to:
Posts (Atom)