Changes in access permissions for virtual functions in C + +

Source: Internet
Author: User

If access to a virtual function in a base class is a case, the derived class can redefine the access rights of the base class virtual function when it inherits the base class, and the instance verifies that it is correct.
It also shows that the override or redefinition of the function is not much related to the previous access permission modification.

Base.h
#pragma once#include <iostream>using namespace Std;class base{public:base (void) {}~base (void) {}virtualvoid fun ( ) {cout<< "This is Base::fun" <<ENDL;} virtual void func () = 0;};




Son.h
#pragma once#include "base.h" class Son:p ublic Base{public:son (void) {}~son (void) {}virtual void fun () {cout<< " This is a son function "<<ENDL; protected:virtual void func () {cout<< "What's a fuck day it's" <<endl;}};


T.h
#pragma once#include "Son.h" class T:p ublic son{public:t (void) {}~t (void) {}void xy () {Base *b = new Son (); B->func ();}};

Main.cpp

#include "testInline.h" #include "Son.h" #include "T.h" void Main () {base* PB; son* PS = new Son ();p B = Ps;pb->fun (); T tst;tst.xy ();}


Results:

This is a son function
What's a fuck day it's


Changes in access permissions for virtual functions in C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.