0% found this document useful (0 votes)
39 views32 pages

Program of List Using Array

The document contains C++ code examples demonstrating various object-oriented programming concepts like classes, objects, inheritance, polymorphism, templates, and function overloading. It includes programs for list operations using arrays, binary search, call by reference, public and private members, static members, constructors, destructors, friend functions, operator overloading, inheritance, pure virtual functions, and function templates for finding the maximum of three values and swapping variables.

Uploaded by

Samuel Davis
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
39 views32 pages

Program of List Using Array

The document contains C++ code examples demonstrating various object-oriented programming concepts like classes, objects, inheritance, polymorphism, templates, and function overloading. It includes programs for list operations using arrays, binary search, call by reference, public and private members, static members, constructors, destructors, friend functions, operator overloading, inheritance, pure virtual functions, and function templates for finding the maximum of three values and swapping variables.

Uploaded by

Samuel Davis
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 32

/* Program of list using array */

# include<stdio.h> # define MAX 20 # include <conio.h> # include <iostream.h>

int arr[2]; int n; /*Total number of elements in the list */ void input(int); int search(int item); void insert(); void del(); void display(); // del(); //display(); int main() { int choice,item,pos; while(1) { cout<<"1.Input list\n"; cout<<"2.Insert\n"; cout<<"3.Search\n"; cout<<"4.Delete\n"; cout<<"5.Display\n";

cout<<"6.Quit\n"; cout<<"Enter your choice : "; cin>>choice;

switch(choice) { case 1: cout<<"Enter the number of elements to be entered : "; cin>>n; input(n); break; case 2: insert(); break; case 3: cout<<"Enter the element to be searched : "; cin>>item; pos = search(item); if(pos >= 1) cout<<" found at position \n"<<item<<pos; else cout<<"Element not found\n"; break; case 4: del();

break; case 5: display(); break; case 6: return 0; break; default: cout<<"Wrong choice\n"; } /*End of switch */ }/*End of while */ }/*End of main() */

void input(int n) { int i; for(i = 0; i< n ; i++) { cout<<"Input value for element : "<<i+1; cin>>arr[i]; } }/*End of input()*/

int search(int item) {

int i; for(i=0; i < n; i++) { if(item == arr[i]) return(i+1); } return(0); /* If element not found */ }/*End of search()*/

void insert() { int temp,item,position; if(n == MAX) { cout<<"List overflow\n"; return; } cout<<"Enter position for insertion : "; cin>>position; cout<<"Enter the value : "; cin>>item; if(position > n+1 ) { cout<<"Enter position less than or equal to \n"<<n+1; return;

} if( position == n+1 ) /*Insertion at the end */ { arr[n] = item; n = n+1; return; } /* Insertion in between */ temp=n-1; while( temp >= position-1) { arr[temp+1] = arr[temp]; /* shifting right */ temp --; } arr[position-1] = item; n = n +1 ; }/*End of insert()*/

void del() { int temp,position,item; if(n == 0) { cout<<"List emptyvg\n"; return;

} cout<<"Enter the element to be deleted : "; cin>>item; if(item==arr[n-1]) /*Deletion at the end*/ { n = n-1; return; } position=search(item); if(position==0) { cout<<"Element not present in array\n"; return; } /*Deletion in between */ temp=position-1; while(temp <= n-1) { arr[temp] = arr[temp+1]; /* Shifting left */ temp ++; } n=n-1; }/*End of del()*/

void display()

{ int i; if(n==0) { cout<<"List is empty\n"; return; } for(i = 0; i< n; i++) cout<<"Value at position \n"<<i+1<<arr[i]; }/*End of display()*/

/*Program for binary search*/


#include <stdio.h>

main() { int arr[20],start,end,middle,n,i,item;

cout<<"How many elements you want to enter in the array "; cin>>n;

for(i=0; i < n;i++) { cout<<"Enter element "; cin>>arr[i]; } cout<<"Enter the element to be searched : "; cin>>item; start=0; end=n-1; middle=(start+end)/2; while(item != arr[middle] && start <= end) { if(item > arr[middle]) start=middle+1; else

end=middle-1; middle=(start+end)/2; } if(item==arr[middle]) printf("%d found at position %d\n",item,middle+1); if(start>end) printf("%d not found in array\n",item); }/*End of main()*/

//Example of CALL BY reference


#include <iostream.h> #include <conio.h> void swap(int & num,int &num1)//*pointer variable { int c; c=num; num=num1; num1=c;

} main() { int x,y; clrscr(); cout<<"Enter a number:";//8 cin>>x; cout<<"Enter a number:";//8 cin>>y; swap(x,y); cout<<endl<<" x and y="<<x<<"\t"<<y; getch(); }

//Example of public and private data member and member

#include <stdio.h> #include <conio.h> #include <iostream.h> class subtraction { private: int liNumber1; int liNumber2; public: void getData(); //Declaration of function void Sub();

};//end of the class //Defination of function void subtraction::getData() { cout<<"Enter first number:"; cin>>liNumber1; cout<<"Enter second number:"; cin>>liNumber2; } void subtraction::Sub() {

cout<<endl<<"Subtraction="<<liNumber1-liNumber2; } void main() { subtraction obj; // similar int i; clrscr(); obj.getData(); obj.Sub(); getch();

/*Example of static data member


Design a class which display the number of times display operation is performed irrespective of the object responsible for display. */

#include <stdio.h> #include <conio.h> #include <iostream.h> class abc { static int count;//here we just declare it public: void display() { count++; cout<<endl<<"Display called "<<count<<" items"; }

}; int abc::count;// here we define the static data member

void main() { abc obj1,obj2,obj3;

obj1.display(); obj1.display(); obj1.display(); obj2.display(); obj3.display(); }

//Define a class to calculate the factorial of the number given by the user.Intialize the factorial to 1 at the creation of each object. If the number is zero or less default factorial value needs to displayed.
#include <stdio.h> #include <iostream.h> class Factorial { int f; public: Factorial()//default constructor { cout<<"default constructor:"; f=1; } int factorial(int no) { //4 if(no<=0) cout<<endl<<"Factorial="<<f; else { while(no>1) { f=f*no; no--; //cout<<endl<<Factorial<<f; }

cout<<endl<<"Factorial="<<f; } } };//End of class void main() { Factorial obj; obj.factorial(4);

//example of copy constructor


#include <stdio.h> #include <conio.h> #include <iostream.h> class rectangle { int l,b; public: rectangle() { l=0; b=0; } rectangle(int l,int b) { this->l=l; this->b=b; } rectangle(rectangle & obj)//copy constructor { l=obj.l; b=obj.b; } int area() {

return l*b; }

};

main() {

rectangle r1(3,2); rectangle r2=r1;//initialize one object from another cout<<"area of r1:" <<r1.area(); cout<<endl<<"area of r2:" <<r2.area(); getch(); }

//example of Destructor
#include <stdio.h> #include <conio.h> #include <iostream.h> class rectangle { int l,b; public: rectangle() { l=0; b=0;

rectangle(int l1,int b1) { l=l1; b=b1; }

~rectangle() { cout<<endl<<"Destructor invoked";

int area() { return l*b;

};

main() {

rectangle r1;

r1.rectangle(3,2); cout<<"area of r1:" <<r1.area();

getch(); }

//example of friend function


#include <stdio.h> #include <conio.h> # include<iostream.h> class rectangle { int l,b; public: rectangle()//default constructor { l=0; b=0; } rectangle(int l1,int b1)//parametrized constructor { l=l1; b=b1; } int area() { return l*b; } friend rectangle sum(rectangle r1,rectangle r2);//declaring friend function within the class }; rectangle sum(rectangle r1,rectangle r2)

{ rectangle tmp; tmp.l=r1.l+r2.l; tmp.b=r1.b+r2.b; return tmp; } void main() { rectangle r1(3,3),r2(2,2); rectangle r3; r3=sum(r1,r2); cout<<endl<<"Area of r3:"<<r3.area(); }

//unary oprator overloading


#include <stdio.h> #include<iostream.h> class index { private: int count;

public: index() { count=0; } index(int i) { count=i; } index operator++() //prefix { return index(++count); } index operator++(int) //postfix { return index(count++);

} void show() { cout<<" Count="<<count; } }; main() { index c,e;

e=++c;//1 cout<<endl<<"c="; c.show();//1 cout<<endl<<"e="; e.show();

//program of inheritance
#include <stdio.h> #include <conio.h> #include <iostream.h> class Radio { public: Radio() { cout<<endl<<"Radio created"; } }; class Mobile : public Radio { public: Mobile() { cout<<endl<<"Mobile created"; }

}; class WiFi:public Mobile { public:

WiFi() { cout<<endl<<"WiFi created"; } }; main() { WiFi obj; }

//pure virtual function & virtual function


#include <stdio.h> #include <conio.h> #include <iostream.h> class shape { public: virtual void draw()=0;//here the body is zero }; class rectangle:public shape { public:

void draw() { cout<<endl<<"Draw Rectangle"; } }; class circle:public shape { public: void draw() { cout<<endl<<"Draw Circle"; }

}; class Square:public shape { public:

void draw() { cout<<endl<<"Draw Square"; } }; void main() {

shape * ptr; int opt; cout<<endl<<"1.Rectangle"; cout<<endl<<"2.Circle"; cout<<endl<<"3.Square"; cout<<endl<<"Enter you choice:"; cin>>opt; switch(opt) { case 1: ptr=new rectangle(); break;

case 2: ptr=new circle(); break; case 3: ptr=new Square(); break;

} ptr->draw(); }

Write a function to find max out of three basic type using function template.

#include <stdio.h> #include <conio.h> #include <iostream.h>

template <class T>

T greater(T a,T b,T c) { if (a>b && a>c) { return a; } else if(b>a && b>c) { return b; } else { return c; } }

void main() { cout<<endl<<greater(2,5,8); cout<<endl<<greater('A','X','T'); cout<<endl<<greater(2.5,5.8,8.8); }

Write a function to swap two variables of basic type or user defines type using function template.

#include <stdio.h> #include <conio.h> #include <iostream.h> template <class T> T swap(T &a,T &b) { T s; s=a; a=b; b=s; } main() { int a=6,b=66; swap(a,b); cout<<endl<<"a : "<<a; cout<<endl<<"b : "<<b; }

You might also like