public private protected c++

#include <iostream>
// Visibility is how visible certain members or methods of class are , who can see them ,who can call them and who can use them
//Visibility has no effect on performance of your program it is ust for organizing code
//Three basic visibility modifers are:
//1 private
//2 public
//3 protected
//default visibility of a struct is public
//default visibility of class is private 
class Entity
{
protected://means all sub classes and base class can access these functions and variables butcan't be accessed outside classes
	int P;
	void InitP () {
		P = 0;
		//initializes P to 0
	}
public://Pubic methods and variables can be accessed inside and outside of the class
	int a, b;
	void Init() {
		a = 0;
		b = 0;
	}
private://only entity class can read and write the variables exeption is friend
	int X , Y;
	void print(){
		// Content
		// only this function can be acessed inside the class unless you use friend keyword
	}
public:
	Entity() {
		X = 0;// can initialize x inside the class but can't access it from outside the class unsless you use friend keyword 
	}

};
class Player : public Entity// class palyer is a sub class of  class Entity
{
public:
	Player() {
		//X = 2;	// Error can't access the private members from base class
		//print();	// can't access it in sub class because it is private 
		a = 1;	// can acces it because it is public in base class
		b = 1;	// can acces it because it is public in base class
		Init(); // can acces it because it is public in base class
		P = 0;	// can access it in subclass because its visibility is protected
		InitP(); //can access it in subclass because its visibility is protected
	}
	 
};
int main()
{
	Entity e1;
	Player a;
	//e1.x;	//error can't access private members from here
	//e1.print(); // error inaccessible due to its visibility being private
	e1.a = 5;//can access from here because it's visibility is public 
	e1.Init();//can access from here because it's visibility is public
	a.a = 5;//can access from here because it's visibility in base class is public 
	a.Init();//can access from here because it's visibility in base class is public
	//e1.P;	//can't access it because  visibility is protected
	//e1.InitP; //can't access it because  visibility is protected
	// a.P;		//can't access it because  visibility is protected in base class
	// a.InitP; //can't access it because  visibility is protected in base class
	std::cin.get();
}

Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
c++ class public private protected c++ public private and protected class private public c++ public private protected in c++ public private protected class in c++ public private cpp how public and private work in c++ private and public in c++ private public in c++ what happens to private public protected in c++ during compilation what is public and private used for in c++ public, private protected c++ private public cpp what are public private and protected in c++ why is private public and protected in c++ why are public and private used in c++ can something be private and protected c++ private protected public C++ inheritance private public protected c++ private protected public protected and private in c++ c++ private protected public public private and protected c++ what is private public protected in c++ private public c++ why public protected private in c++ public private and protected class in c++ c++ class public private protected example private protected private c++ private,public &amp; protected private public and protected access specifier in c++ public private protected c++ deference protected private public c++ what is public private and protected in c++ private protected public inheritance c++ example of public private and protected in c++ public and private c++ c++ program using public private protected protected public private private public protected in c++ with example c++ public private private public members c++ private public c++ public private and protected in c++ with example public or private in c++ Public, Protected, Private public private protected access modifiers in c++ public private protected default inheritance c++ private variables private public protected c++ private and protected in c++ what is private public and protected in c++ cpp private public protected private public protected public protected private public private protected what are public protected and private in c++ private public function c++ protected private public private protected public cpp public private protected in c++ example public private protected scope in cpp how to access private and protected variable and member function in oops how to access private and protected variable and member function in opps class private public protected c++ public vs private vs protected c++ protected c ++ c++ protected private public protected in c++ one example Private and public in cpp protected in c++ cpp class private public protected protected members in c++ Members of a class are public by default in c++ modifiers class c++ what is private protected and public in c++ all about class public private in c++ c++ default access specifier for class method class access modifiers in c++ publoc , private and protected keywords in c++ public private c++ default public or private for variables in class c++ privat public class c++ protected and privat data type in C++ c++ protected access public vs protected c++ protected class c++ access modifiers in c++ c++ private public c++ class modifier c++ private public protected default c++ private public protected c++ private protected public friend Accsess modifier in c++ class private c++ c++ new access memebers of class c++ class modifiers how to get a private variable c++ c++ public vs protected public protected private c++ protected data members public and private in c++ c++ public private proetcted' cpp tool to speprate public and private member modifier c++ private vs public c++ public privte in c++ protected access modifier c++ private member functions how are public abd private members listed in C++ private variable c+ c++ public private protected do classes have protected data member in c++ data members are public by default in class c++ access modifiers in cpp private public protected class in c++ private protected and public c++ private protected public in c++ public private protected c++ public protected c++ private and public class in c++ c++ in class default access specifier private class public vs private c++ access public private in c++ public private protected c++
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source