1.动态内存分配
C语言中使用malloc()申请内存空间 ,用free()释放内存空间
C++中使用new申请内存空间 ,用delete释放内存空间
1.new 用于分配内存
new是根据类型来自动分配空间
int *p = new int;
delete用于释放内存
delete p;
2.new在分配空间时能同时初始化
int *p = new int(100);
cout << *p << endl;// 100
3.new为数组分配空间
int *p = new int[10];
char *p = new char[5];
注意:释放时要加中括号
delete []p;
--------------------------------------------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int main()
{
int *pi = new int;
*pi = 20;
cout << *pi << endl;
delete pi;
// pi = NULL;
// pi = 0;
pi = nullptr;
int *pi2 = new int(100);
// *pi = 200;
cout << *pi2 << endl;
delete pi2;
pi2 = nullptr;
int *pi3 = new int[5]{1,2,3,4,5};
cout << pi3[1] << endl;
delete []pi3;
return 0;
}
2.面向对象编程OOP object-oriented programming
面向过程:程序=算法+数据结构
面向对象:程序=对象+对象...+消息
面向对象的三大基本特征:封装、继承、多态(如果还有第四种的话就是抽象)
类和对象
对象:万物皆对象
静态的属性(数据 )和动态的行为( 函数)
类class:一组相同对象的共同的属性和行为的抽象描述类类型就是自定义的抽象数据类型
结构体与类的区别:
默认情况下,结构体的成员都是公有的
默认情况下,类的成员都是私有的。
访问修饰符:
public:
公有的,在任何地方都可直接访问
private:
私有的,在类的内部可以直接访问,在类的外部不能直接访问
protected:
保护的,在类的内部可以直接访问,在类的外部不能直接访问,其子类可以直接访问
类的一般形式:
class 类名
{
public:
//公有成员,一般是成员函数
private:
//私有成员,一般是成员变量和成员函数
protected:
//保护成员,可以是变量和函数
};
使用:如果自定义类型中包括数据成员和函数成员,那么就用class。如果自定义类型中只有数据成员,那么就用struct
在类外实现成员函数:
返回类型 类名::成员函数名(参数列表)
{
//....
}
先声明一个类型,再用该类型来实例化一个对象类类型的变量就叫做对象
--------------------------------------------------------------------------------------------------------------------
#include<iostream>
using namespace std;
class Student_c
{
public:
void setName(string n)
{
name = n;
}
void setAge(int a)
{
if(a < 0)
{
return;
}
age = a;
}
void show();
void study();
private:
string name;
int age;
};
void Student_c::study()
{
cout << "I am studying C ++" << endl;
}
void Student_c::show()
{
cout << name << endl;
cout << age << endl;
}
int main()
{
Student_s s;
s.study();
Student_c c;
c.study();
c.setName("zhangfei");
c.setAge(20);
c.show();
return 0;
}
--------------------------------------------------------------------------------------------------------------------
练习:自定义一个类,功能实现:,给定一个整型数组,实例化一个对象,该对象能够返回给定数组的最大值
#include <iostream>
using namespace std;
class the_max
{
private:
int *array;
public:
int find_max(int *array,int len)
{
int i,max = array[0];
for(i=1;i<len;i++)
{
if(max < array[i])
max = array[i];
}
return max;
}
};
int main()
{
int a[5] = {3,52,62,34,7};
the_max t;
int max = t.find_max(a,5);
cout<<"the max value:"<<max<<endl;
return 0;
}
3.this指针(自引用指针)
在类的成员函数中隐藏着一个this指针
this指针的值:谁调用就指向谁
--------------------------------------------------------------------------------------------------------------------
练习:如何在成员函数中去表示将来要调用该函数的那个对象:
#include<iostream>
using namespace std;
class Student_c
{
public:
void setName(string name)
{
this->name = name;
}
void setAge(int age)
{
if(age < 0)
{
return;
}
this->age = age;
}
void show();
void study();
private:
string name;
int age;
};
void Student_c::study()
{
cout << "I am studying C ++" << endl;
}
void Student_c::show()
{
cout << this << endl;
cout << this->name << endl;
cout << this->age << endl;
}
int main()
{
Student_c c;
cout << "&c = " << &c << endl;
c.setName("zhangfei");
c.setAge(20);
c.show();
Student_c c1;
cout << "&c1 = " << &c1 << endl;
c1.setName("guanyu");
c1.setAge(22);
c1.show();
return 0;
}
4.构造函数
构造函数是类的一个特殊的成员函数
作用:用于初始化对象
特性:
构造函数名与类名相同
构造函数在创建对象时自动调用,且一定会调用一个构造函数
构造函数没有返回类型,可以有参数
如果类中没有显式的定义构造函数,编译器会自动生成一个构造函数
构造函数名(){}//默认构造函数
注意:如果显式定义了任一构造函数,编译器就不会再自动生成
5.析构函数
析构函数是类的一个特殊的成员函数
作用:用于对象的清理工作
特性:
析构函数的名称是~类名
析构函数在对象被销毁的时候自动调用
析构函数没有返回类型,同时也没有参数
如果类中没显式的定义析构函数,编译器也会自动生成一个析构函数
~类名(){}
--------------------------------------------------------------------------------------------------------------------
#include<iostream>
using namespace std;
class Student_c
{
public:
Student_c()
{
cout << "构造函数" << endl;
name = "Wuming";
age = 0;
}
Student_c(string name,int age)
{
cout << "带参数的构造函数" << endl;
this->name = name;
this->age = age;
}
void setName(string name)
{
this->name = name;
}
void setAge(int age)
{
if(age < 0)
{
return;
}
this->age = age;
}
void show();
void study();
~Student_c()
{
cout << "析构函数被调用" << endl;
}
private:
string name;
int age;
};
void Student_c::study()
{
cout << "I am studying C ++" << endl;
}
void Student_c::show()
{
cout << this << endl;
cout << this->name << endl;
cout << this->age << endl;
}
int main()
{
Student_c c;
cout << "&c = " << &c << endl;
c.setName("zhangfei");
c.setAge(20);
c.show();
Student_c c1;
cout << "&c1 = " << &c1 << endl;
c1.setName("guanyu");
c1.setAge(22);
c1.show();
cout << "----------------------" << endl;
Student_c c2;
c2.show();
cout << "----------------------" << endl;
Student_c c3("liubei",20);
c3.show();
return 0;
}
6.对象的创建与销毁
1.在栈中创建对象
类名 对象名;
类名 对象名(初始参数);
类名 对象数组名[元素个数];
//int a[10];
2.在堆中创建对象
类名 *对象指针 = new 类名;
类名 *对象指针 = new 类名(初始参数);
delete 对象指针;
注意:new会自动调用构造函数
delete会自动调用析构函数
类名 *对象指针 = new 类名[元素个数];
//int *p = new int[10];
delete []对象指针;
--------------------------------------------------------------------------------------------------------------------
#include<iostream>
using namespace std;
class Student_c
{
public:
Student_c()
{
cout << "构造函数" << endl;
name = "Wuming";
age = 0;
}
Student_c(string name,int age)
{
cout << "带参数的构造函数" << endl;
this->name = name;
this->age = age;
}
void setName(string name)
{
this->name = name;
}
void setAge(int age)
{
if(age < 0)
{
return;
}
this->age = age;
}
void show();
void study();
~Student_c()
{
cout << "析构函数被调用" << endl;
}
private:
string name;
int age;
};
void Student_c::study()
{
cout << "I am studying C ++" << endl;
}
void Student_c::show()
{
cout << this << endl;
cout << this->name << endl;
cout << this->age << endl;
}
int main()
{
Student_c sarr[3] = {
Student_c("aaa",111),
Student_c("bbb",222),
};
sarr[0].setName("zhangfei");
sarr[0].setAge(20);
sarr[0].show();
sarr[1].show();
cout << "-----------------" << endl;
Student_c *ps1 = new Student_c("guanyu",22);
ps1->show();
delete ps1;
cout << "-----------------" << endl;
Student_c *ps2 = new Student_c[3]{Student_c("cccc",333)};
// ps2[0].show();
Student_c *tmp = ps2;
tmp->show();
tmp++;
tmp->show();
delete[] ps2;
cout << "-----------------" << endl;
return 0;
}