OOP Practicals
Practical No. 1
Write programs to evaluate any expression using Input/Output functions
#include<iostream.h>
#include<conio.h>
void main()
float basic,hra,da,gross;
clrscr();
cout<<”Enter basic salary = ”;
cin>>basic;
da=50*basic/100;
hra=40*basic/100;
gross=hra+da+basic;
cout<<”\nGross salary = ”<<gross;
getch();
#include<iostream.h>
#include<conio.h>
void main()
int a,b,c;
float x;
clrscr();
cout<<”Enter value of a,b,c =”;
cin>>a>>b>>c;
x=(-b-(b*b-4*a*c))/2*a;
cout<<”\nx= ”<<x;
getch();
Practical No. 2
Write a programs using- scope resolution operator, Memory management operator, Manipulators
1. #include <iostream.h>
2. int x = 10; // Global variable
3. int main()
4. {
5. int x = 20; // Local variable
6. int *ptr;
7. cout << "Local x: " << x << endl;
8. cout << "Global x: " << ::x << endl;
9. // Dynamic memory allocation
10. ptr = new int;
11. *ptr = 100;
12. cout << "Value stored: " << *ptr << endl;
13. // Deallocate memory
14. delete ptr;
15. cout<<setw(8)<<x;
16. return 0;
17. }
18.
Practical No. 3
Write programs to demonstrate use of- • Implicit type casting • Explicit type casting
1. #include <iostream.h>
2. int main()
3. {
4. int a = 10;
5. float b = 2.5;
6. double c = 3.14;
7. int d = 2;
8. // Implicit Type Casting
9. float result1 = a + b; // a is implicitly converted to float
10. cout << "Result of implicit casting: " << result1 << endl;
11. // Explicit Type Casting
12. int result2 = (int)c + d; // c is explicitly converted to int
13. cout << "Result of explicit casting: " << result2 << endl;
14. return 0;
15. }
16.
Practical No. 4
Write programs to show use of classes and objects to define the function inside the class
#include<iostream.h>
#include<conio.h>
class circle
{
float r,a;
public:
void read()
{
cout<<"\nenter radius =";
cin>>r;
}
void compute()
{
a=3.14*r*r;
}
void display()
{
cout<<"\nArea = "<<a;
}
};
void main()
{
circle c;
clrscr();
c.read();
c.compute();
c.display();
getch();
}
Practical No. 5
Write programs to define the function outside the class.
#include<iostream.h>
#include<conio.h>
class circle
{
float r,a;
public:
void read();
void compute();
void display();
};
void circle::read()
{
cout<<"\nenter radius =";
cin>>r;
}
void circle::compute()
{
a=3.14*r*r;
}
void circle::display()
{
cout<<"\nArea = "<<a;
}
void main()
{
circle c;
clrscr();
c.read();
c.compute();
c.display();
getch();
}
Practical No. 6
Write programs to implement inline function.
#include<iostream.h>
#include<conio.h>
class circle
{
float r,a;
public:
void read();
void compute();
void display();
};
inline circle::read()
{
cout<<"\nenter radius =";
cin>>r;
}
inline circle::compute()
{
a=3.14*r*r;
}
inline circle::display()
{
cout<<"\nArea = "<<a;
}
void main()
{
circle c;
clrscr();
c.read();
c.compute();
c.display();
getch();
}
Practical No. 7
Write programs to implement friend function using- • Two different classes • External function
#include<iostream.h>
#include<conio.h>
class C1
{
int x;
public:
void read()
{
cout<<"Enter a number =";
cin>>x;
}
friend int Greatest(C1 c1,C2 c2);
};
class C2
{
int x;
public:
void read()
{
cout<<"\nEnter a number = ";
cin>>x;
}
friend int Greatest(C1 c1,C2 c2);
};
int Greatest(C1 c1,C2 c2)
{
if(c1.x > c2.x)
{
return c1.x;
}
else
{
return c2.x;
}
}
void main()
{
C1 c1;
C2 c2;
int a;
clrscr();
c1.read();
c2.read();
a=Greatest(c1,c2);
cout<<"\nGreatest number is "<<a;
getch();
}
Practical No. 8
Write programs to implement- • Static data member • Static member function
#include<iostream.h>
#include<conio.h>
class interest
{
private:
static float rate;
float principal,duration,sim_int;
public:
void accept()
{
cout<<"Enter principal amount and duration = ";
cin>>principal>>duration;
}
void calculate()
{
sim_int=principal*rate*duration;
}
void display()
{
cout<<"\nsimple interest ="<<sim_int;
}
};
float interest::rate=10;
void main()
{
interest i;
clrscr();
i.accept();
i.calculate();
i.display();
getch();
}
Practical No. 9
Write programs to create array of objects.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class staff
{
private:
char name[20],dept[20];
public:
void accept()
{
cout<<"Enter name and department = ";
cin>>name>>dept;
}
void display()
{
cout<<name<<"\t"<<dept<<endl;
}
};
void main()
{
int i;
staff s[5];
clrscr();
for(i=0;i<5;i++)
{
s[i].accept();
}
cout<<"\nName\tDepartment\n";
for(i=0;i<5;i++)
{
s[i].display();
}
getch();
}
Practical No. 10
Write programs for- • Default constructor • Parameterized constructor • Copy constructor • Multiple
constructors in one class