This document contains 17 programming problems and their solutions involving object oriented programming concepts like classes, objects, functions, arrays, pointers etc. The problems cover basic concepts like calculating factorial, checking prime number, Fibonacci series, arithmetic operations using menus. More advanced concepts covered include sorting, searching, function overloading, complex numbers, class/object concepts like constructors, destructors and member functions to maintain student records.
Introduction to Object Oriented Programming lab practicals by B.Sc. IT – II students, submitted to Ms. Neetu Gupta.
WAP for calculating factorial of a number n using loops in C++. Outputs the factorial result.
WAP for checking if a number is prime or not using a while loop and conditional statements.
WAP to print Fibonacci series of ‘n’ numbers input by the user, using a while loop.
WAP providing a menu for arithmetic operations (add, subtract, multiply, divide). It handles inappropriate entries and performs calculations based on user choice.
WAP to find the largest number in an array by reading user input for the size and elements, and then evaluating.
WAP to implement bubble sort algorithm for sorting an array of numbers provided by the user.
WAP for sorting names in ascending order using string comparison functions.
WAP to read a set of numbers from the keyboard using a function to compute the sum of array elements.
WAP to implement bubble sort using functions. Sorts a predefined array and prints the sorted elements.
WAP to exchange contents of two variables using call by value technique.
WAP to exchange variable contents using call by reference technique for variable manipulation.
WAP to find the sum of three numbers using pointer-to-function method.
WAP to display array contents using pointers, including array input and output.
WAP to calculate area of geometrical figures using function overloading, handling different shapes.
WAP to add two complex numbers using friend functions in a class.
WAP to maintain student records using classes and methods for data handling and display.
WAP to increment employee salaries based on designation using classes for structure.
WAP class for managing bank operations including deposit, withdrawal, and balance display.
WAP to define nested classes to manage student information with birthdate encapsulation.
WAP to generate Fibonacci series using a copy constructor defined outside the class.
WAP to compare two strings and overload the equality operator for string object comparison.
WAP to concatenate two strings by overloading the addition operator in a string class.
WAP to create a class with operator overloading to negate values of its data members.
WAP implementing a class hierarchy for Employee types (Programmer, Analyst, Project Leader) utilizing inheritance.
WAP demonstrating multiple inheritance with classes for employee and qualification attributes.
WAP to write data to a file and read it back, demonstrating file handling capabilities in C++.
INTRODUCTION TO OBJECTORIENTED
PROGRAMMING LAB
PRACTICAL FILE
BY: _____________
B.Sc. IT – II
SUBMITED TO:
MS.NEETU GUPTA
2.
1. WAP tocalculate 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 tocheck 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 toprint 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 todo 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 ENTERYOUR 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)
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 << "Contentsof 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 toimplement 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 method
for(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;
8. WAP toread 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 toimplement 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]; //swap
array[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;
}
11. WAP toexchange 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 storage
tmp = 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 tofind 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,intsize)
{
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<<”enterradius 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;
16. WAP tomaintain 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;
17. WAP toincrement 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()
18. Write aclass 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<<"amountto 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()
19. WAP todefine 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;
20. WAP togenerate 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;
else
return 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.
else
cout<< "All stringsare 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()
{
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)
26. WAP toread 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();