I. Introduction:
- What is interpreted by a polymorphic
- Virtual function
- Pure virtual function
- Relationships, and methods derived class and call the base class
Second, the text
2.1 What is polymorphic (I understand)
A member function to determine the specific type of implement used until runtime
2.2 virtual function
Briefly, the former is the base class virtual function marked virtual member function of that part of the virtual viod get ();
2.2.1 override 和 final
These two functions need to declare a function in the final surface (such as void get () const override;)
override the role identifier
1 compiler checks whether the wrong function
2 function improve readability
final action identifier
1 subclass is not allowed to override the function
class A {
private:
virtual void getVal(){
std::cout << "A:123" << std::endl;
}
virtual void getTest() final { //不允许子类重写
std::cout << "A:223" << std::endl;
}
};
class B : private A {
public:
void getVal() override {//即便基类是私有类,子类依旧可以重写
std::cout << "B:321" << std::endl;
}
void getTest() override; //这样无法通过编译
};
2.2.2 base class virtual function privatization
2.2.1 As illustrated in the example, its role is twofold:
1 Subclasses may redefine the virtual function normally
2 avoid external call this function, ensure package integrity
2.2.3 with the default parameters virtual function
Conclusion: there is a subclass of the base class virtual function is called, the default parameter depending on a default value of the base class (see specific examples)
class A {
public:
virtual void getVal(int a = 10) {
std::cout << "A:" << a << std::endl;
}
};
class B : public A {
void getVal(int a = 123) override {
std::cout << "B:" << a << std::endl;
}
};
int main() {
A* a = new B;
a->getVal();
}
//output
B:10
2.2.4 dynamic_cast scenarios
Classes and subclasses to the base (with at least a base class virtual function) , as is with the Check dynamic_cast can ensure a non-virtual function, an error is returned nullptr a;
Otherwise you can use static_cast
2.2.5 constructor or destructor virtual function call
This time because it is a static call, the default will directly call the virtual function in the parent class to achieve
2.3 pure virtual function
Wherein a: virtual void test () = 0; end is "= 0"
Wherein two: an object can be constructed
Three characteristics: commonly used in the C ++ interface is declared
class A {
public:
virtual int getVal() = 0;
};
class B : public A {
public:
int getVal() override {
std::cout<<"B:"<<222<<std::endl;
return 100;
}
};
class C : public A {
public:
int getVal() override {
std::cout << "C:"<<333<<std::endl;
return 222;
}
};
int main() {
B b;
C c;
std::vector<A*> aPtr {&b, &c};
for (const auto& temp : aPtr)
temp->getVal();
}
//output
B:222
C:333
2.4 calls between base class and a derived class
2.4.1 address derived class object may be present in the base class pointer or reference, not vice versa
Since the derived class inherits all the members of the base class, and therefore will not be subject to call the base class does not exist in a derived class
class A {
};
class B : public A {
};
class C : public A {
};
int main() {
A *a1 = new B;
A *a2 = new C;
}