Virtual Function Access

C++The access for a virtual function is specified when it is declared. The access rules for a virtual function are not affected by the access rules for the function that later overrides the virtual function. In general, the access of the overriding member function is not known.

If a virtual function is called with a pointer or reference to a class object, the type of the class object is not used to determine the access of the virtual function. Instead, the type of the pointer or reference to the class object is used.

In the following example, when the function f() is called using a pointer having type B*, bptr is used to determine the access to the function f(). Although the definition of f() defined in class D is executed, the access of the member function f() in class B is used. When the function f() is called using a pointer having type D*, dptr is used to determine the access to the function f(). This call produces an error because f() is declared private in class D.

 
 
class  B {
public :
  
virtual   void  f();
};
 
class  D :  public  B {
private :
  
void  f();
};
 
int  main() {
  D dobj;
  B
*  bptr  =   & dobj;
  D
*  dptr  =   & dobj;
 
  
//  valid, virtual B::f() is public,
  
//  D::f() is called
  bptr -> f();
 
  
//  error, D::f() is private
  dptr -> f();
}
 
Here's the updated code: ```c++ #include <iostream> #include <vector> #include <string> using namespace std; class Pet { public: Pet(string name) : m_name(name) {} virtual void printDescription() = 0; // make it pure virtual to make Pet an abstract class protected: string m_name; }; class Dog : public Pet { public: Dog(string name, bool neutered) : Pet(name), m_neutered(neutered) {} void printDescription() override { cout << "Dog named " << m_name << endl; cout << "Neuter/Spayed: " << m_neutered << endl; } private: bool m_neutered; }; class Cat : public Pet { public: Cat(string name, bool neutered) : Pet(name), m_neutered(neutered) {} void printDescription() override { cout << "Cat named " << m_name << endl; cout << "Neuter/Spayed: " << m_neutered << endl; } private: bool m_neutered; }; class Bird : public Pet { public: Bird(string name, bool talks) : Pet(name), m_talks(talks) {} void printDescription() override { cout << "Bird named " << m_name << endl; cout << "Talks: " << m_talks << endl; } private: bool m_talks; }; int main() { vector<Pet*> pets; pets.push_back(new Dog("Fido", true)); pets.push_back(new Cat("Whiskers", false)); pets.push_back(new Bird("Polly", true)); for (Pet* pet : pets) { pet->printDescription(); } // free memory for (Pet* pet : pets) { delete pet; } return 0; } ``` In this updated code, we've used inheritance to create subclasses for Dog, Cat, and Bird. Each subclass has its own private variables `m_neutered` and `m_talks`, respectively. The `printDescription` function is now a virtual function in the `Pet` class, which means that each subclass can override it with its own implementation. In the main function, we create a vector of `Pet` pointers and add instances of each subclass to it. We then loop through the vector and call the `printDescription` function on each `Pet` object, which automatically calls the appropriate subclass implementation of the function. Finally, we free the memory allocated for each `Pet` object using `delete`.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值