OOPs Using C++ (Lab Manual)
OOPs Using C++ (Lab Manual)
Write a C++ program to find both the largest and smallest number in a list of
integers.
#include<iostream.h>
void main ()
{
int arr[10], n, i, max, min;
cout << "Enter the size of the array : ";
cin >> n;
cout << "Enter the elements of the array : ";
for (i = 0; i < n; i++)
cin >> arr[i];
max = arr[0];
for (i = 0; i < n; i++)
{
if (max < arr[i])
max = arr[i];
}
min = arr[0];
for (i = 0; i < n; i++)
{
if (min > arr[i])
min = arr[i];
}
cout << "Largest element : " << max<<endl;
cout << "Smallest element : " << min<<endl;
}
2. Write a C++ program to sort a list of numbers in ascending order.
#include <iostream.h>
void main()
{
int num[100],i,j,size,temp;
cout<<"Enter the total numbers in the list "<<endl;
cin>>size;
//input all the numbers in the list
for(i=0;i<size;i++)
{
cout<<"Enter number "<<i+1<<endl;
cin>>num[i];
}
#include <iostream.h>
void main()
{
int num, digit, sum=0, i;
cout<<"Enter the number "<<endl;
cin>>num;
#include <iostream.h>
void main()
{
int last,i,j,num, flag=0;
cout<<"Enter the last number in the list "<<endl;
cin>>last;
for(i=2;i<=last;i++)
{
num=i;
flag=0;
for(j=2;j<=num/2;j++)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
{
cout<<i<<endl;
}
}
}
5. To write C++ program to implement inline function.
#include<iostream.h>
class Book
{
private:
int book_no;
char book_name[20];
public:
void accept();
void disp();
};
cin>>book_no;
cin>>book_name;
}
inline void Book::disp()
}
void main ()
{
Book bk;
bk.accept();
bk.disp(); }
6. To implement the concept of function with default arguments.
#include<iostream.h>
class Temp
{
private:
int result;
public:
void sum(int x=5, int y=10) //Default arguments
{
result=x+y;
cout<<"Sum is : "<<result<<endl;
};
void main ()
Temp tmp;
#include <iostream.h>
class student
{
private:
int rollno;
char name[25];
public:
void accept()
{
cout<<"Enter rollno of the student : ";
cin>>rollno;
cout<<"Enter name of the students : ";
cin>>name;
}
void disp()
{
cout<<"Rollno : "<<rollno<<endl;
cout<<"Name : "<<name<<endl;
}
};
void main()
{
student st;
st.accept();
st.disp();
}
8. Write a C++ program to declare a class. Declare pointer to class. Initialize and
display the contents of the class member.
#include<iostream.h>
class Circle
{
private:
float radius, area;
public:
void get(float r)
{
radius=r;
void put()
area=3.14*radius*radius;
cout<<"Area of circle is "<<area<<endl;
};
void main ()
{
Circle cr, *ptr;
ptr=&cr;
ptr->get(5.0);
ptr->put();
}
9. Given that an EMPLOYEE class contains the following members: Data Members:
Employee_Number, Employee_Name, Basic, DA, IT,Net_Sal. Member Functions: to
read data, to calculate Net_Sal and to print data members. Write a C++ program to
read data on N employees and compute the Net_Sal of eachemployee(DA = 52% of
Basic and Income Tax = 30% of the gross salary).
#include <iostream.h>
class Employee
{
private:
int employee_number;
char employee_name[25];
float basic, da, it, net_sal;
public:
void accept()
{
cout<<"Enter employee number : ";
cin>>employee_number;
cout<<"Enter employee name : ";
cin>>employee_name;
cout<<"Enter basic salary : ";
cin>>basic;
}
void disp()
{
float gross_sal;
cout<<"Employee Number : "<<employee_number<<endl;
cout<<"Emploee Name : "<<employee_name<<endl;
gross_sal = basic + (basic*52/100);
net_sal=gross_sal - (gross_sal*30/100);
cout<<"Net Salary : "<<net_sal<<endl;
}
};
void main()
{
Employee emp[5];
int i;
cout<<"Details of 5 employees "<<endl;
for(i=0;i<5;i++)
{
emp[i].accept();
}
for(i=0;i<5;i++)
{
emp[i].disp(); }}
10. Write a program to generate fibonacci series use Constructor to initialize the data
members.
#include<iostream.h>
class Fibonacii
{
private:
int a, b, c;
public:
Fibonacii()
{
a=-1;
b=1;
void fibseries(int n)
{
cout<<"Fibonacci Series is : "<<endl;
for(int i=0;i<n;i++)
{
c=a+b;
cout<<c<<endl;
a=b;
b=c;
}
}
};
void main ()
{
Fibonacii fib;
fib.fibseries(15);
}
11. Write a C++ program to illustrate default constructor, parameterized constructor
and copy constructors.
#include <iostream.h>
class Area
{
private:
int length, breadth;
public:
Area() //Default Constructor
{
length=60;
breadth=15;
}
Area(int l, int b) //Parameterised Constructor
{
length=l;
breadth=b;
}
Area(Area &ar) //Copy Constructor
{
length=ar.length;
breadth=ar.breadth;
}
void calc_area()
{
int area;
area=length*breadth;
cout<<"Area of rectangle : "<<area<<endl;
}
};
void main()
{
Area ar1;
ar1.calc_area();
Area ar2(20,70);
ar2.calc_area();
Area ar3=ar2;
ar3.calc_area();
}
12. Write a C++ program to implement single inheritance using c++.
#include <iostream.h>
class Furniture
{
protected:
int length, breadth;
};
#include <iostream.h>
class Employee
{
protected:
long int id;
char name[25];
};
class BusinessMan
{
protected:
int familymembers;
};
class Person:public Employee, public BusinessMan
{
private:
int age;
public:
void accept()
{
cout<<"Enter your id : ";
cin>>id;
cout<<"Enter your name : ";
cin>>name;
cout<<"Enter your age : ";
cin>>age;
cout<<"Enter number of your family members : ";
cin>>familymembers;
}
void disp()
{
cout<<"Resident Id : "<<id<<endl;
cout<<"Name : "<<name<<endl;
cout<<"Age : "<<age<<endl;
cout<<"Family members : "<<familymembers<<endl;
}
};
void main()
{
Person pers;
pers.accept();
pers.disp();
}
14. Write a C++ program to implement multilevel inheritance.
#include <iostream.h>
class GrandParents
{
protected:
int level1;
};
class Parents:public GrandParents
{
protected:
int level2;
};
class GrandChild:public Parents
{
private:
int level3;
public:
void show()
{
level1=1;
level2=2;
level3=3;
cout<<"Grand Parents Level : "<<level1<<endl;
cout<<"Parents Level : "<<level2<<endl;
cout<<"Grand Child Level : "<<level3<<endl;
}
};
void main()
{
GrandChild g;
g.show();
}
15. Write a C++ program to use pointer for both base and derived classes and call the
member function with the help of virtual keyword.
#include <iostream.h>
class Rectangle
{
private:
int length, breadth, area;
public:
virtual void put()
{
length=20;
breadth=10;
area=length*breadth;
cout<<"Area of Rectangle : "<<area<<endl;
}
};
class Square:public Rectangle
{
private:
int side, area;
public:
void put()
{
side=20;
area=side*side;
cout<<"Area of Square : "<<area<<endl;
}
};
void main()
{
Rectangle *rect;
Square s;
Rectangle r;
//creating derived class object
rect=&s;
//calling derived class member function
rect->put();
//creating base class object
rect=&r;
//calling base class member function
rect->put();
}
16. To write a C++ program to implement the concept of Virtual functions.
#include <iostream.h>
class Parent
{
public:
virtual void show() //pure virtual function
{
cout<<"Parent class member function "<<endl;
}
};
#include <iostream.h>
class GrandParents
{
public:
void show()
{
cout<<"Hello Students!";
}
};
class Parents1:virtual public GrandParents
{
};
class Parents2:virtual public GrandParents
{
};
class GrandChild:public Parents1, public Parents2
{
};
void main()
{
GrandChild g;
g.show();
}
18. Write a Program containing a possible exception. Use a Try block and a Catch block
to handle it properly.
#include <iostream.h>
#include <stdexcept>
void main()
{
// try block
try {
int numerator = 10;
int denominator = 0;
int res;
if (denominator == 0)
{
throw runtime_error("Division by zero not allowed!");
}
cout << "Result after division: " << res << endl;
}
#include <iostream.h>
class Count
{
private:
int value;
public:
Count()
{
value=5;
}
void operator ++ ()
{
++value;
}
void operator ++ (int)
{
value++;
}
void display()
{
cout << "Count: " << value << endl;
}
};
void main()
{
Count cnt;
cnt++;
cnt.display();
++cnt;
cnt.display();
}
20. Write a C++ program to implement the concept of binary operator overloading.
#include <iostream.h>
class Addition
{
private:
int x, y;
public:
void input()
{
cout << " Enter the first number: ";
cin >> x;
}
void input2()
{
cout << " Enter the second number: ";
cin >> y;
}
// overloading the binary '+' operator to add number
Addition operator + (Addition &ob)
{
Addition A;
A.x = x + ob.x;
return A;
}
void print()
{
cout << "The sum of two numbers is: " <<x;
}
};
void main ()
{
Addition x1, y1, ans;
x1.input();
y1.input();
ans = x1 + y1;
ans.print();
}