Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why getting errors in c++ program?

Tags:

c++

object

class

I think I have coded everything correctly in this program but still getting errors. The object si it says it's not accessible.

#include<conio.h>
#include<iostream.h>

class s_interest
{
    int p,t;
    float r;
    s_interest(int a, int b, float c)
    {
        p=a;
        r=b;
        t=c;
    }

    void method()
    {
        int result=(float)(p*t*r)/100;
        cout<<"Simple interest:"<<result;
    }
};

void main()
{
    int pr,ti;
    float ra;
    cout<<"\n Enter the principle, rate of interest and time to calculate Simple Interest:";
    cin>>pr>>ti>>ra;
    s_interest si(pr,ti,ra);
    si.method();
}
like image 529
user2621947 Avatar asked Dec 29 '25 07:12

user2621947


1 Answers

When the compiler tells you that something is not accessible, it's talking about the public: vs. protected: vs. private: access control. By default, all members of a class are private:, so you cannot access any of them from main(), including the constructor and the method.

To make the constructor public, add a public: section to your class, and put the constructor and the method there:

class s_interest
{
    int p,t;
    float r;
public: // <<== Add this
    s_interest(int a, int b, float c)
    {
        ...
    }
    void method()
    {
        ...
    }
};
like image 61
Sergey Kalinichenko Avatar answered Dec 30 '25 21:12

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!