public in c 2b 2b

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

showing results for - "public in c 2b 2b"
Enrico
14 Jun 2018
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();