oops all programs (1)

37
// DEFAULT ARGUMENTS #include<iostream.h> #include<conio.h> void printline(char='_',int repeatcount=70); void main() { printline(); printline('/'); printline('*',40); printline('R',55); } void printline(char ch,int repeatcount) { clrscr(); int i; cout<<endl; for(i=0;i<repeatcount;i++) cout<<ch; getch(); }

Upload: gopaltirupur

Post on 05-Mar-2015

92 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Oops All Programs (1)

// DEFAULT ARGUMENTS

#include<iostream.h>

#include<conio.h>

void printline(char='_',int repeatcount=70);

void main()

{

printline();

printline('/');

printline('*',40);

printline('R',55);

}

void printline(char ch,int repeatcount)

{

clrscr();

int i;

cout<<endl;

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

cout<<ch;

getch();

}

Page 2: Oops All Programs (1)

// FRIEND FUNCTION

#include<iostream.h>

#include<conio.h>

class friendexample

{

private:

int a,b;

public:

void test()

{

a=100;

b=200;

}

friend int compute(friendexample f);

};

int compute(friendexample f)

{

return int(f.a+f.b)-5;

}

void main()

{

clrscr();

friendexample f1;

f1.test();

cout<<"the result is:"<<compute(f1);

getch();

}

Page 3: Oops All Programs (1)

//STATIC MEMBERS

#include<iostream.h>

#include<conio.h>

class example

{

private:

static int sum;

int x;

public:

example()

{

sum=sum+1;

x=sum;

}

static void show()

{

cout<<"\n result is"<<sum;

}

void number()

{ cout<<"\n The number is"<<x; }

};

int example::sum;

void main()

{

clrscr();

example e1;

example::show();

example e2,e3,e4;

example::show();

e1.number();

e2.number();

e3.number();

getch();

}

Page 4: Oops All Programs (1)

// STATIC DATA

#include<iostream.h>

#include<conio.h>

class item

{

static int count;

int num;

public:

void getdata(int a)

{ num=a;

count++;

cout<<"Number:"<<num<<"\n";

}

void showcount()

{

cout<<"count:"<<count<<"\n";

}

};

int item::count;

void main()

{

clrscr();

item a,b,c;

a.showcount();

b.showcount();

c.showcount();

a.getdata(20);

b.getdata(30);

c.getdata(40);

a.showcount();

b.showcount();

c.showcount();

getch();

}

Page 5: Oops All Programs (1)

// COMPLEX NUMBER – OPERATOR OVERLOADING

#include<iostream.h>

#include<conio.h>

class complex

{

private:

float real;

float imag;

public:

complex()

{

real=imag=0;

}

void getdata()

{

cin>>real;

cin>>imag;

}

complex operator+(complex c2);

void outdata(char *msg)

{

cout<<msg<<"("<<real;

cout<<","<<imag<<")"<<endl;

}

};

complex complex::operator+(complex c2)

{

complex temp;

temp.real=real+c2.real;

temp.imag=imag+c2.imag;

return(temp);

}

void main()

{

clrscr();

complex c1,c2,c3;

Page 6: Oops All Programs (1)

cout<<"ADDition of integer complex objects:"<<endl;

cout<<"Enter integer complex number c1.."<<endl;

c1.getdata();

cout<<"Enter integer complex number c2.."<<endl;

c2.getdata();

c3=c1+c2;

c3.outdata("c3=c1+c2:");

complex c4,c5,c6;

cout<<"Addition of float numbers..."<<endl;

cout<<"Enter the float complex number c4..."<<endl;

c4.getdata();

cout<<"Enter the float complex numbers c5..."<<endl;

c5.getdata();

c6=c4+c5;

c6.outdata("c6=c4+c5:");

getch();

}

Page 7: Oops All Programs (1)

// MATRIX CLASS with dynamic memory allocation and necessary methods.

#include<iostream.h>

#include<conio.h>

const int TRUE=1;

const int FALSE=0;

class matrix

{

private:

int row;

int col;

int **p;

public:

matrix()

{

row=col=0;

p=NULL;

}

matrix(int r,int c);

~matrix();

void read();

void show();

void add(matrix &a,matrix &b);

void sub(matrix &a,matrix &b);

};

matrix::matrix(int r,int c)

{

row=r;

col=c;

p=new int *[row];

for(int i=0;i<row;i++)

p[i]=new int[col];

}

matrix::~matrix()

{

for(int i=0;i<row;i++)

delete p[i];

Page 8: Oops All Programs (1)

delete p;

}

void matrix::add(matrix &a,matrix &b)

{

int i,j;

row=a.row;

col=b.col;

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

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

p[i][j]=a.p[i][j]+b.p[i][j];

}

void matrix::sub(matrix &a,matrix &b)

{

int i,j;

row=a.row;

col=b.col;

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

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

p[i][j]=a.p[i][j]-b.p[i][j];

}

void matrix::read()

{

int i,j;

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

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

{

cout<<"matrix["<<i<<","<<j<<"]";

cin>>p[i][j];

}

}

void matrix::show()

{

int i,j;

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

{

cout<<endl;

Page 9: Oops All Programs (1)

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

{

cout<<p[i][j]<<" ";

cout<<"\n";

}

}

}

void main()

{

int m,n,p,q;

cout<<"Enter the A matrix...."<<endl;

cin>>m;

cin>>n;

matrix a(m,n);

a.read();

cout<<"Enter the B matrix..."<<endl;

cin>>p;

cin>>q;

matrix b(p,q);

b.read();

cout<<"Matrix A is.....";

a.show();

cout<<endl<<"Matrix b is....";

b.show();

matrix c(m,n);

c.add(a,b);

cout<<endl<<"c=a+b..";

c.show();

matrix d(m,n);

d.add(a,b);

cout<<"d=a-b...";

d.show();

getch();

}

Page 10: Oops All Programs (1)

// OVERLOADING NEW AND DELETE

#include<iostream.h>

#include<conio.h>

int size;

class vector

{

private:

int *array;

public:

void *operator new(size_t s)

{

cout<<"\n My new operator overloading function is invoked \n";

vector *my_vector;

my_vector->array=new int[size];

}

void operator delete(void * vect)

{

cout<<"\n My delete operator overloading function is invoked \n";

vector *my_vect;

delete(int*)my_vect->array;

::delete vect;

}

void read()

{

for(int i=0;i<size;i++)

{

cout<<"["<<i<<"]=";

cin>>array[i];

}

}

int sum()

{

int sum=0;

for(int i=0;i<size;i++)

sum=sum+array[i];

return sum;

Page 11: Oops All Programs (1)

}

};

void main()

{

int k;

clrscr();

cout<<"OPERATOR OVERLOADING FUNCTION FOR NEW AND DELETE OPERATORS \n\n";

cout<<"\n Enter the number of elements \n vector object:";

cin>>k;

size=k;

vector *my_vector= new vector;

cout<<"\n Enter vector data....."<<endl;

my_vector->read();

cout<<"\n Sum of vector:"<<my_vector->sum();

delete my_vector;

getch();

}

Page 12: Oops All Programs (1)

// A template of linked-list class and its methods.

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

template<class t>

class LINKED_LIST

{ struct LIST

{

t data;

t info;

LIST*next;

};

LIST*head;

public:

LINKED_LIST()

{

head=NULL;

}

void insertf()

{

LIST*temp;

t item;

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

cin>>item;

cout<<endl;

temp=new LIST;

temp->next=NULL;

if(head==NULL)

head=temp;

else

temp->next=head;

temp->data=item;

head=temp;

}

void deletef()

{

Page 13: Oops All Programs (1)

LIST*temp;

if(head==NULL)

cout<<"\n No data is present:";

else

{

temp=head;

head=head->next;

cout<<"The deleted data is:"<<temp->data<<endl;

delete temp;

}

}

void display_list()

{

cout<<"\n\n Available list is \n:";

if(head==NULL)

cout<<"no data is present \n";

else

for(LIST*temp=head;temp!=NULL;temp=temp->next)

cout<<temp->data<<"->";

cout<<"NULL"<<endl;

}

};

int main()

{

int k;

clrscr();

cout<<" Enter datatype 1.int 2.float 3.char\n";

int ch=1;

cin>>k;

switch(k)

{

case 1:

LINKED_LIST<int>s1;

ch=1;

while((ch==1)||(ch==2)||(ch==3))

{

Page 14: Oops All Programs (1)

cout<<"\n\t 1.insert_front\n\t 2.delete_front\n\t 3.display_list\n\t press other than 1,2,3 to exit:";

cout<<"\n\t Enter your choice:";

cin>>ch;

switch(ch)

{

case 1:

s1.insertf();

break;

case 2:

s1.deletef();

break;

case 3:

s1.display_list();

break;

}

}

break;

case 2:

LINKED_LIST<float>s2;

ch=1;

while((ch==1)||(ch==2)||(ch==3))

{ cout<<"\n\t 1.insert_front\n\t 2.delete_front\n\t 3.display_list\n\t press other then 1,2,3 to exit:";

cout<<"\n\t Enter your choice:";

cin>>ch;

switch(ch)

{

case 1:

s2.insertf();

break;

case 2:

s2.deletef();

break;

case 3:

s2.display_list();

break;

}

Page 15: Oops All Programs (1)

}

break;

case 3:

LINKED_LIST<char>s3;

ch=1;

while((ch==1)||(ch==2)||(ch==3))

{ cout<<"\n\t 1.insert_front\n\t 2.delete_front\n\t 3.display_list\n\t press other than 1,2,3 to exit:";

cout<<"\n\t Enter your choice:";

cin>>ch;

switch(ch)

{

case 1:

s3.insertf();

break;

case 2:

s3.deletef();

break;

case 3:

s3.display_list();

break;

}

}

break;

default:

cout<<"Error input";

return 0;

}

}

Page 16: Oops All Programs (1)

// Bubble Sort

#include<iostream.h>

#include<conio.h>

template<class T>

class bub

{

T *v;

int s;

public:

bub(int x)

{

s=x;

v=new T[s];

}

void read();

void sort();

void display();

~bub()

{

delete v;

}

};

template<class T>

void bub<T>::read()

{

for(int i=0;i<s;i++)

{

cin>>v[i];

}

}

template<class T>

void bub<T>::display()

{

for(int i=0;i<s;i++)

{

cout<<v[i]<<"\t";

Page 17: Oops All Programs (1)

}

cout<<"\n";

}

template<class T>

void bub<T>::sort()

{

for(int i=0;i<s;i++)

{

for(int j=0;j<s-1;j++)

{

if(v[j]>v[j+1])

{

T t;

t=v[j];

v[j]=v[j+1];

v[j+1]=t;

}

}

}

}

void main()

{

clrscr();

int r;

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

cin>>r;

bub<int>I(r);

cout<<"\n Enter the integer elements:";

I.read();

I.sort();

I.display();

bub<float>I1(r);

cout<<"\n Enter the float elements:";

I1.read();

I1.sort();

Page 18: Oops All Programs (1)

I1.display();

bub<char>I2(r);

cout<<"\n Enter the character elements:";

I2.read();

I2.sort();

I2.display();

getch();

}

Page 19: Oops All Programs (1)

// Insertion Sort

#include<iostream.h>

#include<conio.h>

template<class T>

class insert

{

T*v;

int s;

public:

insert(int x)

{

s=x;

v=new T[s];

}

void read();

void sort();

void display();

~insert()

{

delete v;

}

};

template<class T>

void insert<T>::read()

{

for(int i=0;i<s;i++)

{

cin>>v[i];

}

}

template<class T>

void insert<T>::display()

{

for(int i=0;i<s;i++)

{

cout<<v[i]<<"\t";

Page 20: Oops All Programs (1)

}

cout<<"\n";

}

template<class T>

void insert<T>::sort()

{

for(int i=0;i<s;i++)

{

for(int j=0;j<s-1;j++)

if(v[j]>v[j+1])

{

T t;

t=v[j];

v[j]=v[j+1];

v[j+1]=t;

}

}

}

void main()

{

clrscr();

int r;

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

cin>>r;

insert<int>I(r);

cout<<"\n Enter the integer elements:";

I.read();

I.sort();

I.display();

insert<float>I1(r);

cout<<"\n Enter the float elements:";

I1.read();

I1.sort();

I1.display();

insert<char>I2(r);

cout<<"\n Enter the character elements:";

Page 21: Oops All Programs (1)

I2.read();

I2.sort();

I2.display();

getch();

}

Page 22: Oops All Programs (1)

// Merge Sort

#include <iostream.h>

#include<conio.h>

int a[50];

void merge(int,int,int);

void merge_sort(int low,int high)

{

int mid;

if(low<high)

{

mid=(low+high)/2;

merge_sort(low,mid);

merge_sort(mid+1,high);

merge(low,mid,high);

}

}

void merge(int low,int mid,int high)

{

int h,i,j,b[50],k;

h=low;

i=low;

j=mid+1;

while((h<=mid)&&(j<=high))

{

if(a[h]<=a[j])

{

b[i]=a[h];

h++;

}

else

{

b[i]=a[j];

j++;

}

i++;

}

Page 23: Oops All Programs (1)

if(h>mid)

{

for(k=j;k<=high;k++)

{

b[i]=a[k];

i++;

}

}

else

{

for(k=h;k<=mid;k++)

{

b[i]=a[k];

i++;

}

}

for(k=low;k<=high;k++) a[k]=b[k];

}

void main()

{

int num,i;

cout<<"**********************************************"<<endl;

cout<<" MERGE SORT PROGRAM "<<endl;

cout<<"***********************************************"<<endl;

cout<<endl<<endl;

cout<<"Please Enter THE NUMBER OF ELEMENTS you want to sort [THEN PRESS ENTER]:"<<endl;

cin>>num;

cout<<endl;

cout<<"Now, Please Enter the ( "<< num <<" ) numbers (ELEMENTS) [THEN PRESS ENTER]:"<<endl;

for(i=1;i<=num;i++)

{

cin>>a[i] ;

}

merge_sort(1,num);

cout<<endl;

cout<<"So, the sorted list (using MERGE SORT) will be :"<<endl;

Page 24: Oops All Programs (1)

cout<<endl<<endl;

for(i=1;i<=num;i++)

cout<<a[i]<<" ";

cout<<endl<<endl<<endl<<endl;

getch();

}

Exno:6

#include<iostream.h>

#include<conio.h>

int *a,NUM_ITEMS;

void main()

{

clrscr();

void enterdata();

void quicksort(int,int);

int i;

cout<<"\n\nEnter the number of items:";

cin>>NUM_ITEMS;

a=new int [NUM_ITEMS];

if(a==NULL)

cout<<"Memory allocation error";

cout<<"\nEnter the numbers:";

enterdata();

quicksort(0,NUM_ITEMS-1);

cout<<"Done with sort.\n";

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

cout<<"\n"<<a[i];

getch();

}

void enterdata()

{

int i;

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

cin>>a[i];

}

void quicksort(int lower,int upper)

Page 25: Oops All Programs (1)

{

int split(int,int),pivot;

if(upper>lower)

{

pivot=split(lower, upper);

quicksort(lower,pivot-1);

quicksort(pivot+1,upper);

}

}

int split(int lower,int upper)

{

int i,p,q,t;

p=lower+1;

q=upper;

i=a[lower];

while(q>=p)

{

while(a[p]<i)

p++;

while(a[q]>i)

q--;

if(q>p)

{

t=a[p];

a[p]=a[q];

a[q]=t;

}

}

t=a[lower];

a[lower]=a[q];

a[q]=t;

return q;

}

Page 26: Oops All Programs (1)

// Spaning Tree

#include<iostream.h>

#include<conio.h>

#define MAX 50

#define TRUE 1

#define FALSE 0

#define MAXINT 250

class node

{

public:

int no;

node() {}

node(int a)

{

no=a;

}

};

class arc

{

public:

int adj;

int weight;

arc(){}

arc(int a)

{

adj = a;

}

};

class graph

{

public:

node nodes[MAX];

arc arcs[MAX][MAX];

graph(int n)

Page 27: Oops All Programs (1)

{

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

{

nodes[i].no=0;

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

arcs[i][j].adj=FALSE;

}

}

void join(node n1, node n2, int w)

{

arcs[n1.no][n2.no].adj = w;

arcs[n2.no][n1.no].adj=w;

}

void displayadj(int n)

{

cout<<"\nThe adjacency matrix...\n";

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

{

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

cout<<"\t"<<arcs[i][j].adj;

cout<<endl;

}

cout<<endl;

}

//Applying PRIMS Algorithm

void shortpath(int n)

{

int lcost[20];

int clost[20],i,j,k,min;

for(i=2;i<=n;i++)

{

lcost[i]=arcs[1][i].adj;

clost[i]=1;

}

cout<<"Minimum cost spanning tree edges are:\n";

for(i=2;i<=n;++i)

Page 28: Oops All Programs (1)

{

min=lcost[2];

k=2;

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

if(lcost[j]<min)

{

min=lcost[j];

k=j;

}

cout<<"\n"<<k<<"<->"<<clost[k];

lcost[k]=MAXINT;

for(j=2;j<=n;++j)

if((arcs[k][j].adj<lcost[j])&&(lcost[j]<MAXINT))

{

lcost[j]=arcs[k][j].adj;

clost[j]=k;

}

}

}

};

int main()

{

int n;

cout<<"\nEnter total number of nodes...";

cin>>n;

graph g(n);

cout<<"\nAssigning number for each node...";

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

g.nodes[i].no = i;

char ch='y';

int w;

do

{

node a, b;

cout<<"Create path between the nodes..";

cout<<"\nEnter the source node...";

Page 29: Oops All Programs (1)

cin>> a.no;

cout<<"\nEnter the destination node...";

cin>>b.no;

cout<<"\nEnter the weight";

cin>>w;

g.join(a,b,w);

cout<<"\nWant to continue... [y]es [n]o";

cin>>ch;

}while(ch=='y');

g.displayadj(n);

g.shortpath(n);

cin>>n;

return 0;

}

Page 30: Oops All Programs (1)

// HIERARCHIAL CLASS

#include<iostream.h>

class square

{

private:

float side;

public:

square(float x)

{

side = x;

}

square () {}

void display()

{

cout<<"\n\tSide... "<<side<<endl;

}

float area()

{

return (side*side);

}

};

class rectangle

{

private:

float len;

float bre;

public:

rectangle(){}

rectangle(float x, float y)

{

len=x;

bre=y;

}

void display()

{

Page 31: Oops All Programs (1)

cout<<"\n\tLength... "<<len<<"\n\tBreadth... "<<bre<<endl;

}

float area()

{

return (len*bre);

}

};

class circle

{

private:

float radius;

public:

circle(){}

circle(float r)

{

radius=r;

}

void display()

{

cout<<"\n\tRadius..."<<radius<<endl;

}

float area()

{

return (radius*radius*3.14);

}

};

class triangle

{

private:

float b;

float h;

public:

triangle(){}

triangle(float x, float y)

{

b=x;

Page 32: Oops All Programs (1)

h=y;

}

void display()

{

cout<<"\n\tBreadth... "<<b<<"\n\tHeight... "<<h<<endl;

}

float area()

{

return (0.5*b*h);

}

};

template <class T>

class polygon

{

private:

T shape;

public:

polygon(T x)

{

shape = x;

}

void displayarea()

{

char type[20];

cout<<"\nThe shape is belongs to "<<type<<endl;

shape.display();

cout<<"\nThe area is "<<shape.area()<<endl;

}

};

void main()

{

square s(3.5);

rectangle r(3,5);

circle c(4.4);

Page 33: Oops All Programs (1)

triangle t(2.4, 5.4);

polygon <square> poly1(s);

polygon <rectangle> poly2(r);

polygon <circle> poly3(c);

polygon <triangle> poly4(t);

poly1.displayarea();

cout<<"\n=================\n";

poly2.displayarea() ;

cout<<"\n=================\n";

poly3.displayarea() ;

cout<<"\n=================\n";

poly4.displayarea ();

cout<<"\n=================\n";

}

Page 34: Oops All Programs (1)

// GENERATING COMPLEX NUMBER

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

int main()

{

clrscr();

ofstream myfile("complex.txt");

float real,real1,img,img1;

char op,op1;

cout<<"\n Enter A real part";

cin>>real;

cout<<"\n Enter img part";

cin>>img;

cout<<"\n Enter op";

cin>>op;

myfile<<real<<endl<<img<<endl<<op<<endl;

cout<<"\n Enter B real part";

cin>>real;

cout<<"\n Enter img part";

cin>>img;

cout<<"\n Enter op1";

cin>>op1;

myfile<<real<<endl<<img<<endl<<op1;

myfile.close();

return 0;

}

Page 35: Oops All Programs (1)

//ADDITION OF 2 COMPLEX NUMBERS

#include<iostream.h>

#include<conio.h>

#include<fstream.h>

void main()

{

ifstream fin("complex.txt");

float real,real1,img,img1;

char op,op1;

fin>>real;

fin>>img;

fin>>op;

fin>>real1;

fin>>img1;

fin>>op1;

cout<<"\n A:"<<real <<"+"<<img<<"\n";

cout<<"\n B:"<<real1<<op1<<img1<<"i\n\n";

cout<<real<<"+"<<img<<"i\t"<<op<<"\t"<<real1<<"+"<<img1<<"i";

switch(op)

{

case '+':

cout<<"\t=\t"<<real+real1<<"+"<<img+img1<<"i";

break;

case '-':

cout<<"\t=\t"<<real-real1<<"+"<<img-img1<<"i";

break;

case '*':

cout<<"\t=\t"<<(real*real1)-(img*img1)<<"+"<<(real*img1)+(real1*img)<<"i";

break;

case '/':

float qt=real*real1+img*img1;

cout<<"\t=\t"<<(real*real1+img*img1)/qt<<"+"<<(img*real1-real*img1)/qt<<"i";

} getch();

}