Describe the mechanism of accessing data members and member functions in the following cases: (i) inside the main program
Answers
Answered by
40
the mechanism of accessing data members and member functions in the following cases is done by
1.Using object and dot membership operator.
2.accessing a local variable of a function.
Example for how to access data members and member functions inside a member function of another class.
#include<iostream.h>
class a
{
public:
int x;
void display()
{
cout<<"sample \n";
x=111;
}
};
class b
{
public:
void display()
{
a s; cout<<" Member function \n"; s.
display();
cout<<" x = "<<example<<"\n";
}
};
void main()
{
b b1; // b1 is a object of class b.
b1.display();
}
2.accessing a local variable of a function.
Example for how to access data members and member functions inside a member function of another class.
#include<iostream.h>
class a
{
public:
int x;
void display()
{
cout<<"sample \n";
x=111;
}
};
class b
{
public:
void display()
{
a s; cout<<" Member function \n"; s.
display();
cout<<" x = "<<example<<"\n";
}
};
void main()
{
b b1; // b1 is a object of class b.
b1.display();
}
Similar questions
Social Sciences,
7 months ago
Math,
7 months ago
Science,
7 months ago
Social Sciences,
1 year ago
Social Sciences,
1 year ago
India Languages,
1 year ago
Biology,
1 year ago