C + + in the abstract

Source: Internet
Author: User
Tags abstract new set

In C + +, the data abstraction function represented by class and virtual function is always the core and difficulty of C + +. Here I would like to combine their own experience, to talk about the abstract of C + + a little shallow view!

I think the abstract of C + + should mean: From the problem we need to solve, in the problem related to a group of associated objects to extract the main or common parts-to say simply, the same behavior to manipulate different objects.

It is an interactive and iterative process to ask questions to find the objects associated with the problem. In the abstraction of related objects, as the knowledge goes deeper, we may modify the original goal, and the modification of the original goal may lead to a new set of related objects being added. For example, if you want to design a WAN based mail server, you may first need to encapsulate the underlying protocol through the socket, providing a standard set of interfaces for high-level POP3 and SMTP protocols. Beginning to simplify the problem we may plan to encapsulate only the TCP/IP protocol, but based on the following two points we have reason to modify the initial requirements:

1, POP3, SMTP needs of the underlying interface is simple. In addition to connectivity, only the ability to send and receive a piece of data

2, with sockets for network programming, most common agreement between the difference is very small, there are a lot of just initialization and connection is different only we need to do a little effort can be compatible with most common protocols (such as: ATM, IPX, infrared protocol, etc.).

Now decide to modify the requirements, in addition to the TCP/IP protocol, but also to support a number of other common protocols. By modifying the original target, in addition to the TCP/IP protocol object, a group of related protocol objects will be added. It is easy to make a common part from this group of related objects and abstract it into another common object. Of course, depending on the specific application environment, this may not be the best solution.

Conventional abstractions in C + + are developed in a group of classes that have a "blood" relationship with each other. Such as:

Class Parent
{
virtual ~Parent(){};
virtual void GetValue(){ .... };
virtual void Test(){ ... };
};
class child1 : public parent
{
virtual ~child1(){};
virtual void GetValue(){...};
virtual void Test(){ ... } const;
};
class child2 : public parent
{
virtual ~child2(){};
virtual void GetValue(){...};
virtual void Test(){ ... } ;
};

(By the Way, child1::test () const is not an overload of the base class Parent::test (). )

From the top can be summed up in C + + abstract Some of the basic features:

1. Objects that are abstract must derive directly or indirectly from a class object

2, if you do not have a type-safe operation, such as: downward transition operation or forced type conversion operations (like COM). Then the action in the derived class that requires abstraction must appear in a base class.

3. The destructor of the base class must be a virtual function (hmm ...). A little rogue! )

................

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.