Object Oriented Programming Practical 5
Question 1
Write a C++ program to declare a class "staff' having data members name,
basic salary, DA, HRA and calculate gross salary. Accept and display data for one staff.
Where DA=74.5% of basic
HRA= 30% of basic.
Gross_salary=basic+HRA+DA
staff.cpp
C
#include <iostream.h>
#include <conio.h>
class staff
{
private:
char name[30];
int basic_sal;
float da,hra,gross;
public:
void acceptData()
{
cout << "Enter staff name: ";
cin >> name;
cout << "Enter basic salary: ";
cin >> basic_sal;
}
void calculateSal()
{
da=(74.5*basic_sal)/100;
hra=(30*basic_sal)/100;
gross=basic_sal+hra+da;
cout << "\n--- staff Details ---" << endl;
cout << "Name: " << name << endl;
cout << "Basic salary: " << basic_sal << endl;
cout << "DA: " << da << endl;
cout << "HRA: " << hra << endl;
cout << "Gross salary: " << gross << endl;
}
};
int main()
{
staff staff1;
staff1.acceptData();
staff1.calculateSal();
getch();
return 0;
}
Output

Question 2
Write a program to find area of circle such that the class circle must have three functions namely:
a)read() to accept the radius from user.
b)compute() for calculating the area
c)display() for displaying the result.(Use Scope resolution operator)
circle.cpp
C
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class circle
{
public:
int radius;
float area;
void read();
void compute();
void display();
};
void circle::read()
{
cout<<"Enter radius of circle: ";
cin>>radius;
}
void circle::compute()
{
area=3.14*radius*radius;
}
void circle::display()
{
cout<<"Area of circle is: "<<area<<endl;
}
void main()
{
clrscr();
circle c;
c.read();
c.compute();
c.display();
getch();
}
Output

Question 3
Define a class complex with data members real and imaginary, member function read() and write(). Write a program to perform the addition of two complex number and display the result.
complex.cpp
C
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class complex
{
public:
int real,img;
void read()
{
cout<<"\nEnter real part: ";
cin>>real;
cout<<"\nEnter imaginary part: ";
cin>>img;
}
void write()
{
cout<<"\nReal part: "<<real<<endl;
cout<<"\nImaginary part: "<<img<<endl;
}
};
void main()
{
clrscr();
complex c1,c2,c3;
cout<<"Enter complex number 1:";
c1.read();
cout<<"\nEnter complex number 2:";
c2.read();
c3.real=c1.real+c2.real;
c3.img=c1.img+c2.img;
cout<<"\nAddition of two complex numbers is:"<<endl;
c3.write();
getch();
}
Output
