Sie sind auf Seite 1von 6

/Program to add two complex numbers using friend function

#include<iostream>

using namespace std;

class complex

int real,imag;

public:

void set()

cout<<"enter real and imag part";

cin>>real>>imag;

friend complex sum(complex,complex);

void display();

};

void complex::display()

cout<<"the sum of complex num is"<<real<<"+i"<<imag;

complex sum(complex a,complex b)

complex t;

t.real=a.real+b.real;

t.imag=a.imag+b.imag;

return t;

int main()

{
complex a,b,c;

a.set();

b.set();

c=sum(a,b);

c.display();

return(0);

Using operator overloading

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
class complex
{
int i,r;
public:
void read()
{
cout<<"\nEnter Real Part:";
cin>>r;
cout<<"Enter Imaginary Part:";
cin>>i;
}
void display()
{
cout<<"\n= "<<r<<"+"<<i<<"i";
}
complex operator+(complex a2)
{
complex a;
a.r=r+a2.r;
a.i=i+a2.i;
return a;
}
complex operator-(complex a2)
{
complex a;
a.r=r-a2.r;
a.i=i-a2.i;
return a;
}
complex operator*(complex a2)
{
complex a;
a.r=(r*a2.r)-(i*a2.i);
a.i=(r*a2.i)+(i*a2.r);
return a;
}
complex operator/(complex a2)
{
complex a;
a.r=((r*a2.r)+(i*a2.i))/((a2.r*a2.r)+(a2.i*a2.i));
a.i=((i*a2.r)-(r*a2.i))/((a2.r*a2.r)+(a2.i*a2.i));
return a;
}
};
void main()
{
int ch;
clrscr();
complex a,b,c;
do
{
cout<<"\n1.Addition 2.Substraction";
cout<<" 3.Mulitplication 4.Division 5.Exit\n";
cout<<"\nEnter the choice :";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter The First Complex Number:";
a.read();
a.display();
cout<<"\nEnter The Second Complex Number:";
b.read();
b.display();
c=a+b;
c.display();
break;
case 2:
cout<<"\nEnter The First Complex Number:";
a.read();
a.display();
cout<<"\nEnter The Second Complex Number:";
b.read();
b.display();
c=b-a;
c.display();
break;
case 3:
cout<<"\nEnter The First Complex Number:";
a.read();
a.display();
cout<<"\nEnter The Second Complex Number:";
b.read();
b.display();
c=a*b;
c.display();
break;
case 4:
cout<<"\nEnter The First Complex Number:";
a.read();
a.display();
cout<<"\nEnter The Second Complex Number:";
b.read();
b.display();
c=a/b;
c.display();
break;
}
}while(ch!=5);
getch();
}

Finding the area of triangle using virtual functions

#include <iostream>
using namespace std;
class Shape {
protected:
double x, y;
public:
void set_dim(double i, double j=0) {
x = i;
y = j;
}
virtual void show_area(void) {
cout << "No area computation defined ";
cout << "for this class.\n";
}
} ;

class triangle : public Shape {


public:
void show_area(void) {
cout << "Triangle with height ";
cout << x << " and base " << y;
cout << " has an area of ";
cout << x * 0.5 * y << ".\n";
}
};

class square : public Shape {


public:
void show_area(void) {
cout << "Square with dimensions ";
cout << x << "x" << y;
cout << " has an area of ";
cout << x * y << ".\n";
}
};

class circle : public Shape {


public:
void show_area(void) {
cout << "Circle with radius ";
cout << x;
cout << " has an area of ";
cout << 3.14 * x * x;
}
} ;

main(void)
{
Shape *p;
triangle t;
square s;
circle c;

p = &t;
p->set_dim(10.0, 5.0);
p->show_area();

p = &s;
p->set_dim(10.0, 5.0);
p->show_area();

p = &c;
p->set_dim(9.0);
p->show_area();

return 0;
}

Das könnte Ihnen auch gefallen