0% found this document useful (0 votes)
46 views1 page

Protected Members Are Accessible Only To The Immediate Derived Class (Subtype)

Protected members in a class can only be accessed by subclasses of that class, not by other unrelated classes or subclasses further down the inheritance chain. A protected method in class A can be called by class B which inherits from A, but class C which inherits from B cannot call the protected method in class A, even though C is a subclass of A's subclass B. Virtual functions add a small runtime overhead since the actual function to call is determined based on the object's type at runtime rather than compile-time.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views1 page

Protected Members Are Accessible Only To The Immediate Derived Class (Subtype)

Protected members in a class can only be accessed by subclasses of that class, not by other unrelated classes or subclasses further down the inheritance chain. A protected method in class A can be called by class B which inherits from A, but class C which inherits from B cannot call the protected method in class A, even though C is a subclass of A's subclass B. Virtual functions add a small runtime overhead since the actual function to call is determined based on the object's type at runtime rather than compile-time.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Protected members are accessible only to the immediate derived class (subtype)

#include<iostream>

using namespace std;

class A{

protected:
void test(){
cout<<"A";
}
};

class B:public A{ // son of A

public:
void test2(){
test();
}
};

class C: public B{ // grandson of A


public:
void test3(){
test();
}

};

int main(){

cout<<"dfgd";
C cc;
cc.test();// no access to grandfather’s content :D

return 0;

Virtual functions are less efficient as they are resolved at runtime (on each call!)

You might also like