Wednesday 8 July 2015

Manipulation of Complex Numbers using Operator Overloading
using namespace std;
#include<iostream>
class complex
{
int real,img;
public:

void getdata()
{
cout<<"Enter real and imaginary part:\n";
cin>>real>>img;
}

void putdata()
{
if(img>0)
cout<<real<<"+"<<img<<"i";
else
cout<<real<<img<<"i";
}

complex operator+(complex );
complex operator-(complex );
complex operator*(complex );
complex operator-();
};

complex complex :: opertator+(complex c)
{
complex temp;
temp.real=real+c.real;
temp.img=img+c.img;
return temp;
}

complex complex :: opertator-(complex c)
{
complex temp;
temp.real=real-c.real;
temp.img=img-c.img;
return temp;
}

complex complex :: opertator*(complex c)
{
complex temp;
temp.real=real*c.real-img*c.img;
temp.img=img*c.real+real*c.img;
return temp;
}

complex complex :: opertator-()
{
complex temp;
temp.real=real;

temp.img=-img;
return temp;
}


void main()
{
complex a,b,sum,diff,mul,neg;
a.getdata();
b.getdata();
sum=a+b;
cout<<"\nThe Sum is: ";
sum.putdata();
diff=a-b;
cout<<"\nThe Subtraction is: ";
diff.putdata();
mul=a*b;
cout<<"\nThe Product is: ";
mul.putdata();
neg=-a;
cout<<"Negation is :";
neg.putdata();
}




OUTPUT

Enter real and imaginary part
1
2

Enter real and imaginary part
3
4
The Sum is: 4+6i
The Subtraction is: -2-2i
The Product is: -5+10i
Negation is : 1-2i




----------------------------------------------------------------------


COMPLEX NUMBER MANIPULATION

#include<iostream>
class complex
{
int real,img;
public:

void getdata()
{
cout<<"Enter real and imaginary part:\n";
cin>>real>>img;
}

void putdata()
{
if(img>0)
cout<<real<<"+"<<img<<"i";
else
cout<<real<<img<<"i";
}

complex add(complex );
complex sub(complex );
complex multiply(complex );
complex conjugate();
};

complex complex :: add (complex c)
{
complex temp;
temp.real=real+c.real;
temp.img=img+c.img;
return temp;
}

complex complex :: sub(complex c)
{
complex temp;
temp.real=real-c.real;
temp.img=img-c.img;
return temp;
}

complex complex :: multiply(complex c)
{
complex temp;
temp.real=real*c.real-img*c.img;
temp.img=img*c.real+real*c.img;
return temp;
}

complex complex :: conjugate()
{
complex temp;
temp.real=real;

temp.img=-img;
return temp;
}
void main()
{
complex a,b,sum,diff,mul,neg;
a.getdata();
b.getdata();
sum=a.add(b);
cout<<"\nThe Sum is: ";
sum.putdata();
diff=a.sub(b);
cout<<"\nThe Subtraction is: ";
diff.putdata();
mul=a.multiply(b);
cout<<"\nThe Product is: ";
mul.putdata();
neg=a.conjugate();
cout<<"Negation is :";
neg.putdata();
}

OUTPUT
Enter real and imaginary part
1
2
Enter real and imaginary part
3
4
The Sum is: 4+6i
The Subtraction is: -2-2i
The Product is: -5+10i
Negation is : 1-2i



---------------------------------------------------



MATRIX MANIPULATION

#include<iostream>
class matrix
{
int r,c,a[10][10];
public:
void getdata();
void putdata();
void multiply(matrix);
void add(matrix);
void sub(matrix);
};

void matrix::getdata()
{
int i,j;
cout<<"\nEnter No. of Rows and Columns:\n";
cin>>r>>c;
cout<<"\nEnter Matrix Elements:\n";
for(i=0;i<r;i++)
for(j=0;j<c;j++)
cin>>a[i][j];
}

void matrix::putdata()
{
int i,j;
cout<<"\nThe Matrix is:\n";
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cout<<a[i][j]<<"\t";
cout<<"\n";
}
}

void add(matrix m)
{
matrix temp;
temp.r=r;
temp.c=c;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
temp.a[i][j]=a[i][j]+m.a[i][j];
}
temp.putdata();
}

void sub(matrix m)
{
matrix temp;
temp.r=r;
temp.c=c;
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
temp.a[i][j]=a[i][j]-m.a[i][j];
}
temp.putdata();
}

void multiply(matrix m)
{
matrix temp;
temp.r=r;
temp.c=m.c;
for(int i=0;i<r;i++)
{
for(int j=0;j<m.c;j++)
{
temp.a[i][j]=0;
for(int k=0;k<m.c;k++)
temp.a[i][j]+=a[i][k]*m.a[k][j];
}
}
}
temp.putdata();
}

void main()
{
matrix a,b;
clrscr();
a.getdata();
b.getdata();
a.multiply(b);
a.add(b);
a.sub(b);
getch();
}






--------------------------------------------------------























CONSTRUCTORS

#include<iostream>
class time
{
int hr,min,sec;
public:
time();
time(int,int,int);
time(time &);
void tdisplay();
};


time::time()
{
hr=0;
min=0;
sec=0;
}

time::time(int a,int b,int c)
{
hr=a;
min=b;
sec=c;
}

time::time(time &t)
{
hr=t.hr;
min=t.min;
sec=t.sec;
}

void time::tdisplay()
{
cout<<"\nThe time is:"<<hr<<":"<<min<<":"<<sec;
}


void main()
{
cout<<"Enter Hr,Min and Sec:\n";
cin>>a>>b>>c;
time t1(a,b,c);
cout<<”Result of Parameterized Constructor \n”;
t1.tdisplay();
time t2;
cout<<”Result of Default Constructor \n”;
t2.tdisplay();
cout<<”Result of Copy Constructor \n”;

time t3(t1);
t3.tdisplay();
}

OUTPUT

Enter Hr,Min and Sec:
1
20
30
Result of Parameterized Constructor
The time is:1:20:30
Result of Default Constructor
The time is:0:0:0
Result of Copy Constructor
The time is:1:20:30













------------------------------------------------------------





DEFAULT ARGUMNETS


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

int power(double i=3,double j=2)
{
cout<<"\n"<<i<<"^"<<j<<"=";
int res=1;
for(int v=1; v<=j; v++)
res=res*I;
return res;
}

void main()
{
double num,pow;
clrscr();
cout<<"\n program to calculate power";

cout<<"\n CASE 1:when no argument are entered";
cout<<power()<<"\n";

cout<<"\n CASE 2:when argument for power not entered";
cout<<"\n Enter the number:";
cin>>num;
cout<<power(num)<<"\n";

cout<<"\n CASE 3:when argument and power are entered";
cin>>num>>pow;
cout<<power(2,3)<<"\n";
getch();
}


OUTPUT

program to calculate power
CASE 1:when no argument are entered
3^2=9

CASE 2:when argument for power not entered
Enter the number:2
2^2=4

CASE 3:when argument and power are entered
2
3
2^3=8

------------------------------------------------------------------

MULTIPLE INHERITANCE

#include<iostream>
#include<string.h>

class student
{
int rollno;
char *name,*address;
public:

student(){}

student(int a,char *b,char *c)
{
rollno=a;
name=new char[strlen(b)+1];
strcpy(name,b);
address=new char[strlen(c)+1];
strcpy(address,c);
}

void stud_display()
{
cout<<"\nRoll No.:"<<rollno;
cout<<"\nName:"<<name;
cout<<"\nAddress:"<<address;
}
};

class sportsperson
{
int exp;
char *namesports;
public:

sportsperson(){}

sportsperson(char *a,int b)
{
namesports=new char[strlen(a)+1];
strcpy(namesports,a);
exp=b;
}

void sport_display()
{
cout<<"\nName of the Sport:"<<namesports;
cout<<"\nExperience="<<exp;
}
};

class sportstudent:public student,public sportsperson
{
int points;
public:

sportstudent(int rno,char *name1,char *add,char *sportsname,int exp1,int
points1):student(rno,name1,add),sportsperson(sportsname,exp1)
{
points=points1;
}

sportstudent(){}

void display()
{
stud_display();
sport_display();
cout<<"\nPoints="<<points;
}
};

void main()
{
sportstudent ob(1,"Shiva","RMD Engineering College","Tennis",5,120);
ob.display();
}

OUTPUT
Roll No.:1
Name:Shiva
Address:RMD Engineering College
Name of the Sport:Tennis
Experience=5
Points=120




-------------------------------------------------------------------

STAIC Member and Static Member Function


#include<iostream>

class sample
{
int a;
static int c;
public:
void getdata();
void putdata();
void increment();
static void static_putdata();
};

void sample :: getdata()
{
cout<<"Enter two Nos.:\n";
cin>>a>>b;
}

void sample :: putdata()
{
cout<<"\nThe Result is:\n"<<a<<"\n"<<b<<"\n"<<c;
}

void sample :: increment()
{
a++;
b++;
c++;
}
void sample :: static_putdata()
{
cout<<"Static member c= “ <<c<<"\n";
}


int sample::c=1;

void main()
{
sample o1,o2;
o1.getdata();
o1.increment();
o1.putdata();
o1 :: static_putdata();
o2.getdata();
o2.increment();
o2.putdata();
o2 :: static_putdata();
}


OUTPUT

Enter the number :
10
Member a= 11
Static Member c=2

Enter the number :
20
Member a= 21
Static Member c=3

-----------------------------------------------------------

#include<iostream>
class vector;
class time
{
int hr,min,sec;
public:
time(){}
time(int,int,int);
operator int();
time(int);
void tdisplay();
friend vector;
};

class vector
{
int ele[3];
public:
void display();
vector(){}
vector(time);

};


time::time(int a,int b,int c)
{
hr=a;
min=b;
sec=c;
}

time::operator int()
{
int a=hr*60*60+min*60+sec;
return a;
}

time::time(int a)
{
int b;
hr=a/3600;
b=a%3600;
if(b>=60)
{
min=b/60;
sec=b%60;
}
else
{
min=0;
sec=b;
}
}

void time::tdisplay()
{
cout<<"\nThe time is:"<<hr<<":"<<min<<":"<<sec;
}


void vector::display()
{
cout<<"\nThe vector is:"<<ele[0]<<"i+"<<ele[1]<<"j+"<<ele[2]<<"k";
}

vector::vector(time t)
{
ele[0]=t.hr;
ele[1]=t.min;
ele[2]=t.sec;
}

void main()
{
int a,b,c;
cout<<"Enter Hr,Min and Sec:\n";
cin>>a>>b>>c;
time t1(a,b,c);
a=t1;
cout<<"The time in Seconds is:"<<a;
cout<<"\nEnter a time in Sec:";
cin>>b;
t1=b;
t1.tdisplay();
vector v1;
v1=t1;
v1.display();
}

OUTPUT

Enter Hr,Min and Sec:
1
20
30
The time in Seconds is:4830
Enter a time in Sec:4830

The time is:1:20:30
The vector is:1i+20j+30k


--------------------------------------------------------------

VIRTUAL FUNCTIONS

#include<iostream>
class shape
{
public:

virtual void area()
{
cout<<"\nArea not found";
}
};

class rectangle:public shape
{
int l,b;
public:

void area()
{
cout<<"Area of the rectangle="<<l*b<<"\n";
}
rectangle(){}

rectangle(int a,int c)
{
l=a;b=c;
}
};

class circle:public shape
{
int r;
};

void main()
{
shape *p;
int a,b;
cout<<"Enter two Numbers:\n";
cin>>a>>b;
rectangle ob1(a,b);
circle ob2;
cout<<"When an base pointer is pointing rectangle object \n";
p=&ob1;
p->area();
cout<<"When an base pointer is pointing circle object \n";

p=&ob2;
p->area();
}


OUTPUT
Enter two Numbers:

6
5
When an base pointer is pointing rectangle object
Area of the rectangle=30
When an base pointer is pointing circle object

Area not found

--------------------------------------------------------------------------