Java/c++ public, protected, private; Virtual & Abstract

Source: Internet
Author: User

First, Thread

Java/c++ is an object-oriented third-generation computer high-level programming language, its essence is identical, but the syntax is really different, a little attention is easy to cause confusion. This article summarizes some of the differences between these two languages, for reference only.

Second, C + +

For the C + + language, the type of member access is public, protected and private for its class; In the way of inheritance, it is divided into common inheritance, protection inheritance and private inheritance. In addition, there are virtual functions, pure virtual functions in C + +, there are covers, hidden. Relatively complex.

In terms of its polymorphism: when a base-class pointer points to a derived class, the base-class pointer can access only the members that are already in the base class, but not the members that are unique to the derived class. We understand that even if a derived class overrides a member of a base class, it simply replaces the base class member with a class-derived class member, which is still owned by the base class, and some of the most tricky topics are often derived classes that cover the base class or hide the base class. Overlay and hide are very different, overwriting is no longer the original, and hidden only for the derived class object access can only be a new declaration in the derived class members, the original base class member also exists.

Expand how to differentiate between overlay and hide in C + +:

The overwrite must be met:1 The base class member has the Viirtual keyword before it.

2 function names are the same

3 parameter list same

There are two scenarios in which hiding occurs:

Scenario One: 1 base class members do not have the virtual keyword

2 The function name is the same in the derived class (the argument list doesn't matter anymore)

Scenario Two: 1 base class Members have viirtual keyword

2 The function names in the derived class are the same and the argument list is not limited

The key statement: As long as the inherited function is accessible in the derived class, overwriting and hiding occurs, regardless of access rights: for example, the member function in the accumulation is protected, and through public inheritance, the same function of public is defined in the derived class. Overwrite and hide are also occurring.

For example:

#include <iostream>using namespacestd;classcperson{protected:/* Public*/        /*Virtual*/ voiddisplay () {cout<<"CPerson"<<Endl; }         Public:        voidShow () {display (); }};classCstudent: Publiccperson{ Public:        voiddisplay () {cout<<"cstudent"<<Endl; }};intMain () {CPerson*p=Newcstudent (); //******************** the key to see here ****************//        //when the base class is public, there is no virtualP->display ();//output CPerson, hidden, the original member function (display ()) also exists for the base classP->show ();//output CPerson, this can be explained by the above theory, that is, show is called the CPerson display ()//when the base class is public and there is virtualP->display ();//output cstudent, overwrite, the original member function is replaced for the base classP->display ();//output cstudent can be explained by the above theory, that is, the replacement of the member or CPerson//when the base class is protected and there is no virtual,P->display ();//error, the protected member cannot be accessed outside the class (but in fact it has been hidden)P->show ();//The display () in the output cperson,show is hidden, because it is only hidden, so for the base class, the original D//Isplay () still exists, so not cstudent//when the base class is pritected, there is virtualP->display ();//error, the protected member cannot be accessed outside the class (but has already been overwritten), just after the function member is overwritten to the CPE//Rson, it's still protected.P->show ();//output, Cstudent, because overrides and replacements have occurred.         return 0;}

Third, Java

Java compared to C + + does not have the virtual keyword, so there is no virtual function. Therefore, the difference between covering and hiding the method is only to see the parameter list is not the same, the same is overwritten, and the other is hidden. In addition, the main function in Java is written in the class, so the following example looks like protected is no different from public.

Add: Only single inheritance in Java, and only public inheritance. The assignment in Java is actually equivalent to the assignment between pointers in C + +.

ImportJava.util.*; Public classTestextendsquiz{ Public Static voidMain (String []args) {Quiz P3=Newtest (); P3.display ();//Both public and protected are the same, because it is within the package access//Java does not have the virtual keyword, so the direct default is to overwrite rather than hide//so the output is test;        }         Public voidAdd () {a++; }         Public/*protected*/ voiddisplay () {System.out.println ("Test"); }}classquiz{protected/* Public*/ voiddisplay () {System.out.println ("Quiz"); }} 

Java/c++ public, protected, private; Virtual & Abstract

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.