private in c 2b 2b

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

showing results for - "private in c 2b 2b"
Mattia
28 Apr 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();
queries leading to this page
cpp protectedc 2b 2b protected methodprivate function c 2b 2bwhy to use protected in c 2b 2bwhat are protected variables c 2b 2bprotected c 2b 2b meaningprotected in c 2b 2b and java c 2b 2b what is a protected memberprotected variable classes c 2b 2bhow to define public in class in c 2b 2bwhat is public in c 2b 2bprivate function in c 2b 3dhow to use public c 2b 2bprotected function c 2b 2bwhats protected in cppprivate c 2b 2bc 2b 2b private methodc 2b 2b protected accessis protected in c 2b 2bprotected member is c 2b 2bwhat is protected in c 2b 2bhow to access protected function in c 2b 2bc 2b 2b protected variablec 2b 2b protected classc 2b 2b how to make protectedcpp protected functionc 2b 2b private functionprotected class c 2b 2b 2bc 2b 2b protected variablesprotected variable in c 2b 2bdefine private function c 2b 2bc 2b 2b how to use private methodsc protectedprotected data members in c 2b 2bprotected keyword cppprotected variable c 2b 2bprotected meaning c 2b 2bc 2b 2b classes when to use protectedprotected method c 2b 2bprivate protected c 2b 2bprivate functions in c 2b 2bc 2b 2b protected membersc 2b 2b protected methodsuse of protected in c 2b 2bprotected c 2b 2b classwhat is protected keyword in c 2b 2bc 2b 2b protected keywordhow to call protected function in c 2b 2bpublic function c 2b 2bis protected in c 2b 2b inheritenceaccess private function c 2b 2bc 2b 2b public functionprotected member in c 2b 2bcpp publicc 2b 2b protected meaningprotected member in c 2b 2b 5cwhat does protected do in c 2b 2b 5chow does cpp protected workprotected in c 2b 2bprotected functions c 2b 2bc 2b 2b define private methodpublic c 2b 2bc 2bc 2b protected what is use of protected in c 2b 2bwhy we use protected in c 2b 2bprotected i c 2b 2bprotected in c 2b 2b exampleprotected cppprotected var cppprotected en c 2b 2bhow to access protected members in c 2b 2bc 2b 2b protected function whta is private in c 2b 2bprotected keyword c 2b 2bc 2b 2b use private methodclass protected c 2b 2bprotected c 2b 2bprotected c 2b 2bprotected methods c 2b 2b protected c 2b 2bc 2b 2b what does protected keyword meanwhat does protected do in c 2b 2bwhat is protected c 2b 2bmeaning of protected c 2b 2bprotected field c 2b 2bc 2b 2b private functionsprotected in c 2b 2b classcpp protected variableprotected example c 2b 2b protected cppprotected in cpppprivate method in c 2b 2bwhat does protected mean in c 2b 2b classeswhat does protected mean in c 2b 2bprotected in cppwhy protected in c 2b 2bc 2b 2b protected datahow to access private function in c 2b 2bprotected class c 2b 2b exampleprotected in c 2b 2b eprotected keyword in c 2b 2bprotected class c 2b 2bprotected class in c 2b 2bwhat do protected do in cppc 2b 2b protected not working when defining constructorc 2b 2b protectedprotected members in c 2b 2bwhat is protected variable c 2b 2b classeswhat are protected members in c 2b 2bprivate in c 2b 2bprotected means in c 2b 2bwhat is protected member in c 2b 2bprotected constructor c 2b 2bpublic in c 2b 2bprotected keyword in cppc 2b 2b class protectedpublic vs protected c 2b 2bhow to access protected methods in c 2b 2bprivate in c 2b 2b