public private protected c 2b 2b

Solutions on MaxInterview for public private protected c 2b 2b by the best coders in the world

showing results for - "public private protected c 2b 2b"
Emely
01 Feb 2020
1#include <iostream>
2// Visibility is how visible certain members or methods of class are , who can see them ,who can call them and who can use them
3//Visibility has no effect on performance of your program it is ust for organizing code
4//Three basic visibility modifers are:
5//1 private
6//2 public
7//3 protected
8//default visibility of a struct is public
9//default visibility of class is private 
10class Entity
11{
12protected://means all sub classes and base class can access these functions and variables butcan't be accessed outside classes
13	int P;
14	void InitP () {
15		P = 0;
16		//initializes P to 0
17	}
18public://Pubic methods and variables can be accessed inside and outside of the class
19	int a, b;
20	void Init() {
21		a = 0;
22		b = 0;
23	}
24private://only entity class can read and write the variables exeption is friend
25	int X , Y;
26	void print(){
27		// Content
28		// only this function can be acessed inside the class unless you use friend keyword
29	}
30public:
31	Entity() {
32		X = 0;// can initialize x inside the class but can't access it from outside the class unsless you use friend keyword 
33	}
34
35};
36class Player : public Entity// class palyer is a sub class of  class Entity
37{
38public:
39	Player() {
40		//X = 2;	// Error can't access the private members from base class
41		//print();	// can't access it in sub class because it is private 
42		a = 1;	// can acces it because it is public in base class
43		b = 1;	// can acces it because it is public in base class
44		Init(); // can acces it because it is public in base class
45		P = 0;	// can access it in subclass because its visibility is protected
46		InitP(); //can access it in subclass because its visibility is protected
47	}
48	 
49};
50int main()
51{
52	Entity e1;
53	Player a;
54	//e1.x;	//error can't access private members from here
55	//e1.print(); // error inaccessible due to its visibility being private
56	e1.a = 5;//can access from here because it's visibility is public 
57	e1.Init();//can access from here because it's visibility is public
58	a.a = 5;//can access from here because it's visibility in base class is public 
59	a.Init();//can access from here because it's visibility in base class is public
60	//e1.P;	//can't access it because  visibility is protected
61	//e1.InitP; //can't access it because  visibility is protected
62	// a.P;		//can't access it because  visibility is protected in base class
63	// a.InitP; //can't access it because  visibility is protected in base class
64	std::cin.get();
65}
queries leading to this page
public private and protected variables in c 2b 2bprivate public protected in c 2b 2b with exampleprivate protected public cinheritance public private protected c 2b 2bscope of public private protected and default in cppc 2b 2b public private protected security 3fc 2b 2b visibilityc 2b 2b function visibilityvisibility in c 2b 2bwhy is it necessary to add a visibility level while inheriting from base classc 2b 2b default visibilityprivate protected publicpublic default protected private in c 2b 2b visibility c 2b 2bvisibility mode in inheritance c 2b 2bprotected visibility example in c 2b 2b 2bprivate public and protected c 2b 2bprivate protected public inheritance c 2b 2bc 2b 2b all visibilityprivate protected public c 2b 2bwhy is it necessary to add a visibility label while inheriting from base classwhat is the use of public and private in c 2b 2bpublic protected private cpppublic private protected c 2b 2bpublic protected private in c 2b 2binheritance visibilityc 2b 2b make private member publicpublic private protectedpublic protected and private in oops c 2b 2bprivate and public in c 2b 2bc 2b 2b public protected privateprivate public c 2b 2bprivate public protected in c 2b 2bpublic private c 2b 2b classpublic protected privateinheritance visibility modes in c 2b 2buse of public private and protected in c 2b 2bc 2b 2b what does public private protected inheritanceprivate public protected class in c 2b 2bprivate public protectedin public visibility mode 2c protected members of class becomes members of class and public members of class becomes members of class visibility modes in oopc 2b 2b public private protectedvisibility modes in c 2b 2bpublic and private c 2b 2bpublic private protected c 2b 2b