第一篇:
http://www.lxweimin.com/p/667b7160b3f9
序言
上篇我們熟悉了C++的基本用法,既然C++也是一門面向?qū)ο蟮恼Z言,那么這期我們就來對(duì)比下Java和C++中,方法和類是如何書寫的。
方法
Java
public void hello() {
System.out.print("Hello, World!");
}
C++
int hello(); // C++ 函數(shù)聲明階段
/**
* 在main方法前,可先不聲明,只有類中定義的函數(shù)才允許public修飾符
*/
void hello2(){
cout << "Hello2, World!";
}
int main() {
hello();
hello2();
return 0;
}
/**
* 函數(shù)實(shí)現(xiàn)
* 如果再main方法后實(shí)現(xiàn),必須先聲明
* @return
*/
int hello(){
cout << "Hello, World!";
}
方法參數(shù)
Java
public void hello(String name){
System.out.print("Hello, " + name + "!");
}
C++
void hello(string); // C++ 函數(shù)聲明階段
或者
void hello(string name); // 聲明階段可以帶名字,可以不帶
int main(){
string aa;
hello(aa);
}
void hello(string name){
cout << "Hello, " + name + "!";
}
參數(shù)默認(rèn)值
Java
public void hello(String name) {
if (name == null) {
name = "World";
}
System.out.print("Hello, " + name + "!");
}
C++
void hello(string = "World"); // 聲明階段,添加默認(rèn)值World
int main() {
hello();
return 0;
}
/**
* 函數(shù)實(shí)現(xiàn)
* 如果再main方法后實(shí)現(xiàn),必須先聲明
* @return
*/
void hello(string name) {
cout << "Hello, " + name + "!";
}
方法Return
Java
public boolean hasItems() {
return true;
}
C++
bool hasItems() {
return true;
}
單表達(dá)式
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"); //對(duì)象在堆中,對(duì)象引用在棧中
C++
class Test {
public:
void add() {
}
};
int main() {
Test test1; //棧中分配 ,由操作系統(tǒng)進(jìn)行內(nèi)存的分配和管理
Test *test1; //加*表示為指向Test類對(duì)像的指針變量,不加*則為Test類對(duì)像
Test *test = new Test(); //堆中分配 ,由管理者進(jìn)行內(nèi)存的分配和管理,用完必須delete(),否則可能造成內(nèi)存泄漏
delete test;
return 0;
}
注意:棧中內(nèi)存的分配和管理由操作系統(tǒng)決定,而堆中內(nèi)存的分配和管理由管理者決定
類->不可被繼承
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){}; //實(shí)現(xiàn)
int main() {
User user = User("test");
return 0;
}
類-> 可選的構(gòu)造參數(shù)
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++
/**
* 抽象類
* 需要用純虛函數(shù)
* 純虛函數(shù)是通過在聲明中使用 "= 0" 來指定
*/
class Document{
public:
virtual int calculateSize() = 0; //純虛函數(shù)
};
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;
}
};
在程序運(yùn)行結(jié)束時(shí),系統(tǒng)會(huì)調(diào)用靜態(tài)成員deletor的析構(gòu)函數(shù),該析構(gòu)函數(shù)會(huì)刪除單例的唯一實(shí)例。使用這種方法釋放單例對(duì)象有以下特征:
- 在單例類內(nèi)部定義專有的嵌套類。
- 在單例類內(nèi)定義私有的專門用于釋放的靜態(tài)成員。
- 利用程序在結(jié)束時(shí)析構(gòu)全局變量的特性,選擇最終的釋放時(shí)機(jī)。
枚舉類
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。
編譯系統(tǒng)為每個(gè)枚舉常量指定一個(gè)整數(shù)值,默認(rèn)狀態(tài)下,這個(gè)整數(shù)就是所列舉元素的序號(hào),序號(hào)從0開始。 可以在定義枚舉類型時(shí)為部分或全部枚舉常量指定整數(shù)值,在指定值之前的枚舉常量仍按默認(rèn)方式取值,而指定值之后的枚舉常量按依次加1的原則取值。
總結(jié)一下
本期,我對(duì)比了函數(shù),類,抽象類,單例,枚舉等,整體上大同小異,你可能也注意到了一些細(xì)節(jié)的不同,其實(shí)大多時(shí)候都是很小的細(xì)節(jié)決定了問題的產(chǎn)生,雖然我可以通過這兩篇的文章快速的了解C++,但還是不夠,需要更深入的研究和學(xué)習(xí),下期我們就針對(duì)某些細(xì)節(jié)以及一些概念的理解,來完成C++的從入門到精通。加油。