juejin.cn/post/695882…

The preface

Since C++ is also an object-oriented language, we will take a look at how methods and classes are written in Java and C++.

methods

Java

public void hello(a) {
  System.out.print("Hello, World!");
}
Copy the code

C++

int hello(a); // C++ function declaration stage
/** * the public modifier */ is allowed only for functions defined in a class
void hello2(a){
    cout << "Hello2, World!";
}

int main(a) {
    hello(a);hello2(a);return 0;
}
If the main method is implemented after the main method, it must first declare * @return */
int hello(a){
    cout << "Hello, World!";
}
Copy the code

The method parameters

Java

public void hello(String name){
  System.out.print("Hello, " + name + "!");
}
Copy the code

C++

void hello(string); // C++ function declaration stageorvoid hello(string name); // The declaration stage can have a name or no name
int main(a){
	string aa;
    hello(aa);
}
void hello(string name){
    cout << "Hello, " + name + "!";
}
Copy the code

Parameter Default Value

Java

public void hello(String name) {
  if (name == null) {
    name = "World";
  }

  System.out.print("Hello, " + name + "!");
}
Copy the code

C++

void hello(string = "World"); // In the declaration phase, add the default value World

int main(a) {
    hello(a);return 0;
}

If the main method is implemented after the main method, it must first declare * @return */
void hello(string name) {
    cout << "Hello, " + name + "!";
}
Copy the code

Methods the Return

Java

public boolean hasItems(a) {
  return true;
}
Copy the code

C++

bool hasItems(a) {
    return true;
}
Copy the code

Single expression

Java

public double cube(double x) {
  return x * x * x;
}
Copy the code

C++

double cube(double x) {
    return x * x * x;
}
Copy the code

Class – > New

Java

File file = new File("file.txt"); // The object is in the heap and the object reference is in the stack
Copy the code

C++

class Test {
public:
    void add(a) {}};int main(a) {
    Test test1;  // Stack allocation, memory allocation and management by the operating system
    Test *test1;  // * denotes a pointer variable to the Test object. * denotes a pointer variable to the Test object
    Test *test = new Test(a);// Allocate memory in the heap. The manager allocates and manages the memory. Delete () when the memory is used up, otherwise it may cause memory leak
    delete test;
    return 0;
}
Copy the code

Note: The allocation and management of memory in the stack is determined by the operating system, while the allocation and management of memory in the heap is determined by the manager

Class -> cannot be inherited

Java

public final class User {}Copy the code

C++

// new features in C++11
class User final {
};
Copy the code

Class -> Member variables that cannot be inherited

Java

 class User {
     private final String name;

     public User(String name) {
         this.name = name;
     }

     public String getName(a) {
         returnname; }}Copy the code

C++

class User {
private:
   const string u_name; // Const can only be assigned once
public:
    User(string name); / / declare
};

User::User(string name) : u_name(name){}; / / implementation

int main(a) {
    User user = User("test");
    return 0;
}
Copy the code

Class -> Optional construction parameters

Java

final class User {
     private String name;
     private String lastName;

     public User(String name) {
         this(name, "");
     }

     public User(String name, String lastName) {
         this.name = name;
         this.lastName = lastName;
     }

     // And Getters & Setters
 }
Copy the code

C++

class User {
private:
     string u_name;
     string u_last_name;
public:
    User(string name,string lastName);
};

User::User(string name,string lastName = "1") : u_name(name) ,u_last_name(lastName){};

int main(a) {
    User user = User("test");
    User user1 = User("test"."1");
    return 0;
}
Copy the code

An abstract class

Java

public abstract class Document{
   public abstract int calculateSize(a);
}

public class Photo extends Document{
    @Override
    public int calculateSize(a) {}}Copy the code

C++

/** * Abstract classes * require pure virtual functions */ pure virtual functions are specified by using "= 0" in the declaration
class Document{
public:
    virtual int calculateSize(a) = 0; // A pure virtual function
};

class  Photo : Document{
public:
    int calculateSize(a) override{
        cout << "Photo";
        return 10; }};int main(a) {
    Photo photo;
    photo.calculateSize(a);return 0;
}
Copy the code

The singleton

Java

public class Document {
   private static final Document INSTANCE = new Document();

   public static Document getInstance(a){
       returnINSTANCE; }}Copy the code

C++

/ / idlers

class Document {
private:
    static Document *document;
private:
    Document() {};
    ~Document() {};
    Document(const Document &);
    Document &operator= (const Document &);

private:
    class Deletor {
    public:
        ~Deletor() {
            if(Document::document ! =NULL)
                deleteDocument::document; }};static Deletor deletor;

public:
    static Document *getInstance(a) {
        if (document == NULL) {
            document = new Document(a); }returndocument; }};Copy the code

At the end of the program, the system calls the destructor of the static member deletor, which removes the unique instance of the singleton. Freeing a singleton in this way has the following characteristics:

  • Define a proprietary nested class inside a singleton class.
  • Define private static members within a singleton class that are used exclusively for release.
  • The final release time is selected by taking advantage of the program’s ability to destruct global variables at the end.

Enumeration class

Java

enum Color 
{ 
    RED, BLUE, WHITE, BLACK}; 
} 
Copy the code

C++

enum Color {RED, BLUE, WHITE, BLACK}; // Define the enumeration type Color
enum fruit_set {apple, orange, banana=1, peach, grape}
// Enumeration constants apple=0,orange=1, banana=1,peach=2,grape=3.
Copy the code

The compiler assigns an integer value to each enumeration constant. By default, this integer is the ordinal number of the enumerated element, starting at 0. You can specify integer values for some or all enumeration constants when defining an enumeration type. Enumeration constants before the specified value are still taken by default, while enumeration constants after the specified value are taken by incrementing one.

To summarize

This period, I compared the function, class, abstract class, singleton, enumeration, etc., on the whole the same, you might also notice that some details are different, in fact, most of the time are very small detail decides the problem, although I can through the article quickly learn c + +, it’s not enough, the need for more in-depth research and study, Next time, we will focus on the details and understanding of some concepts to complete the C++ from beginner to master. Come on.