What happens when more restrictive access is given to a derived class method in C++? Last Updated : 18 May, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report We have discussed a similar topic in Java here. Unlike Java, C++ allows to give more restrictive access to derived class methods. For example the following program compiles fine. C++ #include<iostream> using namespace std; class Base { public: virtual int fun(int i) { } }; class Derived: public Base { private: int fun(int x) { } }; int main() { } In the above program, if we change main() to following, will get compiler error because fun() is private in derived class. C++ int main() { Derived d; d.fun(1); return 0; } What about the below program? C++ #include<iostream> using namespace std; class Base { public: virtual int fun(int i) { cout << "Base::fun(int i) called"; } }; class Derived: public Base { private: int fun(int x) { cout << "Derived::fun(int x) called"; } }; int main() { Base *ptr = new Derived; ptr->fun(10); return 0; } Output: Derived::fun(int x) called In the above program, private function "Derived::fun(int )" is being called through a base class pointer, the program works fine because fun() is public in base class. Access specifiers are checked at compile time and fun() is public in base class. At run time, only the function corresponding to the pointed object is called and access specifier is not checked. So a private function of derived class is being called through a pointer of base class. Comment More infoAdvertise with us Next Article Why Can't We Declare a std::vector in C++? K kartik Follow Improve Article Tags : C++ Practice Tags : CPP Similar Reads Hiding of all Overloaded Methods with Same Name in Base Class in C++ In C++, function overloading is possible i.e., two or more functions from the same class can have the same name but different parameters. However, if a derived class redefines the base class member method then all the base class methods with the same name become hidden in the derived class. For exam 3 min read What happens when a virtual function is called inside a non-virtual function in C++ Predict the output of the following simple C++ program without any virtual function. CPP #include <iostream> using namespace std; class Base { public: void print() { cout << "Base class print function \n"; } void invoke() { cout << "Base class invoke function \n 2 min read Why Can't We Declare a std::vector<AbstractClass> in C++? In C++, Standard Template Library (STL) we have a container called std::vector, which is used for managing collections of objects. However, we might encounter difficulties when trying to declare a std::vector of an abstract class in C++. In this article, we will learn why we cannot declare a std::ve 4 min read What all is inherited from parent class in C++? Following are the things that a derived class inherits from its parent: All the public and protected data members and member functions of the base class. Private members are not inherited.The assignment operator (=) of the base class.The type of the base class, which allows the derived class to be t 3 min read Difference between Private and Protected in C++ with Example ProtectedProtected access modifier is similar to that of private access modifiers, the difference is that the class member declared as Protected are inaccessible outside the class but they can be accessed by any subclass(derived class) of that class.Example: CPP // C++ program to demonstrate // prot 3 min read Virtual Functions in Derived Classes in C++ A virtual function is a member function of a base class that is overridden by a derived class. When you use a pointer or a reference to the base class to refer to a derived class object, you can call a virtual function for that object and have it run the derived class's version of the function. In C 2 min read Like