From Java to C++ 第一篇

序言

上篇我们熟悉了C++的基本用法,既然C++也是一门面向对象的语言,那么这期我们就来对比下Java和C++中,方法和类是如何书写的。

方法

Java

  1. public void hello() {
  2. System.out.print("Hello, World!");
  3. }

C++

  1. int hello(); // C++ 函数声明阶段
  2. /**
  3. * 在main方法前,可先不声明,只有类中定义的函数才允许public修饰符
  4. */
  5. void hello2(){
  6. cout << "Hello2, World!";
  7. }
  8. int main() {
  9. hello();
  10. hello2();
  11. return 0;
  12. }
  13. /**
  14. * 函数实现
  15. * 如果再main方法后实现,必须先声明
  16. * @return
  17. */
  18. int hello(){
  19. cout << "Hello, World!";
  20. }

方法参数

Java

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

C++

void hello(string); // C++ 函数声明阶段
或者
void hello(string name); // 声明阶段可以带名字,可以不带
int main(){
    string aa;
    hello(aa);
}
void hello(string name){
    cout << "Hello, " + name + "!";
}

参数默认值

Java

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

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

C++

void hello(string = "World"); // 声明阶段,添加默认值World

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

/**
 * 函数实现
 * 如果再main方法后实现,必须先声明
 * @return
 */
void hello(string name) {
    cout << "Hello, " + name + "!";
}

方法Return

Java

public boolean hasItems() {
  return true;
}

C++

bool hasItems() {
    return true;
}

单表达式

Java

public double cube(double x) {
  return x * x * x;
}

C++

double cube(double x) {
    return x * x * x;
}

类->New

Java

File file = new File("file.txt"); //对象在堆中,对象引用在栈中

C++

class Test {
public:
    void add() {

    }
};

int main() {
    Test test1;  //栈中分配  ,由操作系统进行内存的分配和管理
    Test *test1;  //加*表示为指向Test类对像的指针变量,不加*则为Test类对像
    Test *test = new Test();  //堆中分配  ,由管理者进行内存的分配和管理,用完必须delete(),否则可能造成内存泄漏
    delete test;
    return 0;
}

注意:栈中内存的分配和管理由操作系统决定,而堆中内存的分配和管理由管理者决定

类->不可被继承

Java

public final class User {
}

C++

//C++11的新特性
class User final {
};

类->不可被继承的成员变量

Java

 class User {
     private final String name;

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

     public String getName() {
         return name;
     }
 }

C++

class User {
private:
   const string u_name; // const限制只能被赋值一次
public:
    User(string name); // 声明
};

User::User(string name) : u_name(name){}; //实现

int main() {
    User user = User("test");
    return 0;
}

类-> 可选的构造参数

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
 }

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() {
    User user = User("test");
    User user1 = User("test","1");
    return 0;
}

抽象类

Java

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

public class Photo extends Document{
    @Override
    public int calculateSize() {

    }
}

C++

/**
 * 抽象类
 * 需要用纯虚函数
 * 纯虚函数是通过在声明中使用 "= 0" 来指定
 */
class Document{
public:
    virtual int calculateSize() = 0; //纯虚函数
};

class  Photo : Document{
public:
    int calculateSize() override{
        cout << "Photo";
        return 10;
    }
};

int main() {
    Photo photo;
    photo.calculateSize();
    return 0;
}

单例

Java

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

   public static Document getInstance(){
       return INSTANCE;
   }

 }

C++

//懒汉版

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)
                delete Document::document;
        }
    };
    static Deletor deletor;

public:
    static Document *getInstance() {
        if (document == NULL) {
            document = new Document();
        }
        return document;
    }
};

在程序运行结束时,系统会调用静态成员deletor的析构函数,该析构函数会删除单例的唯一实例。使用这种方法释放单例对象有以下特征:

  • 在单例类内部定义专有的嵌套类。
  • 在单例类内定义私有的专门用于释放的静态成员。
  • 利用程序在结束时析构全局变量的特性,选择最终的释放时机。

枚举类

Java

enum Color 
{ 
    RED, BLUE, WHITE, BLACK}; 
}

C++

enum Color {RED, BLUE, WHITE, BLACK}; // 定义枚举类型Color
enum fruit_set {apple, orange, banana=1, peach, grape}
//枚举常量apple=0,orange=1, banana=1,peach=2,grape=3。

编译系统为每个枚举常量指定一个整数值,默认状态下,这个整数就是所列举元素的序号,序号从0开始。 可以在定义枚举类型时为部分或全部枚举常量指定整数值,在指定值之前的枚举常量仍按默认方式取值,而指定值之后的枚举常量按依次加1的原则取值。

总结一下

本期,我对比了函数,类,抽象类,单例,枚举等,整体上大同小异,你可能也注意到了一些细节的不同,其实大多时候都是很小的细节决定了问题的产生,虽然我可以通过这两篇的文章快速的了解C++,但还是不够,需要更深入的研究和学习,下期我们就针对某些细节以及一些概念的理解,来完成C++的从入门到精通。加油。