//DEFAULT ARGUMENTS #include<iostream.h> #include<conio.

h> void main() { int a; clrscr(); void fib(int c,int a=0,int b=1); cout<<"Enter a no.:"; cin>>a; fib(a); getch(); } void fib(int c,int a,int b) { cout<<"The fibonacci series is:"; int d=0; for(;c>0;c--) { d=d+a; cout<<endl<<d; a=b; b=d; } }

//DEFAULT ARGUMENTS OUTPUT: Enter a no.:8 The fibonacci series is: 0 1 1 2 3 5 8 13

//CALL BY VALUE #include<iostream.h> #include<conio.h> void swap(int,int); void main() { int iVar1,iVar2; clrscr(); cout<<"enter two no.s:"<<endl; cin>>iVar1>>iVar2; swap(iVar1,iVar2); cout<<"In main: "<<iVar1<<" "<<iVar2<<endl; getch(); } void swap(int iNum1,int iNum2) { int iTemp; iTemp=iNum1; iNum1=iNum2; iNum2=iTemp; cout<<"In swap: "<<iNum1<<" "<<iNum2<<endl; }

//CALL BY VALUE OUTPUT: enter two no.s: 2 5 In swap: 5 2 In main: 2 5

//CALL BY REFERENCE #include<iostream.h> #include<conio.h> void swap(int &inum1,int &inum2); void main() { int ivar1,ivar2; clrscr(); cout<<"enter two no.s"<<endl; cin>>ivar1>>ivar2; swap(ivar1,ivar2); cout<<"in main: "<<ivar1<<" "<<ivar2<<endl; getch(); } void swap(int &inum1,int &inum2) { int itemp; itemp=inum1; inum1=inum2; inum2=itemp; cout<<"in swap: "<<inum1<<" "<<inum2<<endl; }

//CALL BY REFERENCE OUTPUT: enter two no.s 5 8 in swap: 8 5 in main: 8 5

//CLASSES WITH ARRAY AS DATA MEMBERS #include<iostream.h> #include<conio.h> class sum { int num[6],total; public: void getdata() { cout<<"enter the numbers:"<<endl; for(int i=0;i<=5;i++) cin>>num[i]; } void putdata() { total=0; for(int i=0;i<=5;i++) total+=num[i]; cout<<"\nTotal="<<total<<endl; } }; void main() { sum t; clrscr(); t.getdata(); t.putdata(); getch(); }

//CLASSES WITH ARRAY AS DATA MEMBERS OUTPUT: enter the numbers: 4 6 7 45 66 3 Total=131

//CLASSES WITH STATIC DATA MEMBERS #include<iostream.h> #include<conio.h> class test { int code; static int count; public: void setcode() { code=++count; } void showcode() { cout<<"object number:"<<code<<endl; } static void showcount() { cout<<"count:"<<count<<endl; } }; int test::count; void main() { clrscr(); test t1,t2; t1.setcode(); t2.setcode(); test::showcount(); test t3; t3.setcode(); test::showcount(); t1.showcode(); t2.showcode(); t3.showcode(); getch(); }

//CLASSES WITH STATIC DATA MEMBERS OUTPUT: count:2 count:3 object number:1 object number:2 object number:3

//CLASSES WITH CONSTRUCTORS #include<iostream.h> #include<conio.h> class crectangle { int width,height; public: crectangle(int,int); int area() { return (width*height); } }; crectangle::crectangle(int a,int b) { width=a; height=b; } void main() { clrscr(); crectangle rect(3,4); crectangle rectb(5,6); cout<<"rect area "<<rect.area()<<endl; cout<<"rectb area"<<rectb.area()<<endl; getch(); }

//CLASSES WITH CONSTRUCTORS OUTPUT: rect area 12 rectb area30

//UNARY OPERATOR OVERLOADING #include<iostream.h> #include<conio.h> class space { int x,y,z; public: void getdata(int,int,int); void display(); void operator-(); }; void space::getdata(int a,int b,int c) { x=a; y=b; z=c; } void space::display() { cout<<"\n"<<x<<"\n"<<y<<"\n"<<z<<endl; } void space::operator-() { x=-x; y=-y; z=-z; } void main() { space s; clrscr(); s.getdata(10,-20,30); cout<<"\nbefore unary operation s:"; s.display(); -s; cout<<"\nafter unary operation s:"; s.display(); getch(); }

//UNARY OPERATOR OVERLOADING OUTPUT: before unary operation s: 10 -20 30 after unary operation s: -10 20 -30

//BINARY OPERATOR OVERLOADING #include<iostream.h> #include<conio.h> class complex { float x,y; public: complex() {} complex(float real,float img) { x=real; y=img; } complex operator+(complex); void display(); }; complex complex::operator+(complex c) { complex temp; temp.x=x+c.x; temp.y=y+c.y; return temp; } void complex::display() { cout<<x<<"+i"<<y<<"\n"; } void main() { clrscr(); complex c1,c2,c3; c1=complex(2.5,3.5); c2=complex(1.6,2.7); c3=c1+c2; cout<<"c1="; c1.display(); cout<<"c2="; c2.display(); cout<<"c3="; c3.display();

getch(); }

//BINARY OPERATOR OVERLOADING OUTPUT: c1=2.5+i3.5 c2=1.6+i2.7 c3=4.1+i6.2

//FUNCTION OVERLOADING #include<iostream.h> #include<conio.h> int volume(int); double volume(double,int); long volume(long,int,int); void main() { clrscr(); cout<<"volume of cube:"<<volume(10)<<"\n"; cout<<"volume of cuboid:"<<volume(100,75,15)<<"\n"; cout<<"volume of sphere:"<<volume(2.18,8)<<"\n"; getch(); } int volume(int s) { return(s*s*s); } double volume(double r,int h) { return(3.14*r*r*h); } long volume(long l,int b,int h) { return(l*b*h); }

//FUNCTION OVERLOADING OUTPUT: volume of cube:1000 volume of cuboid:112500 volume of sphere:119.380288

//MULTILEVEL INHERITENCE #include<iostream.h> #include<conio.h> class student { protected: int roll; public: void getnumber(int); void putnumber(); }; void student::getnumber(int a) { roll=a; } void student::putnumber() { cout<<"\nroll no. "<<roll; } class test:public student { protected: float sub1,sub2; public: void getmarks(float,float); void putmarks(); }; void test::getmarks(float x,float y) { sub1=x; sub2=y; } void test::putmarks() { cout<<"\nsub1 marks:"<<sub1; cout<<"\nsub2 marks:"<<sub2; }

class result:public test { float total; public: void display(); }; void result::display() { total=sub1+sub2; putnumber(); putmarks(); cout<<"\ntotal:"<<total; } void main() { clrscr(); result r; r.getnumber(100); r.getmarks(75.9,59.6); r.display(); getch(); }

//MULTILEVEL INHERITENCE OUTPUT: roll no. 100 sub1 marks:75.900002 sub2 marks:59.599998 total:135.5

//VIRTUAL BASE CLASS #include<iostream.h> #include<conio.h> class student { protected: int roll_number; public: void get_number(int a) { roll_number=a; } void put_number() { cout<<"ROLL NO IS:"<<roll_number<<"\n"; } }; class test:virtual public student { protected: float part1,part2; public: void get_marks(float x,float y) { part1=x;part2=y; } void put_marks() { cout<<"MARKS OBTAINED:"<<"\n"; cout<<"PART1:"<<part1<<"\n"; cout<<"PART2:"<<part2<<"\n"; } }; class sports:public virtual student { protected: float score; public: void get_score(float s) { score=s; }

void put_score() { cout<<"sports wt.:"<<score<<"\n"; } }; class result:public test,public sports { float total; public: void display(); }; void result::display() { total=part1+part2+score; put_number(); put_marks(); put_score(); cout<<"total score:"<<total<<"\n"; } void main() { clrscr(); result student_1; student_1.get_number(678); student_1.get_marks(30.5,25.5); student_1.get_score(7.0); student_1.display(); getch(); }

//VIRTUAL BASE CLASS OUTPUT: ROLL NO IS:678 MARKS OBTAINED: PART1:30.5 PART2:25.5 sports wt.:7 total score:63

//FILE HANDLING #include<iostream.h> #include<conio.h> #include<fstream.h> #include<iomanip.h> class inventory { char name[10]; int code; float cost; public: void getdata() { cout<<"enter name ";cin>>name; cout<<"enter code ";cin>>code; cout<<"enter cost ";cin>>cost; } void putdata() { cout<<setw(10)<<name <<setw(10)<<code <<setprecision(2)<<setw(10)<<cost<<endl; } }; int main() { clrscr(); inventory item; fstream inoutfile; inoutfile.open("stock.dat",ios::ate|ios::out|ios::binary); inoutfile.seekg(0,ios::beg); cout<<"current contents of stock "<<"\n"; while(inoutfile.read((char*)&item,sizeof item)) { item.putdata(); } inoutfile.clear(); //ADD ONE MORE ITEM cout<<"\nADD AN ITEM\n"; item.getdata(); char ch;

cin.get(ch); inoutfile.write((char*)&item,sizeof item); //display the appended file inoutfile.seekg(0); cout<<"contents of appended file\n"; while(inoutfile.read((char*)&item,sizeof item)) { item.putdata(); } //find no of objects in the file int last=inoutfile.tellg(); int n=last/sizeof(item); cout<<"number of objects="<<n<<"\n"; cout<<"total bytes in the file="<<last<<"\n"; //MODIFY THE DETAILS OF AN ITEM cout<<"enter the object number to be updated\n"; int object; cin>>object; cin.get(ch); int location=(object-1)*sizeof(item); if(inoutfile.eof()) inoutfile.clear(); inoutfile.seekp(location); cout<<"enter new values of the objects\n"; item.getdata(); cin.get(ch); inoutfile.write((char*)&item,sizeof item)<<flush; //SHOW UPDATED FILE inoutfile.seekg(0); cout<<"contents of updated file\n"; while(inoutfile.read((char*)&item,sizeof item)) { item.putdata(); } inoutfile.close(); getch(); return(0); }

//FILE HANDLING OUTPUT: current contents of stock ADD AN ITEM enter name san enter code 123 enter cost 45.6 contents of appended file number of objects=0 total bytes in the file=0 enter the object number to be updated 1 enter new values of the objects enter name sam enter code 456 enter cost 556.78 contents of updated file

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.