我想我已经在这个程序中正确编码了一切但仍然出错.si它说的对象无法访问.
#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();
}
Run Code Online (Sandbox Code Playgroud)
当编译器告诉你某些东西不可访问时,它正在谈论public:vs. protected:vs. private:访问控制.默认情况下,类的所有成员都是private:,因此您无法访问其中的任何成员main(),包括构造函数和方法.
要创建构造函数public,public:请在类中添加一个部分,并将构造函数和方法放在那里:
class s_interest
{
int p,t;
float r;
public: // <<== Add this
s_interest(int a, int b, float c)
{
...
}
void method()
{
...
}
};
Run Code Online (Sandbox Code Playgroud)