Your SlideShare is downloading. ×
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

# C++ file

8,120

Published on

Published in: Education, Technology, Business
1 Comment
8 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
No Downloads
Views
Total Views
8,120
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
442
Comments
1
Likes
8
Embeds 0
No embeds

No notes for slide

### Transcript

• 1. INTRODUCTION TO OBJECT ORIENTED PROGRAMMING LAB PRACTICAL FILE BY: _____________ B.Sc. IT – II SUBMITED TO: MS.NEETU GUPTA
• 2. 1. WAP to calculate factorial of a given number n.#include<conio.h>#include<iostream.h>void main(){int x=0;int Fact=1;cout<<"Enter number to calculate Factorial:";cin>>x;for(int c=1;c<=x;c++){Fact=Fact * c;}cout<<"Factorial of number <<x << is: "<<Fact;getch();}OUTPUT:
• 3. 2. WAP to check whether a number is prime or not.#include<iostream.h>#include<conio.h>int main(){Clrscr();int num;cout << "Enter a number ";cin >> num;int i=2;while(i<=num-1){if(num%i==0){cout << "n" << num << " is not a prime number.";break;}i++;}if(i==num)cout << "n" << num << " is a prime number.";getch();}OUTPUT:
• 4. 3. WAP to print Fibonacci series of ‘n’ numbers, where n is given by the programmer.#include<iostream.h>#include<conio.h>int main(){clrscr();int a=0,b=1,c,n=0,lim;cout<<"Enter the limit:n";cin>>lim;cout<<a<<"t"<<b<<"t";while(n!=lim){c=a+b;cout<<c<<"t";a=b;b=c;n++;}getch();return 0;}OUTPUT:
• 5. 4. WAP to do the following: a. Generate the following menu: 1. Add two numbers. 2. Subtract two numbers. 3. Multiply two numbers. 4. Divide two numbers. 5. Exit. b. Ask the user to input two integers and then input a choice from the menu. Perform all the arithmetic operations which have been offered by the menu. Checks for errors caused due to inappropriate entry by user and output a statement accordingly.#include<iostream.h>#include<conio.h>#include<stdlib.h>void main(){clrscr();int a,b,ch;float c;cout<<"Enter the first number";cin>>a;cout<<"Enter the second number";cin>>b;cout<<"n***************MENU******************";cout<<"n1 ADD two numbers.";cout<<"n2 SUBTRACT two numbers.";cout<<"n3 MULTIPLY two numbers.";cout<<"n4 DIVIDE two numbers.";cout<<"n5 EXIT.";
• 6. cout<<"n PLEASE ENTER YOUR CHOICE:";cin>>ch;switch(ch){case 1:{c=a+b;cout<<"n The sum of the two numbers is:"<<c;break;}case 2:{c=a-b;cout<<"n The differnce of two numbers is:"<<c;break;}case 3:{c=a*b;cout<<"n The product of two numbers is:"<<c;break;}case 4:{if(b==0)
• 7. {cout<<"ERROR..!!!";}else{c=a/b;cout<<"The division of two numbers is:"<<c;}case 5:{exit(1);break;}default:{cout<<"n Wrong choice";}}getch();}}
• 8. OUTPUT: 5. WAP to read a set of numbers in an array & to find the largest of them.#include<iostream.h>#include<conio.h>void main (void){Clrscr();int a[100];int i,n,larg;cout << "How many numbers are in the array?" << endl;cinn >> n;cout << "Enter the elements" << endl;for (i=0;i<=n-1;++i){cin >> a[i];
• 9. }cout << "Contents of the array" << endl;for (i=0; i<=n-1;++i){cout << a[i] << t;}cout << endl;larg = a[0];for (i=0;i<=n-1;++i){if (larg < a[i])larg = a[i];}cout << "Largest value in the array =" << larg;Getch();}OUTPUT:
• 10. 6. WAP to implement bubble sort using arrays.#include<iostream.h>#include<conio.h>int main(){clrscr();int temp,n,arr[50];cout<<"enter the no. of elements:";cin>>n;cout<<"Enter the elements of an array:n";for(int i=0;i<n;i++)cin>>arr[i];//Bubble sort methodfor(i=0;i<n;i++){for(int j=0;j<n-i-1;j++){if(arr[j]>arr[j+1]){temp=arr[j];arr[j]=arr[j+1];arr[j+1]=temp;}}}cout<<"Now the sorted array is:n";
• 11. for(i=0;i<n;i++)cout<<arr[i]<<"t";getch();return 0;}OUTPUT: 7. WAP to sort a list of names in ascending order.#include<iostream.h>#include<conio.h>#include<string.h>void main(){char st[10][10],temp[10];int i, j, n;clrscr();cout<<"Enter the no. of names:";cin>>n;
• 12. cout<<"Enter the different names:";for(i=0; i< n; i++)cin>>st[i];for(i=0; i< n; i++){for(j=i; j< n-1; j++){if(strcmp(st[i], st[j+1]) >0){strcpy(temp,st[i]);strcpy(st[i],st[j+1]);strcpy(st[j+1],temp);}}}cout<<"Given names after ascending order:";for(i=0;i<5;i++)cout<< st[i];getch();}OUTPUT:
• 13. 8. WAP to read a set of numbers from keyboard & to find sum of all elements of the given array using a function.#include<iostream.h>Void add(int arr[],int n) { Int I,sum=0;For(i=1;i<=n;i++) { Sum=sum+arr[i];}Cout<<endl<<”the sum is”<<sum<<endl;}Void main() {Int set[10],I,sum=0,limit;Cout<<”enter number of entries: “;Cin>>limit;For(i=1;i<=limit;i++) { Cout<<”enter position “<<i<<” : “; Cin>>set[i];}Add(set,limit);}OUTPUT:
• 14. 9. WAP to implement bubble sort using functions.#include <stdio.h>#include <iostream.h>void bubbleSort(int *array,int length)//Bubble sort function{int i,j;for(i=0;i<10;i++){for(j=0;j<i;j++) {if(array[i]>array[j]){int temp=array[i]; //swaparray[i]=array[j];array[j]=temp;}}}}void printElements(int *array,int length) //print array elements{int i=0;for(i=0;i<10;i++)cout<<array[i]<<endl;}
• 15. void main(){int a[]={9,6,5,23,2,6,2,7,1,8};bubbleSort(a,10);printElements(a,10);}OUTPUT: 10. WAP to exchange contents of two variables using call by value.#include<iostream.h>#include<conio.h>void swap(int&,int&)void main(){Clrscr(),int a=1,b=2;cout<<"Values before swap"<<endl;
• 16. cout<<"a="<<a<<endl;cout<<"b="<<b<<endl;swap(a,b);cout<<"Values after swap"<<endl;cout<<"a="<<a<<endl;cout<<"b="<<b<<endl;}void swap(int& a, int& b){int t;t=a;a=b;b=t;Getch(),}OUTPUT:
• 17. 11. WAP to exchange contents of two variables using call by reference.#include <iostream.h>#include<conio.h>.void swap( int &a, int &b ){int tmp; //Create a tmp int variable for storagetmp = a;a = b;b = tmp;return;}int main( int argc, char *argv[] ){Clrcsr();int x = 3, y = 5; //cout << "x: " << x << std::endl << "y: " << y << std::endl;swap( x, y );cout << "x: " << x << std::endl << "y: " << y << std::endl;getch();}OUTPUT:
• 18. 12. WAP to find the sum of three numbers using pointer to function method.#include<iostream.h>#include<conio.h>int sum_num(int *,int *,int *);int main(){clrscr();int a,b,c;cout<<"Enter three numbers:n";cin>>a>>b>>c;int sum=sum_num(&a,&b,&c);cout<<"**In this program,sum of three numbers are calculatedn";cout<<"by using pointers to a function**nn";cout<<"Sum of three numbers are:n"<<sum;getch();return 0;}int sum_num(int *x,int *y,int *z){int n=*x+ *y+ *z;return n;}
• 19. OUTPUT: 13. WAP to display content of an array using pointers.#include<iostream.h>#include<conio.h>void display(int *a,int size);int main(){clrscr();int i,n,arr[100];cout<<"Enter the size of an array:n";cin>>n;cout<<"Enter the elements of an array:n";for(i=0;i<n;i++)cin>>arr[i];display(arr,n);getch();return 0;}
• 20. void display(int *a,int size){cout<<"Details of an array using pointer are:n";for(int i=0;i<size;i++)cout<<*(a+i)<<endl;}OUTPUT: 14. Calculate area of different geometrical figures (circle, rectangle,square, triangle) using function overloading.#include<iostream.h>Float calc(float r,float cons);Int calc(int l,int h);Int calc(int l);Float calc(int l,int h,float cons);Void main()
• 21. {Int length,height;Float radius;Cout<<”enter radius of circle:”;<<radius;Cout<<endl<<”the area of circle is:”<calc(radius,3.14)<<endl;Cout<<”enter the length of rectangle”<<length;Cout<<”enter the height of rectangle”<<height;Cout<<endl<<”the area of rectangle is:”<calc(length,height)<<endl;Cout<<”enter side of square”<<length;Cout<<endl<<”the area of square is:”<calc(length)<<endl;Cout<”enter base of triangle”;<<length;Cout<”enter height of triangle”;<<height;Cout<<endl<<”the area of triangle is:”<calc(length,height,0.5)<<endl;}Float calc(float r, float cons){return(cons*r*r);}Int calc(int l, int h){return(l*l);}Float calc(int l, int h,float cons){return (l*h*cons);}
• 22. OUTPUT: 15. WAP to add two complex numbers using friend function.#include<iostream.h>#include<conio.h>Class cmplx{ Int real,imagin; Cout<<”ENTER THE REAL PART: “; Cin>>real; Cout<<”ENTER THE IMAGINARY PART: “; Cin>>imagin;} Friend void sum(complx,complx);};Void sun(compx c1,complx c2) Cout<<”RESULT:”; Cout<<”*“<<c1.real<<”+ i”<<c1.imagin; Cout<<”++*“<<c2.real<<”+ i”<<c2.imagin;
• 23. Cout<<”+=”<<c1.real+c2.real<<”+ i”<<c1.imagin+c2.imagin;}Void main()Complx op1,op2;Cout<<endl<<”INPUT OPERAND 1----“;Op1.get();Cout<<endl<<”INPUT OPERAND 2----“;Op2.get();Sum(op1,op2); }OUTPUT:
• 24. 16. WAP to maintain the student record which contains Roll number, Name, Marks1, Marks2, Marks3 as data member and getdata(), display() and setdata() as member functions.#include<iostream.h>#include<conio.h>Class student{Int roll, name, mark1, mark2, mark3, avg, total;Public:Void getdata()Cout<<”enter roll number, name, marks”<<endl;Cin>>roll>>name>>mark1>>mark2>>mark3;}Void setdata(){Total=mark1+mark2+mark3;Avg=total/3;}Void display(){Cout<<roll;Cout<<name;Cout<<marks1;Cout<<marks2;Cout<<marks3;Cout<<total;
• 25. Cout<<avg;}};Void main(){Clrscr();Student n i[10];For (inti=0;i<=5;i++);{Ni[i].getdata();Ni[i].getdata();Ni[i].getdata();Getch(); }}OUTPUT:
• 26. 17. WAP to increment the employee salaries on the basis of there designation (Manager-5000, General Manager-10000, CEO-20000, worker-2000). Use employee name, id, designation and salary as data member and inc_sal as member function#include<iostream.h>#include<conio.h>class employee{int age,basic,da,hra,tsal;char name[20];public:void getdata(){cout<<"enter the name"<<endl;cin>>name;cout<<"enter the age"<<endl;cin>>age;cout<<"enter the basic salary"<<endl;cin>>basic;}void caldata(){hra=.50*basic;da=.12*basic;tsal=basic+hra+da;}void display()
• 27. {cout<<"name is:"<<name<<endl;cout<<"age is:"<<age<<endl;cout<<"basic salary is:"<<basic<<endl;cout<<"hra is:"<<hra<<endl;cout<<"da is:"<<da<<endl;cout<<"toral salary is:"<<tsal<<endl;}};void main(){clrscr();employee e1;e1.getdata();e1.caldata();e1.display();getch();}OUTPUT:
• 28. 18. Write a class bank, containing data member: Name of Depositor, A/c type, Type of A/c, Balance amount. Member function: To assign initial value, To deposit an amount, to withdraw an amount after checking the balance (which should be greater than Rs. 500) , To display name & balance.#include<iostream.h>#include<conio.h>#include<string.h>class bank{int bal,init,d_amt,wid_amt,bamt;char n_depo[20],a_ctype[10],t_ac[10];public:bank(){init=10000;}void depositedata(){cout<<"enter the name of depositer:"<<endl;cin>>n_depo;cout<<"enter the a/c type:"<<endl;cin>>a_ctype;cout<<"enter the type of a/c:"<<endl;cin>>t_ac;cout<<"enter the deposit amount"<<endl;cin>>d_amt;
• 29. bal=init+d_amt;}void withdarawdata(){if (bal>=500){cout<<"amount to be withdrawn:"<<endl;cin>>wid_amt;bamt=bal-wid_amt;}else{cout<<"error"<<endl;}}void display(){cout<<"name of depositer:"<<n_depo<<endl;cout<<"deposite amount is"<<d_amt<<endl;cout<<"balence after deposite"<<bal<<endl;cout<<"withdraw amount"<<wid_amt<<endl;cout<<"end balence"<<bamt<<endl;}};void main()
• 30. {clrscr();bank b1;b1.depositedata();b1.withdarawdata();b1.display();getch();}OUTPUT:
• 31. 19. WAP to define nested class ‘student_info’ which contains data members such as name, roll number and sex and also consists of one more class ‘date’ ,whose data members are day, month and year. The data is to be read from the keyboard & displayed on the screen.#include<iostream.h>#include<conio.h>#include<string.h>#include<stdio.h>class student_info{public: class date { private: char day[20],month[20],year[20]; public: void input_date() { cout<<"Enter Date of Birth (dd/mm/yy):t"; cin>>day>>month>>year; getch(); } void disp() { cout<<"nDOB:t"<<day<<"-"<<month<<"-"<<year;
• 32. } };private: char name[30],roll[30],sex[20]; date d;public: void input() { cout<<"Enter the name of the student:t"; gets(name); cout<<"Enter the roll of the student:t"; gets(roll); cout<<"Enter the sex(male or female):t"; cin>>sex; d.input_date(); } void display() { clrscr(); cout<<"Details of studentnn"; cout<<"Name:t"<<name<<"nRoll:t"<<roll<<"nSex:t"<<sex;
• 33. d.disp(); }};int main(){ clrscr(); student_info a; a.input(); a.display(); getch(); return 0;}OUTPUT:
• 34. 20. WAP to generate a series of Fibonacci numbers using copy constructor, where it is defined outside the class using scope resolution operator.#include<iostream.h> { Public: Fibonacci():limit(0) {} Fibonacci(int li):limit(li) Int fibo=1,1=0,j=0; Cout<<0<<””; While(limit!=0) Cout<<fibo<<””; I=j; J=fibo; Fibo=i+j; Limit--; } }};Void main() { Int n; Cout<<”Enter the number of the instances: “; Cin>>n; Fibonacci f(n);
• 35. Cout<<endl; }OUTPUT: 21. Write a class string to compare two strings, overload (= =) operator.#include< iostream.h >#include< conio.h >#include< string.h >const int SIZE=40;class String{private:char str [SIZE];public:String (){strcpy (str," ");}String (char ch []){strcpy (str, ch);
• 36. }void getString (){cout<< "Enter the string: -";cin.get (str, SIZE);}int operator == (String s){if (strcmp (str, s.str)==0)return 1;elsereturn 0;}};void main (){clrscr ();String s1, s2, s3;s1="Satavisha";s2="Suddhashil";s3.getString ();if (s3==s1)cout<< "1st and 3rd string are same.";else if (s2==s3)cout<< "2nd and 3rd string are same.";
• 37. elsecout<< "All strings are unique.";getch ();}OUTPUT: 22. Write a class to concatenate two strings, overload (+) operator.#include<iostream.h>#include<string>Using namespace std;Class mystring{ Char a[10,b[10]; Public: Void getdata() { Cout<<”Enter first string=”; Gets(a); Cout<<”Enter second string=”; Gets(b); }
• 38. };Int main(){ Mystring x; x.getdata(); +x; System(“pause”); Return 0;}OUTPUT: 23. Create a class item, having two data members x & y, overload ‘-‘(unary operator) to change the sign of x and y.#include<iostream.h>Using namespace std;Class item{ Int x,y; Public: Void getdata() {
• 39. Cout<<”Enter the vale of x & y=”; Cin>>x; Cin>>y; } Void operator –(void) { X= -x; Y= -y; } Void display() { Cout<<”nx=”<<x<<”ny=”<<y; }};Int main(){Item x;x.getdata();-x;x.display();cout<<endl;system(“pause”);return 0;}
• 40. OUTPUT: 24. Create a class Employee. Derive 3 classes from this class namely, Programmer, Analyst & Project Leader. Take attributes and operations on your own. WAP to implement this with array of pointers.#include<iostream.h>#include<conio.h>#include<string.h>class employee{ private: char name[20]; int salary; public : void putdata(int sal, char nam[20]) { strcpy(name,nam); salary=sal;}char* getName(void){ return name;}int getSal(void)
• 41. {return salary;}};class programmer:public employee{ private: char skill[10]; public: programmer(char name[20],int sal,char skil[20]) { putData(sal,name); strcpy(skill,skil); } void display(void) { cout<<"nProgrammer : n"; cout<<"nName : "<<getName(); cout<<"nSalary : "<<getSal(); cout<<"nSkill : "<<skill; }};class analyst:public employee{ private:
• 42. char type[10]; public: analyst(char name[20],int sal,char typ[20]) { putData(sal,name); strcpy(type,typ); } void display(void) { cout<<"nn Analyst :n"; cout<<"nName :"<<getName(); cout<<"nsalary :"<<getSal(); cout<<"nType :"<<type; }};class proj_leader:public employee{ private: char pName[10]; public: proj_leader(char name[20], int sal,char pNam[20]){putData(sal,name);strcpy(pName,pNam);}
• 43. };void display(void){clrscr();proj_leader prl("akshay",10000,"software Development");analyst al("amita",8600,"post");programmer pl("xyz",12000,"C++");prl.display();al.display();pl.display();getch();}OUTPUT:
• 44. 25. Create two classes namely Employee and Qualification. Using multiple inheritance derive two classes Scientist and Manager. Take suitable attributes & operations. WAP to implement this class hierarchy.#include<iostream.h>#include<conio.h>#include<stdio.h>class employee{ char empname[10]; int empid; public: void getemp() { cout<<endl<<"Enter Emlpoyee Name :"; gets(empname); cout<<"Enter Employee Id"; cin>>empid; } void display() { cout<<endl<<"Name :"<<empname; cout<<endl<<"Id :"<<empid; }};class qualification{
• 45. int exp; public: void getqual() { cout<<"Enter Year Of Working Experience :"; cin>>exp; } void dispqual() { cout<<endl<<"Experiece="<<exp<<"years"; }};class scientist:public employee,public qualification{ int projid; public: void getproject() { cout<<"Enter Project Id :"; cin>>projid; } void dispproj() { cout<<endl<<"PROJECT ID : "<<projid; }};
• 46. class manager:public employee,public qualification{ int groupid; public: void getgroup() { cout<<"Enter Group Id :"; cin>>groupid; } void dispgroup() { cout<<endl<<"Group ID :"<<groupid; }};void main(){ clrscr(); scientist s; manager m; cout<<"FOR SCIENTIST::::"<<endl; s.getemp(); s.getqual(); s.getproject(); s.display();
• 47. s.dispqual(); s.dispproj(); cout<<endl<<endl<<endl<<"FOR MANAGER::::"<<endl; m.getemp(); m.getqual(); m.getgroup(); m.display(); m.dispqual(); m.dispgroup(); getch();}OUTPUT:
• 48. 26. WAP to read data from keyboard & write it to the file. After writing is completed, the file is closed. The program again opens the same file and reads it.#include<iostream.h>#include<fstream.h>Void main(void)Char string[255];Int ch;Cout<<”nMENUn)Write To Filen2)Read From FilenEnter Choice : “;Cin>>ch;Switch(ch){ Case 1: Cout<<’nEnter String To Write To File :”; Cin>>string; Ofstream fout; Fout.open(“myfile.txt”); Fout<<string; Fout<<flush; Fout.close(); Break; Case 2: Ifstream fin; Fin.open(“myfile.txt”) Fin>>string; Cout<<”nFile Read : n”<<string; Fin.close();
• 49. Break; Default: Cout<<”INVALID CHOICE”; }}OUTPUT: