#7 | C++ | Write a program to calculate Simple Interest using friend function The input must be taken in a member function designed outside the class.

 

 

CompTech


 C++ Practical Assignment

#practical 7








YouTube Video :- 




Question :- 

Write a program to calculate Simple Interest using friend function The input must be taken in a member function designed outside the class.

Code :-

/* 4. Write a program to calculate Simple Interest using friend function.
The input must be taken in a member function designed outside the class. */

#include<iostream>
using namespace std;

// Cretae class Simpal Interest
class Simpal_Interest
{
    //Declare the Variables to store data in Private Section
    // p = Principal Amount
    // r = Rate of Interest
    // n = Number of Years
    int p,r,n;
    float si=0;

    // Public section, declare member function and friend function
    public:
        void getdata();
        friend void display(Simpal_Interest);
};

// Define the body of member function getdata()
void Simpal_Interest :: getdata()
{
    // Pass the message to user to Enter the values and
    // Store them in our variables

    cout<<"- Enter Principal Amount :- ";
    cin>>p;
    cout<<"- Enter Rate of Interest:- ";
    cin>>r;
    cout<<"- Enter Number of Years:- ";
    cin>>n;
}

// Define body of friend function display()
void display(Simpal_Interest s)
{
    // We will calculate the simpal interest and display
    // Si = (p * r *n)/100

    s.si = (float) (s.p * s.r * s.n)/100;

    cout<<"\n\t- Simpal Interest = "<<s.si;
}

int main()
{
    // Declaring object of class
    Simpal_Interest s;

    // Calling member function getdata()
    s.getdata();

    //Calling Friend function display()
    display(s);
}Description :-
1. Create Class and give the name as Maximum.
2. In the class Private Section, declare variables to store the three numbers.
3. Write Public for public section in Class and in Public Declare two Functions.
1. Member Function -> getdata() -> Get The Value from the user and store in variables
2. Friend Function -> max() -> Find the maximum Number from three numbers.
4. Friend function is only declare in class. It can not be define in class.
5. We have to use "friend" keyword to declare the friend function. In this we have to pass the class name as argument
6. In getdata() function body, we have to pass the message that "Enter a Number " and after entering a number we have to store the values in variables that are declared in private section of class.
7. Friend funtion body is define outside the class as the normal function defination.
8. In arrgument of friend function, we have to pass the class name and object name as Parameters.
9. We can not use the private section of class directly, We have to use object to access the private members of class
Ex. object name = m , Private variable = a;
- right method -> m.a = 5;
- Wrong method -> a = 5;
10. In Friend function max() body, we will use the if...else if.... statements to find the maximum number.
11. In main() function, first we have to declare our object to access the data of class.
12. Then we will call the getdata() member function with the use of our object.
13. At Last, we will call our friend function max() as normal function Calling but we have to pass the object name as parameter.


! Thank You For Visiting !



Comments

Popular posts from this blog

#9 | Pointer | Write a program to count vowels and consonants in string using pointer

#6 | Pointers | Write a Program to store n Elements in an array and print them using Pointers

#5 | Pointers | Find Maximum Number Using Pointers | #comptech #prathamrathod