1、c语言就是面向过程,c++是面向对象。
2、c++的灵魂—类(C语言的灵魂—指针)
类的成员可以是变量,也可以是函数。
3、如何定义一个类
class student
{
public:
char name[64];
char age;
};
很像C语言的结构体;
4、什么是对象
对象是类的实例化。
(1)直接定义:
student my;//student是类;my是对象
student是类;my是对象。
(2)在堆里面定义
student *my = new student;//在堆里面定义对象
删除在堆里面创建的对象;释放堆里面的内存;
delete my;
5、访问成员
student my;
student *my1 = new student;//在堆里面定义对象
my.age = 18;
my1->age = 19;
cout << my.age << endl;
cout << my1->age << endl;
访问方法和C语言一样,普通变量通过. 指针变量通过->
6、类的函数成员
因为类里面的成员可以是变量,也可以是函数。
第一步:在类里面声明
第二步:实现这个函数。我们可以直接写在类里面,也可以写在类的外面。
(1)直接写在类的里面
class student
{
public:
char name[64];
int age;
void test(){
cout << 123 << endl;
};
};
(2)写在类的外面
class student
{
public:
char name[64];
int age;
void test();
};