Object Oriented Programming Practical 8
Question 1
Write a Program to calculate weight of object at different planets using formula weight=m*g, Where m=mass of object G=gravitational force Declare g as static member variable.
weight.cpp
C
#include <iostream.h>
#include <conio.h>
class Planet
{
public:
static double g;
static void setGravity(double gravity)
{
g = gravity;
}
static double calculateWeight(double mass)
{
return mass * g;
}
};
double Planet::g=9.8;
int main()
{
double mass;
double gravity;
cout << "Enter the mass of the object: ";
cin >> mass;
cout << "Enter the gravitational force on the planet: ";
cin >> gravity;
Planet::setGravity(gravity);
long double weight = Planet::calculateWeight(mass);
cout << "The weight of the object on the planet is: " << weight << " N" << endl;
getch();
return 0;
}
Output

Question 2
Write a Program to define a class having data members principal, duration and rate of interest. Declare rate _of_ interest as static member variable .calculate the simple interest and display it.
interest.cpp
C
#include <iostream.h>
#include <conio.h>
class Interest
{
private:
float principal;
int duration;
public:
static float rate_of_interest;
void accept(float p,int d)
{
principal=p;
duration=d;
}
float calculateInterest()
{
return (principal * duration * rate_of_interest) / 100;
}
void display()
{
float interest = calculateInterest();
cout << "The simple interest is: " << interest << endl;
}
};
float Interest::rate_of_interest = 5.0;
int main()
{
float principal;
int duration;
float rate;
cout << "Enter the principal amount: ";
cin >> principal;
cout << "Enter the duration (in years): ";
cin >> duration;
cout << "Enter the rate of interest: ";
cin >> rate;
Interest::rate_of_interest = rate;
Interest i;
i.accept(principal, duration);
i.display();
getch();
return 0;
}
Output
