In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to apply C++ inheritance". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn how to apply C++ inheritance.
The inheritance of the class will first look for the base class. If the base class is not implemented, it will look for the function of the derived class.
1. Class inherits, but functions do not inherit.
# include class Base {public: Base () {} ~ Base () {} int a; void setA () {a = 1;}}; class A:public Base {public: a () {} ~ A () {} void setA () {a = 2;}}; class B:public Base {public: B () {} ~ B () {} void setA () {a = 3;}} Int main () {A * ax = new A (); B * bx = new B (); Base * aClass = ax; Base * bClass = bx; aClass- > setA (); bClass- > setA (); printf ("a value of a% d\ n", aClass- > a); printf ("a value of b% d\ n", bClass- > a); return 0;}
two。 Both function and Class inherit
# include class Base {public: Base () {} ~ Base () {} int a; virtual void setA () {a = 1;}}; class A:public Base {public: a () {} ~ A () {} virtual void setA () {a = 2;}}; class B:public Base {public: B () {} ~ B () {} virtual void setA () {a = 3;}} Int main () {A * ax = new A (); B * bx = new B (); Base * aClass = ax; Base * bClass = bx; aClass- > setA (); bClass- > setA (); printf ("a value of a% d\ n", aClass- > a); printf ("a value of b% d\ n", bClass- > a); return 0;}
Run result: note that virtual can not be written in the derived class. It is best to write it to identify function inheritance.
If the function of the base class in 2 is written as a pure virtual function, the result is the same, but if the base class is a pure virtual function, the derived class must implement the corresponding function.
Class Base {public: Base () {} ~ Base () {} int a; virtual void setA () = 0;}
3. If there is no function in class A, it inherits from the base class
Class A:public Base {public: a () {} ~ A () {}}; at this point, I believe you have a deeper understanding of "how to apply C++ inheritance". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.