C + + virtual functions, pure virtual functions, abstract classes, overrides, overloads, hidden

Source: Internet
Author: User

C + + virtual function table parsing (GO)--writing really good, can't help turning http://blog.csdn.net/hairetz/article/details/4137000

On the http://blog.csdn.net/hackbuteer1/article/details/7475622 of C + + polymorphism

C + + abstract class http://www.cnblogs.com/dongsheng/p/3343939.html

The essence of C + +-virtual function http://blog.chinaunix.net/uid-26851094-id-3327323.html

Summary of memory space occupied by classes in C + + http://blog.sina.com.cn/s/blog_69c189bf0100mkeu.html

#include <iostream> #include <stdio.h>using namespace std; #include <iostream>using namespace std; Class A{public:void Foo () {printf ("1\n");} virtual void Fun () {printf ("2\n");}}; Class B:public a{public:void foo () {printf ("3\n");} void Fun () {printf ("4\n");}}; int main (void) {a A; b b; A *p = &a;p->foo ();p->fun () cout<<endl;p = &b;//Note P or a pointer P->foo ();p->fun ();cout<< Endl B *PB = &b;pb->foo ();p b->fun ();    cout<<endl; b *ptr = (b *) &a;ptr->foo ();p tr->fun (); Cout<<endl;return 0;}

  

12143432Process returned 0 (0x0)   execution time:0.030 spress any key to continue.

  

Overloading, overwriting, and hiding of member functions
The overloading, overwriting (override) and hiding of member functions are easily confused, and C + + programmers have to figure out
concept, otherwise errors will be impossible to guard against.
8.2.1 Overloading and overwriting
Features that are overloaded by member functions:
(1) The same range (in the same class);
(2) The function has the same name;
(3) different parameters;
(4) The virtual keyword is optional.
Overrides refer to a derived class function overriding a base class function, characterized by:
(1) different ranges (in the derived and base classes, respectively);
(2) The function has the same name;
(3) the same parameters;
(4) The base class function must have the virtual keyword.

Confusing hidden rules
It is not difficult to distinguish between overloading and overwriting, but the hidden rules of C + + increase the complexity of the problem abruptly.
"Hide" here refers to a function of a derived class that masks a base class function with the same name as the following rule:
(1) If the function of the derived class has the same name as the function of the base class, but the parameters are different. At this point, whether or not virtual
keyword, the function of the base class is hidden (note that it is not confused with overloading).
(2) If a function of a derived class has the same name as a function of the base class and the parameters are the same, the base class function has no virtual
Key word. At this point, the function of the base class is hidden (be careful not to confuse the overlay).
In the above program:
(1) The function derived::f (float) is covered with base::f (float).
(2) the function derived::g (int) hides base::g (float) instead of overloading.
(3) The function derived::h (float) hides the base::h (float) instead of the overlay.

Summary: 1, there is virtual can occur polymorphic phenomenon//2, does not occur polymorphic (no virtual) call on the original type call #include<iostream>using namespace Std;class base{public : virtual void F (float x) {cout<< "base::f (float)" << x <<endl;} void g (float x) {cout<< "base::g (float)" << x <<endl;} void h (float x) {cout<< "base::h (float)" << x <<endl;}};   Class derived:public base{public:virtual void F (float x) {cout<< "derived::f (float)" << x <<endl;     polymorphic, overlay}void g (int x) {cout<< "derived::g (int)" << x <<endl;   Hide}void H (float x) {cout<< "derived::h (float)" << x <<endl; Hide}};int Main (void) {Derived D;   Base *PB = &d;derived *PD = &d;//Good:behavior depends solely on type of the objectpb->f (3.14f);   Derived::f (float) 3.14pd->f (3.14f);   Derived::f (float) 3.14//bad:behavior depends on type of the Pointerpb->g (3.14f);   Base::g (float) 3.14pd->g (3.14f); Derived::g (int) 3//Bad:behavior depends on type of the pointerPb->h (3.14f);   Base::h (float) 3.14pd->h (3.14f); Derived::h (float) 3.14return 0;}

  

C + + pure virtual function
First, the definition
A pure virtual function is a virtual function declared in a base class that is not defined in the base class, but requires that any derived class define its own implementation method. The method of implementing a pure virtual function in a base class is to add "= 0" after the function prototype
virtual void funtion () =0
Second, the reasons for the introduction
1, in order to facilitate the use of polymorphic features, we often need to define virtual functions in the base class.
2, in many cases, the base class itself is unreasonable to generate objects. For example, animals as a base class can be derived from tigers, peacocks and other sub-categories, but the animals themselves generated objects are obviously unreasonable.
In order to solve the above problems, the concept of pure virtual function is introduced, the function is defined as pure virtual function (method: Virtual ReturnType functions () = 0;), the compiler requirements must be overridden in the derived class to achieve polymorphism. A class that contains a purely virtual function is called an abstract class, and it cannot produce an object. This is a good solution to both of these problems.
Iii. Concept of similarity
1. Polymorphism
A different implementation action occurs when the same object receives different messages or when different objects receive the same message. C + + supports two kinds of polymorphism: compile-time polymorphism, run-time polymorphism.
A, compile-time polymorphism: by overloading the function of the implementation
B, run-time polymorphism: through virtual function implementation.

2. Virtual function
A virtual function is a member function that is declared as virtual in a base class and redefined in a derived class to implement dynamic override of a member function (override)
3. Abstract class
Classes that contain pure virtual functions are called abstract classes. Because an abstract class contains a pure virtual function that is not defined, an object of an abstract class cannot be defined.

C + + virtual functions, pure virtual functions, abstract classes, overrides, overloads, hidden

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.