C++_ Base _ Operator Overloading 2

Source: Internet
Author: User
Tags control characters

Content:

(1) Operators that can only be overloaded with members
(2) Overloading of the new/delete operator
(3) Initial knowledge of encapsulation and inheritance
(4) Characteristics of inheritance
(5) Characteristics of subclasses and their functions
(6) Multiple inheritance and virtual inheritance

1. Operators that can only be overloaded with members
(1) [] subscript operator
(2) () function operator
(3) *, indirect operator

Overloading of the 2.new/delete operator

Attention:
If you need to do some initialization work before calling the constructor/doing some cleanup after the destructor is called, you can do so by using the New/delete operator overloaded function.

3. Encapsulation and inheritance of the first knowledge
3.1 Concepts
(1) Package
Mechanisms for restricting access to properties and behaviors in types using the access control
Public protected private
(2) Inheritance
A linguistic mechanism for the interdependence of commonalities and attributes between expression types

Such as:
Student class:
Attribute: Name Age study number
Behavior: Eating and learning
Teacher Class:
Attribute: Name Age title
Behavior: Meal Classes

The characteristics of students and teachers are extracted to form the human:
Attribute: Name Age
Behavior: Eating

The rest is the characteristics of the student class and the teacher class:
Student class:
Properties: Study Number
Behavior: Learning
Teacher Class:
Properties: Job Title
Behavior: Teaching

Then found:
Human: (name, age, meal)
/ \
(School number, study) Student class teacher Class (title, teaching)

Human: Parent class/Base class
Student class/Teacher class: Subclass/Derived class

3.2 Inherited syntax formats
Class subclass Name: Inherit Way parent class name 1, inheritance mode 2 parent class Name 2, ...
{
Definition of subclass type;
};

How to Inherit:
Public: common-mode inheritance
Protected: Protection mode inheritance
Private: Privately-way inheritance

Practice:
VI 07inherit.cpp file, customizing the person class and the teacher class, providing constructors and behaviors

4. Characteristics of inheritance
(1) An object of a subclass type can be viewed at any time as an object of the class's base class type
(2) An object of a base class type may throw an illegal memory access error if an object of the subclass type is accessed
(3) Public and protected members in the base class can be accessed in subclasses, but private members cannot be accessed
(4) In subclasses can be defined and the base class with the same name of the identifier, the subclass hides the parent class (first name Hidden)

5. Inheritance mode and access control

5.1 Access control characters

Visit control property This class subclass class friend
---------------------------------
Public ok okay ok ok
Protected protected ok OK no ok
Private OK no no OK

5.2 Characteristics of inheritance methods
Private subclass of public subclass protection subclass in base class
----------------------------------
Private of the public public protection
Protection of the protected protection of the private
Private Private Private private

Attention:
Generally inherited by public means, protection and private inheritance are seldom used

Such as:
Class A
{
Public
void print (void) {...}
};
Class B:private A
{
Public
void Show (void)
{
Print ();
...
}
};

Class C
{
Print ();
};

6. Constructors and destructors in subclasses
(1) in the subclass of the constructor can be explicitly constructed its base class part ("Base class class name (Argument)"), otherwise the system will attempt to construct the base class sub-object in the form of a non-parameter
(2) The destructor in the subclass automatically calls the destructor in the parent class to free the base class sub-object, and the destructor in the parent class does not automatically call the subclass's destructor
(3) When you delete a pointer to a child class object, it is called only the destructor in the parent class, and the destructor does not automatically call the destructor in the subclass, causing a memory leak, which can be solved by using a virtual destructor (tomorrow).

C++_ Base _ Operator Overloading 2

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.