一、从一个Person类开始
public class Person
{
private int age;
private char sex;
private String name;
public Person(){}
public Person(String name)
{
this.setName(name);
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
public void setAge(int age)
{
this.age = age;
}
public int getAge()
{
return this.age;
}
}
我们建立了一个传统的javabean。现在我们要思考这样一个javabean在C++中是如何存在的呢。
C++由于是由C语言过渡过来的所以他保留了C语言的特性。并增加了许多新内容。
C语言经常能够看到#include<stdio.h>这样一句话。表示导入了stdio.h这个文件。就相当于java中默认的 import java.lang.* 这个包一样。
而C++中你可以把这个.h文件看做是一个类。这个文件里我们做出所有函数的声明。
然后我们建立一个同名的 .cpp文件导入这个.h文件并实现里面的函数就可以了。
下面我们写一个Person.h文件
#pragma once
class Person
{
public:
Person(void);
Person(const char *name);
void setAge(const int age);
int getAge(void) const;
void setName(const char* name);
const char* getName(void) const;
private:
int age;
char sex;
char *name;
};
可以看到我们这里仿照java定义了一堆共有方法和三个私有成员函数。
我还很"聪明得"用了#pragma once防止重复编译
还用了几个const防止值被修改
然后我们再写一个Person.cpp文件
#include "Person.h"
#include "string.h"
Person::Person(void)
{
}
Person::Person(const char *name)
{
this->setName(name);
}
void Person::setAge(const int age)
{
this->age = age;
}
//后面加 const 表示常成员函数。表示函数内不能改变成员变量的值
int Person::getAge(void) const
{
return this->age;
}
void Person::setName(const char* name)
{
int len = strlen(name);
this->name = new char[len+1]; //空间多开辟一点。安全些
strcpy(this->name,name);
}
//由const修饰表示不能改变常量成员name。 返回值为 const char*指针表示不能修改内容
const char* Person::getName(void) const
{
return this->name;
}
这已经和我们的java代码很像了是不是?当然这还不够完全还要继续改进。
这里我直接将最终版贴出来
Person.h
#pragma once
class Person
{
public:
Person(void);
Person(const char *name);
Person(const Person &person);
Person &operator=(const Person &person);
~Person(void);
void setAge(const int age);
int getAge(void) const;
void setName(const char* name);
const char* getName(void) const;
private:
int age;
char sex;
char *name;
};
Person.cpp
#include "Person.h"
#include "string.h"
#include "iostream"
//宏定义安全释放name语句;
#define SAFE_DELETE_ARRAY(p) do { if(p) { delete[] (p); (p) = 0; } } while(0);
#define SAFE_DELETE(p) do { if(p) { delete (p); (p) = 0; } } while(0);
/*
*1)构造函数使用 :var(value),var(value)...的形式初始化.
*2)构造函数之间调用使用 new (this) Person(param...);互相调用。一般写个私有方法比较好
*3)C++中new 与 delete是相互对应的。new出来的是在堆中C++不会回收。必须使用delete。而在栈中的变量则函数结束后会自动出栈。
*4)对于字符串一般说要开的长度是 len+1 要为'\0'留空间。防止delete报
* CRT detected that the application wrote to memory after end of heap buffer
*/
Person::Person(void):name(0)
{
}
Person::Person(const char *name):name(0)
{
this->setName(name);
}
Person::Person(const Person &person):name(0)
{
this->setName(person.name);
this->age=person.age;
this->sex=person.sex;
}
Person &Person::operator=(const Person &person)
{
this->setName(person.name);
this->age=person.age;
this->sex=person.sex;
return *this;
}
Person::~Person(void)
{
printf("%s\n",this->name);
SAFE_DELETE_ARRAY(this->name);
}
void Person::setAge(const int age)
{
this->age = age;
}
//后面加 const 表示常成员函数。表示函数内不能改变成员变量的值
int Person::getAge(void) const
{
return this->age;
}
void Person::setName(const char* name)
{
SAFE_DELETE_ARRAY(this->name);
int len = strlen(name);
this->name = new char[len+1]; //对于开动态开辟空间+1防止报错
strcpy(this->name,name);
}
//由const修饰表示不能改变常量成员name。 返回值为 const char*指针表示不能修改内容
const char* Person::getName(void) const
{
return this->name;
}