Advertisement
#include<iostream>
using namespace std;
class A
{
public:
int a = 10;
void message(); //declaration of the function named message()
};
//Defition of the function message of class A
void A :: message()
{
cout<< "Hello from the class A";
}
int main()
{
A ob; //Creating an object of class A
//Accessing the data member a, associated with the object of class A
cout<< "The value of a : " << ob.a << "\n";
//Calling the function message of class A, using its object
ob.message();
}
The value of a : 10
Hello from the class A
Advertisement
#include<iostream>
using namespace std;
class A
{
public:
int a = 10;
void message(); //declaration of the function named message()
};
//Defition of the function message of class A
void A :: message()
{
cout<< "Hello from the class A";
}
int main()
{
A ob; //Creating an object of class A
//Accessing the data member a, associated with the object of class A
cout<< "The value of a : " << ob.a << "\n";
//Calling the function message of class A, using its object
ob.message();
}
B.java
class B
{
public static void main(String... ar)
{
A ob= new A();
System.out.println(ob.a);
}
}
A.cpp: In function 'int main()':
A.cpp:26:34: error: 'int A::a' is private within this context
cout<< "The value of a : " << ob.a << "\n";
^
A.cpp:8:9: note: declared private here
int a = 10;
^~
A.cpp:29:12: error: 'void A::message()' is private within this context
ob.message();
^
A.cpp:14:6: note: declared private here
void A :: message()
#include<iostream>
using namespace std;
class A
{
protected:
int a = 10;
void message(); //declaration of the function named message()
};
//Defition of the function message of class A
void A :: message()
{
cout<< "Hello from the class A";
}
int main()
{
A ob; //Creating an object of class A
//Accessing the data member a, associated with the object of class A
cout<< "The value of a : " << ob.a << "\n";
//Calling the function message of class A, using its object
ob.message();
}
A.cpp: In function 'int main()':
A.cpp:26:34: error: 'int A::a' is protected within this context
cout<< "The value of a : " << ob.a << "\n";
^
A.cpp:8:9: note: declared protected here
int a = 10;
^~
A.cpp:29:12: error: 'void A::message()' is protected within this context
ob.message();
^
A.cpp:14:6: note: declared protected here
void A :: message()
^
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement