In object-oriented some similarities and differences between Java and C ++

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_43946347/article/details/102733055

C ++ and Java small talk


C ++ ------------- Java

-------- ordinary function virtual function
pure virtual function -------- abstract
abstract class -------- abstract class
virtual base class interfaces --------


Virtual function is defined : declaration of a virtual base class member function and one or more of the derived class is redefined, usage format: virtual function return type function name (parameter list) {} function body; multi-state resistance, by base-class pointer or reference to a derived class, the derived class access cover member function of the same name. ----(Baidu Encyclopedia)

#include<iostream>
using namespace std;
class A
{
    public:
        void virtual FUN()
        {
            cout<<"FUN in A is called"<<endl;
        }
};
 
class B : public A
{
    public:
        void FUN()
        {
            cout<<"FUN in B is called"<<endl;
        }
};
 
int main()
{
    A a;
    B b;
    A *p;
    p = &a;
    p->FUN();
    p = &b;
    p->FUN();
    return 0;
}

operation result

FUN in A is called
FUN in B is called

If after removing the virtual, becomes operational results

FUN in A is called
FUN in A is called

That base class defines a pointer to a different object, to achieve polymorphism.

In C ++, if the function is not a virtual function, a function is called, the current pointer based on the determined type, not in accordance with the type of the object pointer points.

In Java, if the function is not abstract function, but an ordinary function, which is the default implementation in C ++ virtual functions similar function , that is to say, call a function, is based on the type of the current object pointer points to judge, instead determined according to the type of pointer. The opposite of the ordinary C ++ function. Namely: JAVA automatically in the virtual function.

E.g:

package Virtual;

public class VirtualTest {
	public static void main(String args[])  {
        A a = new A();
        B b = new B();
        A p;
 
        p = a;
        p.FUN();
        p = b;
        p.FUN();
    }
}
class A{
	public void FUN(){
        System.out.println("FUN in A is called");
    }
}
class B extends A{
    public void FUN(){
        System.out.println("FUN in B is called");
    }
}

Run results

FUN in A is called
FUN in B is called

Abstract class with a pure virtual function

Definitions : pure virtual function is declared in the base class virtual function, it is not defined in the base class, but requires that any derived class must define your own implementation. A method to achieve pure virtual function in the base class is added after the function prototype "= 0."

virtual void funtion1()=0

The purpose of defining pure virtual function in that the derived classes inherit just the interface functions.
Meaning pure virtual function, so that all class objects (mainly derived class objects) can perform actions pure virtual function, but the class can not provide a reasonable default implementation for the pure virtual functions. So pure virtual function declaration class is a subclass of the designer told, "You must provide an implementation of pure virtual functions, but I do not know how you will achieve it."

Containing pure virtual function class is an abstract class.

Abstract method and abstract class

If a class does not contain enough information to describe a specific target, such a class is an abstract class.

Since the abstract class can not be instantiated objects, abstract class must be inherited, can be used.
Parent class contains a set of common subclass, but because of the parent class itself is abstract, so you can not use these methods.
In Java abstract class represents is an inheritance, a class can only inherit an abstract class, but a class can implement multiple interfaces.

Java language used to define abstract class abstract class.

public abstract class leiming{
---------
}

The syntax for declaring an abstract method is

abstract <方法返回值类型><方法名>(参数列表);

It includes abstract methods must be abstract class, but an abstract class does not necessarily contain abstract methods


Virtual base class

Virtual base class with the keyword virtual inheritance parent class declaration, even if the base class is a subclass inherits multiple links, but this subclass contains only a backup of the virtual base class, the base class virtual primarily to resolve the ambiguity in the succession issue, which is the role of virtual base class is located.

Call the constructor of the virtual base class, regardless of whether the virtual base class is not a direct effect of this is due parent virtual base class, it must be displayed in the constructor for each subclass.

Secondly, call the constructor of the virtual base class constructor earlier than other non-virtual base class is called.

It is characteristic of the above two virtual base classes.

interface

In Java, a given form of the following interfaces:

[public] interface InterfaceName {
}

Interfaces may contain variables and methods.
Note, however, the interface variables are implicitly designated as public static final variables (and only public static variable Final , compilation errors will be reported with private modification), and methods will be implicitly designated as public abstract method and the only public abstract method (with additional keywords, such as private, protected, static, final and other modifications will report compilation errors), and the interface method does not have all the specific implementation, that is, the method must interface They are abstract . From here we can see the difference between vaguely abstract classes and interfaces, the interface type is an extremely abstract, it is more "abstract" than an abstract class, and the variable is not defined in the interface under normal circumstances.

An interface can inherit multiple interfaces, can be implemented by the extends keyword, syntax is identical to class inheritance, inherited class interface is called the parent class interface, when there are more than one parent class interface, separated by commas.

With implements represent the class implements an interface clause. A class can implement multiple interfaces simultaneously. In the body must be added to the class code that implements the interface methods.

Details can be found
in-depth understanding of Java interfaces and abstract classes

Guess you like

Origin blog.csdn.net/weixin_43946347/article/details/102733055