Anda di halaman 1dari 61

WRITE A PROGRAM TO FIND AREA OF RECTANGLE USING INLINE FUNCTIONS #include<iostream.h> #include<conio.

h> inline int area(int a,int b) { return (a*b); } void main() { int a,b,ar; cout<<"enter the length"; cin>>a; cout<<"enter the breath"; cin>>b; ar=area(a,b); cout<<"the area is "<<ar; } OUTPUT enter the length 42 enter the breath 30 the area is 1260

WRITE A PROGRAM TO FIND SIMPLE INTEREST USING DEFAULT ARGUMENT #include<iostream.h> #include<conio.h> #include<stdio.h> float interest(int p,int t,float r=0.15) { return(p*t*0.15); } void main() { int p,t,inter; cout<<"enter the principle"; cin>>p; cout<<"enter the time period in year"; cin>>t; inter=interest(p,t); cout<<"the interest is "<<inter; getch(); } OUTPUT enter the principle30000 enter the time period in year 1 the interest is 4500

WRITE A PROGRAM TO FIND VOLUME OF CYLINDER CUBE AND CUBOID USING FUNCTION OVERLOADING #include<iostream.h> #include<conio.h> #include<stdio.h> void volume(int a,int b); void volume(int a,int b,int c); void volume(int a); void main() { volume(2,4); volume(2,4,6); volume(3); getch(); } void volume(int a,int b) { float vol; float pie=3.14; vol=a*b*pie; cout<<"\nthe volume of cylinder is"<<vol; } void volume(int a,int b,int c) { int vol; vol=a*b*c; cout<<"\nthe volume of cuboid is"<<vol; } void volume(int a) { int vol;

vol=a*a*a; cout<<"\nthe volume of cube is"<<vol; } OUTPUT the volume of cylinder is 25.12 the volume of cuboid is 48 the volume of cube is 27

WRITE A PROGRAM TO FIND LARGEST OF TWO NUMBERS USING CLASS AND NESTING THE FUNCTION #include<iostream.h> #include<conio.h> #include<stdio.h> class large { int a,b; public: void getdata() { cout<<"enter two numbers"; cin>>a>>b; } void largest() { if (a>b) cout<<"largest number="<<a; else cout<<"largest number="<<b; } void putdata() { largest(); } }; void main() { large obj; obj.getdata(); obj.putdata(); }

OUTPUT enter two numbers 52 30 largest number=52

WRITE A PROGRAM TO SHOW SCOPE RESOLUTION OPERATOR #include<iostream.h> #include<conio.h> int a=10; void main() { int a=20; { int b; b=a; int a=30; cout<<"\na="<<a; cout<<"\nb="<<b; cout<<"\na="<<::a; } cout<<"\na="<<a; } OUTPUT a=30 b=20 a=10 a=20

WRITE A PROGRAM TO SHOW STATIC DATA MEMBER #include<conio.h> #include<iostream.h> class item { int number; static int count; public: void getdata() { count ++; } void getcount() { cout<<"\ncount="<<count; } }; int item :: count; void main() { item a,b,c; a.getcount(); b.getcount(); c.getcount(); a.getdata(); b.getdata(); c.getdata(); cout<<"\nafter reading data"; a.getcount(); b.getcount(); c.getcount(); }

OUTPUT count=0 count=0 count=0 after reading data count=3 count=3 count=3

WRITE A PROGRAM TO SHOW STATIC MEMBER FUNCTION #include<conio.h> #include<iostream.h> class item { int code; static int count; public : void setcode() { code=count ++; } void showcode() { cout<<"\nobject number"<<code; } static void showcount() { cout<<"\ncount"<<count; } }; int item ::count; void main() { item a,b; a.setcode(); b.setcode(); item :: showcount(); item c; c.setcode(); item :: showcount();

a.showcode(); b.showcode(); c.showcode(); } OUTPUT Count 2 Count 3 object number 0 object number 1 object number 2

WRITE A PROGRAM TO ENTER NAME AGE SALARY OF 5 EMPLOYEES USING ARRAY OF OBJECTS #include<iostream.h> #include<conio.h> class employee { char name[20]; int age,salary; public: void getdata(); void putdata(); }; const int size=5; void employee :: getdata() { cout<<"\n enter name="; cin>>name; cout<<"\nenter age="; cin>>age; cout<<"\nsalary="; cin>>salary; } void employee :: putdata() { cout<<"\ndata"; cout<<"\nname is="<<name; cout<<"\nage is="<<age; cout<<"\nsalary is="<<salary; } void main() { employee emp[5]; for(int i=0;i<size;i++)

emp[i].getdata(); for(int j=0;j<size;j++) emp[j].putdata(); } OUTPUT enter name=Himanshu enter age=18 salary=50000 enter name=Akash enter age=15 salary=20000 enter name=Shivam enter age=20 salary=30000 enter name=Raaj enter age=28 salary=25000 enter name=Kaku

enter age=23 salary=16000 data name is=Himanshu age is=18 salary is=50000 data name is=Akash age is=15 salary is=20000 data name is=Shivam age is=20 salary is=30000 data name is=Raaj age is=28 salary is=25000 data name is=Kaku age is=23 salary is=16000

WRITE A PROGRAM TO ADD TWO TIMES #include<stdio.h> #include<iostream.h> #include<conio.h> class time { int hours; int minutes; public: void gettime(int h,int m) { hours=h; minutes=m; } void puttime() { cout<<"\nhours="<<hours<<" minutes="<<minutes; } void sum(time,time); }; void time:: sum(time t1,time t2) { cout<<"\n\nAfter adding two times"; minutes=t1.minutes+t2.minutes; hours=minutes/60; minutes=minutes%60; hours=hours+t1.hours+t2.hours; } void main() { time t1,t2,t3; t1.gettime(2,45);

t1.puttime(); t2.gettime(3,45); t2.puttime(); t3.sum(t1,t2); t3.puttime(); getch(); } OUTPUT hours=2 minutes=45 hours=3 minutes=45 After adding two times hours=6 minutes=30

WRITE A PROGRAM TO SWAP PRIVATE DATA MEMBER OF TWO DIFFERENT CLASSES USING FRIEND FUNCTION #include<conio.h> #include<iostream.h> class two; class one { int x; public: void setvalue(int i) { x=i; } void display() { cout<<"x="<<x; }; friend void swap(one,two); }; class two { int y; public: void setvalue(int j) { y=j; } void display() { cout<<"y="<<y; }; friend void swap(one,two);

}; void swap(one a,two b) { int temp; temp=a.x; a.x=b.y; b.y=temp; cout<<"x="<<a.x<<"y="<<b.y; } void main() { one on; two tw; cout<<"\nBefore swapping "; on.setvalue(10); tw.setvalue(20); on.display(); tw.display(); cout<<"\nAfter swapping "; swap(on,tw); } OUTPUT Before swapping x=10 y=20 After swapping x=20 y=10

WRITE A PROGRAM TO FIND MAXIMUM OF TWO NUMBERS BELONGING TO TWO DIFFERENT CLASSES USING FRIEND FUNCTION #include<conio.h> #include<iostream.h> class XYZ; class ABC { int x; public: void setvalue(int i) { x=i; } friend void max(ABC,XYZ); }; class XYZ { int y; public: void setvalue(int j) { y=j; } friend void max(ABC,XYZ); }; void max(ABC a,XYZ b) { if(a.x>=b.y) cout<<"maximum value is "<<a.x; else

cout<<"maximum value is "<<b.y; } void main() { ABC abc; abc.setvalue(10); XYZ xyz; xyz.setvalue(20); max(abc,xyz); } OUTPUT maximum value is 20

WRITE A PROGRAM TO FIND MEAN OF TWO NUMBERS USING FRIEND FUNCTION #include<conio.h> #include<iostream.h> class sample { int a; int b; public: void setvalue() { a=25; b=40; } friend float mean(sample s); }; float mean(sample s) { return float(s.a+s.b)/2; } void main() { sample x; x.setvalue(); cout<<"mean="<<mean(x); } OUTPUT mean=32.5

WRITE A PROGRAM TO SWAP PRIVATE DATA OF TWO CLASSES USING REFERENCE OF THE OBJECT WITH THE HELP OF FRIEND FUNCTION #include<iostream.h> #include<conio.h> #include<stdio.h> class class2; class class1 { int var1; public: void setvalue(int a) { var1=a; } void display() { cout<<var1; }; friend void swap(class1 &obj1,class2 &obj2); }; class class2 { int var2; public: void setvalue(int a) { var2=a; } void display() { cout<<var2; };

friend void swap(class1 &obj1,class2 &obj2); }; void swap(class1 &obj1,class2 &obj2) { int temp; temp=obj1.var1; obj1.var1=obj2.var2; obj2.var2=temp; } void main() { class1 c1; class2 c2; c1.setvalue(100); c2.setvalue(200); cout<<"before swapping"; c1.display(); c2.display(); swap(c1,c2); cout<<"after swapping"; c1.display(); c2.display(); getch(); } OUTPUT before swapping 100 200 after swapping 200 100

WRITE A PROGRAM TO ADD TWO COMPLEX NUMBERS USING CONSTRUCTOR #include<iostream.h> #include<stdio.h> #include<conio.h> class complex { float x,y; public: complex() { x=y=0; } complex(float a) { x=y=a; } complex(float real,float img) { x=real; y=img; } friend complex sum(complex,complex); friend void show(complex); }; complex sum(complex c1,complex c2) { complex c3; c3.x=c1.x+c2.x; c3.y=c1.y+c2.y; return(c3); } void show(complex c)

{ cout<<c.x<<"\t"<<c.y; } void main() { complex a(2.7,3.5); complex b(1.6); complex c; c=sum(a,b); cout<<"a="; show(a); cout<<"b="; show(b); cout<<"c="; show(c); getch(); } OUTPUT a=2.7 3.5b=1.6 1.6c=4.3 5.1

WRITE A PROGRAM TO SHOW COPY CONSTRUCTOR #include<stdio.h> #include<conio.h> #include<iostream> class code { int id; public: code(int a) { id=a; } code(code&x) { id=x.id; } void display() { cout<<id; } }; void main() { code A(100); code B(A); code C=A; cout<<"\nid of A"; A.display(); cout<<"\nid of B"; B.display(); cout<<"\nid of C"; C.display();

getch(); } OUTPUT id of A100 id of B100 id of C100

WRITE A PROGRAM TO SHOW DESTRUCTOR #include<stdio.h> #include<conio.h> #include<iostream> int count =0; class alpha { public: alpha() { count++; cout<<"\nnumber of object created"<<count; } ~alpha() { cout<<"\nnumber of object destroy"<<count; count--; } }; int main() { cout<<"\nENTER MAIN"; alpha A1,A2,A3,A4; { cout<<"\nENTER BLOCK1"; alpha A5; } { cout<<"\nENTER BLOCK2"; alpha A6; } cout<<"\nRE-ENTER MAIN"; getch();

} OUTPUT ENTER MAIN number of object created1 number of object created2 number of object created3 number of object created4 ENTER BLOCK1 number of object created5 number of object destroy5 ENTER BLOCK2 number of object created5 number of object destroy5 RE-ENTER MAIN

WRITE A PROGRAM TO SHOW SINGLE INHERITANCE #include<stdio.h> #include<conio.h> #include<iostream> class B { int a; public: int b; void get_ab(); int get_a(); void show_a(); }; class D : public B { int c; public: void mul(); void display(); }; void B :: get_ab() { a=5; b=10; } int B :: get_a() { return a; } void B :: show_a() { }

void D :: mul() { c=b * get_a(); } void D :: display() { cout<<"\na="<<get_a(); cout<<"\nb="<<b; cout<<"\nc="<<c; } int main() { D d; d.get_ab(); d.mul(); d.show_a(); d.display(); cout<<"\nafter b =20"; d.b=20; d.mul(); d.display(); getch(); } OUTPUT a=5 b=10 c=50 after b =20 a=5 b=20 c=100

WRITE A PROGRAM TO SHOW MULTIPLE INHERITANCE #include<iostream.h> #include<conio.h> class A { protected: int m; public: void getm(int); }; class B { protected: int n; public: void getn(int); }; class C : public A, public B { public: void display(void); }; void A :: getm(int x) { m=x; } void B :: getn(int y) { n=y; } void C :: display() {

cout<<"\nm="<<m; cout<<"\nn="<<n; cout<<"\nm*n="<<m*n; } void main() { C c; c.getm(10); c.getn(20); c.display(); return 0; } OUTPUT m=10 n=20 m*n=200

WRITE A PROGRAM TO SHOW MULTILEVEL INHERITANCE #include<iostream.h> #include<conio.h> class student { protected: int rollno; public: void getnumber(int); void putnumber(); }; void student::getnumber(int a) { rollno=a; } void student::putnumber() { cout<<"Roll no="<<rollno; } class test : public student { protected: int sub1; int sub2; public : void getmarks(int,int); void putmarks(); }; void test::getmarks(int x,int y) { sub1=x; sub2=y;

} void test:: putmarks() { cout<<"\nMarks in sub1="<<sub1; cout<<"\nMarks in sub2="<<sub2; } class result: public test { protected : int total; public: void display(); }; void result ::display() { total=sub1+sub2; putnumber(); putmarks(); cout<<"\nTotal marks="<<total; } void main() { result R1; R1.getmarks(70,90); R1.getnumber(150); R1.display(); } OUTPUT Roll no=150 Marks in sub1=70 Marks in sub2=90 Total marks=160

WRITE A PROGRAM TO SHOW HYBRID INHERITANCE #include<iostream.h> #include<conio.h> class student { protected: int rollnumber; public: void getnumber(int a) { rollnumber=a; } void putnumber() { cout<<"\nRoll no="<<rollnumber; } }; class test : public student { protected: int part1,part2; public: void getmarks(int x,int y) { part1=x,part2=y; } void putmarks() { cout<<"\n Marks obtained"; cout<<"\n Part1="<<part1; cout<<"\n Part2="<<part2; }

}; class sports { protected : int score; public: void getscore(int s) { score=s; } void putscore() { cout<<"\nSports marks="<<score; } }; class result : public test,public sports { int total; public: void display(); }; void result :: display() { total=part1+part2+score; putnumber(); putmarks(); putscore(); cout<<"\nTotal score="<<total; } void main() { result student1; student1.getnumber(108); student1.getmarks(60,70);

student1.getscore(50); student1.display(); } OUTPUT Roll no=108 Marks obtained Part1=60 Part2=70 Sports marks=50 Total score=180

WRITE A PROGRAM TO CALL A MEMBER FUNCTION OF A CLASS USING A NON MEMBER FUNCTION #include<iostream.h> #include<conio.h> class member { int a,b; public: void putdata(int num1,int num2); void getdata(); }; void member :: putdata(int num1,int num2) { a=num1; b=num2; } void member :: getdata() { cout<<"a="<<a; cout<<"\nb="<<b; } void display() { member m; m.putdata(10,20); m.getdata(); } void main() { display(); }

OUTPUT a=10 b=20

WRITE A PROGRAM TO SHOW HIERARCHICAL INHERITANCE #include<iostream.h> #include<conio.h> #include<math.h> class bank { char name[20]; int ac_no; float r; float t; int ac_type; public: float p; float ret_si(); float ret_ci(); int chk_ac(); char show_name(); int show_acno(); void detail(void); void input(void); }; class current:public bank { float s; float bal; public: void si(void); void display(void); }; class saving:public bank {

float c; float total; public: void ci(void); void display1(void); }; //-------------------------------------------------------int bank::chk_ac() { cout<<"\n\t\DOLLAR BANK"; cout<<"\n\n\t##Main menu##" ; cout<<"\nPress 1 for current account"; cout<<"\nPress 2 for saving account"; cout<<"\n\nEnter Customer account type: "; cin>> ac_type; cout<<"\n-----------------------------------------------------------------\n"; return ac_type; } void bank::detail(void) { cout<<"\nEnter customer name: "; cin>>name; cout<<"\nEnter customer Account Number: "; cin>>ac_no; cout<<"\n------------------------------------------------------------------\n"; } char bank::show_name() { cout<<"\nName of Customer:"<< name; } int bank::show_acno() {

cout<<"\nCustomer's account no: "<< ac_no; } void bank::input(void) { cout<<"\n\nEnter principal: "; cin>> p; cout<<"\nEnter Rate: "; cin>>r; cout<<"\nEnter Time in Years: "; cin>>t; } //--------------------------------------------------------------float bank::ret_si() { return t*r; } void current::si(void) { s=(p*ret_si())/100; bal=s+p; } void current::display(void) { show_name(); show_acno(); cout<<"\nAccount type: Current Account"; cout<<"\nAmount recieved from Customer: "<<p; cout<<"\nInterest on amount: "<<s; cout<<"\nBalance: "<<bal; } //-----------------------------------------------------float bank::ret_ci() { return p*(pow((1+r/100),t));

} void saving::ci(void) { c=ret_ci()-p; total=p+c; } void saving::display1(void) { show_name(); show_acno(); cout<<"\nAccount type: Saving Account"; cout<<"\nAmount recieved: "<<p; cout<<"\nInterest on amount: "<<c; cout<<"\ntotal amount: "<<total; } float main() { bank b; saving s; current c; int x; x=b.chk_ac(); if (x==1) { c.detail(); c.input(); c.si(); c.display(); } if(x==2) { s.detail(); s.input(); s.ci();

s.display1(); } return 0; } OUTPUT DOLLAR BANK ##Main menu## Press 1 for current account Press 2 for saving account Enter Customer account type: 1 -----------------------------------------------------------------Enter customer name: HIMANSHU Enter customer Account Number: 12536 ------------------------------------------------------------------Enter principal: 10000 Enter Rate: 2 Enter Time in Years: 2 Name of Customer:HIMANSHU Customer's account no: 12536

Account type: Current Account Amount recieved from Customer: 10000 Interest on amount: 400 Balance: 10400

WRITE A PROGRAM TO SHOW OVERLOADING OF BINARY OPERATOR #include<iostream.h> #include<conio.h> #include<stdio.h> class complex { float x,y; public: complex() { } complex(float real,float imag) { x=real,y=imag; } 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() { complex c1,c2,c3;

c1=complex(2.5,1.5); c2=complex(3.2,1.7); c3=c1+c2; cout<<"c1="; c1.display(); cout<<"c2="; c2.display(); cout<<"c3="; c3.display(); } OUTPUT c1=2.5+i1.5 c2=3.2+i1.7 c3=5.7+i3.2

WRITE A PROGRAM TO SHOW USE OF THIS POINTER #include<iostream.h> #include<conio.h> class person { char name[50]; int age; public: person(char *s,int a) { strcpy(name,s); age=a; } person greater(person &x) { if(x.age>=age) return x; else return *this; } void display() { cout<<"\nName:"<<name; cout<<"\nAge:"<<age; } }; void main() { person p1("John",37); person p2("Ram",29); person p3("Shyam",43); person p= p1.greater(p3);

cout<<"\nElder person is:"; p.display(); p=p1.greater(p2); cout<<"\nElder person is:"; p.display(); }

OUTPUT Elder person is: Name:Shyam Age:43 Elder person is: Name:John Age:37

WRITE A PROGRAM USING VIRTUAL FUNCTIONS #include<iostream.h> #include<conio.h> class base { public: void display() { cout<<"\nDisplay base"; } virtual void show() { cout<<"\nShow base"; } }; class derived :public base { public: void display() { cout<<"\nDisplay derived"; } void show() { cout<<"\nShow derived"; } }; void main() { base b; derived d; base *bptr; cout<<"\n bptr points to base";

bptr=&b; bptr-> display(); bptr->show(); cout<<"\n bptr points to derived"; bptr=&d; bptr-> display(); bptr->show(); return 0; } OUTPUT bptr points to base Display base Show base bptr points to derived Display base Show derived

WRITE A PROGRAM TO SHOW FUNCTION TEMPLATE #include<iostream.h> #include<conio.h> template <class e> void exchange(e&a,e&b) { e t=a; a=b; b=t; } void main() { int x=5,y=8; cout<<"\nBefore exchange"<<x<<y; exchange(x,y); cout<<"\nAfter exchange"<<x<<y; } OUTPUT Before exchange5 8 After exchange8 5

WRITE A PROGRAM TO SHOW CONCATENATION OF STRINGS USING OPERATOR OVERLOADING #include<iostream.h> #include<conio.h> class string { char str[80]; public: string() { strcpy(str," "); } string(char s[]) { strcpy(str,s); } void display() { cout<<str; } string operator+ (string ss) { string temp; strcpy(temp.str,str); strcat(temp.str,ss.str); } }; void main() { string s1="\nmerry christmas"; string s2="\nhappy new year"; string s3; s1.display();

s2.display(); s3.display(); s3=s1+s2; s3.display(); }

OUTPUT

WRITE A PROGRAM TO SHOW THE USE OF TEMPLATE #include<iostream.h> #include<conio.h> template <class T> class data { public: data(T c) { cout<<"\nc="<<c; cout<<"\nsize in bytes="<<sizeof(c); } }; void main() { data <char> h('A'); data <int> i(10); data <float> j(1.2); } OUTPUT c=A size in bytes=1 c=10 size in bytes=4 c=1.2 size in bytes=4

WRITE A PROGRAM TO SHOW MULTIPLE CATCHES #include<iostream.h> #include<conio.h> void test (int x) { try { if (x == 1) throw x; else if (x == 0) throw 'x'; else if (x == -1) throw 1.0; cout<<"\nEND OF TRY BLOCK"; } catch(char c) { cout<<"\nCAUGHT CHRACTER"; } catch(int m) { cout<<"\nCAUGHT INTEGER"; } catch(float d) { cout<<"\nCAUGHT FLOAT"; } cout<<"\n\nEND OF TRY"; } void main() { cout<<"MULTIPLE CATCHES";

cout<<"\nx==1"; test(1); cout<<"\nx==0"; test(0); cout<<"\nx==-1"; test(-1); cout<<"\nx==2"; test(2); } OUTPUT MULTIPLE CATCHES x==1 CAUGHT INTEGER END OF TRY x==0 CAUGHT CHRACTER END OF TRY x==-1 CAUGHT DOUBLE END OF TRY x==2 END OF TRY

WRITE A PROGRAM TO READ DATA FROM TWO FILES SIMULTANEOUSLY #include<iostream.h> #include<fstream.h> #include<stdlib.h> void main() { ofstream fout; fout.open("COUNTRY"); fout<<"\nUSA="; fout<<"\nUK="; fout<<"\nSOUTH KOREA="; fout.close(); fout.open("CAPITAL"); fout<<"\nWASINGTON"; fout<<"\nLONDON"; fout<<"\nSEOUL"; fout.close(); const int size=80; char line[size]; ifstream fin1,fin2; fin1.open("COUNTRY"); fin2.open("CAPITAL"); for (int i=1;i<=10;i++) { if(fin1.eof()!=0) { cout<<"\nEXIT FROM COUNTRY"; exit(1); } fin1.getline(line,size); cout<<"\nCAPITAL OF:"<<line;

if(fin2.eof()!=0) { cout<<"\nEXIT FROM CAPITAL"; exit(1); } fin2.getline(line,size); cout<<line<<"\n"; } }

OUTPUT

CAPITAL OF: CAPITAL OF:USA=WASINGTON CAPITAL OF:UK=LONDON CAPITAL OF:SOUTH KOREA=SEOUL EXIT FROM COUNTRY

Anda mungkin juga menyukai