In c++ programming for the access specifiers ,
Can I write
Protected: int a , b;
Or is it mandatory to write like
Protected:
int a,b;
That is can I write protected data members in the same line or should I have to write it in the nextline
And pls give an example of the syntax in the program pls pls then only my heart will be satisfied
Answers
you can write int a, b ; and int a,b;
because c++ not allows spaces and underscores if you give a space it cant seen to the c compiler
hopes helps
Answer - Both are valid.
What is protected Access Specifier:
- Protected members and functions cannot be accessed from other classes directly, but they can be used by the class derived from this class (or) can be accessed from derived class.
Sample Examples:
(i) Protected: int a,b;
#include<iostream.h> (or) #include<iostream>
using namespace std;
class Brainly
{
protected: int a,b;
};
class Quora: public Brainly
{
public:
void display()
{
a = 10;
b = 20;
cout <<"\nThe value of a: "<<a<<endl;
cout <<"The value of b: "<<b<<endl;
}
};
int main()
{
Quora Q;
Q.display();
}
Output:
The value of a : 10
The value of b : 20
(ii) Protected: a,b{Next line}
#include<iostream>
using namespace std;
class Super
{
protected:
int a,b;
};
class sub: public Super
{
public:
void display(int c,int d)
{
a = c;
b = d;
cout <<"\nThe value of a:" <<c<<endl;
cout<<"The value of b:" <<d<<endl;
}
};
int main()
{
sub s;
s.display(10,20);
}
Output:
The value of a : 10
The value of b : 20
Hope it helps!