Public members are accessible from outside the class. They can be accessed directly using an object of the class.
class MyClass {
public:
int myNumber;
};
int main() {
MyClass obj;
obj.myNumber = 15;
return 0;
}
Here, myNumber is a public member of MyClass, thus it can be accessed and modified directly using an object of MyClass.
Private members cannot be accessed, or even viewed from outside the class. Only the class's own methods can access these members.
class MyClass {
private:
int myNumber;
public:
void setNumber(int n) {
myNumber = n;
}
int getNumber() {
return myNumber;
}
};
int main() {
MyClass obj;
obj.setNumber(15);
return obj.getNumber();
}
Here, myNumber is a private member of MyClass, so it can only be accessed or modified through the public methods setNumber and getNumber.
Protected members are similar to private, but they can also be accessed in derived classes.
class Base {
protected:
int myNumber;
};
class Derived : public Base {
public:
void setNumber(int n) {
myNumber = n;
}
int getNumber() {
return myNumber;
}
};
int main() {
Derived obj;
obj.setNumber(15);
return obj.getNumber();
}
Here, myNumber is a protected member of Base, allowing it to be accessed in the Derived class.
In C++, the default access specifier for members of a class is private.
class MyClass {
int myNumber; // private by default
public:
void setNumber(int n) {
myNumber = n;
}
int getNumber() {
return myNumber;
}
};
int main() {
MyClass obj;
obj.setNumber(15);
return obj.getNumber();
}
Without specifying, myNumber is private by default, and it requires public methods to access or modify it.
A friend function can access private and protected members of a class in which it is declared as a friend.
class MyClass {
private:
int myNumber;
public:
MyClass() : myNumber(0) {}
friend void setNumber(MyClass &obj, int n);
};
void setNumber(MyClass &obj, int n) {
obj.myNumber = n;
}
int main() {
MyClass obj;
setNumber(obj, 15);
return 0;
}
The function setNumber is a friend of MyClass and can access its private members directly.
A friend class can access private and protected members of another class in which it is declared as a friend.
class MyClass {
private:
int myNumber;
public:
MyClass() : myNumber(0) {}
friend class FriendClass;
};
class FriendClass {
public:
void setNumber(MyClass &obj, int n) {
obj.myNumber = n;
}
};
int main() {
MyClass obj;
FriendClass fObj;
fObj.setNumber(obj, 15);
return 0;
}
FriendClass is a friend of MyClass and can access its private members directly.
In public inheritance, public members of the base class become public in the derived class, while protected members remain protected.
class Base {
public:
int publicVar;
protected:
int protectedVar;
};
class Derived : public Base {
public:
void setVars(int pub, int prot) {
publicVar = pub;
protectedVar = prot;
}
};
int main() {
Derived obj;
obj.setVars(10, 20);
return 0;
}
Derived class can access public and protected members of the Base class, but not private members.
In protected inheritance, both public and protected members of the base class become protected in the derived class.
class Base {
public:
int publicVar;
protected:
int protectedVar;
};
class Derived : protected Base {
public:
void setVars(int pub, int prot) {
publicVar = pub;
protectedVar = prot;
}
};
int main() {
Derived obj;
// obj.publicVar = 10; // Error: publicVar is protected in Derived
obj.setVars(10, 20);
return 0;
}
In this case, publicVar is not directly accessible from outside the Derived class since it becomes protected.
In private inheritance, both public and protected members of the base class become private in the derived class.
class Base {
public:
int publicVar;
protected:
int protectedVar;
};
class Derived : private Base {
public:
void setVars(int pub, int prot) {
publicVar = pub;
protectedVar = prot;
}
};
int main() {
Derived obj;
// obj.publicVar = 10; // Error: publicVar is private in Derived
obj.setVars(10, 20);
return 0;
}
In this scenario, publicVar and protectedVar are private in Derived and cannot be accessed directly from outside.
In C++, members of a struct are public by default, unlike a class where they are private by default.
struct MyStruct {
int myNumber; // public by default
};
int main() {
MyStruct obj;
obj.myNumber = 15;
return obj.myNumber;
}
Here, myNumber is public by default in MyStruct, allowing direct access and modification.
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies