Vous êtes sur la page 1sur 46

FUNCTION OVERLOADING AND ENUMERATION #include<iostream.h> #include<conio.

h> class over { public: int max(int,int); float max(float,float); }; int over::max(int num1,int num2) { if(num1>num2) return num1; else return num2; } float over::max(float num1,float num2) { if(num1>num2) return num1; else return num2; } void main() { enum month{Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec}; int n; clrscr(); month m; cout<<"\n\tFunction Overloading and enumeration "; cout<<"\n\t-------------------------------------"; cout<<"\n Enumeration"; cout<<"\n\n Enter a number between 1 to 12 :"; cin>>n; m=(month)n; switch(m) { case 1: case 2: case 3: cout<<"\n\t Autumn"<<endl;

break; case 4: case 5: cout<<"\n\t Summer"<<endl; break; case 6: case 7: case 8: cout<<"\n\t Spring"<<endl; break; case 9: case 10: case 11: case 12: cout<<"\n\t Winter"<<endl; } cout<<"\nFunction overloading to find max of two number\n"; over o1; cout<<"\n\n Max(5.4f,8.6f) = "; cout<<o1.max(5.4f,8.6f)<<endl; cout<<"\n Max(19,34) = "; cout<<o1.max(19,34)<<endl; getch(); }

OUTPUT

Function Overloading and enumeration -----------------------------------------------Enumeration Enter a number between 1 to 12:5 Summer Function overloading to find max of two numbers

Max (5.4f, 8.6f) = 8.6 Max (19, 34) = 34

RESULT The program is done successfully and output is displayed

SCOPE AND STORAGE CLASSES

Storage1.cpp #include<iostream.h> #include<conio.h> int value; void setvalue() { cout<<"\n Enter the value for external variable: "; cin>>value; } void getvalue() { cout<<"\n The value of external variable is "<<value<<endl; } Storage2.cpp #include<D:\veena\OPP\STOR.CPP> #include<iostream.h> #include<conio.h> extern int value; void autofunction(void); void registerfunction(void); void staticfunction(void); void externfunction(void); void refresh() { cout<<"\n Press any key "<<endl; getch(); clrscr(); } void main() { clrscr(); cout<<"\n The result of AUTO_VAR function :"<<endl; autofunction(); refresh(); cout<<"\n The result of REGISTER_VAR function :"<<endl; registerfunction(); refresh(); cout<<"\n The result of STATIC_VAR function :"<<endl; staticfunction();

refresh(); cout<<"\n The result of EXTERN_VAR function :"<<endl; externfunction(); getch(); } void autofunction() { auto int var1,var2; cout<<"\nEnter two numbers :\n"; cin>>var1>>var2; cout<<"\n Now value in var1 = "<<var1; cout<<"\n Now value in var2 = "<<var2; cout<<"\n Swapping without temporary variables "; var2=var1+var2; var1=var2-var1; var2=var2-var1; cout<<"\n Now the value in var1= "<<var1; cout<<"\n Now the value in var2= "<<var2; } void registerfunction() { register int sum=0,n; cout<<"\n Enter a number :\n"; cin>>n; for(int i=1;i<=n;i++) sum+=i; cout<<"\n Sum of first "<<n<<" natural numbers are "<<sum<<endl; } void staticfunction() { static int count=0; count++; cout<<"\n The function retaining previous value is called "<<count<<" times"<<endl; } void externfunction() { setvalue(); getvalue(); }

OUTPUT SCOPE AND STORAGE CLASSES

--------------------------------------------The result of AUTO_VAR function : Enter two numbers : 45 56 Now value in var1 = 45 Now value in var2 = 56 Swapping without temporary variables Now the value in var1= 56 Now the value in var2= 45 The result of REGISTER_VAR function : Enter a number : 10 Sum of first 10 natural numbers are 55

The result of STATIC_VAR function : The function retaining previous value is called 1 times

The result of EXTERN_VAR function : Enter the value for external variable: 5 The value of external variable is 5 RESULT The program is done successfully and output is displayed

IMPLEMENTATION OF ABSTRACT DATATYPE

#include<iostream.h> #include<conio.h> #include<stdio.h> class stack { private: int top; int a[80]; int max; public: void menu(); void push(); void pop(); void peep(); void disp(); void setsize(); }; void stack::setsize() { cout<<"\nEnter the maxmimum capacity of stack :"; cin>>max; max--; top=0; } void stack::menu() { int op; cout<<"\n\t1.push \n\t2.pop \n\t3.peep \n\t4.display"; cout<<"\n Enter your choice:"; cin>>op; switch(op) { case 1: if(top<=max) push(); else cout<<"\n Overflow"; break; case 2: if(top!=0) pop(); else cout<<"\n underflow"; break; case 3: if(top!=0)

peep(); else cout<<"\n stack is empty"; break; case 4: if(top!=0) disp(); else cout<<"\n stack is empty"; break; default: cout<<"\n Invalid option "; } } void stack::push() { int n; cout<<"\n Enter element for stack:"; cin>>n; a[top++]=n; } void stack::pop() { a[top]=0; cout<<"\n The element pop out is "<<a[--top]; } void stack::peep() { cout<<"\n The peep element is"<<a[top-1]; } void stack::disp() { cout<<"\n The stack elements are...... "; for(int i=0;i<top;i++) cout<<a[i]<<"\t"; } class queue { private: int a[10],last,max; public: void menu(); void front(); void rear(); void disp(); void setsize();

}; void queue::setsize() { cout<<"\n Enter the maxmimum capacity of queue :"; cin>>max; max--; last=0; } void queue::menu() { int op; cout<<"\n\t1.Rear \n\t2.Front \n\t3.Display"; cout<<"\n\n Enter your choice : "; cin>>op; switch(op) { case 1: if(last<=max) rear(); else cout<<"\nQueue if full"; break; case 2: if(last!=0) front(); else cout<<"\n Queue is empty"; break; case 3: if(last!=0) disp(); else cout<<"\n Queue is empty"; break;

default: cout<<"\n Invalid option "; } } void queue::rear() { int n; cout<<"\n Enter an element for queue : "; cin>>n; a[last++]=n;

} void queue::front() { cout<<"\n The eliminated item is "<<a[0]; for(int i=0,j=i+1;i<last;i++,j++) a[i]=a[j]; last--; } void queue::disp() { cout<<"\n The queue element are...."; for(int i=0;i<last;i++) cout<<a[i]<<"\t"; } void main() { clrscr(); int op; cout<<"\n\t IMPLEMENTATION OF ABSTRACT DATATYPE"; cout<<"\n\t -----------------------------------------------------------------"; cout<<"\n\n 1.Stack \n 2.Queue"; cout<<"\n Enter your option :"; cin>>op; switch(op) { case 1: cout<<"\n \t Stack program"; cout<<"\n \t --------------"; stack s; s.setsize(); do { s.menu(); cout<<"\n Do you want to continue(y/n)? "; }while(getch()=='y'); break; case 2: cout<<"\n\t Queue Program \n"; cout<<"\n \t --------------"; queue q; q.setsize(); do { q.menu(); cout<<"\n Do you want to continue(y/n)?";

}while(getch()=='y'); break; } getch(); }

OUTPUT

IMPLEMENTATION OF ABSTRACT DATATYPE ----------------------------------------------------------------1.Stack 2.Queue

Enter your option :1 Stack program ------------------Enter the maximum capacity of stack: 4 1.push 2.pop 3.peep 4.display Enter your choice: 1 Enter element for stack: 23 Do you want to continue(y/n)? y Enter your choice: 1 Enter element for stack: 45 Do you want to continue(y/n)? y 1.push 2.pop 3.peep 4.display Enter your choice:1 Enter element for stack:56 Do you want to continue(y/n)? y 1.push 2.pop 3.peep 4.display Enter your choice: 4 The stack elements are...... 23 Do you want to continue(y/n)?y 1.push 2.pop 3.peep 4.display Enter your choice:2 45 56

The element pop out is 56 Do you want to continue(y/n)? y 1.push 2.pop 3.peep 4.display Enter your choice:4 The stack elements are...... 23 45 Do you want to continue(y/n)? y 1.push 2.pop 3.peep 4.display Enter your choice:3 The peep element is45 Do you want to continue(y/n)?y 1.push 2.pop 3.peep 4.display Enter your choice:4 The stack elements are...... 23 Do you want to continue(y/n)?n RESULT The program is done successfully and output is displayed 45

CONSTRUCTOR , DESTRUCTOR AND CONSTRUCTOR OVERLOADING

#include<iostream.h> #include<conio.h> class demo { private:

int length,breadth,area; char *ch; public: demo() { cout<<"\n\t Initialization using default constructor"; cout<<"\n Press any key to continue:\n"; area=0; length=0; breadth=0; ch="DEFAULT"; getch(); } demo(int l,int b,char *name) { cout<<"\n\tInitialization using constructor with arguments"; cout<<"\n press any key to continue"; length=l; breadth=b; ch=name; area=0; getch(); } ~demo() { area=0.5*(length*breadth); cout<<"\n Area of the triangle calculated by"<<ch<<" object is"<<area; getch(); } }; void main() { int l,b; clrscr(); demo d1; cout<<\n To calculate the area of triangle; cout<<"\n\t Enter length and breadth :"; cin>>l>>b; demo(l,b,"overloaded"); }

OUTPUT

Initialization using default constructor Press any key to continue: To calculate the area of triangle Enter length and breadth: 2 4 Initialization using constructor with arguments

Press any key to continue: Area of the triangle calculated by overloaded object is 4 Area of the triangle calculated by DEFAULT object is 0

RESULT The program is done successfully and output is displayed

STATIC MEMBER AND METHODS

#include<iostream.h> #include<conio.h> class staticdemo { private: static int count; public:

char c; void hit() { c=getch(); if(!kbhit()) { cout<<c<<" is pressed\n"; count++; } } static void show() { cout<<count<<" no of items key pressed"; } }; int staticdemo::count=0; void main() { clrscr(); staticdemo sd; cout<<"\n\t STATIC MEMBER AND METHODS"; cout<<"\n --------------------------"; cout<<"\n\n Press any key.... and q to quit\n\n"; do { sd.hit(); } while(sd.c!='q'); staticdemo::show(); getch(); }

OUTPUT

STATIC MEMBER AND METHODS -----------------------------------------------Press any key.... and q to quit D is pressed H is pressed

E is pressed T is pressed Q is pressed q is pressed 6 no of items key pressed

RESULT The program is done successfully and output is displayed

BITFIELDS

#include<iostream.h> #include<conio.h> struct { char name[20]; unsigned int age; unsigned int is_degree:1; unsigned int is_employed:1;

unsigned int is_married:1; unsigned int is_male:1; unsigned int is_indian:1; }person; class bitfields { private: int degree,employe,married,male,indian; public: void setvalues() { cout<<"\n Enter the name: "; cin>>person.name; cout<<"\n Enter the age of "<<person.name<<": "; cin>>person.age; cout<<"\n Enter 1 for true and 0 for false\n\n"; cout<<"\n Whether "<<person.name<<" is employed(0/1): "; cin>>employe; cout<<"\n Whether "<<person.name<<" is married(1/0) :"; cin>>married; cout<<"\n Whether "<<person.name<<" is male(1/0): "; cin>>male; cout<<"\n Whether "<<person.name<<" is indian(1/0):"; cin>>indian; person.is_degree=degree; person.is_employed=employe; person.is_married=married; person.is_male=male; person.is_indian=indian; } void report() { cout<<"\n The person....\n"; if(person.is_degree) cout<<" has degree"; else cout<<" has no degree"; if(person.is_employed) cout<<" is employed"; else cout<<" not employed"; if(person.is_married) cout<<" is married"; else cout<<" not married"; if(person.is_male)

cout<<" is male"; else cout<<" is female"; if(person.is_indian) cout<<" is indian"; else cout<<" not not indian"; } }; void main() { bitfields fields; clrscr(); fields.setvalues(); fields.report(); cout<<"\n Size(byte) of unsigned int is :"<<sizeof(person.age); cout<<"\n Total size(byte) of the struct(person) is: "<<sizeof(person); getch(); }

OUTPUT

BITFIELDS ----------------Enter the name: veena Enter the age of veena: 21 Enter 1 for true and 0 for false

Whether veena is employed(0/1): 1

Whether veena is married(1/0) :0

Whether veena is male(1/0): 0 Whether veena is indian(1/0):1 The person.... has degree is employed not married is female is indian Size(byte) of unsigned int is :2 Total size(byte) of the struct(person) is: 23

RESULT The program is done successfully and output is displayed

BINARY OPERATOR OVERLOADING USING FRIEND FUNCTION #include<iostream.h> #include<conio.h> class binaryoverload { private: int number; public: binaryoverload() { number=0; }

binaryoverload(int val) { number=val; } void display() { cout<<"\n The result is "<<number<<endl; } friend binaryoverload operator+(binaryoverload b1,binaryoverload b2) { binaryoverload temp; temp.number=b1.number+b2.number; return temp; } }; void main() { int n1,n2; clrscr(); cout<<"\n \t BINARY OPERATOR OVERLOADING USING FUNCTION "; cout<<"\n \t --------------------------------------------------"; cout<<"\n\n Enter two numbers :"; cin>>n1>>n2; binaryoverload b1(n1); binaryoverload b2(n2); binaryoverload b3=b1+b2; b3.display(); getch(); }

FRIEND

OUTPUT

BINARY OPERATOR OVERLOADING USING FRIEND FUNCTION --------------------------------------------------------------------------------------Enter two numbers: 32 45 The result is 77

RESULT

The program is done successfully and output is displayed

BINARY OPERATOR OVERLOADING USING MEMBER FUNCTION #include<iostream.h> #include<conio.h> class binaryoverload { private: int number; public: binaryoverload() { number=0; } binaryoverload(int val)

{ number=val; } void display() { cout<<"\n The result is "<<number<<endl; } binaryoverload operator+(binaryoverload b1) { binaryoverload temp; temp.number=number+b1.number; return temp; } }; void main() { int n1,n2; clrscr(); cout<<"\n BINARY OPERATOR OVERLOADING USING MEMBER FUNCTION "; cout<<"\n \t --------------------------------------------------"; cout<<"\n\n Enter two numbers :"; cin>>n1>>n2; binaryoverload b1(n1); binaryoverload b2(n2); binaryoverload b3=b1+b2; b3.display(); getch(); }

OUTPUT

BINARY OPERATOR OVERLOADING USING MEMBER FUNCTION --------------------------------------------------------------------------------------------Enter two numbers: 34 45 The result is 79

RESULT The program is done successfully and output is displayed

UNARY OPERATOR OVERLOAD POSTFIX

#include<iostream.h> #include<conio.h> class unaryoverload { private: int val; public: unaryoverload() { val=0; } void operator++(int)

{ val=val+1; cout<<"\n "<<val; } }; void main() { clrscr(); cout<<"\n\t UNARY OPERATOR OVERLOAD POSTFIX"; cout<<"\n\t -------------------------------\n"; unaryoverload u1; do { u1++; cout<<"\n Continue increment press y"; }while(getch()=='y'); getch(); }

OUTPUT UNARY OPERATOR OVERLOAD POSTFIX ----------------------------------------------------------1 Continue increment press y 2 Continue increment press y 3 Continue increment press y

RESULT The program is done successfully and output is displayed

UNARY OPERATOR OVERLOAD PREFIX

#include<iostream.h> #include<conio.h> class unaryoverload { private: int val; public: unaryoverload() { val=0; } void operator++(void) { cout<<"\n "<<val;

val=val+1; } }; void main() { clrscr(); cout<<"\n\t UNARY OPERATOR OVERLOAD PREFIX"; cout<<"\n\t -------------------------------\n"; unaryoverload u1; do { ++u1; cout<<"\n Continue increment press y"; }while(getch()=='y'); getch(); }

OUTPUT UNARY OPERATOR OVERLOAD PREFIX --------------------------------------------------------0 Continue increment press y 1 Continue increment press y 2 Continue increment press y

RESULT

The program is done successfully and output is displayed

ITERATOR AND CONTAINER #include<iostream> #include<list> using namespace std; void show(list<int>&num) { list<int>::iterator n; for(n=num.begin();n!=num.end();++n) cout<<*n<<" "; } void main() { list<int>list; list.push_back(23); list.push_back(6); list.push_back(13);

list.push_back(18); list.push_back(3); list.push_back(20); cout<<"\n Unsorted list :"; show(list); cout<<"\n Sorted list:"; list.sort(); show(list); cout<<"\n"; }

OUTPUT

Unsorted list :23 6 13 18 3 20 Sorted list:3 6 13 18 20 23 Press any key to continue

RESULT The program is done successfully and output is displayed

FUNCTION TEMPLATE #include<iostream.h> #include<conio.h> //using namespace std; template<class T> void GenericSearch(T TempGenericArray[],T EleToBeSearched) { for(int i=0;i<=5;i++) { if(EleToBeSearched==TempGenericArray[i]) cout<<"\n The element "<<EleToBeSearched<<"is at position "<<i+1; } } void main() { int i; int Array1[]={10,33,34,12,45};

clrscr(); cout<<"\n\t FUNCTION TEMPLATE"; cout<<"\n\t -----------------"; GenericSearch(Array1,12); char Array2[]="asdef"; GenericSearch(Array2,'f'); getch(); }

OUTPUT

FUNCTION TEMPLATE --------------------------------The element 12is at position 4 The element fis at position 5

RESULT The program is done successfully and output is displayed

CLASS TEMPLATE #include<iostream.h> #include<conio.h> template<class T> class stack { int stackpointer; T stackarray[15]; public: stack() { stackpointer=0; } void push(T element); T pop(); } template<class T> void stack<T>::push(T Element) {

if(stackpointer==4) { cout<<"\n Stack Overflow, can't insert"; } else { stackarray[stackpointer]=Element; } } template<class T> T stack<T>::pop() { if(stackpointer==0) { cout<<"\n Stack underflow, can't pop "; } else { stackpointer--; } return stackarray[stackpointer]; } void main() { clrscr(); cout<<"\n\t CLASS TEMPLATE"; cout<<"\n\t -----------------"; stack<int>mystack; mystack.push(10); mystack.push(20); mystack.push(30); cout<<mystack.pop()<<"\n\t"; stack<char>yourstack; yourstack.push('a'); yourstack.push('b'); yourstack.push('c'); cout<<yourstack.pop()<<"\n\t"; cout<<yourstack.pop()<<"\n\t"; cout<<yourstack.pop()<<"\n\t"; getch(); }

OUTPUT

CLASS TEMPLATE -----------------------------Stack underflow, can't pop 30 Stack underflow, can't pop c Stack underflow, can't pop c Stack underflow, can't pop c

RESULT The program is done successfully and output is displayed

INHERITANCE #include<iostream.h> #include<conio.h> class A { protected: int a; public: void getA() { cout<<"\n Enter a :"; cin>>a; } void putA() { cout<<"\n The value of a is "<<a; } }; class B {

protected: int b; public: void getB() { cout<<"\n Enter b:"; cin>>b; } virtual void putB() { cout<<"\n Value of b is "<<b; } }; class C:public B { protected: int c; public: void getC() { cout<<"\n Enter c:"; cin>>c; }

void putC() { cout<<"\n Value of c is "<<c; } }; class D:public A,public B { protected: int d; public: void getD() { cout<<"\n Enter d:"; cin>>d; } void putD() { cout<<"\n Value of d is "<<d; } }; class E:public C

{ protected: int e; public: void getE() { cout<<"\n Enter e:"; cin>>e; } void putE() { cout<<"\n Value of e is "<<e; } }; class F:public A,public C { protected: int f; public: void getF() { cout<<"\n Enter f:"; cin>>f; } void putF() { cout<<"\n Value of f is "<<f; } }; class G:public B { protected: int g; public: void getG() { cout<<"\n Enter g:"; cin>>g; } void putG() { cout<<"\n Value of g is "<<g; } }; class H:public C,public G {

protected: int h; public: void getH() { cout<<"\n Enter h:"; cin>>h; } void putH() { cout<<"\n Value of h is "<<h; } }; void main() { clrscr(); cout<<"\n Single inheritance \n"; cout<<"\n-------------------------\n"; C c; c.getB(); c.getC(); c.putB(); c.putC(); cout<<"\n Multiple inheritance \n "; cout<<"\n-------------------------\n"; D d; d.getA(); d.getB(); d.getD(); d.putA(); d.putB(); d.putD(); cout<<"\n Multilevel inheritance"; cout<<"\n-------------------------\n"; E e; e.getB(); e.getC(); e.getE(); e.putB(); e.putC(); e.putE(); cout<<"\n Hybrid inheritance \n"; cout<<"\n-------------------------\n"; F f; f.getA(); f.getB();

f.getC(); f.getF(); f.putA(); f.putB(); f.putC(); f.putF(); cout<<"\n Hierarchical inheritance \n "; cout<<"\n-------------------------\n"; C c1; G g; cout<<"\n B thru C :"; c1.getB(); cout<<"\n B thru G :"; g.getB(); cout<<"\n B thru C:"; c1.putB(); cout<<"\n B thru G :"; g.putB(); cout<<"\n Multipath inheritance \n"; cout<<"\n-------------------------\n"; H h; h.C::getB(); h.C::putB(); getch(); }

OUTPUT

Single inheritance Enter b:1 Enter c:5 Value of b is 1 Value of c is 5

Multiple inheritance Enter a :4 Enter b:74 Enter d:4 The value of a is 4 Value of b is 74 Value of d is 4

Multilevel inheritance Enter b:5 Enter c:8 Enter e:7 Value of b is 5 Value of c is 8 Value of e is 7

Hybrid inheritance Enter a :5 Enter b:4 Enter c:5 Enter f:5 The value of a is 5 Value of b is 4 Value of c is 5 Value of f is 5

Hierarchical inheritance B thru C : Enter b:4 B thru G : Enter b:4 B thru C: Value of b is 4B thru G : Value of b is 4 Multipath inheritance Enter b:4 Value of b is 4

RESULT The program is done successfully and output is displayed

VIRTUAL FUNCTION

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

cout<<"\n\t -----------------"; shape *ptr,s; circle c; ptr=&s; ptr->draw(); ptr=&c; ptr->draw(); getch(); }

OUTPUT

VIRTUAL FUNCTION ----------------------------Shape is drawn Circle is drawn

RESULT The program is done successfully and output is displayed

EXCEPTION HANDLING #include<iostream.h> #include<conio.h> void main() { int a,b,c; float d; //clrscr(); cout<<\n\t\t EXCEPTION HANDLING; cout<<\n\t\t---------------------------------; cout<<"\n Enter the value of a:"; cin>>a; cout<<"\n Enter the value of b:"; cin>>b; cout<<"\n Enter the value of c:"; cin>>c; try { if((a-b)!=0) { d=c/(a-b); cout<<"\n Result is :"<<d; } else {

throw(a-b); } } catch(int i) { cout<<"\n Answer is infinite because a-b is :"<<i<<"\n"; } getch(); }

OUTPUT EXCEPTION HANDLING ---------------------------------Enter the value of a: 2 Enter the value of b:2 Enter the value of c:4 Answer is infinite because a-b is :0 Press any key to continue

RESULT The program is done successfully and output is displayed

Vous aimerez peut-être aussi