Sie sind auf Seite 1von 51

OBJECT ORIENTED PROGRAMMING USING C++

BACHELOR OF TECHNOLOGY IN COMPUTER SCIENCE AND ENGG.

SOFTWARE LAB - II (OOPS) CS - 254

SUBMITTED TO: Er. AMRITPAL KAUR Assistant Professor (CSE/IT)

SUBMITTED BY Name Roll No Class

CT INSTITUTE OF TECHNOLOGY SHAHPUR (JALANDHAR), PUNJAB (INDIA) (Affiliated to Punjab Technical University, Jalandhar, Punjab (India))

TABLE OF CONTENTS

S.NO. NAME OF EXPERIMENT 1 Write a program to swap two numbers without using third variable. 2 Write a program to find the reverse of entered number. 3 Write a program to check that entered number is palindrome or not. 4 Write a program to find the sum of digits of entered number. 5 Write a program to find the greatest number among 3 using nested if-else. 6 7 8 9 Write a program to make calculator using switch statement. Write a program to input indefinite numbers and calculate sum of only +ve numbers. The program terminates when number is ve. Write a program to check that entered number is digit, character or any special character. Write a program to check grade according to given conditions:- if marks are between 70-80 then print C grade if marks are between 80-90 then print B grade or if marks are between 90100 then print A grade. Write program to find fibbonacci series upto n. Write a program to print given pattern. Write a program to print given pattern. Write a program to swap two numbers using call by reference argument passing technique. Write a program to find factorial of a number using recursion. Write a program to show the use of inline function. Write a program to format the output of program using manipulators. Write a program to find sum of elements entered in an array. Write a program to sort the elements using bubble sort technique. Write a program to add two matrices. Write a program to find the transpose of matrix. Write a program to multiply two matrices. Write a program to illustrate the memory concept of union and structure. Write a program to calculate area of rectangle by defining member function outside the class. Write a program to show the use of parameterized constructor.

PAGE NO.

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

PRACTICAL NO. 1
Write a program to swap two numbers without using third variable.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int a,b; cout<<"ENTER THE VALUE OF a "; cin>>a; cout<<"ENTER THE VALUE OF b "; cin>>b; cout<<"AFTER SWAPPING "<<endl; a=a+b; b=a-b; a=a-b; cout<<"VALUE OF a "<<a<<endl; cout<<"VALUE OF b "<<b; getch(); } Its output is:-

PRACTICAL NO. 2
Write a program to find the reverse of entered number.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int i, n, r, s=0; cout<<"ENTER THE NUMBER "; cin>>n; while(n>0) { r=n%10; s=s*10+r; n=n/10; } cout<<"REVERSE IS "<<s; getch(); } Its output is:-

PRACTICAL NO. 3
Write a program to find that entered number is palindrome or not.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int i,t,n,r,s=0; cout<<"ENTER THE NUMBER "; cin>>n; t=n; while(n>0) { r=n%10; s=s*10+r; n=n/10; } cout<<"REVERSE IS "<<s; if(t==s) cout<<"STRING IS PALINDROME"; else

cout<<"STRING IS NOT PALINDROME"; getch(); } Its output is:-

PRACTICAL NO. 4
Write a program to calculate the sum of digits of entered number.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int i,n,r,s=0; cout<<"ENTER THE NUMBER "; cin>>n; while(n>0) { r=n%10; s=s+r; n=n/10; } cout<<"SUM IS "<<s; getch(); } Its output is:-

PRACTICAL NO. 5
Write a program to find the greatest number among 3 using nested if-else.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int a,b,c; cout<<"ENTER THE 1st NUMBER "; cin>>a; cout<<"ENTER THE 2nd NUMBER "; cin>>b; cout<<"ENTER THE 3rd NUMBER "; cin>>c; if(a>b){ if(a>c) cout<<"1st ELEMENT IS GREATEST"; else cout<<"3rd ELEMENT IS GREATEST"; } else{

if(b>c) cout<<"2nd ELEMENT IS GREATEST"; else cout<<"3rd ELEMENT IS GREATEST"; } getch(); } Its output is:-

PRACTICAL NO. 6
Write a program to make a calculator using switch.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int a,b; char ch; cout<<"ENTER THE 1st NUMBER "; cin>>a; cout<<"ENTER THE 2nd NUMBER "; cin>>b; cout<<"\n\nCHOICES AVAILABLE ARE\n\nADDITION (+)\n\nSUBTRACTION (-)\n\nMULTIPLICATION (*)\n\nDIVISION (/)\n\n" cout<<ENTER WHAT YOU WANT TO DO ; cin>>ch; switch(ch) { case '+': cout<<"RESULT IS "<<a+b;

break; case '-': cout<<"RESULT IS "<<a-b; break; case '*': cout<<"RESULT IS "<<a*b; break; case '/': cout<<"RESULT IS "<<a/b; break; default: cout<<"INVALID ENTRY" getch(); }

Its output is:-

PRACTICAL NO. 7
Write a program to input indefinite numbers and calculate sum of only +ve numbers. The program terminates when number is ve.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int s=0,n; i=1; while(i<3) { cout<<"ENTER ANY THREE TERMS "; cin>>n; } if(n>=0) { s=s+n; } else {

cout<<s; break; } getch(); } Its output is:-

PRACTICAL NO. 8
Write a program to find that entered number is digit, character or special character.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int n; cout<<"ENTER WHAT YOU WANT TO FEED\n"; cin>>n; if(n>=0&&n<=9) cout<<"YOU HAVE ENTERED A DIGIT"; else if((n>=a || n>=A) || (n<=z || n<=Z)) cout<<"YOU HAVE ENTERED A CHARACTER"; else cout<<"YOU HAVE ENTERED A SPECIAL CHARACTER"; getch(); } Its output is:-

PRACTICAL NO. 9
Write a program to check grade according to given conditions a. if marks are between 70-80 then print C grade b. if marks are between 80-90 then print B grade c. if marks are between 90-100 then print A grade
#include<iostream.h> #include<conio.h> void main() { clrscr(); int marks; cout<<"ENTER PERCENTAGE OF MARKS OBTAINED BY STUDENT (ABOVE 70) "; cin>>marks; if(marks>=70) { if(marks>=70&&marks<=80) cout<<"STUDENT HAS EARNED C GRADE"; else if(marks>80&&marks<=90) cout<<"STUDENT HAS EARNED B GRADE"; else cout<<"STUDENT HAS EARNED A GRADE";

} else cout<<"STUDENT HAS BEEN FAILED"; getch(); } Its output is:-

PRACTICAL NO. 10
Write a program to print Fibonacci series up to n terms.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int a=0,b=1,c,n; cout<<"ENTER THE NUMBER OF TERMS UPTO YOU WANT THE SERIES "; cin>>n; cout<<a<<endl<<b<<endl; for(int i=1;i<=n-2;i++) { c=a+b; cout<<c; a=b; b=c; } getch(); }

Its output is:-

PRACTICAL NO. 11
Write a program to calculate factorial of given number.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int f=1,i,n; cout<<"ENTER THE NUMBER "; cin>>n; for(i=1;i<=n;i++) { f=f*i; } cout<<"FACTORIAL OF NUMBER IS "<<f; getch(); } Its output is:-

PRACTICAL NO. 12
Write a program to print given pattern 1 12 123 1234 12345
#include<iostream.h> #include<conio.h> void main() { clrscr(); int n; for(int i=1;i<=5; i++) { for(int j=1;j<=i; j++) { cout<<j; }

cout<<endl; } getch(); } Its output is:-

PRACTICAL NO. 13
Write a program to print given pattern 1 22 333 4444 55555
#include<iostream.h> #include<conio.h> void main() { clrscr(); int n; for(int i=1;i<=5; i++) { for(int j=1;j<=i; j++) { cout<<i; }

cout<<endl; } getch(); } Its output is:-

PRACTICAL NO. 14
Write a program to swap two numbers using call by reference argument passing technique.
#include<iostream.h> #include<conio.h> void swap(int *,int *); void main() { clrscr(); int a,b; cout<<"ENTER THE VALUE OF a "; cin>>a; cout<<"ENTER THE VALUE OF b "; cin>>b; swap(&a, &b); getch(); } void swap(int *m, int *n) { int c; c=*m; *m=*n; *n=*c;

cout<<"AFTER SWAPPING \n\n"; cout<<"VALUE OF a "<<*m<<endl<<"VALUE OF b "<<*n; }

Its output is:-

PRACTICAL NO. 15
Write a program to find factorial of a number using recursion.
#include<iostream.h> #include<conio.h> int fact(int); void main() { clrscr(); int n,k; cout<<"ENTER ANY INTEGER "; cin>>n; k=fact(n); cout<<"FACTORIAL OF ENTERED INTEGER IS "<<k; getch(); } int fact(int n) { int f; if(n==1) return(1);

else { f=n*fact(n-1); return(f); } } Its output is:-

PRACTICAL NO. 16
Write a program to show the use of inline function.
#include<iostream.h> #include<conio.h> inline float mul(float x, float y) { return(x*y); } void main() { float a=12.345; float b=9.82; cout<<endl<<mul(a,b); getch(); } Its output is:-

PRACTICAL NO. 17
Write a program to format the output of program using manipulators.
#include<iostream.h> #include<conio.h> #include<iomanip.h> void main() { clrscr(); int basic=950, allowance=95, total=1045; cout<<setw(10)<<"BASIC"<<setw(10)<<basic<<endl; cout<<setw(10)<<"ALLOWANCE"<<setw(10)<<allowance<<endl; cout<<setw(10)<<"TOTAL"<<setw(10)<<total<<endl; getch(); }

Its output is:-

PRACTICAL NO. 18
Write a program to find the sum of entered elements in an array.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int n,a[100],i,s=0; cout<<"ENTER THE NUMBER UPTO WHICH YOU WANT TO ENTER THE DIGITS "; cin>>n; for(i=1;i<=n;i++) { cin>>a[i]; }

for(i=1;i<=n;i++) { s=s+a[i]; } cout<<"SUM OF ARRAY IS "<<s<<endl; getch(); } Its output is:-

PRACTICAL NO. 19
Write a program to sort the elements using bubble sort technique.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int n,j,a[100],i; cout<<"ENTER THE NUMBER UPTO WHICH YOU WANT TO ENTER THE DIGITS "; cin>>n; for(i=0;i<n;i++) { cin>>a[i]; } for(i=0;i<n-1;i++)

{ for(j=0;j<n-i-1;j++) { if(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } } cout<<"SORTED ARRAy IS \n\n"; for(i=0;i<n;i++) { cout<<a[i]<<endl; } getch(); }

Its output is:-

PRACTICAL NO. 20
Write a program to add two matrices.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int r,c,m,n,a[50][50],i,j,b[50][50],p[100][100]; cout<<"ENTER THE NUMBER OF ROWS OF 1st MATRIX "; cin>>r; cout<<"ENTER THE NUMBER OF COLUMNS OF 1st MATRIX "; cin>>c; cout<<"ENTER THE NUMBER OF ROWS OF 2nd MATRIX "; cin>>m; cout<<"ENTER THE NUMBER OF COLUMNS OF 2nd MATRIX "; cin>>n; cout<<"\n\nNOW,ENTER THE ELEMENTS IN 1st MATRIX \n";

for(i=0;i<r;i++) { for(j=0;j<c;j++) { cin>>a[i][j]; } } cout<<"\nNOW,ENTER THE ELEMENTS IN 2nd MATRIX \n"; for(i=0;i<r;i++) { for(j=0;j<c;j++) { cin>>b[i][j]; } } for(i=0;i<r;i++) { for(j=0;j<c;j++) { p[i][j]=a[i][j]+b[i][j]; } }

if(r==m&&c==n) { cout<<"\nSUM OF MATRICES IS\n\n"; for(i=0;i<r;i++) { for(j=0;j<c;j++) { cout<<p[i][j]<<" "; } cout<<endl; } } getch(); } Its output is:-

PRACTICAL NO. 21
Write a program to find the transpose of matrix.
#include<iostream.h> #include<conio.h> void main() { clrscr(); int r,c,a[50][50],i,j; cout<<"ENTER THE NUMBER OF ROWS "; cin>>r; cout<<"ENTER THE NUMBER OF COLUMNS "; cin>>c; cout<<"\n\nNOW,ENTER THE ELEMENTS IN MATRIX \n"; for(i=0;i<r;i++) { for(j=0;j<c;j++) {

cin>>a[i][j]; }} cout<<"TRANSPOSE OF MATRIX IS \n\n"; for(i=0;i<r;i++) { for(j=0;j<c;j++) { cout<<a[j][i]<<" "; } cout<<endl; } getch(); }

Its output is:-

PRACTICAL NO. 22
Write a program to multiply two matrices.

#include<iostream.h> #include<conio.h> void main() { clrscr(); int r,c,m,n,a[50][50],i,j,b[50][50],h[100][100]; cout<<"ENTER THE NUMBER OF ROWS OF 1st MATRIX "; cin>>r; cout<<"ENTER THE NUMBER OF COLUMNS OF 1st MATRIX "; cin>>c; cout<<"ENTER THE NUMBER OF ROWS OF 2nd MATRIX "; cin>>m; cout<<"ENTER THE NUMBER OF COLUMNS OF 2nd MATRIX "; cin>>n; cout<<"\n\nNOW,ENTER THE ELEMENTS IN 1st MATRIX \n"; for(i=0;i<r;i++) { for(j=0;j<c;j++) { cin>>a[i][j]; } } cout<<"\n\nNOW,ENTER THE ELEMENTS IN 2nd MATRIX \n"; for(i=0;i<m;i++) { for(j=0;j<n;j++) { cin>>b[i][j]; } } for(i=0;i<r;i++) { for(j=0;j<n;j++) { { h[i][j]=0; for(int k=0;k<c;k++) { h[i][j]=h[i][j]+a[i][k]*b[k][j]; } } } } cout<<"PRODUCT OF MATRICES IS \n";

for(i=0;i<r;i++) { for(j=0;j<c;j++) { cout<<h[i][j]<<" "; } cout<<endl; } getch(); } Its output is:-

PRACTICAL NO. 23
Write a program to show the memory concept of union.

#include<iostream.h> #include<conio.h> struct gurdeep { int a,b,c; char p; }abc; union oops { int a,b,c; char p; }xyz; void main() { clrscr(); cout<<"SIZE OF abc is "<<sizeof(abc)<<endl; cout<<"SIZE OF xyz is "<<sizeof(xyz); getch(); } Its output is:-

PRACTICAL NO. 24

Write a program to find area of rectangle by defining member functions outside the class.
#include<iostream.h> #include<conio.h> class rectangle { private: int area,l,b; public: void put(); void show(); }; rectangle::void put(); { cout<<"ENTER THE LENGTH "; cin>>l; cout<<"ENTER THE BREATH "; cin>>b; } rectangle::void show() { area=l*b; cout<<"AREA IS "<<area; } void main() { clrscr(); rectangle gurdeep; gurdeep.put(); gurdeep.show(); getch(); }

Its output is:-

PRACTICAL NO. 25
Write a program to show use of parameterized constructor.

#include<iostream.h> #include<conio.h> class fact { private: int n,f,i; public: fact(int m) { f=1; n=m; } void ans() { for(i=1;i<=n;i++) { f=f*i; } cout<<"FACTORIAL IS "<<f; }}; void main() { clrscr(); fact f(5); f.ans(); getch(); } Its output is:-

PRACTICAL NO. 26
Write a program to show the use of copy constructor.

#include<iostream.h> #include<conio.h> class dist { int feet; float inch; public: dist() {} dist(int f1, float in1) { feet=f1; inch=in1; } dist(dist&d) { cout<<"COPY CONSTRUCTOR INVOKED\n"; feet=d.feet; inch=d.inch; } void show() { cout<<feet<<'\t'<<inch; } dist add(dist d) { dist t; t.feet=feet+d.feet; t.inch=inch+d.inch; if(t.inch>=12.00) { t.inch-=12.00; t.feet++; } return(t); } };

void main() { clrscr();

dist d1(5,7) dist d2=d1; dist d3; d3=d1.add(d2); d3.show(); getch(); } Its output is:-

PRACTICAL NO. 27

Write a program to find area of rectangle using constructor overloading.


#include<iostream.h> #include<conio.h> class rectangle { private: int l,b; public: rectangle() { cout<<"VALUE OF l & b ARE \n"; } rectangle(int i,int j) { l=i; b=j; cout<<l<<endl<<b<<endl; } void area() { cout<<"AREA OF RECTANGLE "<<l*b; } }; void main() { clrscr(); rectangle r1; rectangle r(10,5); r.area(); getch(); }

Its output is:-

PRACTICAL NO. 28

Write a program to show the flow of execution of destructor in a class.


#include<iostream.h> #include<conio.h> class counter { int id; public: counter(int i) { id=i; cout<<"CONSTRUCTOR OF OBJECT WITH ID "<<id<<" runs"; } ~counter() { cout<<"\nOBJECT WITH ID <<id<<" destroyed"; }}; void main() { clrscr(); counterc1(1); counterc2(2); counterc3(3); cout<<"\nEND OF MAIN"; getch(); } Its output is:-

PRACTICAL NO. 29

Write a program to show the use of hybrid inheritance.


#include<iostream.h> #include<conio.h> class cse { protected: int a,b; public: void enter() { cout<<"VALUE OF a "; cin>>a; cout<<"VALUE OF b "; cin>>b; cout<<VALUE OF c ; cin>>c; } }; class cse1:public virtual cse { public: void show() { cout<<"ENTERED VALUE OF a "<<a; cout<<"\nENTERED VALUE OF b "<<b; } }; class cse2:public virtual cse { public: void find() { cout<<"VALUE OF a AFTER ADDING c "<<a+c; } };

class cse3:public cse1, public cse2 {

public: void now() { cout<<"VALUE OF b AFTER ADDING c "<<b+c; } }; void main() { clrscr(); cse3 c; c.enter(); c.show(); c.find(); c.now(); getch(); } Its output is:-

PRACTICAL NO. 30

Write a program to overload unary operators.


#include<iostream.h> #include<conio.h> class unary { int a; public: void read() { cout<<"ENTER THE VALUE OF a "; cin>>a; } void operator++() { a++; } void print() { cout<<"VALUE OF a AFTER INCREMENTING "; cout<<a;}}; void main() { clrscr(); unary x; x.read(); x++; x.print(); getch(); } Its output is:-

PRACTICAL NO. 31

Write a program to overload binary operators.


#include<iostream.h> #include<conio.h> class sum { int a, s; public: void read() { cout<<"ENTER THE VALUE "; cin>>a; } void print() { cout<<"VALUE OF s "<<s; } void operator+(sum t) { s=a+t.a; }}; void main() { clrscr(); sum x,y; x.read(); y.read(); x+y; x.print(); getch(); } Its output is:-

PRACTICAL NO. 32

Write a program of bubble sort using class template functions.


#include<iostream.h> #include<conio.h> template<class T> void bubble(T a[], int n) { for(int i=0;i<n-1;i++) for(int j=n-1;i<j;j--) if(a[j]<a[j-1]) { swap(a[j],a[j-1]); } } template <class X> void swap(X &a, X &b) { X temp=a; a=b; b=temp; } int main() { int x[5]={10,20,30,40,50}; float y[5]={1.1,5.5,3.3,4.4,2.2}; bubble(x,5); bubble(y,5); cout<<"SORTED X-ARRAY "; for(int i=0;i<5;i++) cout<<x[i]<<" "; cout<<endl; cout<<"SORTED Y-ARRAY "; for(int j=0;j<5;j++) cout<<y[j]<<" "; cout<<endl; return 0; }

Its output is:-

PRACTICAL NO. 33

Write a program of working with single file.


#include<iostream.h> #include<conio.h> int main() { ofstream outf("ITEM"); cout<<"ENTER ANY ITEM NAME "; char name[30]; cin>>name; outf<<name<<endl; cout<<"ENTER ITEM COST "; float cost; cin>>cost; outf<<cost<<endl; outf.close(); ifstream inf("ITEM"); inf>>name; inf>>cost; cout<<"ITEM NAME "<<name<<endl; cout<<"ITEM NAME "<<cost<<endl; inf.close(); return 0; } Its output is:-

PRACTICAL NO. 34

Write a program of working with multiple files.


#include<iostream.h> #include<fstream.h> int main() { clrscr(); ofstream fout; fout.open("COUNTRY"); fout<<"UNITED STATES OF AMERICA\n"; fout<<"UNITED KINGDOM\n"; fout<<"SOUTH KOREA\n"; fout.close(); fout.open("CAPITAL"); fout<<"WASHINGTON\n"; fout<<"LONDON\n"; fout<<"SEOUL\n"; fout.close(); const int N=80; char line[N]; ifstream fin; fin.open("COUNTRY"); cout<<"CONTENTS OF COUNTRY FILE\n"; while(fin) { fin.getline(line,N); cout<<line; } fin.close(); fin.open("CAPITAL"); cout<<"\nCONTENTS OF CAPITAL FILE\n"; while(fin) { fin.getline(line,N); cout<<line; } fin.close(); return 0; }

Its output is:-

PRACTICAL NO. 35

Write a program to read two files simultaneously.


#include<iostream.h> #include<fstream.h> #include<stdlib.h> int main() { 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<<"EXIT FROM COUNTRY\n"; exit(1); } fin1.getline(line,size); cout<<"CAPITAL OF "<<line; if(fin2.eof()!=0) { cout<<"EXIT FROM CAPITAL\n"; exit(1); } fin2.getline(line,size); cout<<line<<"\n"; } return 0; }

Its output is:-

Das könnte Ihnen auch gefallen