HOME PAGE www.pradheep.cjb.net (or) www.geocities.com/pradheepkumar OOPS LAB ~~~~ ~~~ // 1 - OBJECTS AND CLASSES - BANK ACCOUNT # include # include # include class bankaccount { private : char name[15]; char acctype; double balance; int acctno; public : bankaccount() { balance=0; } void deposit(double amount) { balance=balance+amount; cout<<"Balance = Rs."<>choice; switch(choice) { case 1 : cout<<"acct = "<>acct; cout<<"Enter amount : "; cin>>amount; person[acct-1].deposit(amount); break; case 3 : cout<<"Enter account number : "; cin>>acct; cout<<"Enter amount : "; cin>>amount; person[acct-1].withdraw(amount); break; case 4 : cout<<"Enter account number : "; cin>>acct; person[acct-1].display(); break; case 5 : exit(1); default : cout<<"Illegal choice - try again"; } getch(); clrscr(); }while(choice!=5); } void bankaccount::withdraw(double amount) { while((balance-amount)<500) { cout<<"Minimum balance in account should be Rs.500/-"<>amount; } balance=balance-amount; cout<<"Balance = Rs."<>name; cout<<"Enter amount : "; cin>>balance; while (balance<500) { cout<<"Minimum of Rs.500/-"; cout<>balance; }; cout<<"Enter account type : "; cin>>acctype; acctno=++ac; cout<<"Account number : "< # include class string { private : char str[20]; public : string () { str[0]='\0'; } string (char text[]) { strcpy(str,text); } void readstring() { cout<<"Enter string : "; cin>>str; } void putstring() { cout< # include class DB ; enum boolean {FALSE,TRUE}; class DM { private : float mtr, cm; public : DM () {mtr=cm=0.0;} void getinput () { cout<<"Enter in metre : "; cin>>mtr; cout<<"Enter centimetres : "; cin>>cm; } void display () { cout<>feet; cout<<"Enter inches : "; cin>>inch; } void display () { cout<>choice; switch (choice) { case 1 : a.getinput(); b.getinput(); flag=TRUE; break; case 2 : if (flag!=FALSE) { c=a+b; c.display(); } else cout<<"No input data"; break; case 3 : if (flag!=FALSE) { d=a+b; d.display(); } else cout<<"No input data"; break; case 4 : exit(0); default : cout<<"Illegal choice"< #include class rec { private : float xco, yco; public : rec() {xco=yco=0.0;} rec(float x, float y) { xco=x;yco=y; } void put () { cout<<"x = "<>rad; cout<<"Enter angle : "; cin>>ang; } }; void main () { polar p; rec r; p.get(); r=p; r.put(); } // 5 - TYPE CONVERSION IN DESTINATION // RECTANGULAR TO POLAR #include #include class rec; class polar { private : float rad, ang; public : polar() { rad=ang=0.0;} void get () { cout<<"Enter radians : "; cin>>rad; cout<<"Enter angle : "; cin>>ang; } float getrad() { return (rad);} float getang() { return (ang);} }; class rec { private : float xco, yco; public : rec() {xco=yco=0.0;} void put () { cout<<"x = "<>price; } void putdata() { cout<<"\nTitle : "<>page; } void putdata() { publication::putdata(); cout<<"\nPages : "<>time; } void putdata() { publication::putdata(); cout<<"\nTime : "<>ch; switch (ch) { case 1 : books[i].getdata(); ++i; break; case 2 : tapes[j].getdata(); ++j; break; case 3 : if (i==0) cout<<"List is empty"; else for (temp=0;temp # include class polar { private : float r, ang; public : polar () { r=ang=0.0; } polar (float x, float y) { r=x; ang=y; } void getdata() { cout<<"Enter r : "; cin>>r; cout<<"Enter angle(in degree) : "; cin>>ang; } void display() { cout<<"r = "< # include struct node { int data; node *link; }; class list { private : node *first; public : list () {first=NULL;} void insert (int element); void delet(int element); void display (); }; void list :: insert (int element) { int flag=0; node *temp; node *newnode=new node; newnode->data=element; newnode->link=NULL; if (first==NULL) first=newnode; else { if (elementdata) { newnode->link=first; first=newnode; } else { temp=first; while (temp->link!=NULL) { if (temp->link->datalink; else { newnode->link=temp->link; temp->link=newnode; flag=1; break; } } if (!flag) temp->link=newnode; } } } void list :: delet(int element) { int flag=0; node *temp; temp=first; if (!temp) cout<<"List is empty\n"; else if (temp->data==element) { first=temp->link; free(temp); cout<<"List after deletion..\n"; } else { while (temp->link!=NULL) { if (temp->link->data==element) { temp->link=temp->link->link; flag=1; cout<<"List after deletion...\n"; break; } temp=temp->link; } if (!flag) cout<<"Element not found"; } } void list :: display () { node *temp; temp=first; if (!temp) cout<<"List is empty..."; else { cout<<"List contains...\n"; while (temp) { cout<data<<" "; temp=temp->link; } cout<>ch; switch (ch) { case 1 : cout<<"Enter data : "; cin>>data; obj.insert(data); cout<<"List after insertion..."; obj.display(); break; case 2 : cout<<"Enter data : "; cin>>data; obj.delet(data); obj.display(); break; case 3 : obj.display(); break; case 4 : break; default : cout<<"Illegal choice\n"; } }while (ch!=4); } // 9 - DATA STRUCTURES - STACK # include # include # include # include # include struct node { int data; node *link; }; class stack { private : node *top; public : stack() { top=NULL; } void push(); void pop(); void display(); }; void stack :: push() { node *newnode=new node; newnode->link=NULL; cout<<"Enter data : "; cin>>newnode->data; if (top==NULL) top=newnode; else { newnode->link=top; top=newnode; } } void stack :: pop() { if (top==NULL) cout<<"Stack is empty"; else { node *temp; temp=top; top=top->link; free(temp); } } void stack :: display () { node *temp=top; if (!temp) cout<<"Stack is empty"; else { while (temp!=NULL) { cout<data<<" "; temp=temp->link; } cout<<"\n|"; } } void main () { int ch; stack obj; do { clrscr(); cout<<"\nStack - Menu\n1 - Push\n2 - Pop\n3 - Display"; cout<<"\n4 - Exit\nEnter choice : "; cin>>ch; switch(ch) { case 1 : obj.push(); break; case 2 : obj.pop(); break; case 3 : obj.display(); break; case 4 : break; } getch(); } while (ch!=4); } // 10 - DATA STRUCTURES - QUEUE # include # include struct node { int data; node *link; }; class queue { private : node *front, *rear; public : queue () { front=rear=NULL; } void display(); void insert (int element) { node *newnode=new node; newnode->data=element; newnode->link=NULL; if (front==NULL&&rear==NULL) front=rear=newnode; else if (rear==NULL) { rear=newnode; if (front==NULL) front=newnode; } else { rear->link=newnode; rear=newnode; } } void deleto() { node *temp; if (front==NULL) cout<<"Queue is empty"; else { temp=front; if (front==rear) { free(temp); front=rear=NULL; } else { front=front->link; free(temp); } } } }; void queue :: display() { node *temp; temp=front; if (!temp) cout<<"Queue is empty"; else { while (temp) { cout<data<<" "; temp=temp->link; } } } void main () { queue obj; int choice, element; do { cout<<"\nQueue - Menu\n1 - Insert\n2 - Delete\n3 - Display"; cout<<"\n4 - Exit\nEnter choice : "; cin>>choice; switch(choice) { case 1 : cout<<"Enter data : "; cin>>element; obj.insert(element); break; case 2 : obj.deleto(); break; case 3 : cout<<"\nQueue is ...\n"; obj.display(); break; case 4 : break; default : cout<<"Illegal choice"; } } while (choice!=4); } // 11 - DATA STRUCTURES - BINARY TREE # include struct node { int data; node *left, *right; }; class Btree { private : node *root; public : Btree() { root=NULL; } node *getroot() { return root ;} void insert(); node *search(info); void traverse(); void inorder (node *rt); }; node* Btree :: search(int info) { struct node *temp, *prev; temp=root; while (temp) { if (info>temp->data) { prev=temp; temp=temp->right; } else if (infodata) { prev=temp; temp=temp->right; } else prev=temp=NULL; } return prev; } void Btree :: insert() { node *new1, *temp, *prev; temp=root; new1=new node; cout<<"Enter data : "; cin>>new1->data; new1->left=new1->right=NULL; if (!temp) root=new1; else { prev=search(new1->data); if (prev) { if (new1->data>prev->data) prev->right=new1; else prev->left=new1; } else cout<<"Data exists - cannot insert "; } } void Btree :: inorder (node *rt) { if (!rt) return; inorder(rt->left); cout<data<<" "; inorder(rt->right); return; } void Btree :: traverse () { if(!root) cout<<"Empty tree"; else inorder(root); } void main () { int ch; Btree a; do { cout<<"\nBinary Tree - Menu\n1 - Insert\n2 - Display\n3 - Exit"; cout<<"\nEnter choice : "; cin>>ch; switch(ch) { case 1 : a.insert(); break; case 2 : a.traverse(); break; case 3 : break; } } while (ch!=3); } // 12 - VIRTUAL FUNCTIONS # include # include class shape { protected : float x,y; public : shape () {x=y=0.0;} virtual void display_area()=0; }; class triangle : public shape { protected : float area; public : triangle() { area=0.0;} void getdata() { cout<<"Enter base length : "; cin>>x; cout<<"Enter height : "; cin>>y; area=0.5*x*y; } void display_area() { cout<<"\nArea of triangle = "<>x; cout<<"Enter breadth : "; cin>>y; area=x*y; } void display_area() { cout<<"\nArea of rectangle = "<>ch; switch (ch) { case 1 : tri=&triobj; tri->getdata(); ptr[i]=tri; ptr[i]->display_area(); i++; break; case 2 : rec=&recobj; rec->getdata(); ptr[i]=rec; ptr[i]->display_area(); i++; break; case 3 : for (j=0;jdisplay_area(); break; case 4 : break; default : cout<<"\nIllegal choice"; } getch(); } while (ch!=4); } // 13 - COPYING FILES # include # include int main (int argc, char *argv[]) { char ch; int blank=1; ifstream infile; if (argc!=3) { cout<<"Not enough parameters\n"; return 1; } ofstream outfile (argv[2]); infile.open(argv[1],ios::nocreate); if (!infile) { cout<<"File "< class record { private : char name[32]; long int phone; public : record() { name[0]=NULL; phone=0; } void getdata() { cout<<"Enter name : "; cin>>name; cout<<"Enter phone : "; cin>>phone; } void putdata() { cout.setf(ios::left, ios::adjustfield); cout.width(15);cout<>n; for (int i=0;i