成员函数和运算符的重载
2013-04-05 20:32
323 查看
成员函数和运算符的重载
一、构造函数的重载
构造函数可以重载,使得生成实例时非常方便。构造函数一般要对成员变量赋初值,有2种写法:
string name;
stuff() { //这是写法一
cout << name << "---" << age << endl;
name = "空";
age = 0;
cout << name << "---" << age << endl;
}
stuff(string n,
{
cout << name << "---" << age << endl;
}
string getName() {
}
}
};
{
stuff st2;
stuff st1("小雅", 27);
}
[/code]
写法一是在构造函数体中赋值,赋值前成员变量已经有了地址空间,尚未有值。写法二是一种特殊方法,是在成员变量分配空间的同时将参数的值赋给成员变量。虽然写法二用的人少,但明显优于写法一。
事实上,如果将成员变量的定义改为常量,“const string name;”和“const int age;”,写法一将出错,而写法二仍然正确。
二、运算符重载
运算符重载对于普通函数和成员函数来说,格式稍有不同。
普通函数: 返回值类型
//双目运算符
成员函数: 返回值类型
普通函数: 返回值类型
//函数调用
成员函数: 返回值类型
//数组元素
成员函数: 返回值类型
//增1/减1运算符
成员函数: 返回值类型
普通函数: 返回值类型
[/code]
string name;
stuff(string n,
{
cout << name << "---" << age << endl;
}
string getName() {
}
}
};
{
age = age + x;
}
{
name = name + s;
}
{
stuff st2("小雅", 27);
st2 + 3; //+运算
st2 + ".诗经"; //+运算
cout << st2.getName() << st2.getAge() << endl;
}
[/code]
三、拷贝构造函数和赋值运算符
本节内容较深,初学者请跳过。“拷贝构造函数”和“赋值运算符”都是将对象的值复制一份然后传给另一对象。这二个功能也是类本身就具有的,但有很多场合原封不动地复制给另外一个对象时反而会出错,例如在成员函数中有动态分配内存,或者参数指针指向外部某一地址时,就有可能出错。
要避免这些错误,我们可以重载“=”运算符以及拷贝构造函数,将出错的因素排除。下例中为了演示,故意将赋值运算符重载函数中不复制“姓名”,而拷贝构造函数中固定“年龄”。
string name;
stuff(string n,
{
cout << "构造函数 " << name << age << endl;
}
string getName() {
}
}
stuff&
stuff(stuff& x):name(x.name),age(20) //拷贝构造函数重载
{
cout << "拷贝构造函数 " << name << age << endl;
}
};
stuff& stuff::
{
age = x.age;
cout << "赋值运算符 " << name << age << endl;
}
{
stuff st("小雅", 25); //调用通常的构造函数
stuff st1("劝学网", 2); //调用通常的构造函数
st1 = st; //因为不产生新的实例,所以调用的是赋值运算符
stuff st2 = st; //因为产生新的实例,所以调用的是拷贝构造函数
cout << st.getName() << st.getAge() << endl;
cout << st1.getName() << st1.getAge() << endl;
cout << st2.getName() << st2.getAge() << endl;
}
[/code]
四、类型转换
当需要将当前类的实例直接赋值给其它类型的变量时自动转换类型,这其实还是运算符重载。当需要其它类型直接赋值给当前类的实例时,只要增加构造函数就行。
string name;
stuff(string n,
string getName() {
}
}
}
}
};
{
stuff st("小雅", 25);
string m_name = st; //stuff → string
cout << m_name << endl;
cout << m_age << endl;
}
[/code]
一、构造函数的重载
构造函数可以重载,使得生成实例时非常方便。构造函数一般要对成员变量赋初值,有2种写法:
[code]#include<iostream>
#include<string>
using namespacestd;
classstuff {
string name;
intage;
public:
stuff() { //这是写法一
cout << name << "---" << age << endl;
name = "空";
age = 0;
cout << name << "---" << age << endl;
}
stuff(string n,
inta):name(n),age(a) //这是写法二
{
cout << name << "---" << age << endl;
}
string getName() {
returnname;
}
intgetAge() {
returnage;
}
};
intmain ( )
{
stuff st2;
stuff st1("小雅", 27);
return0;
}
[/code]
写法一是在构造函数体中赋值,赋值前成员变量已经有了地址空间,尚未有值。写法二是一种特殊方法,是在成员变量分配空间的同时将参数的值赋给成员变量。虽然写法二用的人少,但明显优于写法一。
事实上,如果将成员变量的定义改为常量,“const string name;”和“const int age;”,写法一将出错,而写法二仍然正确。
二、运算符重载
运算符重载对于普通函数和成员函数来说,格式稍有不同。
//单目运算符 成员函数: 返回值类型 [code]operator运算符 () ;
普通函数: 返回值类型
operator运算符 (对象的类型) ;
//双目运算符
成员函数: 返回值类型
operator运算符 (对象的类型) ;
普通函数: 返回值类型
operator运算符 (对象的类型1, 对象的类型2) ;
//函数调用
成员函数: 返回值类型
operator(任意的参数列) ;
//数组元素
成员函数: 返回值类型
operator[] (参数类型) ;
//增1/减1运算符
成员函数: 返回值类型
operator运算符 (int) ;
普通函数: 返回值类型
operator运算符 (对象的类型, int) ;
[/code]
[code]#include<iostream>
#include<string>
using namespacestd;
classstuff {
string name;
intage;
public:
stuff(string n,
inta):name(n),age(a)
{
cout << name << "---" << age << endl;
}
string getName() {
returnname;
}
intgetAge() {
returnage;
}
voidoperator +(
intx); //运算符重载的定义
voidoperator +(string s); //运算符重载的定义
};
voidstuff::operator +(
intx) //运算符重载的实装
{
age = age + x;
}
voidstuff::operator +(string s) //运算符重载的实装
{
name = name + s;
}
intmain ( )
{
stuff st2("小雅", 27);
st2 + 3; //+运算
st2 + ".诗经"; //+运算
cout << st2.getName() << st2.getAge() << endl;
return0;
}
[/code]
三、拷贝构造函数和赋值运算符
本节内容较深,初学者请跳过。“拷贝构造函数”和“赋值运算符”都是将对象的值复制一份然后传给另一对象。这二个功能也是类本身就具有的,但有很多场合原封不动地复制给另外一个对象时反而会出错,例如在成员函数中有动态分配内存,或者参数指针指向外部某一地址时,就有可能出错。
要避免这些错误,我们可以重载“=”运算符以及拷贝构造函数,将出错的因素排除。下例中为了演示,故意将赋值运算符重载函数中不复制“姓名”,而拷贝构造函数中固定“年龄”。
[code]#include<iostream>
#include<string>
using namespacestd;
classstuff {
string name;
intage;
public:
stuff(string n,
inta):name(n),age(a)
{
cout << "构造函数 " << name << age << endl;
}
string getName() {
returnname;
}
intgetAge() {
returnage;
}
stuff&
operator=(stuff& x); //赋值运算符重载
stuff(stuff& x):name(x.name),age(20) //拷贝构造函数重载
{
cout << "拷贝构造函数 " << name << age << endl;
}
};
stuff& stuff::
operator=(stuff& x)
{
age = x.age;
cout << "赋值运算符 " << name << age << endl;
return*
this;
}
intmain ( )
{
stuff st("小雅", 25); //调用通常的构造函数
stuff st1("劝学网", 2); //调用通常的构造函数
st1 = st; //因为不产生新的实例,所以调用的是赋值运算符
stuff st2 = st; //因为产生新的实例,所以调用的是拷贝构造函数
cout << st.getName() << st.getAge() << endl;
cout << st1.getName() << st1.getAge() << endl;
cout << st2.getName() << st2.getAge() << endl;
return0;
}
[/code]
四、类型转换
当需要将当前类的实例直接赋值给其它类型的变量时自动转换类型,这其实还是运算符重载。当需要其它类型直接赋值给当前类的实例时,只要增加构造函数就行。
[code]#include<iostream>
#include<string>
using namespacestd;
classstuff {
string name;
intage;
public:
stuff(string n,
inta):name(n),age(a) { }
string getName() {
returnname;
}
intgetAge() {
returnage;
}
operator
int() { //stuff → int
returnage;
}
operatorstring() { //stuff → string
returnname;
}
};
intmain ( )
{
stuff st("小雅", 25);
string m_name = st; //stuff → string
intm_age = st; //stuff → int
cout << m_name << endl;
cout << m_age << endl;
return0;
}
[/code]
相关文章推荐
- 运算符-重载为成员函数 和友元函数的区别.
- 运算符重载的方式 双目运算符重载为类的成员函数
- 2013-4-19.1~用类的成员函数完成运算符的重载
- 第八周项目一の任务一----用类的成员函数完成运算符的重载
- C++中的四个默认成员函数与运算符重载详解
- 运算符-重载为成员函数 和友元函数的区别.
- 第八周任务1:方案二:请用类的友元函数,而不是成员函数,完成上面提及的运算符的重载;
- 【C++】成员函数重载二元和一元运算符
- C++ 60分钟入门教程 - 7、C++成员函数和运算符的重载
- 第八周项目一:用类的成员函数完成运算符的重载(任务一)
- 改:用类的成员函数完成运算符的重载
- 成员函数和友元函数完成一元运算符重载
- 用成员函数重载算术运算符
- 详解C++中的函数调用和下标以及成员访问运算符的重载
- 利用类的成员函数重载双目运算符其最多只有一个参数
- 类和对象(this指针、默认成员函数及运算符的重载)
- 第8周任务1-1(用类的成员函数完成运算符的重载)
- C++程序设计语言练习11.4 算术运算符重载的类成员函数写法
- c++类成员函数单目和双目运算符重载
- 第8周项目1:任务1:用类的成员函数完成运算符的重载