Function Overloading
Function Overloading
Function Overloading
#include<iostream.h> #include<conio.h> class student { int sqr,rect,tria; public: void area(int a); void area(int a,int b); void area(int a,int b,float c); }; void student::area(int a) { sqr=a*a; cout<< " The Area Of Square Is : "<<sqr<<"\n"; } void student::area(int a,int b) { rect=a*b; cout<< " The Area Of Rectangle Is : "<<rect<<"\n"; } void student::area(int a,int b,float c) { tria=a*b*c; cout<<" The Area Of Triangle Is : "<<tria<<"\n"; } void main() { clrscr(); student s; int a,b; float c=0.5; cout<< " Enter the two values \n" ; cin>>a>>b; s.area(a); s.area(a,b); s.area(a,b,c); getch(); }
OUTPUT
Enter The Two Values : 10,20 The Area Of Square Is : 100 The Area Of Rectangle Is : 200 The Area Of Triangle Is : 100
DEFAULT ARGUMENT
#include<iostream.h> #include<conio.h> class defarg { int p,n,r; float si,amount; public: void getdata(int p,int n,int r=10); }; void defarg::getdata(int p,int n,int r) { si=(p*n*r)/100; amount=amount+si; cout<<Simple Interest is<<si<< "\n"<<Amount is<<amount; } void main() { clrscr(); defarg s; s.getdata(10,5); getch(); }
OUTPUT
Simple Interest is 5 Amount is 5
OUTPUT
ENTER THE NUMBER: 10 20 SUM 30 AVERAGE 15
OUTPUT
ENTER THE STRING OBJECTORIENTED ENTER THE STRING PROGRAMMING OBJECTORIENTEDPROGRAMMING
COPY CONSTRUCTOR
#include<iostream.h> #include<conio.h> class student { int i,j; public: student() { i=0; j=0; } student(int a,int b) { i=a; j=b; } student(student&a) { i=a.i; j=a.j; } void display() { cout<<" THE VALUE OF I IS : "<<i<<endl; cout<<" THE VALUE OF J IS : "<<j<<endl; } }; void main() { clrscr(); student s(10,20); cout<<OBJECT S; s.display(); student s1(s); cout<<OBJECT S1; s1.display(); getch(); }
OUTPUT
OBJECT S THE VALUE THE VALUE OBJECT S1 THE VALUE THE VALUE OF I IS 10; OF J IS 20; OF I IS 10; OF J IS 20;
DESTRUCTOR
#include<iostream.h> #include<conio.h> class stud { public: int rollno; stud(){} void get() { cin>>rollno; } void print() { cout<<"Roll number is"<<rollno<<"\n"; } ~stud() { cout<<"Object Destroyed"<<"\n"; } }; void main() { { cout<<"Pgm start"<<"\n"<<Scope1 start; stud s1,s2,s3; s1.get(); s1.print(); s2.get(); s2.print(); s3.get(); s3.print(); cout<<"Scope1 end"<<"\n"; } cout<<"New Object"<<"\n"<<Scope2 start; stud s4; s4.get(); s4.print(); { cout<<"New Object2 "<<"\n"<<Scope3 start; stud s5; s5.get(); s5.print(); cout<<"Scope3 end"<<"\n"; } cout<<"Pgm end"<<"\n"; cout<<Scope2 end; getch(); }
OUTPUT
Pgm start Scope1 start 10 Roll number is 10 15 Roll number is 15 20 Roll number is 20 Scope1 end Object destroyed Object destroyed Object destroyed New object Scope2 start 30 Roll number is 30 New object2 Scope3 start 35 Roll number is 35 Scope3 end Object destroyed Pgm end Scope2 end Object Destroyed
OPERATOR OVERLOADING
#include<iostream.h> #include<conio.h> class OP { int a; public: OP() { } OP(int k) { a=k; } OP operator + (OP n) { OP o; o.a=a+n.a; return o; } OP operator - (OP n) { OP o; o.a=a-n.a; return o; } OP operator * (OP n) { OP o; o.a=a*n.a; return o; } OP operator / (OP n) { OP o; o.a=a/n.a; return o; } void display() { cout<<a<<endl; }; void main() { int a,b; cout<<ENTER THE NUMBERS:; cin>>a>>b; OP o1(a); OP o2(b); OP o3,o4,o5,o6; o3=o1+o2;
o4=o1-o2; o5=o1*o2; o6=o1/o2; cout<<THE SUM IS<<endl; o3.display(); cout<<THE DIFFERENCE IS<<endl; o4.display(); cout<<THE PRODUCT IS<<endl; o5.display(); cout<<THE QUOTIENT IS<<endl; o6.display(); getch(); }
OUTPUT
ENTER THE NUMBERS: 10 5 THE SUM IS 15 THE DIFFERENCE IS 5 THE PRODUCT IS 50 THE QUOTIENT IS 2
OUTPUT
Enter roll number 1 Enter the mark 50 Enter the total mark 200 Roll Number is 1 Mark is 50 Total is 200
OUTPUT
Roll Number is 5
void show() { cout<<x<<"\n"<<y<<"\n"; }}; void main() { clrscr(); first f1(20,20); second s1(10,10); first f2; second s2; s2=f1; s2.show(); f2=s1; f2.show(); getch(); }
OUTPUT
40 0 20 0
INHERITANCE
#include<iostream.h> #include<conio.h> class stud { public: int roll; char name; void getdata() { cout<< ENTER THE NAME:; cin>>name; cout<<ENTER THE ROLL NUMBER:; cin>>roll; } void show() { cout<< NAME IS<<name<<endl; cout<< ROLL NUMBER IS<<roll<<endl; } }; class intm:virtual public stud { public: int m1; void getdata1() { cout<< ENTER THE MARK1:; cin>>m1; } void show1() { cout<<"MARK1 IS"<<m1<<"\n"; } }; class extm:virtual public stud { public: int m2; void getdata2() { cout<< ENTER THE MARK2:; cin>>m2; } void show2() { cout<<"MARK2 IS"<<m2<<"\n"; } }; class tot:public intm,public extm {
public: int tot; void total() { tot=m1+m2; cout<< THE TOTAL MARKS IS; cout<<tot<<"\n"; } }; void main() { clrscr(); tot t1; t1.getdata(); t1.getdata1(); t1.getdata2(); t1.show(); t1.show1(); t1.show2(); t1.total(); getch(); }
OUTPUT
ENTER THE NAME: AAA ENTER THE ROLL NUMBER: 01 ENTER THE MARK1: 90 ENTER THE MARK2: 99 NAME IS AAA ROLL NUMBER IS 01 MARK1 IS 90 MARK2 IS 99 THE TOTAL MARKS IS 189
RUNTIME POLYMORPHISM
#include<iostream.h> #include<conio.h> class base { public: virtual void show() { cout<<"Base"<<"\n"; } }; class derived:public base { public: void show() { cout<<"Derived"<<"\n"; } }; void main() { clrscr(); base b,*ptr_b; derived d,*ptr_d; b.show(); d.show(); ptr_b=&b; ptr_d=&d; ptr_b->show(); ptr_d->show(); ptr_b=&d; ptr_b->show(); getch(); }
OUTPUT
Base Derived Base Derived Derived
CLASS TEMPLATE
#include<iostream.h> #include<conio.h> template<class t,class t1> class stack { t stkpointer; t1 stkarray[10]; public: stack() { stkpointer=0; } void push(t1 value) { if(stkpointer==9) cout<<" stack overflow"; else { stkarray[stkpointer]=value; stkpointer++; } } t1 pop() { if(stkpointer==0) cout<<" Stack empty "; else { stkpointer--; return stkarray[stkpointer]; } } void display() { int i; for(i=0;i<stkpointer;i++) cout<<stkarray[i]<<endl; } void search(int k) { for(int j=0;j<stkpointer;j++) { if(stkarray[j]==k) cout<<" ELEMENT FOUND AT POSITION "<<j<<endl; else cout<<" NOT FOUND"<<endl; } } };
void main() { clrscr(); int s; stack<int,int> s1; s1.push(5); s1.push(6); s1.push(7); cout<<ELEMENTS IN STACK<<endl; s1.display(); cout<<ELEMENT POPPED<<endl; cout<<s1.pop(); s1.display(); cout<<" ENTER THE VALUE TO BE SEARCHED : "; cin>>s; s1.search(s); getch(); }
OUTPUT
Elements in stack 5 6 7 Element popped:7 Elements in stack 5 6 ENTER THE VALUE TO BE SEARCHED: 5 ELEMENT FOUND AT POSITION 1
FUNCTION TEMPLATE
#include<iostream.h> #include<conio.h> template <class A> void bubble(A a[],int n) { for(int i=0;i<n-1;i++) for(int j=n-1;j>i;j--) { if(a[j]<a[j-1]) { A a1; a1=a[j]; a[j]=a[j-1]; a[j-1]=a1; } } } void main() { clrscr(); int r[5]={4,3,5,2,1}; float s[5]={4.4,3.3,5.5,2.2,1.1}; bubble(r,5); bubble(s,5); cout<<THE SORTED VALUES ARE; for(int i=0;i<5;i++) cout<<"\n"<<r[i]; for(int j=0;j<5;j++) cout<<"\n"<<s[j]; getch(); }
OUTPUT
THE SORTED VALUES ARE: 1,2,3,4,5 1.1,2.2,3.3,4.4,5.5.
EXCEPTION HANDLING
#include<iostream.h> #include<conio.h> class exception { public: int n; } int main() { int ch; cout<<Enter Your Choice" cout<<1.Integer\n2.Character\n3.DecimalNumber\n4.Obbject; cin>>ch; try { switch(ch) { case 1: cout<<enter a integer; cin>>a throw a; case 2: cout<<enter a character; cin>>b; throw b; case 3: cout<<enter a decimal number; cin>>c; throw c; case 4: cout<<Enter a value for member variable; cin>>e.n; throw e.n; } } catch(int a) { cout<<a<<"Integer exception caught"; } catch(char a) { cout<<a<<Character exception caught; } catch(double d) { cout<<a<<Decimal exception caught; } catch(exception e) {
OUTPUT
Enter ur choice 1.Integer 2.Character 3.Decimal Value 4.Object 1 enter a integer 5 5 Integer Exception Caught 2 enter a character a a Character Exception Caught 3 enter a decimal value 5.3 5.3 Decimal Exception Caught 4 Enter a value for member variable 100 100 Object Exception Caught
OUTPUT
Value*********Square Root +...1...............+1.0000 +...2...............+1.4142 +...3...............+1.7321 +...4...............+2.0000 +...5...............+2.2361
#include<iostream.h> #include<conio.h> #include<iomanip.h> int main() { cout<<setw(15); cout<<setiosflags(ios::left|ios::fixed); cout<<setprecision(2); cout<<setiosflags(ios::showpoint); cout<<Roll Number; cout<<setw(15)<<Name; cout<<setw(10)<<setprecision(2)<<Marks<<endl; cout<<setw(15)<<1; cout<<setw(15)<<AAAA; cout<<setw(10)<setprecision(2)<<355.50<<endl; cout<<setw(15)<<2; cout<<setw(15)<<BBBB; cout<<setw(10)<<setprecision(2)<<275.00<<endl; cout<<setw(15)<<3; cout<<setw(15)<<CCCC; cout<<setw(10)<<setprecision(2)<<290.75<<endl; cout<<setw(15)<<4; cout<<setw(15)<<DDDD; cout<<setw(10)<<setprecision(2)<<270.0<<endl; cout<<setw(15)<<5; cout<<setw(15)<<EEEE; cout<<setw(10)<<setprecision(2)<<200.30<<endl; cout<<setfill(*)<<\n; getch(); return 0; }
OUTPUT
Roll Number Name Marks 1 AAAA 355.50 2 BBBB 275.00 3 CCCC 290.75 4 DDDD 270.00 5 EEEE 200.60 **************************************
OUTPUT
ROLL NAME ROLL NAME ROLL NAME NO 1 AAA NO 2 BBB NO 3 CCC
Integer vector 10 15
OUTPUT
ROLL NO 1 NAME CCC ROLL NO 2 NAME BBB ROLL NO 3 NAME AAA INTEGER DEQUE 15 10
OUTPUT
ROLL NO 2 NAME BBB
OUTPUT
ROLL NO 13 NAME CCC ROLL NO 23 NAME BBB ROLL NO 33 NAME AAA
OUTPUT
Z:\>javac squareno.java Z:\>javadoc squareno.java Loading source file squareno.java... Constructing Javadoc information... Standard Doclet version 1.6.0_15 Building tree for all the packages and classes... Generating squareno.html... Generating package-frame.html... Generating package-summary.html... Generating package-tree.html... Generating constant-values.html... Building index for all the packages and classes... Generating overview-tree.html... Generating index-all.html... Generating deprecated-list.html... Building index for all classes... Generating allclasses-frame.html... Generating allclasses-noframe.html... Generating index.html... Generating help-doc.html... Generating stylesheet.css...
INHERITANCE
class superclass { public int a; public int b; superclass(int a1,int b1) { a=a1; b=b1; } void show() { System.out.println("Super Class Show Function"); System.out.println("a="+a+"b="+b); } } class subclass extends superclass { public int c; subclass (int a1,int b1,int c1) { super(a1,b1); c=c1; } void sum() { System.out.println("Subclass Sum Function"); System.out.println("c="+c); System.out.println("Sum="+(a+b+c)); } public static void main(String args[]) { subclass s2=new subclass(5,10,5); s2.sum(); s2.show(); } }
OUTPUT
Z:\>javac subclass.java Z:\>java subclass Subclass Sum Function c=5 Sum=20 Super Class Show Function a=5b=10
INTERFACE
import java.io.*; interface showinterface { int a=10; void show(int p); void print(String p); } abstract class example implements showinterface { public int b=20; public void show(int p) { System.out.println("Abstract class"); System.out.println("Inside Show Function"); System.out.println(" Hello world "+p); } } public class example1 extends example { public void print(String n) { System.out.println(Derived class); System.out.println(Inside Print Function); System.out.println(String is +n); } public static void main(String args[]) { example1 e=new example1(); e.show(5); e.print(hello); e.sum(5); } public void sum(int c1) { int c=c1; int d=a+b+c; System.out.println(Derived Class); System.out.println(Inside Sum function); System.out.println(Sum=+d); } }
OUTPUT
Z:\>javac example1.java Z:\>java example1 Abstract class Inside Show Function Hello world 5 Derived class Inside Print Function String is hello Derived Class Inside Sum function Sum=35
JAVA IO
import java.io.*; class ioexample { public static void main(String args[]) throws IOException { InputStreamReader s=new InputStreamReader(System.in); BufferedReader b=new BufferedReader(s); char c; String str; System.out.println("Enter Character q to quit"); do{ c=(char)b.read(); System.out.println(c); }while(c!='q'); System.out.println("Enter the strings, stop to quit"); do { str=b.readLine(); System.out.println(str); }while(!str.equals("stop")); } }
OUTPUT
Z:\>javac ioexample.java Z:\>java ioexample Enter Character q to quit object oriented programming q o b j e c t o r i e n t e d p r o g r a m m i n g q Enter the strings, stop to quit object oriented programming object oriented programming stop stop
MULTITHREADING
import java.util.*; import java.io.*; class threadA extends Thread { public void run() { for(int i=0;i<5;i++) { System.out.println("ThreadA="+i); } } } class threadB extends Thread { public void run() { for(int i=0;i<5;i++) { System.out.println("ThreadB="+i); } } } class multithread { public static void main(String args[]) { threadA a=new threadA(); threadB b=new threadB(); a.start(); b.start(); } }
OUTPUT
Z:\>javac multithread.java Z:\>java multithread ThreadA=0 ThreadB=0 ThreadA=1 ThreadB=1 ThreadA=2 ThreadB=2 ThreadA=3 ThreadB=3 ThreadA=4 ThreadB=4
OUTPUT
Z:\>javac exetry.java Z:\>java exetry a0 Z:\>java exetry hi a1 Divide by zero java.lang.ArithmeticException: / by zero Z:\>java exetry hi hello a2 Array index out of boundjava.lang.ArrayIndexOutOfBoundsException: 50
exceptions e=new exceptions(); e.throweg1(a); e.throweg2(); System.out.println("throw, throws and finally are implemented successfully"); } }
OUTPUT
Z:\>javac exceptionmain.java Z:\>java exceptionmain inside throw example 1 throw eg1 exception caught java.lang.SecurityException: example throw eg1 finally throw eg2 exception caught java.lang.NullPointerException: example throw eg2 finally throw, throws and finally are implemented successfully
PACKAGE
Package p1 Class addsub package p1; public class addsub { public void addsub1(int x ,int y) { System.out.println("Package 1"); System.out.println("Class addsub"); System.out.println("Addition="+(x+y)); System.out.println("Subraction="+(x-y)); } } Package p2 Class muldiv package p2; public class muldiv { public void muldiv1(int x,int y) { System.out.println("Package 2"); System.out.println("Class Muldiv"); System.out.println("multiplication="+(x*y)); System.out.println("division="+(x/y)); } } Class packmain import java.util.*; import p1.*; import p2.*; class packmain { public static void main(String args[]) { System.out.println("Enter two numbers"); Scanner in=new Scanner(System.in); int a=in.nextInt(); int b=in.nextInt(); addsub a1=new addsub(); muldiv m1=new muldiv(); a1.addsub1(a,b); m1.muldiv1(a,b); } }
OUTPUT
Z:\>cd p1 Z:\p1>javac addsub.java Z:\p1>cd.. Z:\>cd p2 Z:\p2>javac muldiv.java Z:\p2>cd.. Z:\>javac packmain.java Z:\>java packmain Enter two numbers 10 20 Package 1 Class addsub Addition=30 Subraction=-10 Package 2 Class Muldiv multiplication=200 division=0