Sei sulla pagina 1di 49

PROGRAM: /* Implementing class with static data member */

#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:"; cout<<count<<"\n"; } }; int item :: count; void main() { item a,b,c; clrscr(); a.showcount(); b.showcount(); c.showcount(); a.getdata(20); b.getdata(30); c.getdata(40); a.showcount(); b.showcount(); c.showcount(); getch(); }

OUTPUT:
Count:0 Count:0 Count:0 Number:20 Number:30 Number:40 Count:3 Count:3 Count:3

PROGRAM: /* Implementing class with static member function */


#include<iostream.h> #include<conio.h> class test { int code; static int count; public: void setcode() { code=++count; } void showcode() { cout<<"Object number:"<<code<<"\n"; } static void showcount(void) { cout<<"Count="<<count<<"\n"; } }; int test::count; int main() { test t1,t2; clrscr(); t1.setcode(); t2.setcode(); test::showcount(); test t3; t3.setcode(); test::showcount(); t1.showcode(); t2.showcode(); t3.showcode(); getch(); return(0); }

OUTPUT:
Count=2 Count=3 Object number:1 Object number:2 Object number:3

PROGRAM: /* Implementing class with friend function and default argument */


#include<iostream.h> #include<conio.h> class vector; class matrix { int m[3][3]; public: void getmatrix(void); void dismatrix(void); friend void multiply(matrix&,vector&); }; class vector { int v[10]; public: void getvector(int n=3); void disvector(void); friend void multiply(matrix&,vector&); }; void vector::getvector(int n) { int i; cout<<"\n Enter the elements for vector one by one:\n"; for(i=0;i<n;i++) cin>>v[i]; } void vector::disvector() { int i; cout<<"\n The vector elements are:\n"; for(i=0;i<3;i++) cout<<v[i]<<"\t"; } void matrix::getmatrix() { int i,j; cout<<"\n Enter the matrix:\n"; for(i=0;i<3;i++) for(j=0;j<3;j++) cin>>m[i][j]; }

void matrix::dismatrix() { int i,j; cout<<"\n Entered matrix is:\n"; for(i=0;i<3;i++) { for(j=0;j<3;j++) { cout<<m[i][j]<<"\t"; } cout<<"\n"; } } void multiply(matrix&m1,vector&v1) { int ans[3],i,j; cout<<"\n The resultant matrix is:\n"; for(i=0;i<3;i++) { ans[i]=0; for(j=0;j<3;j++) ans[i]=ans[i]+m1.m[i][j]*v1.v[i]; cout<<ans[i]<<"\t"; } } void main() { matrix m1; vector v1; clrscr(); m1.getmatrix(); m1.dismatrix(); v1.getvector(); v1.disvector(); multiply(m1,v1); getch(); }

OUTPUT:
Enter the matrix: 2 2 2 2 2 2 2 2 2 Entered matrix is: 2 2 2 2 2 2 2 2 2 Enter the elements of vector one by one: 2 2 2 The vector elements are: 2 2 2 The resultant matrix is: 12 12 12

PROGRAM:
/* Implementing complex number class with operator overloading */ #include<iostream.h> #include<conio.h> #include<iomanip.h> class complex { private: float real; float imag; public: complex() { real=imag=0.0; } complex(int r,int i) { real=r; imag=i; } complex(double r,double i) { real=r; imag=i; } friend istream& operator>>(istream&,complex&); friend ostream& operator<<(ostream&,complex&); complex operator+(complex); complex operator-(complex); complex operator*(complex); complex operator/(complex); friend double condou(complex t); }; double condou(complex t) { return t.real+t.imag; }

istream& operator>>(istream &in,complex &c) { cout<<"\nReal part:"; in>>c.real; cout<<"\nImag part:"; in>>c.imag; return in; } ostream& operator<<(ostream &out,complex &c) { if(c.imag<0) out<<c.real<<c.imag<<"i"; else out<<c.real<<"+"<<c.imag<<"i"; return out; } complex complex::operator+(complex c) { complex temp; temp.real=real+c.real; temp.imag=imag+c.imag; return temp; } complex complex::operator-(complex c) { complex temp; temp.real=real-c.real; temp.imag=imag-c.imag; return temp; } complex complex::operator*(complex c) { complex temp; temp.real=real*c.real-imag*c.imag; temp.imag=real*c.imag+imag*c.real; return temp; } complex complex::operator/(complex c) { complex temp; float qt; qt=c.real*c.real+c.imag*c.imag; temp.real=(real*c.real+imag*c.imag)/qt; temp.imag=(imag*c.real-real*c.imag)/qt; return temp; }

void main() { clrscr(); complex c1,c2,c3,c4(4,9),c5(3.23004,4.666304444); double t; t=condou(c5); cout<<"Enter complex number 1:"; cin>>c1; cout<<"\nEnter complex number 2:"; cin>>c2; cout<<"\nEntered complex numbers are:"; cout<<"\nComplex 1:"<<c1; cout<<"\nComplex 2:"<<c2; c3=c1+c2; cout<<"\n\nThe result of addtion is:"<<c3; c3=c1-c2; cout<<"\n\nThe result of subtraction is:"<<c3; c3=c1*c2; cout<<"\\n\nThe result of multiplication is:"<<c3; c3=c1/c2; cout<<"\n\nThe result of division is:"<<c3; cout<<"\n\nInteger->complex:"<<c4; cout<<"\n\nDouble->complex::"<<c5; cout<<"\n\nConverted to double:"<<t; getch(); }

OUTPUT:
Enter complex number 1: Real part: 5.0 Imag part: 6.0 Enter the complex number 2: Real part: 7.0 Imag part: 4.5 Entered complex numbers are: Complex 1: 5+6i Complex 2: 7+4.5i The result of addition is: 12+10.5i The result of subtraction is: -2+1.5i The result of multiplication is: 8+64.5i The result of division is: 0.895307+0.281588i Integer->complex: 4+9i Double->complex:3.23004+4.666305i Converted to double: 7.896345

PROGRAM: /* Implementing class with dynamic memory allocation */


#include<iostream.h> #include<conio.h> class matrix { int **m; int row, col; public: matrix() { row=col=0; m=NULL; } matrix(int r ,int c); ~matrix();s void getmatrix(); void showmatrix(); matrix(matrix &m2); matrix& operator=(matrix &m2); }; matrix::~matrix() { for(int i=0;i<row;i++) delete m[i]; delete m; } matrix::matrix(int r ,int c) { row = r; col = c; m = new int*[row]; for(int i=0;i<row;i++) m[i]=new int[col]; }

matrix::matrix(matrix &m2) { cout<<"\nCopy constructor invoked \n"; row = m2.row; col = m2.col; m = new int*[row]; for(int i=0;i<row;i++) m[i]=new int[col]; for(i=0;i<row;i++) for(int j=0;j<row;j++) m[i][j]=m2.m[i][j]; } matrix& matrix::operator=(matrix &m2) { cout<<"\nAssignment Operator Overloading \n"; row = m2.row; col = m2.col; m = new int*[row]; for(int i=0;i<row;i++) m[i]=new int[col]; for(i=0;i<row;i++) for(int j=0;j<row;j++) m[i][j]=m2.m[i][j]; return *this; } void matrix::getmatrix() { for(int i=0;i<row;i++) for(int j=0; j<col; j++) cin>>m[i][j]; } void matrix::showmatrix() { for(int i=0;i<row;i++) { for(int j=0;j<col;j++) cout<<"\t"<<m[i][j]; cout<<"\n"; } }

void main() { int r,c; clrscr(); cout<<"\nEnter rows and cols of the matrix \n"; cin>>r>>c; matrix m1(r,c); cout<<"\nEnter the matrix elements one by one "; m1.getmatrix(); cout<<"\nEntered matrix is \n"; m1.showmatrix(); matrix m2=m1; cout<<"\nResult of copy constructor is \n"; m2.showmatrix(); matrix m3; m3=m1; cout<<"\nResult of assignment operator overloading \n"; m3.showmatrix(); getch(); }

OUTPUT:

Enter the rows and cols of the matrix 2 2 Enter the matrix elements one by one 1 2 3 4 Entered matrix is 1 2 3 4 Copy constructor invoked Result of copy constructor is 1 2 3 4 Assignment Operator Overloading Result of assignment operator overloading 1 2 3 4

PROGRAM:

/* Overloading the new and delete operators to provide dynamic allocation of memory */
#include<iostream.h> #include<conio.h> const int SIZE=10; class vector { private: int *array; public: void *operator new(size_t) { vector *array; vector *my_vector; my_vector=::new vector; my_vector->array=new int[SIZE]; return my_vector; } void operator delete(void *vec) { vector *my_vect; my_vect=(vector*)vec; delete(int*)my_vect->array; ::delete vec; } void read(); int sum(); }; void vector::read() { for(int i=0;i<SIZE;i++) { cout<<"Vector["<<i<<"]="; cin>>array[i]; } }

int vector::sum() { int sum=0;

for(int i=0;i<SIZE;i++) sum=sum+array[i]; return sum; } void main() { clrscr(); vector *my_vector=new vector; cout<<"Enter vector data..."<<endl; my_vector->read(); cout<<"Sum of vector="<<my_vector->sum(); delete my_vector; getch(); }

OUTPUT:

Enter vector data vector [0]=1 vector [1]=2 vector [2]=3 vector[3]=4 vector[4]=5 vector[5]=6 vector[6]=7 vector[7]=8 vector[8]=9 vector[9]=10 Sum of vector=55

PROGRAM: /* To develop a template for linked list class and its methods */

#include<iostream.h> #include<conio.h> #include<stdlib.h> template<class type> struct node { type data; node *next; }; template<class type> class list { public: list(); int length(void)const; void makeempty(void); void insert(void); void remove(void); void display(void); private: node<type>*linklist; int count; }; template<class type> void list<type>::display(void) { node<type>*cur=linklist; cout<<"\n The Linked list is:\n"; while(cur!=NULL) { cout<<cur->data<<"->"; cur=cur->next; } cout<<"NULL\n"; } template<class type> list<type>::list() { count=0; linklist=NULL; } template<class type> int list<type>::length(void)const { return count; }

template<class type> void list<type>::makeempty(void) { node<type>*temp; while(linklist!=NULL) { temp=linklist; linklist=linklist->next; delete temp; } count=0; cout<<"\nNow list is empty"; } template<class type> void list<type>::insert(void) { node<type>*temp; type item; cout<<"\nEnter the item to be inserted:"; cin>>item; temp=new node<type>; temp->data=item; temp->next=linklist; linklist=temp; count++; } template<class type> void list<type>::remove(void) { node<type>*cur=linklist; type item; cout<<"\nEnter the item to remove:"; cin>>item; node<type>*temp; if(item==linklist->data) { temp=cur; linklist=linklist->next; } else { while(!((item==(cur->next)->data))) cur=cur->next; temp=cur->next; cur->next=(cur->next)->next; }

delete temp; count--; } void main() { int ch; list<int> list1; clrscr(); while(1) { cout<<"\nSingle linked list menu\n"; cout<<"\n-------------------------\n"; cout<<"\n1.INSERT\t 2.DELETE\t 3.EMPTY\t 4.EXIT\n"; cout<<"\n Enter your choice..."; cin>>ch; switch(ch) { case 1: list1.insert(); list1.display(); break; case 2: if(list1.length()>0) { list1.remove(); list1.display(); } else cout<<"\nList empty!!!"; break; case 3: list1.makeempty(); break; case 4: exit(0); default: cout<<"\nINVALID CHOICE"; } }}

OUTPUT:
Single linked list menu -----------------------------1.INSERT 2.DELETE 3.EMPTY

4.EXIT Enter your choice 1 Enter the item to be inserted:30 The linked list is 30->NULL Single linked list menu -----------------------------1.INSERT 2.DELETE 3.EMPTY 4.EXIT Enter your choice 1 Enter the item to be inserted:40 The linked list is 40->30->NULL Single linked list menu -----------------------------1.INSERT 2.DELETE 3.EMPTY 4.EXIT Enter your choice 1 Enter the item to be inserted: 50 The linked list is 50->40->30->NULL Single linked list menu -----------------------------1.INSERT 2.DELETE 3.EMPTY 4.EXIT Enter your choice 2 Enter the item to remove: 30 The linked list is 50->40->NULL

Single linked list menu -----------------------------1INSERT 2.DELETE 3.EMPTY 4.EXIT Enter your choice 3

Now list is empty Single linked list menu -----------------------------1.INSERT 2.DELETE 3.EMPTY 4.EXIT Enter your choice4

PROGRAM: /* Develop a template for bubble sort */


#include<iostream.h> #include<conio.h> template<class X> void bubble(X items,int count) {

register int a,b; float t; for(a=1;a<count;a++) for(b=count-1;b>=a;b--) if(items[b-1]>items[b]) { t=items[b-1]; items[b-1]=items[b]; items[b]=t; } } void main() { int iarray[7]={7,5,4,3,9,8,6}; double darray[5]={4.3,2.5,-0.9,100.2,3.0}; int i; clrscr(); cout<<"Here is an unsorted integer array:\n"<<endl; for(i=0;i<7;i++) cout<<iarray[i]<<" "<<endl; cout<<"Here is an unsorted double array:\n"<<endl; for(i=0;i<5;i++) cout<<darray[i]<<" "<<endl; bubble(iarray,7); bubble(darray,5); cout<<"Here is an sorted integer array:\n"<<endl; for(i=0;i<7;i++) cout<<iarray[i]<<" "<<endl; cout<<"Sorted double"<<endl; for(i=0;i<5;i++) cout<<darray[i]<<" "<<endl; getch(); }

OUTPUT:
Here is an unsorted integer array: 7 5 4 3 9 8 6

Here is an unsorted double array: 4.3 2.5 -0.9 100.2 3.0 Here is a sorted integer array: 3 4 5 6 7 8 9 Sorted double: -0.9 2.5 3 4.3 100.2

PROGRAM: /* Develop a template for insertion sort */


#include<iostream.h> #include<conio.h> #define ELEMENTS 6 template <class T> void insertion_sort(T x[],int length) { int key,i;

for(int j=1;j<length;j++) { key=x[j]; i=j-1; while(x[i]>key && i>=0) { x[i+1]=x[i]; i--; } x[i+1]=key; } } void main() { int iarray[7]={7,5,4,3,9,8,6}; double darray[5]={4.3,2.5,-0.9,100.2,3.0}; int i; clrscr(); cout<<"Here is an unsorted integer array:\n"<<endl; for(i=0;i<7;i++) cout<<iarray[i]<<" "<<endl; cout<<"Here is an unsorted double array:\n"<<endl; for(i=0;i<5;i++) cout<<darray[i]<<" "<<endl; insertion_sort(iarray,7); insertion_sort(darray,5); cout<<"Here is a sorted integer array:\n"<<endl; for(i=0;i<7;i++) cout<<iarray[i]<<" "<<endl; cout<<"Here is a sorted double array:\n"<<endl; for(i=0;i<5;i++) cout<<darray[i]<<""<<endl; getch(); }

OUTPUT:
Here is an unsorted integer array: 7 5 4 3 9 8 6 Here is an unsorted double array:

4.3 2.5 -0.9 100.2 3.0 Here is a sorted integer array: 3 4 5 6 7 8 9 Sorted double: -0.9 2.5 3 4.3 100.2

PROGRAM: /* Develop a program for merge sort */


#include<iostream.h> #include<conio.h> #include<iomanip.h> template <class t> class sort { t a[10]; public: void get(int);

void merge(int,int); void mergesort(int,int,int); void display(int); }; template <class t> void sort<t>::get(int n) { int i; cout<<"Enter the array of elements:\n"; for(i=1;i<=n;i++) cin>>a[i]; } template <class t> void sort<t>::display(int n) { int i; cout<<"\nThe sorted array is:\n"; for(i=1;i<=n;i++) cout<<a[i]<<"\t"<<setw(5); } template <class t> void sort<t>::merge(int low,int high) { int mid; if(low<high) { mid=(low+high)/2; merge(low,mid); merge(mid+1,high); mergesort(low,mid,high); } } template <class t> void sort<t>::mergesort(int low,int mid,int high) { t b[10]; int h,i,j,k; h=low; i=low; j=mid+1; while((h<=mid) && (j<=high)) { if(a[h]<=a[j]) { b[i]=a[h]; h=h+1;

} else { b[i]=a[j]; j=j+1; } i=i+1; } if(h>mid) { for(k=j;k<=high;k++) { b[i]=a[k]; i=i+1; } } else { for(k=h;k<=mid;k++) { b[i]=a[k]; i=i+1; } } for(k=low;k<=high;k++) a[k]=b[k]; }

void main() { int n; clrscr(); cout<<"\n\t\tMERGE SORT USING TEMPLATES"; cout<<"\n\t\t~~~~~ ~~~~ ~~~~~ ~~~~~~~~~\n"; sort<int>n1; sort<float>n2; cout<<"\nEnter the array size:"; cin>>n; n1.get(n); n1.merge(1,n); n1.display(n); n2.get(n);

n2.merge(1,n); n2.display(n); getch(); }

OUTPUT:
MERGE SORT USING TEMPLATES ~~~~~~ ~~~~ ~~~~~~ ~~~~~~~~~~ Enter the array size: 4 Enter the array of elements: 10 30 20 40 The sorted array is 10 20 30 40 Enter the array of elements: 34 56

23 98 The sorted array is 23 34 56

98

PROGRAM: /* Develop a template for quick sort */


#include<iostream.h> #include<conio.h> #include<iomanip.h> template <class T> inline void swap(T &v1,T &v2) { T temp=v2; v2=v1; v1=temp; } template<class T>

void quicksort(T *array,int hi,int lo=0) { while(hi>lo) { int i=lo; int j=hi; do { while(array[i]<array[lo] && i<j) i++; while(array[--j]>array[lo]) if(i<j) swap(array[i],array[j]); }while(i<j); swap(array[lo],array[j]); if(j-lo>hi-(j+1)) { quicksort(array,j-1,lo); lo=j+1; } else { quicksort(array,lo,j+1); lo=j+1; } } }

int main() { int n; clrscr(); cout<<"Enter the number of data you want to sort:"; cin>>n; int *arrs=new int[n+1]; clrscr(); cout<<"Enter any"<<n<<"datas"; for(int i=0;i<n;i++) cin>>arrs[i]; cout<<endl<<"Unsorted array is:"<<endl; for(i=0;i<n;i++) cout<<setw(8)<<arrs[i]; quicksort(arrs,n); cout<<endl<<"Sorted array is"<<endl; for(i=0;i<n;i++)

cout<<setw(8)<<arrs[i]; getch(); delete arrs; return 0; }

OUTPUT:
Enter the number of datas you want to sort:5 Enter any 5 datas : 56 90 98 32 14 Unsorted array is 56 90 98 32 14 Sorted array is 14 32 56 90 98

PROGRAM: /* Implementing stack using class */


#include<iostream.h> #include<conio.h> #define SIZE 20 class stack { int a[SIZE]; int tos; public: stack(); void push(int); int pop(); int isempty(); int isfull(); }; stack::stack()

{ tos=0; } int stack::isempty() { return (tos==0?1:0); } int stack::isfull() { return (tos==SIZE?1:0); } void stack::push(int i) { if(!isfull()) { cout<<"pushing"<<i<<endl; a[tos]=i; tos++; } else { cout<<"Stack overflow error!possible data loss!"; } }

int stack::pop() { if(!isempty()) { cout<<"popping"<<a[tos-1]<<endl; return(a[--tos]); } else { cerr<<"Stack is empty!what to pop!"; } return 0; } void reverse(stack s) { stack s2; while(!s.isempty()) { s2.push(s.pop()); }

cout<<"Reversed contents of the stack..."<<endl; while(!s2.isempty()) { cout<<s2.pop()<<endl; } } void main() { clrscr(); stack s; s.push(1); s.push(2); s.push(3); reverse(s); getch(); }

OUTPUT:
Pushing 1 Pushing 2 Pushing 3 Popping 3 Pushing 3 Popping 2 Pushing 2 Popping 1 Pushing 1 Reversed contents of stack Popping 1 1 Popping 2 2 Popping 3 3

PROGRAM: /* Implementation of queue */


#include<conio.h> #include<iostream.h> #include<process.h> #define MAX_SIZE 150 class queues { int front,rear; int queue[MAX_SIZE]; public: queues() { front=(-1); rear=(-1); } void insert_rear(int); void delete_front(); void display(); };

void queues::insert_rear(int item) { if((front==0 && rear==MAX_SIZE)||(front==(rear+1))) { cout<<"Queue is full!\n overflow"; getch(); exit(0); } else { if(front==(-1) && rear==(-1)) { rear=0; front=0; } else if(front!=0 && rear==MAX_SIZE) rear=0; else rear=rear+1; queue[rear]=item; } } void queues::delete_front() { if(front==(-1) && rear==(-1)) { cout<<"Queue is empty!\nUnderflow"; return; } else { if(front==rear) front=rear=(-1); else if(front==MAX_SIZE && rear==MAX_SIZE) front=0; else front=front+1; } cout<<"\nItem deleted"; getch(); } void queues::display() { int ptr; if(front==0 && rear==0)

{ cout<<"Queue is empty"; getch(); } cout<<"\nThe queue is\n"; if(front<=MAX_SIZE && rear<=front) { for(ptr=front;ptr<MAX_SIZE;ptr++); cout<<queue[ptr]<<"\n"; for(ptr=0;ptr<=rear;ptr++) cout<<queue[ptr]<<"\n"; } else for(ptr=front;ptr<=rear;ptr++) cout<<queue[ptr]<<" "; }

int main() { clrscr(); int length,i,element,choice; queues q1; while(1) { clrscr(); cout<<"1.Insert an item.\n2.Delete an item."; cout<<"\n3.Display elements.\n4.Exit."; cout<<"\n\nEnter your choice:"; cin>>choice; switch(choice) { case 1: cout<<"\nHow many elements are in the queue:\n"; cin>>length; cout<<"\nEnter"<< length <<"elements:\n"; for(i=0;i<length;i++) { cin>>element;q1.insert_rear(element); } q1.display();

getch(); break; case 2: q1.delete_front(); q1.display(); getch(); break; case 3: q1.display(); getch(); break; case 4: exit(0); default: cout<<"Please re-enter your choice:"; getch(); break; } } getch(); return(0); }

OUTPUT:
1.Insert an item 2.Delete an item 3.Display the elements 4.Exit Enter your choice: 1 How many elements are in the queue: 5 Enter 5 elements 10 20 30 40 50 The queue is 10 20 30 40 50 1.Insert an item 2.Delete an item 3.Display the elements

4.Exit Enter your choice: 2 Item deleted The queue is 20 30 40 50 1.Insert an item 2.Delete an item 3.Display the elements 4.Exit Enter your choice: 2 Item deleted The queue is 30 40 50

1.Insert an item 2.Delete an item 3.Display the elements 4.Exit Enter your choice:3 The queue is 30 40 50 1.Insert an item 2.Delete an item 3.Display the elements 4.Exit Enter your choice:4

PROGRAM: /* Implementation of graph */


#include<iostream.h> #include<conio.h> #define ROW 5 #define COL 5 #define infi 5000 class min { int graph[ROW][COL],nodes; public: void create(); min(); void tree(); }; min::min() { for(int i=0;i<ROW;i++) for(int j=0;j<COL;j++) graph[i][j]=0; } void min::create()

{ int i,j; cout<<"\nEnter total nodes"; cin>>nodes; cout<<"\nEnter adjacency matrix:"; for(i=0;i<nodes;i++) for(j=0;j<nodes;j++) cin>>graph[i][j]; for(i=0;i<nodes;i++) for(j=0;j<nodes;j++) if(graph[i][j]==0) graph[i][j]=infi; }

void min::tree() { int s[ROW],i,j,ne; int false=0,true=1,min,x,y; for(i=0;i<nodes;i++) s[i]=false; s[0]=true; ne=0; cout<<"The minimum spanning tree is:"; while(ne<nodes-1) { min=infi; for(i=0;i<nodes;i++) if(s[i]==true) for(j=0;j<nodes;j++) if(s[j]==false) if(min>graph[i][j]) { min=graph[i][j]; x=i; y=j; } s[y]=true; cout<<"\n"<<x+1<<"-->"<<y+1; ne=ne+1; }

} void main() { min m; clrscr(); cout<<"Algorithm for minimum spanning tree"; m.create(); m.tree(); getch(); }

OUTPUT:
Algorithm for minimum spanning tree Enter total nodes 5 Enter the adjacency matrix: 0 6 1 6 0 6 0 5 0 3 1 5 0 5 6 6 0 5 0 0 0 3 6 0 0 The minimum spanning tree is: 1- ->3 3- ->2 2- ->5 3- ->4

PROGRAM: /* Polymorphic objects */


#include<iostream.h> #include<conio.h> class shape { public: virtual void draw() { cout<<"Shape is drawn\n"; } }; class circle:public shape { public: void draw() { cout<<"Circle is drawn\n"; } }; class dot:public circle { public: void draw() {

cout<<"Dot is drawn\n"; } }; void main() { clrscr(); shape some,*ptrshape; circle rr; dot d1; some.draw(); ptrshape=&some; rr.draw(); cout<<"ptrshape is pointing to circle object"<<endl; ptrshape=&d1; d1.draw(); cout<<"ptrshape is pointing to dot object"<<endl; getch(); }

OUTPUT:
Shape is drawn Circle is drawn Ptrshape is pointing to circle object Dot is drawn Ptrshape is pointing to dot object

PROGRAM: /* File handling */


#include<iostream.h> #include<conio.h> #include<fstream.h> void main() { clrscr(); ofstream fout; fout.open("country.txt"); fout<<"\nUS\n"; fout<<"\nUK\n"; fout<<"\nSOUTHKOREA\n"; fout.close(); fout.open("capital.txt"); fout<<"\nWashington\n"; fout<<"\nLondon\n"; fout<<"\nSeoul\n"; fout.close(); const int n=80; char line[n]; ifstream fin; fin.open("country.txt"); cout<<endl<<"\nContents of country:"<<endl; while(!fin.eof()) { fin.getline(line,n);

cout<<line; } fin.close(); fin.open("c://capital.txt"); cout<<line; fin.close(); fin.open("capital.txt"); cout<<endl<<"Contents of capital:"<<endl; while(!fin.eof()) { fin.getline(line,n); cout<<line; } fin.close(); getch(); }

OUTPUT:
Contents of country: US Contents of capital: Washington UK London SOUTHKOREA Seoul

Potrebbero piacerti anche