In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how to use C++ private inheritance. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article, let's take a look at it.
1. Introduction to private inheritance
We can describe the has-a relationship, that is, the inclusion relationship, by making one class a member of another.
For example, we are going to create a Student class that contains a name of type string and a scores of type valarray. Valarray here is a template class in C++, which can be understood as an array of generics, similar to vector and array, but with more functionality. For example, it has min, size, max, sum and other methods.
In addition to using member variables to describe has-a relationships, we can also use private inheritance.
In private inheritance, both the public and protected members of the base class become private members of the derived class. This means that the methods of the base class are described by the private keyword, which we can use in the derived class, but the class object cannot be called directly.
two。 Grammar
First of all, let's take a look at the syntax of private inheritance, which is actually similar to common inheritance, except that the public keyword is replaced with private, and because the inheritance relationship defaults to private, it can be left unfilled.
Using namespace std;class Student: private string, private valarray {...}
The private here can be omitted.
The second is the initialization of the member list in the constructor, if it is the normal way of defining members, that is:
Class Student {private: string name; valarray scores;}
Our constructor signature can be written as follows:
Student (const string& n, const double * pd, int n): name (n), scores (pd, n) {}
But in our example, we use private inheritance, and the values stored are in the base class, so we can only initialize with the class name:
Student (const string& n, const double * pd, int n): string (n), valarray (pd, n) {}
For ease of writing, we can define a type conversion to replace valarray with ArrayDb, so the above code can be written like this:
Student (const string& n, const double * pd, int n): string (n), ArrayDb (pd, n) {}
Student (const string& n, const double * pd, int n): string (n), ArrayDb (pd, n) {} 3. Class declaration code
Finally, let's take a look at the complete class declaration code:
Using namespace std;class Student: string, valarray {private: typedef valarray ArrayDb; ostream & arr_out (ostream & os) const Public: Student (): string ("null"), ArrayDb () {} explicit Student (const string& s): string (s), ArrayDb () {} explicit Student (int n): string ("null"), ArrayDb (n) {} Student (const string& s, int n): string (s), ArrayDb (n) {} Student (const string& s, const ArrayDb& a): string (s), ArrayDb (a) {} Student (const char* str) Const double * pd, int n): string (str), ArrayDb (pd, n) {} ~ Student () {} double Average () const Double & operator [] (int I); double operator [] (int I) const; const string& Name () const; friend istream & operator > > (istream & is, Student& stu); friend istream & getline (istream & is, Student& stu); friend ostream & operator
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.