You are on page 1of 35

// 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(); }

// 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(); }

//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(); }

showcount().getdata(30). getch(). b. b. a. int item::count. c.h> class item { static int count. c.getdata(20). c. public: void getdata(int a) { num=a.getdata(40).c. int num.showcount().// STATIC DATA #include<iostream. cout<<"Number:"<<num<<"\n".h> #include<conio. a.showcount(). } }.showcount(). a. b. item a.showcount(). } . void main() { clrscr().b.showcount(). count++. } void showcount() { cout<<"count:"<<count<<"\n".

complex c1.// COMPLEX NUMBER OPERATOR OVERLOADING #include<iostream. return(temp). } void main() { clrscr(). temp. cin>>imag.h> #include<conio.c3."<<imag<<")"<<endl.imag. } void getdata() { cin>>real. cout<<". void outdata(char *msg) { cout<<msg<<"("<<real.real=real+c2. complex complex::operator+(complex c2) { complex temp.c2.real. } }. . public: complex() { real=imag=0.h> class complex { private: float real.imag=imag+c2. float imag. temp. } complex operator+(complex c2).

"<<endl.. cout<<"Enter integer complex number c1. cout<<"Enter the float complex numbers c5... cout<<"Addition of float numbers.."<<endl..c6."<<endl.getdata(). c3. c1. c2."<<endl. c6.getdata().. cout<<"Enter the float complex number c4. c6=c4+c5.c5. getch(). complex c4.cout<<"ADDition of integer complex objects:"<<endl.getdata(). cout<<"Enter integer complex number c2..outdata("c6=c4+c5:")..outdata("c3=c1+c2:"). c5."<<endl.getdata(). } . c3=c1+c2. c4.

void sub(matrix &a.matrix &b). const int c).i++) delete p[i].matrix &b).h> #include<conio. class matrix { private: int row.// MATRIX CLASS with dynamic memory allocation and necessary methods. #include<iostream.i<row. ~matrix(). int **p. }. matrix::matrix(int r. . void read().h> const int TRUE=1. } matrix::~matrix() { for(int i=0. for(int i=0. p=NULL. p=new int *[row]. void show().int c) { row=r.i++) p[i]=new int[col]. int col. col=c. } matrix(int r.i<row. public: matrix() { row=col=0. void add(matrix &a.

j.matrix &b) { int i.i++) for(j=0.j++) p[i][j]=a. cin>>p[i][j].i++) for(j=0.j<col. .i<row. col=b. } void matrix::sub(matrix &a.i++) for(j=0.p[i][j]-b. for(i=0.matrix &b) { int i.j<col.delete p.j++) p[i][j]=a.i<row. col=b. for(i=0. } } void matrix::show() { int i.j.i++) { cout<<endl.j. row=a. for(i=0."<<j<<"]".j<col.j++) { cout<<"matrix["<<i<<". } void matrix::read() { int i.i<row.row.col.row.p[i][j]+b.p[i][j].p[i][j]. row=a.j. } void matrix::add(matrix &a.i<row. for(i=0.col.

."<<endl.. a..n).read(). cout<<"\n". c. cin>>q.p. matrix b(p.q.b). } . cout<<"Matrix A matrix a(m.. cin>>n.for(j=0..n).".show().show(). matrix c(m. b."<<endl.. cout<<"Enter the A matrix.add(a. a..". cout<<"Enter the B matrix. cin>>m. b. d.".. cin>>p...j++) { cout<<p[i][j]<<" ".n).add(a. getch(). c.". matrix d(m.j<col. d. cout<<"d=a-b. cout<<endl<<" cout<<endl<<"Matrix b is.n..b)..q). } } } void main() { int

i++) sum=sum+array[i]. class vector { private: int *array. for(int i=0. public: void *operator new(size_t s) { cout<<"\n My new operator overloading function is invoked \n".i++) { cout<<"["<<i<<"]=".i<size. vector *my_vect. .h> int size.i<size. } } int sum() { int sum=0. my_vector->array=new int[size]. } void operator delete(void * vect) { cout<<"\n My delete operator overloading function is invoked \n". } void read() { for(int i=0.// OVERLOADING NEW AND DELETE #include<iostream. return sum. vector *my_vector. delete(int*)my_vect->array. cin>>array[i].h> #include<conio. ::delete vect.

delete my_vector. vector *my_vector= new vector. clrscr().. getch(). cout<<"\n Enter the number of elements \n vector object:". size=k. my_vector->read(). cout<<"OPERATOR OVERLOADING FUNCTION FOR NEW AND DELETE OPERATORS \n\n"."<<endl... cout<<"\n Sum of vector:"<<my_vector->sum(). } . cout<<"\n Enter vector data.} }. cin>>k. void main() { int k..

else temp->next=head. temp->data=item.h> template<class t> class LINKED_LIST { struct LIST { t data. } void deletef() { . cout<<endl. }.h> #include<stdio. head=temp. temp->next=NULL. LIST*next. #include<iostream. temp=new LIST. public: LINKED_LIST() { head=NULL. cin>>item.// A template of linked -list class and its methods.h> #include<conio. cout<<"\n Enter the data:". LIST*head. t info. t item. if(head==NULL) head=temp. } void insertf() { LIST*temp.

clrscr(). cin>>k. cout<<"The deleted data is:"<<temp->data<<endl.temp=temp ->next) cout<<temp->data<<"->".temp!=NULL. } }. cout<<"NULL"<< 2.char\n". ch=1. while((ch==1)||(ch==2)||(ch==3)) { . else { temp=head. if(head==NULL) cout<<"\n No data is present:".LIST*temp. switch(k) { case 1: LINKED_LIST<int>s1. cout<<" Enter datatype 1. int ch=1. head=head->next.float 3. } } void display_list() { cout<<"\n\n Available list is \n:". delete temp. if(head==NULL) cout<<"no data is present \n". int main() { int k. else for(LIST*temp=head.

ch=1. break. switch(ch) { case 1: s1.deletef().display_list(). break.delete_front\n\t 3. break. break.insertf(). case 2: s2. case 2: s1.2.display_list(). break. case 2: LINKED_LIST<float>s2. switch(ch) { case 1: s2.3 to exit:".display_list\n\t press other than 1. cout<<"\n\t Enter your choice:". case 3: s1.insert_front\n\t 2. case 3: s2.2. break. cout<<"\n\t Enter your choice:". while((ch==1)||(ch==2)||(ch==3)) { cout<<"\n\t 1. cin>>ch.display_list\n\t press other then 1.cout<<"\n\t 1. cin>>ch.insertf().delete_front\n\t 3.deletef().3 to exit:". } } break. } .insert_front\n\t 2.

break. case 2: s3. } } .deletef(). return 0. } } break. break.insert_front\n\t 2. switch(ch) { case 1: s3. cout<<"\n\t Enter your choice:". case 3: LINKED_LIST<char>s3. while((ch==1)||(ch==2)||(ch==3)) { cout<<"\n\t 1.delete_front\n\t 3. case 3: s3. ch=1. break.display_list().insertf().} break.3 to exit:".2. cin>>ch.display_list\n\t press other than 1. default: cout<<"Error input".

void sort(). template<class T> void bub<T>::read() { for(int i=0.i++) { cin>>v[i]. public: bub(int x) { s=x. void display(). ~bub() { delete v. } void read().h> template<class T> class bub { T *v. } }.h> #include<conio. v=new T[s].// Bubble Sort #include<iostream. int s. } } template<class T> void bub<T>::display() { for(int i=0.i<s.i++) { cout<<v[i]<<"\t".i<s. .

i++) { for(int j=0.sort(). I.sort().read().j<s-1. cin>>r. } } } } void main() { clrscr(). int r. I1. t=v[j]. v[j]=v[j+1]. } template<class T> void bub<T>::sort() { for(int i=0. bub<int>I(r). I. v[j+1] { if(v[j]>v[j+1]) { T t. cout<<"\n\n Enter the size:". bub<float>I1(r). .display(). cout<<"\n Enter the float elements:".} cout<<"\n". I.i<s. cout<<"\n Enter the integer elements:". I1.

cout<<"\n Enter the character elements:". I2.I1.display().sort(). getch().read(). } . I2.display(). I2. bub<char>I2(r).

} void read(). } }. int s.i<s.// Insertion Sort #include<iostream. void sort(). .h> template<class T> class insert { T*v. public: insert(int x) { s=x. template<class T> void insert<T>::read() { for(int i=0.i<s.i++) { cout<<v[i]<<"\t". v=new T[s].i++) { cin>>v[i]. } } template<class T> void insert<T>::display() { for(int i=0.h> #include<conio. ~insert() { delete v. void display().

} } } void main() { clrscr(). .j<s-1. insert<char>I2(r).sort(). insert<int>I(r).sort().} cout<<"\n". t=v[j]. I1. cout<<"\n Enter the character elements:".display().read(). I. cout<<"\n Enter the integer elements:".display(). v[j]=v[j+1]. I.i++) { for(int j=0. } template<class T> void insert<T>::sort() { for(int i=0.i<s. cin>>r. v[j+1]=t. cout<<"\n\n Enter the size:". I. cout<<"\n Enter the float elements:". insert<float>I1(r).read().j++) if(v[j]>v[j+1]) { T t. int r. I1. I1.

getch(). I2. I2.display().sort().I2. } .read().

j=mid+1. void merge_sort(int if(low<high) { mid=(low+high)/2. merge_sort(low. j++.i. merge( } else { b[i]=a[j].j. void merge(int.h> int a[50].mid).// Merge Sort #include <iostream. } } void merge(int low.k. } i++.b[50]. h++. merge_sort(mid+1. while((h<=mid)&&(j<=high)) { if(a[h]<=a[j]) { b[i]=a[h].int high) { int h.h> #include<conio. h=low. } .high). mid.high).int high) { int mid.

for(i=1. cin>>num.if(h>mid) { for(k=j. cout<<"***********************************************"<<endl.k<=mid.k++) { b[i]=a[k]. cout<<"So.k++) a[k]=b[k]. } merge_sort(1.i.k<=high. } } for(k=low. } } else { for(k=h.k++) { b[i]=a[k]. i++. the sorted list (using MERGE SORT) will be :"<<endl. Please Enter the ( "<< num <<" ) numbers (ELEMENTS) [THEN PRESS ENTER]:"<<endl.i<=num.k<=high. cout<<"**********************************************"<<endl. } void main() { int num. cout<<endl.num).i++) { cin>>a[i] . i++. cout<<" MERGE SORT PROGRAM "<<endl. . cout<<endl. cout<<"Please Enter THE NUMBER OF ELEMENTS you want to sort [THEN PRESS ENTER]:"<<endl. cout<<endl<<endl. cout<<"Now.

h> int * upper) . enterdata(). cin>>NUM_ITEMS. void main() { clrscr(). cout<<"Done with sort. void enterdata().i<=num.i++) cin>>a[i]. if(a==NULL) cout<<"Memory allocation error". } void quicksort(int lower. for (i = 0. void quicksort(int.i<NUM_ITEMS. getch(). for(i=0. i <NUM_ITEMS. getch().i++) cout<<a[i]<<" ". a=new int [NUM_ITEMS]. for(i=1. cout<<endl<<endl<<endl<<endl. cout<<"\n\nEnter the number of items:".h> #include<conio. quicksort(0.NUM_ITEMS-1).\n". int i. } void enterdata() { int i.cout<<endl<<endl.NUM_ITEMS. i++) cout<<"\n"<<a[i]. } Exno:6 #include<iostream. cout<<"\nEnter the numbers:".int).

pivot-1). } . a[q]=t. while(a[q]>i) q--.t. } } int split(int lower.pivot. p=lower+1. i=a[lower].p. upper). if(q>p) { t=a[p].upper).int upper) { int i.q. a[q]=t. q=upper. quicksort(lower.{ int split(int. a[p]=a[q]. if(upper>lower) { pivot=split(lower. } } t=a[lower]. return q. while(q>=p) { while(a[p]<i) p++. a[lower]=a[q].int). quicksort(pivot+1.

class graph { public: node nodes[MAX]. } }. arc arcs[MAX][MAX].h> #include<conio.h> #define MAX 50 #define TRUE 1 #define FALSE 0 #define MAXINT 250 class node { public: int no. class arc { public: int adj. graph(int n) . int weight. node() {} node(int a) { no=a.// Spaning Tree #include<iostream. arc(){} arc(int a) { adj = a. } }.

clost[i]=1.adj=w.i<].no][n2.j<=n.. } cout<<"Minimum cost spanning tree edges are:\n". node n2.k. for(i=2.i++) { for(int j=1.j++) arcs[i][j].adj = w. arcs[n2.i. } //Applying PRIMS Algorithm void shortpath(int n) { int lcost[20].adj.adj. j++) cout<<"\t"<<arcs[i][j].i<=n. int clost[20].adj=FALSE.i++) { lcost[i]=arcs[1][i]. } } void join(node n1. for(int j=1.++i) . } void displayadj(int n) { cout<<"\nThe adjacency matrix. cout<<endl. for(i=2. } cout<<]..i<=n.\n".j.i<][n1. for(int i=1.{ for(int i=1. j<=n. int w) { arcs[ { nodes[i].

. for (int i=1. for(j=3. char ch='y'.." = i. cout<<"\nAssigning number for each node.. clost[j]=k. } } } }.++j) if(lcost[j]<min) { min=lcost[j]. cout<<"Create path between the nodes.j<=n.{ min=lcost[2]. graph g(n).j<=n. cout<<"\nEnter total number of nodes. k=2. .nodes[i].. i++) g. i<=n. for(j=2. lcost[k]=MAXINT. int main() { int n.".. do { node a. } cout<<"\n"<<k<<"<->"<<clost[k].. cout<<"\nEnter the source node.". k=j..adj<lcost[j])&&(lcost[j]<MAXINT)) { lcost[j]=arcs[k][j].++j) if((arcs[k][j]. int w. cin>>n.". b.

}while(ch=='y'). cin>>w. } .join(a. g.. return 0.. cout<<"\nEnter the destination node.w). cout<<"\nEnter the weight".cin>> a.. g. cout<<"\nWant to continue.".displayadj(n). g.b. cin>>n. cin>>b.. cin>> [y]es [n]o".shortpath(n).no.

} }. } square () {} void display() { cout<<"\n\tSide. public: square(float x) { side = x. float bre. bre=y.h> class square { private: float side. } void display() { . public: rectangle(){} rectangle(float x.. "<<side<<endl.. } float area() { return (side*side). float y) { len=x.// HIERARCHIAL CLASS #include<iostream. class rectangle { private: float len.

14). public: circle(){} circle(float r) { radius=r."<<radius<<endl. class circle { private: float radius.cout<<"\n\tLength. public: triangle(){} triangle(float x. } float area() { return (radius*radius*3. } }. float h. "<<len<<"\n\tBreadth. } }.... "<<bre<<endl.. } void display() { cout<<"\n\tRadius. } float area() { return (len*bre). class triangle { private: float b.. .. float y) { b=x.

h=y. shape..5). } float area() { return (0.. void main() { square s(3. public: polygon(T x) { shape = x. } }. } void displayarea() { char type[20].5*b*h). } }. . circle c(4.area()<<endl..4). } void display() { cout<<"\n\tBreadth. cout<<"\nThe area is "<<shape.5). "<<b<<"\n\tHeight. cout<<"\nThe shape is belongs to "<<type<<endl. rectangle r(3.display().. template <class T> class polygon { private: T shape. "<<h<<endl.

cout<<"\n=================\n".triangle t(2.4.displayarea() . polygon <square> poly1(s). poly1. polygon <rectangle> poly2(r).4). polygon <triangle> poly4(t). poly3. cout<<"\n=================\n". 5. cout<<"\n=================\n". } .displayarea() . poly4. cout<<"\n=================\n".displayarea(). poly2.displayarea (). polygon <circle> poly3(c).

cout<<"\n Enter img part". cin>>op.// GENERATING COMPLEX NUMBER #include<iostream. cout<<"\n Enter B real part". myfile<<real<<endl<<img<<endl<<op<<endl. ofstream myfile("complex. cout<<"\n Enter op1".real1. myfile<<real<<endl<<img<<endl<<op1. return 0.h> #include<conio. } .h> int main() { clrscr().txt"). myfile. float real.img1.op1. cin>>real. cout<<"\n Enter A real part".close(). cin>>img.img. cin>>img.h> #include<fstream. cout<<"\n Enter img part". cin>>real. char op. cout<<"\n Enter op". cin>>op1.

op1. fin>>img1. char op. cout<<"\n B:"<<real1<<op1<<img1<<"i\n\n". case '-': cout<<"\t=\t"<<real-real1<<"+"<<img-img1<<"i".real1. fin>>op1. fin>>real1. cout<<"\n A:"<<real <<"+"<<img<<"\n". } getch(). } .txt"). switch(op) { case '+': cout<<"\t=\t"<<real+real1<<"+"<<img+img1<<"i". cout<<"\t=\t"<<(real*real1+img*img1)/qt<<"+"<<(img*real1-real*img1)/qt<<"i".img1. case '*': cout<<"\t=\t"<<(real*real1)-(img*img1)<<"+"<<(real*img1)+(real1*img)<<"i". break.h> #include<conio.img. break. fin>>img. cout<<real<<"+"<<img<<"i\t"<<op<<"\t"<<real1<<"+"<<img1<<"i". fin>>real. float real.h> #include<fstream. break. fin>>op. case '/': float qt=real*real1+img*img1.//ADDITION OF 2 COMPLEX NUMBERS #include<iostream.h> void main() { ifstream fin("complex.