构造函数和析构函数
当我们建立了一个类并说明了这个类的对象后 对象的初始状态
c++提供了一个工具 用来自动完成类的对象的初始化 构造函数
~析构函数
不带参数的构造函数 一般形式
类名::函数名(与类名相同)(){函数体}
例子
有类 counter 其构造函数可以为
counter(void)
{
value=0;
};
析构函数
类名::~与类名相同的函数名 ()
{
}
如果在类的对象中分配有动态内存 则应定义析构函数 来完成释放内存的工作
如下形式
类名::~与类名相同的函数名()
{
delete 指针;
}
或
类名::~与类名相同的函数名()
{
delete [] 数组名;
}
一个类中可以有多个构造函数 但析构函数只有一个 对象被析构的顺序 与其建立的顺序相反 即像构造的后析构
带参数的相似就是有参数
构造函数参数的缺省值 和普通函数一样
例子
#pragma warning(disable:4996)
#include<stdio.h>
#include<iostream>//ctrl+d 选中所有相同的单词(代码)
using namespace std;
class student
{
public:
student();
student(int ,int ,int );
student(int, int );
student(const char* n, int s);
~student();
void show();
private:
char* name;
int score;
};
void student::show()
{
cout << name << " :name score:" << score << endl;
}
student::student(const char *n,int s)
{
name = new char[strlen(n) + 1];
strcpy(name, n);
score = s;
cout << " object initialized" << endl;
}
student::~student()
{
delete name;
cout << "ibject destroyed" << endl;
}
student::student()
{
score = 10;
name[0] = 'a';
}
student::student(int a=100, int b=100)
{
score = a * b;
}
student::student(int x, int y=10, int z=1)//使用了前半部分实参
{
score = x * y * z;
}
int main()
{
const char* p;
p = new char[10];
/*p[0] = 'a';
for (int i = 1; i < 10; i++)
{
p[i] = p[i - 1] + 1;
}*/
p = { "tiancai" };
student st1(p, 100);
st1.show();
return 0;
}
一句话说完 函数重载
;两个函数同名 只要求 函数的参数说明不同 类型 数目 不等 即可但无法重载仅按返回类型区分的函数
运算符重载
运算符重载是c++实现多态性的另一个手段 运算符重载是 使一些运算符对于某个类含有特定的意义 同时不影响运算符原来的基本含义 你可以给基本操作符赋予新的一种操作 c++的可扩充性得到增强
可重载的运算符
! | ~ | + | - | * | / |
---|---|---|---|---|---|
& | % | ^ | << | >> | < |
> | >= | <= | == | != | 位或 |
&& | += | -= | *= | /= | %= |
&= | ^= | <<= | >>= | , | ->* |
-> | () | [] | = | ++ | - - |
new | delete |
为了实现重载运算符 必须定义指定运算符在类中的具体操作 的含义 所以 必须定义一个叫做operator的运算符函数 一般形式
函数返回值类型 类名 ::operator 重载运算符(形式参数表)
{
函数体//
}
这里的函数返回值类型通常与取该类相同的类型 operator为关键字 函数的参数 具有类的数据类型 重载操作符的新功能 由函数体的操作符定义代码规定
事实上c ++语言中的每个运算符都对应着一个运算符函数 在运算过程中 系统将指定的运算符转化为调用相应的运算符函数运算对象转化为 函数的实参 在这个过程实在编译阶段完成的
如 x+y
编译时 将调用 operator +(x,y)
例子
用成员函数重载运算符
#pragma warning(disable:4996)
#include<stdio.h>
#include<iostream>//ctrl+d 选中所有相同的单词(代码)
using namespace std;
class X
{
public:
X(int a = 10) { x = a; }
~X();
void show() { cout << x << endl; }
X operator+(X s);
X operator++();
private:
int x;
};
X X::operator++()
{
this->x++;
return *this;
}
X X::operator+(X s)
{
X temp;
temp.x = this->x + s.x;
cout << this->x <<"is ";
return temp;
}
X::~X()
{
}
int main()
{
X x1, x2(150), x3;
x3 = x1 + x2+ x3;//x1与x2 相加以后保存一个新的X类的对象里面在进行+操作
x3.show();
return 0;
}
重载运算符 不能改变运算符的优先级 不能改变运算符的操作数目
下列运算符不能重载 :: .* ?:
用友元函数重载运算符
由于友元运算符没有隐含的参数this指针 因此重载双目 二元 运算符 两个操作数都必须现实的传递个函数 重载一元函数也不能省略唯一操作数
不能使用友元函数重载的运算符有 = , () [] ->
例子
多数情况下 用友元函数代替成员函数重载运算符并没有多大意义 但有一种情况 必须使用 友元操作符
调用成员运算符函数时 指向对象的指针 传给了this 在二元运算时 运算符的友元 调用该函数 这不会有问题 下面这是正确的
A=A+1;//A是对象
然而下面就是错的
A=1+A
因为运算符 的友元 是一个整数 是一个内部数据类型
如果用友元函数 来重载运算符函数 +就能解决有内部数据类型是运算符 +的友元带来的问题
#pragma warning(disable:4996)
#include<stdio.h>
#include<iostream>//ctrl+d 选中所有相同的单词(代码)
using namespace std;
class X
{
public:
X(int a = 10) { x = a; }
~X();
void show() { cout << x << endl; }
X operator+(X s);
X operator++();
friend X operator*(X &a,X &b);
friend X operator+(int p, X s);
private:
int x;
float y;
};
X operator+(int p,X s)
{
X temp;
temp.x = p + s.x;
return temp;
}
X operator*(X &a,X &b)
{
X sample;
sample.x = a.x * b.x;
return sample;
}
X X::operator++()
{
this->x++;
return *this;
}
X X::operator+(X s)
{
X temp;
temp.x = this->x + s.x;
cout << this->x <<"is ";
return temp;
}
X::~X()
{
}
int main()
{
X x1, x2(150), x3;
x3 = x1 + x2+ x3;//x1与x2 相加以后把指针指向对象的指针传递给了this再进行+操作
x3.show();
x3=x3 + 1;
//x3 = 1 + x3;如果不使用友元函数代替成员函数重载操作符就会出现不正确 。
x3 = 1 + x3;
x3.show();
return 0;
}
对象的动态存储管理
内存分配new 和释放delete
为对象申请内内存和释放内存的一般格式
obj_pointer=new class_name(init_list);
delete obj_pointer;
其中 obj_pointer为用户定义的对象指针 class_name 为用户定义的类名 (init_list)为对象的初始化值 初始化部分是任选的
例子
#pragma warning(disable:4996)
#include<stdio.h>
#include<iostream>//ctrl+d 选中所有相同的单词(代码)
using namespace std;
class A
{
public:
A(int x,int y);
~A();
void show() { cout << a << " " << b << endl; }
private:
int a, b;
};
A::A(int x,int y)
{
cout << "constructing" << endl;
a = x;
this->b = y;
}
A::~A()
{
cout << "destructing" << endl;
}
int main()
{
A* ptr;
ptr = new A(11, 12);
if (!ptr)
{
cout << "allocating failure\n";
return 0;
}
ptr->show();
delete ptr;//释放
return 0;
}
如果在类中为对象分配内存 通常是在构造函数中 进行内存的动态分配 并初始化 在这种情况下 要为该类提供相应的析构函数 以便完成释放内存的工作
定义一个 类
#pragma warning(disable:4996)
#include<stdio.h>
#include<iostream>//ctrl+d 选中所有相同的单词(代码)
using namespace std;
class text
{
public:
text(const char* ch);
void show();
~text();
private:
char* str;
};
text::text(const char* ch )
{
str = new char[strlen(ch) + 1];
strcpy(str, ch);
cout << "object initialized" << endl;
}
text::~text()
{
delete [strlen(str) + 1]str;
cout << "object destroyed" << endl;
}
void text::show()
{
cout << str << endl;
}
int main()
{
text p("wxvz");
p.show();
return 0;
}