#include <iostream>
class Abc // created class with name Abc
{
void display()//member function
{
cout<<"Displaying NO."<<endl;
}
};
int main()
{
Abc obj;//creating object of the class
obj.display();//calling member function inside class
}
Run Code Online (Sandbox Code Playgroud)
它返回错误为
main.cpp: In function 'int main()':
main.cpp:5:10: error: 'void Abc::display()' is private
void display()
^
main.cpp:13:17: error: within this context
obj.display();
^
Run Code Online (Sandbox Code Playgroud)
我试图使显示功能,public int main但它给出了错误
main.cpp:5:11: error: expected ':' before 'void'
public void display()
^
Run Code Online (Sandbox Code Playgroud)
声明为:
class Abc
{
public:
void display()
{
cout<<"Displaying NO."<<endl;
}
};
Run Code Online (Sandbox Code Playgroud)
要么:
struct Abc
{
void display()
{
cout<<"Displaying NO."<<endl;
}
};
Run Code Online (Sandbox Code Playgroud)
struct的默认保护是公共的,类的默认保护是私有的