>num; additem(num); } } void additem(T item); void display(); }; template >num; additem(num); } } void linkedlist::copylist(linkedlist &A) { link *temp,*q1; q1=A.head; while(q1!=NULL)
22
{ link
template
INPUT AND OUTPUT:
23
------For integer data type-----enter number of items you want to insert:4 enter data:34 enter data:67 enter data:12 enter data:89 34-->67-->12-->89-->END ------For double data type-----enter number of items you want to insert:3 enter data:567.67 enter data:341.22 enter data:789.908 567.67-->341.22-->789.908-->END ------For character data type-----enter number of items you want to insert:5 enter data:H enter data:E enter data:L enter data:L enter data:O H-->E-->L--> L-->O-->END
DISCUSSIONS:
The advantage of this class is that it can of any datatype. The other advantage of this class is it can be of any size. Here only few operations of linked list are shown. The class can be further modified to perform other operations of linked list.
24
ASSIGNMENT 5
PROBLEM STATEMENT:
Write a C++ program to implement generic stack using array. Also provide methods to handle exceptions that will arise during push or pop operations.
ALGORITHM:
/*Algorithm for class stack*/ template
25
/*Algorithm for pop*/ template
26
call A.display() break case 4: call exit(0) break default: write "Wrong choice" break endswitch endtry catch(stack
PROGRAM CODE:
#include
27
template
28
break; case 4: exit(0); default: cout<<"Wrong choice"; break; } } catch(stack
INPUT AND OUTPUT: Enter 1.for push 2.for pop 3.for display 4.for exit enter your choice:1 enter item:55 Enter 1.for push 2.for pop 3.for display 4.for exit enter your choice:1 enter item:45 Enter 1.for push 2.for pop 3.for display 4.for exit enter your choice:1 enter item:78 Enter 1.for push 2.for pop 3.for display
29
4.for exit enter your choice:1 enter item:47 Enter 1.for push 2.for pop 3.for display 4.for exit enter your choice:3 The stack is: 55 45 78 47 Enter 1.for push 2.for pop 3.for display 4.for exit enter your choice:1 enter item:101 stack is full Enter 1.for push 2.for pop 3.for display 4.for exit enter your choice:2 popped element:47 Enter 1.for push 2.for pop 3.for display 4.for exit enter your choice:2 popped element:78 Enter 1.for push 2.for pop 3.for display 4.for exit enter your choice:2 popped element:45 Enter 1.for push 2.for pop 3.for display 4.for exit enter your choice:2
30
popped element:55 Enter 1.for push 2.for pop 3.for display 4.for exit enter your choice:2 stack is empty Enter 1.for push 2.for pop 3.for display 4.for exit enter your choice:4
DISCUSSION
To show the exception handling we use here a array of size 4. To handle large amount of data the size can be extended. The advantage of this class is that it can work with any datatype.
31
ASSIGNMENT 6
PROBLEM STATEMENT:
Write a C++ program that performs the addition of two polynomials using linked list.
ALGORITHM:
/*definition of structure poly*/ structure poly begin variable coeff (integer) variable pow (integer) pointer variable next (ploy type) end of structure poly
/*Algorithm for add2poly class*/ class add2poly begin private members: pointer variables poly1, poly2, poly3 (poly type) public members: /*Algorithm for no argument constructor*/ add2poly() begin set poly1=poly2=poly3=NULL end of add2poly() /*function declaration*/ function addpoly()
32
function display() end of class add2poly
/*Algorithm for addpoly()*/ function add2poly :: addpoly() begin variable i,p (integer) painter variables newl, end (poly type) set new1=end=NULL write "Enter highest power for x\n" read p /*Creation of 1st polynomial*/ write "\nFirst Polynomial\n" for i=p to 0 do begin set newl=new poly set newl->pow=p write "Enter Co-efficient for degree" write i write ":: " read newl->coeff set newl->next=NULL if(poly1==NULL) then set poly1=newl else set end->next=newl end if set end=newl endfor /* Creation of 2 nd polynomial */ write "\n\nSecond Polynomial\n" set end=NULL
33
for i=p to 0 do begin set newl=new poly set newl->pow=p write "Enter Co-efficient for degree" write i write ":: " read newl->coeff set newl->next=NULL if(poly2==NULL) then set poly2=newl else set end->next=newl endif set end=newl endfor
/*Addition Logic*/ pointer variable p1, p2 (poly type) set p1 = poly1 set p2 = poly2 set end=NULL while(p1 !=NULL AND p2!=NULL) do begin if(p1->pow == p2->pow) than set newl=new poly set newl->pow=p-set newl->coeff=p1->coeff + p2->coeff set newl->next=NULL if(poly3==NULL) then set poly3=newl else
34
set end->next=newl endif set end=newl endif set p1=p1->next set p2=p2->next endwhile end of function addpoly()
/*Algorithm for display*/ function add2poly :: display() begin pointer variable t (poly type) set t=poly3 write "\n\nAnswer after addition is : " while(t!=NULL) do begin write t->coeff write " X" write t->pow write " " set t=t->next endwhile end of function display()
/*Algorithm for main function*/ function main() begin call clrscr(); object obj (add2poly type) call obj.addpoly() call obj.display()
35
call getch() end of function main()
PROGRAM CODE:
#include
struct poly{ int coeff; int pow; poly *next; };
class add2poly { poly *poly1, *poly2, *poly3; public: add2poly(){poly1=poly2=poly3=NULL;} void addpoly(); void display(); };
void add2poly :: addpoly() { int i,p; poly *newl=NULL,*end=NULL; cout<<"Enter highest power for x\n"; cin>>p;
36
//Read first poly cout<<"\nFirst Polynomial\n"; for(i=p;i>=0;i--) { newl=new poly; newl->pow=p; cout<<"Enter Co-efficient for degree"<>newl->coeff; newl->next=NULL; if(poly1==NULL) poly1=newl; else end->next=newl; end=newl; }
//Read Second poly cout<<"\n\nSecond Polynomial\n"; end=NULL; for(i=p;i>=0;i--) { newl=new poly; newl->pow=p; cout<<"Enter Co-efficient for degree"<>newl->coeff; newl->next=NULL; if(poly2==NULL) poly2=newl; else end->next=newl; end=newl; }
37
//Addition Logic poly *p1=poly1,*p2=poly2; end=NULL; while(p1 !=NULL && p2!=NULL) { if(p1->pow == p2->pow) { newl=new poly; newl->pow=p--; newl->coeff=p1->coeff + p2->coeff; newl->next=NULL; if(poly3==NULL) poly3=newl; else end->next=newl; end=newl; } p1=p1->next; p2=p2->next; } }
void add2poly :: display() { poly *t=poly3; cout<<"\n\nAnswer after addition is : "; while(t!=NULL) { cout<
38
t=t->next; } }
void main() { clrscr(); add2poly obj; obj.addpoly(); obj.display(); getch(); }
INPUT AND OUTPUT: Enter highest power for x 4 First Polynomial Enter Co-efficient Enter Co-efficient Enter Co-efficient Enter Co-efficient Enter Co-efficient
for for for for for
Second Polynomial Enter Co-efficient for Enter Co-efficient for Enter Co-efficient for Enter Co-efficient for Enter Co-efficient for
degree4:12 degree3: 9 degree2: 0 degree1: 7 degree0: 23 degree4: degree3: degree2: degree1: degree0:
0 4 9 9 4
Answer after addition is : 12 X4 13 X3 9 X2 16 X1 27 X0
DISCUSSIONS: The above program only performs addition of two polynomial. It can be further modified to permor other operations of polynomial.
39
ASSIGNMENT 7
PROBLEM STATEMENT:
Write a C++ program that overloads stream operators-‘>>’ and ‘<<’.
ALGORITHM:
/*Algorithm for number class*/ class number begin private members: variable data_1 (integer) variable data_2 (integer) public members: /*Algorithm for no argument constructor*/ number() begin set data_1=0 set data_2=0 end of number() /*Algorithm for two argument constructor*/ number(integer num_1,integer num_2) begin set data_1=num_1 set data_2=num_2; end of number(integer num_1, integer num_2) /*declaration of functions*/ friend function operator<<(ostream &s, number &obj); friend function &operator>>(istream &s,number &obj); end of class number /*Algorithm for overloading << operator*/ function operator<<(ostream &s,number &obj) begin s<<"("<
40
return s end of function operator>>(istream &s,number &obj) /*Algorithm for main function*/ function main() begin call clrscr() objects obj_1(5,6), obj_2(7,8), obj_3 (number type) write "\n Value of obj_1 = "<
41
istream &operator>>(istream &s,number &obj) { s>>obj.data_1>>obj.data_2; return s; } void main() { clrscr(); number obj_1(5,6); number obj_2(7,8); number obj_3; cout<<"\n Value of obj_1 = "<
INPUT AND OUTPUT: Value of obj_1 = (5,6) Value of obj_2 = (7,8) Enter the value of obj_3 : 25 32 Value of obj_3 = (25,32)
DISCUSSIONS:
Normally we can not read or write directly object of class using cin and cout respectively. But by overloading << and >> operators we can do that.
42
ASSIGNMENT 8
PROBLEM STATEMENT:
Write a c++ program to create a class fraction and which represents a fractional number and perform addition and subtraction of this fractional numbers.
ALGORITHM:
/*Algorithm for class fraction*/ class fraction begin private members: variable numerator,denominator (integer) public members: /*Algorithm for no argument constructor*/ fraction() begin set numerator=0 set denominator=1 end of fraction() /*Algorithm for two argument constructor*/ fraction(integer i, integer j) begin set numerator=i set denominator=j end of fraction(integer i, integer j) /*Algorithm for overload + operator*/ function operator +(fraction op) begin variable LCM (integer) set LCM=lcm(denominator,op.denominator) variable a (integer) set a=LCM/denominator variable b (integer) set b=LCM/op.denominator variable num (integer) set num=numerator*a+op.numerator*b variable deno (integer) set deno=LCM return(fraction(num,deno)) end of function operator +(fraction op) /*Algorithm for overloading * operator*/ function operator *(fraction op)
43
begin variable num (integer) set num=numerator*op.numerator variable deno (integer) set deno=op.denominator*denominator return(fraction(num,deno)) end of function operator *(fraction op) /*Algorithm for overloading = operator*/ function operator =(fraction op) begin set numerator=op.numerator set denominator=op.denominator end of function operator =(fraction op) /*Algorithm for overloading – operator*/ function operator -() begin set numerator=-numerator set denominator=denominator return(fraction(numerator,denominator)) end of function operator -() /*Algorithm for display()*/ function display() begin write "\ndenom=" write denominator write "\t numerator=" write numerator end of function display() /*function declaration*/ funtion lcm(integer a,integer b); end of class fraction /*Algorithm for lcm*/ function fraction::lcm(integer a,integer b) begin for i=1to infinity do begin if(i%a==0 AND i%b==0) than return i else continue endif endfor end of function lcm(integer a,integer b)
function main() begin objects A(3,5),B(4,6),C(2,3),D(2,5),E (fraction type) set E=(-A+B)*C+D write "\n\t-----data for fraction A-----\n" call A.display()
44
write "\n\t-----data for fraction B-----\n" call B.display() write "\n\t-----data for fraction C-----\n" call C.display() write "\n\t-----data for fraction D-----\n" call D.display() write "\n\t-----Final Result-----\n" call E.display() call getch() end of function main()
PROGRAM CODE:
#include
class fraction { private: int numerator,denominator; public: fraction() { numerator=0; denominator=1; } fraction(int i,int j) { numerator=i; denominator=j; } fraction operator +(fraction op) { int LCM=lcm(denominator,op.denominator); int a=LCM/denominator; int b=LCM/op.denominator; int num=numerator*a+op.numerator*b; int deno=LCM; return(fraction(num,deno)); }
45
fraction operator *(fraction op) { int num=numerator*op.numerator; int deno=op.denominator*denominator; return(fraction(num,deno)); } void operator =(fraction op) { numerator=op.numerator; denominator=op.denominator; } fraction operator -() { numerator=-numerator; denominator=denominator; return(fraction(numerator,denominator)); } void display() { cout<<"\ndenom="<
46
D.display(); cout<<"\n\t-----Final Result-----\n"; E.display(); getch(); }
INPUT AND OUTPUT:
-----data for fraction A----denom=5 numerator=-3 -----data for fraction B----denom=6 numerator=4 -----data for fraction C----denom=3 numerator=2 -----data for fraction D----denom=5 numerator=2 -----Final Result----denom=90 numerator=40
DISCUSSIONS:
Only few operations for fractional numbers are shown here. The class can be further modified to include other operations of fractional numbers.
47
ASSIGNMENT 9
PROBLEM STATEMENT:
Write a c++ program to overload pre-increment and post-increment operators.
ALGORITHM:
/*Algorithm for index class*/ class index begin private members: variable count (integer)
public members: /*Algorithm for no argument constructor*/ index() begin set count=0 end of index() /*Algorithm for one argument constructor*/ index(integer i) begin write "\n one argument constructor is called:" set count=i end of index(integer i) /*Algorithm for display()*/ function display() begin write "\ndata=" write count end of function display()
48
/*Algorithm for copy constructor*/ index(index &a) begin set count=a.count end of index(index &a) /*Algorithm for overloading pre-increment operator*/ function operator ++() begin return(++count) end of function operator ++() /*Algorithm for post-increment operator*/ function operator ++(integer) begin return(count++) end of function operator ++(integer) /*Algorithm for overloading = operator*/ function operator =(index &b) begin set count=b.count return index(count) end of function operator =(index &b) /*Algorithm for destructor*/ ~index() begin write "\n\n destructor is called" end of ~index() end of class index
/*Algorithm for main()*/ function main() begin object A(34), B, C, E, F, D (index type)
49
set D=A
call A.display() call D.display() set C=D set E=F=D call C.display() call F.display() call E.display() set F=C++ call F.display() set E=++F call E.display() end of function main()
PROGRAM CODE:
#include
50
count=i; } void display() { cout<<"\ndata="<
void main() { index A(34),B,C,E,F;
51
index D=A; A.display(); D.display(); C=D; E=F=D; C.display(); F.display(); E.display(); F=C++; F.display(); E=++F; E.display(); }
INPUT AND OUTPUT: one argument constructor is called: data=34 data=34 one argument constructor is called: destructor is called one argument constructor is called: one argument constructor is called: destructor is called destructor is called data=34 data=34 data=34 one argument constructor is called: one argument constructor is called: destructor is called destructor is called data=34 one argument constructor is called: one argument constructor is called: destructor is called destructor is called data=35
52
destructor is called destructor is called destructor is called destructor is called destructor is called destructor is called
DISCUSSIONS:
The above class shows how destructors are called. This class’s objects may be use for counting purpose.
53
ASSIGNMENT 10
PROBLEM STATEMENT:
Write a C++ program to implement Generic Bubble sort.
ALGORITHM:
/*Algorithm for bubble class*/ template
/*Algorithm for get() which collect data from user*/ template
54
/*Algorithm for display()*/ template
/*Algorithm for bubble sort*/ template
55
/*Algorithm for main()*/ function main() begin variable n (integer) object b1 (bubble
PROGRAM CODE:
#include
template
56
t a[25]; public: void get(int); void sort(int); void display(int); };
template
template
template
57
for(j=i+1;j
void main() { int n; bubble
58
INPUT AND OUTPUT: Bubble Sort on Integer Values Enter the size of array:8 Enter the array elements:12 34 3 56 33 67 7 66 The sorted array is: 3 7 12 33 34 56 66 67 Bubble Sort on Float Values Enter the size of array: 6 Enter the array elements:4.78 3.67 6.7 2.68 8.97 7.89 The sorted array is: 2.68 3.67 4.78 6.7 7.89 8.97
59
DISCUSSIONS:
The bubble sort is not efficient in comparison to other sorting techniques. But for smaller lists this sorting technique works fine. The bubble sort is data sensitive that is if the list is sorted it will stop sorting. To take more elements program can be modified. The Complexity of bubble sort is
60
( ).
O n
2
ASSIGNMENT 11
PROBLEM STATEMENT:
Write a C++ program to show the usefulness of virtual function.
ALGORITHM:
/*Algorithm for Base class*/ class Base begin public members: /*Algorithm for display()*/ function display () begin write "\n Display base" end of function display() /*Algorithm for show() which is virtual*/ function virtual show() begin write "\n show base" end of function virtual show() end of class Base /*Algorithm for Derived class which derived publicly from Base class*/ class Derived:public Base begin public members: /*Algorithm for overloaded display()*/ function display() begin write "\n Display derived" end of function display() /*Algorithm for overloaded show()*/ function show() begin write "\n show derived" end of function show() end of class Derived /*Algorithm for main()*/ function main () begin object B (Base) object D (Derived) object pointer bptr (Base)
61
Write "\n bptr points to Base\n" set bptr=&B call bptr->display() call bptr->show() write "\n\n bptr points to Derived\n" set bptr=&D call bptr->display() call bptr->show() end of function main()
PROGRAM CODE:
#include
62
bptr->show() ; cout<<"\n\n bptr points to Derived\n"; bptr=&D; bptr->display() ; bptr->show() ; return 0; }
INPUT AND OUTPUT: bptr points to Base Display base Show base bptr points to Derived Display base Show derived
DISCUSSION:
Polymorphism refers to the property by which objects belonging to different classes are able to respond to the same message, but in different forms. When we use the same function name in both the base and derived classes,the function in base class is declared as virtual using the keyword virtual preceding its normal declaration. When bptr is made to point to the object D,the statement bptr-+displayO; calls only the function associated with the Base (i.eBase::displayO), whereas the statement bptr-+showO; calls the Derived version of showO.This is because the function display() has not been made virtual in the Base class.
63
ASSIGNMENT 12
PROBLEM STATEMENT:
Write a C++ program that implements linear search using FRIEND functions & INLINE functions.
ALGORITHM: /*Algorithm for array class*/ class array begin private members: pointer varibale p (integer) variable size (integer) public members: /*function declarations*/ function read(); function create(); function display(); friend function search(array); end of class array /*Algorithm for read()*/ inline function array::read() begin write "\n\nEnter the size of the array:" read size set p = new integer[size] /*allocate memory dynamically*/ end of inline function read() /*Algorithm for create()*/ function array::create() begin write "\n\nEnter the array elements\n\n" for i=0 to size-1 do begin write "element" write i+1 write ":" read *(p+i) endfor write "\n\nThe array is:" call display() end of function create() /*Algorithm for display()*/ function array::display() begin
64
for i=0 to size-1 do begin write " " write *(p+i) endfor /*Algorithm for search*/ finction search(array ob) begin variable item (integer) write "\n\nEnter the item to be searched:" read item for i=0 to ob.size-1 do begin if(*(ob.p+i)==item) then write "\n\nItem found at location:" write i+1 return endif endfor write "\n\nItem not found" end of function search(array ob) /*Algorithm for main()*/ function main() begin object obj (array type) call clrscr() call obj.read() call obj.create() call search(obj) call getch() return(0) end of function main()
PROGRAM CODE:
#include
65
p = new int[size]; //allocate memory dynamically } void array::create() { cout<<"\n\nEnter the array elements\n\n"; for(int i=0;i
INPUT AND OUTPUT:
Enter the size of the array: 5 Enter the array elements element1:12 element2:13
66
element3:24 element4:35 element5:46 The array is: 12 13 24 35 46 Enter the item to be searched: 24 Item found at location: 3
DISCUSSION:
1. The complexity of BINARY SEARCH is measured by the number f(n) of comparison where n is the number of data elements & is given by O(n). 2. A solution to this complexity problem is to use Binary search technique having a complexity O(log 2(n)).
67
ASSIGNMENT 13
PROBLEM STATEMENT:
Write a program that implements a binary search tree(BST) and performs inorder traversal and searching operations on the tree. ALGORITHM: /*Algorithm for BST class*/ class BST bagin private members: /*definition of tree structure*/ structure tree begin variable data (integer) pointer variables rchild, lchild (tree type) end of structure tree pointer variable root (tree type) public members: /*function declaration*/ function inorder(structure tree *root) function createBST() function Return() function search(structure tree *root) end of class BST /*Algorithm for Return()*/ function BST::Return() begin return(root); end of function Return() /*Algorithm for BST creation*/ Function CreateBST() begin pointer variable p,q,r (tree type) variable i,num (integer) set root=NULL write "\n\nenter the no.of nodes:" read num; for i=0 to num-1 do begin set p = new tree; /*create new node*/ write "\nenter value of node" write i+1 write ":" read p->data set p->lchild=p->rchild=NULL if(root==NULL) then set root=p
68
else set q=root set r=NULL while(q!=NULL) do begin if(p->data
69
object ob (BST type) call clrscr() while(true) do begin write "\n\n\nwhat do you wish\n\n0.exit\n1.createBST\n2.inorder\n3.seacrh\n?" read ch if(ch==0) then return(0) endif if(ch==1) then call ob.createBST() endif if(ch==2) then write "\n\nthe inorder teaversal is as follows:" call ob.inorder(ob.Return()) write "\n\nassure that the list is sorted in ascending order" endif if(ch==3) then call ob.search(ob.Return()) endif endwhile end of function main()
PROGRAM CODE: #include
70
{ p = new tree; /*create node*/ cout<<"\nenter value of node"<>p->data; p->lchild=p->rchild=NULL; if(root==NULL) { root=p; } else { q=root; r=NULL; while(q!=NULL) { if(p->data
71
if(root->data
72
INPUT AND OUTPUT: what do you wish? 0.exit 1.createBST 2.inorder 3.seacrh ?1 enter enter enter enter
the no.of nodes:3 value of node1:10 value of node2:11 value of node3:9
what do you wish? 0.exit 1.createBST 2.inorder 3.seacrh ?2 The inorder traversal is as follows: 9 10 11 Assure that the list is sorted in ascending order 62 what do you wish? 0.exit 1.createBST 2.inorder 3.seacrh ?3 enter the value to be searched:9 item found at location: what do you wish 0.exit 1.createBST 2.inorder 3.seacrh ?0
DISCUSSION:
1. Suppose we are searching for an item in a BST. The number of comparison is bounded by the depth of the tree. This comes from the fact that we proceed down single path of the tree as we search the item. Accordingly the running time of the search will be proportional to the depth of the tree . thus the time complexity of BST searching algorithm is o(log2n). 2. The inorder traversal gives a sorted list of elements.
73
ASSIGNMENT 14
PROBLEM STATEMENT:
Write a C++ program that performs addition, subtraction, multiplication and division of two complex numbers. Overload operators ‘+’, ‘-‘, ‘*’,’/’ to perform the above operations.
ALGORITHM:
/*Algorithm for complex class*/ class complex begin private members: variables real,img (float) public members: /*Algorithm for display()*/ function display() begin write "\n\n\t"; if(img>=0) then write real write "+" write img write "i" else write real write img write "i" endif end of function display() /*Algorithm for no argument constructor*/ complex() begin set real=0 set img=0 end of complex() /*Algorithm for two argument constructor*/ complex(float r,float i) begin set real=r set img=i end of complex(float r,float i) /*function declaration*/ function operator +(complex &b)
74
function operator -(complex &b) function operator *(complex &b) function operator /(complex &b) end of class complex /*Algorithm for overloading + operator*/ function complex::operator +(complex &b) begin variable e,f (float) set e=real+b.real set f=img+b.img return(complex(e,f)) end of function operator +(complex &b) /*Algorithm for overloading - operator*/ function complex::operator -(complex &b) begin variable e,f : float set e=real-b.real set f=img-b.img return(complex(e,f)) end of function operator -(complex &b) /*Algorithm for overloading * operator*/ function complex::operator *(complex &b) begin varibale e,f : float set e=real*b.real-img*b.img set f=img*b.real+real*b.img return(complex(e,f)); end of function operator *(complex &b) /*Algorithm for overloading / operator*/ function complex::operator /(complex &b) begin variable num1,num2,deno : float variable e,f : float set num1=real*b.real+img*b.img set num2=img*b.real-real*b.img set deno=b.real*b.real+b.img*b.img set e=num1/deno set f=num2/deno return(complex(e,f)) end of function operator /(complex &b) /*Algorithm for main()*/ function main() begin objects x(3,2),y(2,5),z,a(8,8),b(3,2),e (complex type) set z=x+y write "\n complex number x(3,2)" call x.display() write "\n complex number y(2,5)" call y.display()
75
write "\n complex call z.display() set z=a-b; write "\n complex call a.display() write "\n complex call b.display() write "\n complex call z.display() set z=x*y write "\n complex call x.display() write "\n complex call y.display() write "\n complex call z.display() set e=a/b write "\n complex call a.display() write "\n complex call b.display() write "\n complex call e.display() end of function main()
number z=x+y"
number a(8,8)" number b(3,2)" number z=a-b"
number x(3,2)" number y(2,5)" number z=x*y"
number a(8,8)" number b(3,2)" number e=a/b"
PROGRAM CODE: #include
76
{ real=r; img=i; } complex complex complex complex
operator operator operator operator
+(complex &b); -(complex &b); *(complex &b); /(complex &b);
}; complex complex::operator +(complex &b) { float e,f; e=real+b.real; f=img+b.img; return(complex(e,f)); } complex complex::operator -(complex &b) { float e,f; e=real-b.real; f=img-b.img; return(complex(e,f)); } complex complex::operator *(complex &b) { float e,f; e=real*b.real-img*b.img; f=img*b.real+real*b.img; return(complex(e,f)); } complex complex::operator /(complex &b) { float num1,num2,deno; float e,f; num1=real*b.real+img*b.img; num2=img*b.real-real*b.img; deno=b.real*b.real+b.img*b.img; e=num1/deno; f=num2/deno; return(complex(e,f)); } void main() { complex x(3,2),y(2,5),z,a(8,8),b(3,2),e; z=x+y; cout<<"\n complex number x(3,2)"; x.display();
77
cout<<"\n complex number y(2,5)"; y.display(); cout<<"\n complex number z=x+y"; z.display(); z=a-b; cout<<"\n complex number a(8,8)"; a.display(); cout<<"\n complex number b(3,2)"; b.display(); cout<<"\n complex number z=a-b"; z.display(); z=x*y; cout<<"\n complex number x(3,2)"; x.display(); cout<<"\n complex number y(2,5)"; y.display(); cout<<"\n complex number z=x*y"; z.display(); e=a/b; cout<<"\n complex number a(8,8)"; a.display(); cout<<"\n complex number b(3,2)"; b.display(); cout<<"\n complex number e=a/b"; e.display(); }
INPUT AND OUTPUT: complex number x(3,2) 3+2i complex number y(2,5) 2+5i complex number z=x+y 5+7i complex number a(8,8) 8+8i complex number b(3,2) 3+2i complex number z=a-b 5+6i complex number x(3,2) 3+2i complex number y(2,5) 2+5i complex number z=x*y -4+19i complex number a(8,8) 8+8i complex number b(3,2) 3+2i complex number e=a/b 3.07692+0.615385i
78
DISCUSSION:
The above class can be further modified to include other operations of complex numbers.
79
ASSIGNMENT 15
PROBLEM STATEMENT:
Create a class called Time that has separate integer member data for hours,minutes and second. One constructor should initialize this data to zero and another initialize it to fix values. A member function should display it in hh:mm:ss format. The final member function should add two objects of type Time passed as arguments. A main program should create to initialized the time objects,and one that isn't initialized. Then it should add two initialized values together, the result in the third time variable. Finally it should display the value of this third variable.
ALGORITHM:
/*Algorithm for time class*/ class time begin private members: variable hour (integer) variable min (integer) variable sec (integer) public members: /*Algorithm for no argument constructor*/ time() baegin set hour=0 set min=0 set sec=0 end of time() /*Algorithm for three argument constructor*/ time(integre h, integer m, integer s) begin set hour=h set min=m set sec=s end of time(integre h, integer m, integer s) /*Algorithm for getdata()*/
80
function getdata() begin write "\nEnter the Hour:" read hour write "\nEnter the Minute:" read min write "\nEnter the Second:" read sec end of getdata() /*Algorithm for display()*/ function display() begin if (hour<10 AND min<10 AND sec<10) then write "0" write hour write ":0" write min write ":0" write sec write " Hr.\n" else if(hour<10 AND min<10) then write "0" write hour write ":0" write min write ":" write sec write " Hr.\n" else if (hour<10 AND sec<10) then write "0" write hour wrute ":" write min write ":" write "0" write sec write " Hr.\n" else if(sec<10 AND min<10) then write hour
81
write ":" write "0" write min write ":" write "0" write sec write " Hr.\n" else if(sec<10) than write hour write ":" write min write ":" write "0" write sec write " Hr.\n" else if(min<10) then write hour write ":" write "0" write min write ":" write sec write " Hr.\n" else if(hour<10) write "0" write hour write ":" write min write ":" write sec write " Hr.\n" else write hour write ":" write min write ":" write sec write " Hr.\n" endif
82
end of function display() /*Algorithm for overloading + operator*/ function operator+(time t1) begin variable hr, mn, sc (integer) set hr=hour+t1.hour set mn=min+t1.min set sc=sec+t1.sec if(sc>=60) then set sc= sc – 60 set mn = mn + 1 endif if(mn>=60) then set mn=mn-60 hr = hr + 1 endif return time(hr,mn,sc); end of function operator+(time t1) end of class time /*Algorithm for main()*/ function main() begin objects t1,t2,t3 (time type) call clrscr() write "\nEnter the first time:" call t1.getdata() write "\nEnter the second time:" call t2.getdata() set t3=t1+t2 call clrscr() write "\n\n\n" write "\t\tThe entered Time:: " call t1.display() write "\n\n\n" write "\t\tThe entered Time:: " call t2.display() write "\n\n\t\t\t The Total Time \n\n" call gotoxy(27,14) call t3.display()
83
call getch(); end of function main()
PROGRAM CODE:
#include
class time { private: int hour; int min; int sec; public: time() { hour=0; min=0; sec=0;} time(int h,int m,int s) { hour=h; min=m; sec=s; } void getdata() { cout<<"\nEnter the Hour:"; cin>>hour; cout<<"\nEnter the Minute:"; cin>>min; cout<<"\nEnter the Second:" ; cin>>sec; } void display() { if (hour<10 && min<10 && sec<10) { cout<<"0"<
84
} else if(hour<10 && min<10) cout<<"0"<
void main() { time t1,t2,t3; clrscr();
85
cout<<"\nEnter the first time:"; t1.getdata(); cout<<"\nEnter the second time:"; t2.getdata(); t3=t1+t2; clrscr(); cout<<"\n\n\n"; cout<<"\t\tThe entered Time:: "; t1.display(); cout<
OUTPUT: OUTPUT:
Output Sample - I Enter the first time: Enter the Hour:2 Enter the Minute:52 Enter the Second:38
Enter the second time: Enter the Hour:6 Enter the Minute:25 Enter the Second:26 The entered Time:: 02:52:38 Hr. The entered Time:: 06:25:26 Hr. The Total Time 09:18:04 Hr. Output Sample -II
Enter the first time:
86
Enter the Hour:2 Enter the Minute:56 Enter the Second:48
Enter the second time: Enter the Hour:4 Enter the Minute:8 Enter the Second:15 The entered Time:: 02:56:48 Hr. The entered Time:: 4:08:15 Hr. The Total Time 07:05:03 Hr.
DISCUSSIONS: DISCUSSIONS:
On the above program we take two input of time in different frame, hour, minute & second each object of time class taken user input data and the overloaded binary operator '+' user to add two time object and assigning it into the third object. The display function will call to display the time in [hh:mm:ss] format. That is if the hour, minute and second is less than 10 then '0' will be padded to its left.
87
ASSIGNMENT 16
PROBLEM STATEMENT:
Write a C++ program to overload subscript operator.
ALGORITHM: /*Algorithm for atype class*/ class atype begin private members: variable a[5] : integer public members: /*Algorithm for one argument constructor*/ atype(integer i, integer j, integer k) begin set a[0]=i set a[1]=j set a[2]=k end of atype(integer i, integer j, integer k) /*Algorithm for overloading [] operator*/ function operator [](integer i) begin return a[i] end of function operator [](integer i) /*Algorithm for showdata()*/ function showdata() begin write "\n data's are:" for i=0 to 2 do begin write "\n" write a[i] endfor end of function showdata()
88
end of class atype /*Algorithm for main()*/ function main() begin object A(12,34,45) (atype type) variable c (integer) for j=0 to 2 write "\ndata:" write (j+1) write "\t" write A[j] endfor set c=A[1]+A[0]*A[2] write "\nfinal answer is:" write c call A.showdata() call getch(); end of function main()
PROGRAM CODE:
#include
class atype { private: int a[5]; public: atype(int i,int j,int k) { a[0]=i;a[1]=j;a[2]=k; } int operator [](int i) { return a[i];
89
} void showdata() { cout<<"\n data's are:"; for(int i=0;i<3;i++) cout<<"\n"<
INPUT AND OUTPUT:
data:1 12 data:2 34 data:3 45 final answer is:574 data's are: 12 34 45
DISCUSSION:
He above program shows a demo for overloading subscript ([]) operator. It can be used to other programs to perform a specific operation.
90
ASSIGNMENT 17
PROBLEM STATEMENT:
Write a C++ program to overload these operators- ‘+=’, ‘+’, ‘>=’, ‘<=’, ‘!=’, ‘==’.
ALGORITHM: constant true = 1 constant false = 0 /*Algorithm for class sample*/ class sample begin private members: variable data (integer) public members: /*Algorith for one argument constructor*/ sample(integer i) begin set data=i end of sample(integer i) /*Algorithm for no argument constructor*/ sample() begin end of sample() /*Algorithm for overloading + operator*/ function operator +(sample s) begin return sample(data+s.data) end of function operator +(sample s) /*Algorithm for overloading +=*/ function operator +=(sample s) begin return sample(data+=s.data) end of function operator +=(sample s) /*Algorithm for overloading >=*/ function operator >=(sample s) begin if(data>=s.data) then return true else return false endif end of function operator >=(sample s) /*Algorithm for overloading <= operator*/ function operator <=(sample s) begin if(data<=s.data) then return true else return false endif
91
end of function operator <=(sample s) /*Algorithm for overloading != operator*/ function operator !=(sample s) begin if(data!=s.data) then return true else return false endif end of function operator !=(sample s) /*Algorithm for overloading == operator*/ function operator ==(sample s) begin if(data==s.data) then return true else return false endif end of function operator ==(sample s) /*Algorithm for showdata()*/ function showdata() begin write "\n the value is:" write data end of function showdata() end of class sample /*Algorithm for main()*/ function main() begin variable x,y (integer) write "\n enter two values for a and b:" read x read y objects a(x),b(y),c (sample) if(a>=b) then write "\n a is greater than or equal to b" endif if(a<=b) then write "\n b is greater than or equal to a" end if if(a==b) then write "\n a is equal to b" endif if(a!=b) then write "\n a is not equal to b" endif set c=a+b call c.showdata() set a+=b call a.showdata() end of function main()
PROGRAM CODE:
#include
92
#define true 1 #define false 0 class sample { private: int data; public: sample(int i) { data=i; } sample() {} sample operator +(sample s) { return sample(data+s.data); } sample operator +=(sample s) { return sample(data+=s.data); } int operator >=(sample s) { if(data>=s.data) return true; else return false; } int operator <=(sample s) { if(data<=s.data) return true; else return false; } int operator !=(sample s) { if(data!=s.data) return true; else return false; } int operator ==(sample s) { if(data==s.data) return true; else return false; } void showdata() { cout<<"\n the value is:"<
93
int x,y; cout<<"\n enter two values for a and b:"; cin>>x>>y; sample a(x),b(y),c; if(a>=b) cout<<"\n a is greater than or equal to b"; if(a<=b) cout<<"\n b is greater than or equal to a"; if(a==b) cout<<"\n a is equal to b"; if(a!=b) cout<<"\n a is not equal to b"; c=a+b; c.showdata(); a+=b; a.showdata(); }
INPUT AND OUTPUT: enter two values for a and b:2 3 b is greater than or equal to a a is not equal to b the value is:5 the value is:5
DISCUSSIONS:
The above program shows a demonstration for overload logical operators. It can be used with some specific programs to perform a specific task.
94
ASSIGNMENT 18
PROBLEM STATEMENT:
Write a C++ program to create a singly linked list and perform the following functions: a)copy the list into another list b)remove duplicate elements from the list. c)display the list.
ALGORITHM: /*Definition of structure link*/ structure link begin variable data : integer pointer variable next (link type) end of structure link /*Algorithm for linkedlist class*/ class linkedlist begin private members: pointer variable head (link type) variable p,num (integer) public members: /*Algorithm for no argument constructor*/ linkedlist() begin set head=NULL end of linkedlist() /*function declarations*/ function getdata() function additem(integer item) function copylist(linkedlist &A) function remDup() function display() end of class linkedlist /*Algorithm for getdata()*/ function linkedlist::getdata() begin write "enter number of items you want to insert:"; read p for i=0 to p-1 do begin
95
write "\n enter data:" read num call additem(num) endfor end of function getdata() /*Algorithm for copy liked list*/ function linkedlist::copylist(linkedlist &A) begin structure link *temp,*q1 set q1=A.head while(q1!=NULL) do begin set temp=new (link type) set temp->data=q1->data set temp->next=NULL if(head==NULL) then set head=temp else pointer variable list (link type) set list=head while(list->next!=NULL) do set list=list->next endwhile set list->next=temp endif set q1=q1->next endwhile end of function copylist(linkedlist &A) /*Algorithm for adding a item to the linked list*/ function linkedlist::additem(int item) begin pointer variable temp (link type) set temp=new link set temp->data=item set temp->next=NULL if(head==NULL) then set head=temp else pointer variable list (link type) set list=head while(list->next!=NULL) do begin set list=list->next endwhile set list->next=temp endif end of function additem(int item) /*Algorithm for removing duplicates from linked list*/ function linkedlist::remDup() begin
96
pointer variables ptr1, ptr2, dup (link type) set ptr1 = head /* Pick elements one by one */ while(ptr1 != NULL AND ptr1->next != NULL) do begin set ptr2 = ptr1 /* Compare the picked element with rest of the elements */ while(ptr2->next != NULL) do begin /* If duplicate then delete it */ if(ptr1->data == ptr2->next->data) then /* sequence of steps is important here */ set dup = ptr2->next set ptr2->next = ptr2->next->next delete dup else set ptr2 = ptr2->next endif endwhile set ptr1 = ptr1->next endwhile end of function remDup() /*Algorithm for displaying the linked list*/ function linkedlist::display() begin pointer variable start (link type) set start=head write "\n\n"; while(start!=NULL) do begin write start->data write "-->" set start=start->next endwhile write "END" end of function display() /*Algorithm for main()*/ function main() begin objects A,B (linkedlist type) call A.getdata(); write "\n 1st list:" call A.display() call B.copylist(A) write "\n copied list:" call B.display() call A.remDup() write "\n 1st list after removal of duplicate elements:" call A.display() call getch();
97
end of function main()
PROGRAM CODE:
#include
98
{ temp=new link; temp->data=q1->data; temp->next=NULL; if(head==NULL) head=temp; else { link *list; list=head; while(list->next!=NULL) list=list->next; list->next=temp; } q1=q1->next; } } void linkedlist::additem(int item) { link *temp; temp=new link; temp->data=item; temp->next=NULL; if(head==NULL) head=temp; else { link *list; list=head; while(list->next!=NULL) list=list->next; list->next=temp; } }
void linkedlist::remDup() { link *ptr1, *ptr2, *dup; ptr1 = head; /* Pick elements one by one */ while(ptr1 != NULL && ptr1->next != NULL) { ptr2 = ptr1; /* Compare the picked element with rest of the elements */ while(ptr2->next != NULL) { /* If duplicate then delete it */ if(ptr1->data == ptr2->next->data) { /* sequence of steps is important here */
99
dup = ptr2->next; ptr2->next = ptr2->next->next; delete dup; } else ptr2 = ptr2->next; } ptr1 = ptr1->next; } }
void linkedlist::display() { link *start; start=head; cout<<"\n\n"; while(start!=NULL) { cout<
INPUT AND OUTPUT:
enter number of items you want to insert:7 enter data:12 enter data:56 enter data:89 enter data:45 enter data:89 enter data:12 enter data:22
100