文章目录
1. 对象数组基础
声明对象数组的方法与声明标准类型数组相同:
stock mystuff[4];// creates an array of 4 Stock objects
当程序创建未被显式初始化的类对象时,总是调用默认构造函数。上述声明要求,这个类要么没有显式地定义任何构造函数(在这种情况下,将使用不执行任何操作的隐式默认构造函数),要么定义了一个显式默认构造函数(就像这个例子那样)。每个元素(mystuff[0]、mystuff[1]等)都是Stock对象,可以使用Stock方法:
// apply update() to 1st element
mystuff[0].update();
// apply show() to 4th element
mystuff[3].show();
const Stock * tops = mystuff[2].topval(mystuff[1]);
// compare 3rd and 2nd elements and set tops
// to point at the one with a higher total value
可以用构造函数来初始化数组元素。在这种情况下,必须为每个元素调用构造函数:
const int STKS =4;
Stock stocks [STKS] = {
Stock("NanoSmart",12.5,20),
Stock("Boffo Objects",200,2.0),
Stock("Monolithic Obelisks",130,3.25),
Stock("Fleep Enterprises",60,6.5)
};
这里的代码使用标准格式对数组进行初始化:用括号括起的、以逗号分隔的值列表。
其中,每次构造函数调用表示一个值。
如果类包含多个构造函数,则可以对不同的元素使用不同的构造函数:
const int STKS = 10;
Stock stocks [STKS] = {
Stock("NanoSmart",12.5,20),
Stock(),
Stock("Monolithic Obelisks",130,3.25),
};
上述代码使用Stock(const string & co,long n,double pr)
初始化stock[0]和stock[2],使用构造函数Stock()初始化stock[1]。
由于该声明只初始化了数组的部分元素,因此余下的7个元素将使用默认构造函数进行初始化。
初始化对象数组的方案是,首先使用默认构造函数创建数组元素,然后花括号中的构造函数将创建临时对象,然后将临时对象的内容复制到相应的元素中。
因此,要创建类对象数组,则这个类必须有默认构造承数。
2. 一个实例演示对象数组的例子
1. 没有提供默认的构造函数,能成功的创建对象数组吗?
object_array.cpp
#include <iostream>
#include <string>
/*
author:梦悦foundation
公众号:梦悦foundation
可以在公众号获得源码和详细的图文笔记
*/
using namespace std;
const int SIZE = 3;
class Demo {
private:
int iDemo;
string str;
public:
Demo(int iDemo, const string & str);
void Show();
};
Demo::Demo(int iDemo, const string &str = "meng-yue")
{
this->iDemo = iDemo;
this->str = str;
}
void Demo::Show()
{
cout << "iDemo:" << iDemo << ", str:" << str << endl;
}
int main(int argc, char *argv[])
{
int i = 0;
cout << "---------------开始--->公众号:梦悦foundation---------------" << endl;
Demo d[SIZE];
for (i = 0; i < SIZE; i++) {
cout << "d[" << i << "]:";
d[i].Show();
}
cout << "---------------结束--->公众号:梦悦foundation---------------" << endl;
return 0;
}
Demo d[SIZE];
创建3个 Demo类的实例,这会调用默认的构造函数。
编译会报错,因为没有提供默认的构造函数!
object_array.cpp: In function ‘int main(int, char**)’:
object_array.cpp:35:13: error: no matching function for call to ‘Demo::Demo()’
Demo d[SIZE];
^
object_array.cpp:20:1: note: candidate: Demo::Demo(int, const string&)
Demo::Demo(int iDemo, const string &str = "meng-yue")
^~~~
object_array.cpp:20:1: note: candidate expects 2 arguments, 0 provided
object_array.cpp:11:7: note: candidate: Demo::Demo(const Demo&)
class Demo {
^~~~
object_array.cpp:11:7: note: candidate expects 1 argument, 0 provided
object_array.cpp:11:7: note: candidate: Demo::Demo(Demo&&)
object_array.cpp:11:7: note: candidate expects 1 argument, 0 provided
meng-yue@ubuntu:~/MengYue/c++/object_class/04$
有两种解决这个问题的方式,一种是给其提供默认的构造函数,一种是将现有的构造函数都加上默认值。
1. 提供默认的构造函数的方式
object_array01.cpp
class Demo {
private:
int iDemo;
string str;
public:
Demo();
Demo(int iDemo, const string & str);
void Show();
};
Demo::Demo(int iDemo, const string &str = "meng-yue")
{
this->iDemo = iDemo;
this->str = str;
}
Demo::Demo()
{
this->iDemo = 1;
this->str = "default";
}
这样,编译就不会报错,且能够运行成功。
运行结果: 打印出来的都是默认的初始值
meng-yue@ubuntu:~/MengYue/c++/object_class/04$ ./object_array
---------------开始--->公众号:梦悦foundation---------------
d[0]:iDemo:1, str:default
d[1]:iDemo:1, str:default
d[2]:iDemo:1, str:default
---------------结束--->公众号:梦悦foundation---------------
meng-yue@ubuntu:~/MengYue/c++/object_class/04$
2. 把原来的构造函数改成带有默认参数的方式
object_array02.cpp
class Demo {
private:
int iDemo;
string str;
public:
Demo(int iDemo, const string & str);
void Show();
};
Demo::Demo(int iDemo = 3, const string &str = "meng-yue")
{
this->iDemo = iDemo;
this->str = str;
}
这样这个所有参数带默认值的构造函数也是匹配的!
运行结果:
meng-yue@ubuntu:~/MengYue/c++/object_class/04$ ./object_array02
---------------开始--->公众号:梦悦foundation---------------
d[0]:iDemo:3, str:meng-yue
d[1]:iDemo:3, str:meng-yue
d[2]:iDemo:3, str:meng-yue
---------------结束--->公众号:梦悦foundation---------------
meng-yue@ubuntu:~/MengYue/c++/object_class/04$
3. 思考:如果两个构造函数都有呢?会报错吗?
object_array03.cpp
class Demo {
private:
int iDemo;
string str;
public:
Demo();
Demo(int iDemo, const string & str);
void Show();
};
Demo::Demo(int iDemo = 3, const string &str = "meng-yue")
{
this->iDemo = iDemo;
this->str = str;
}
Demo::Demo()
{
this->iDemo = 1;
this->str = "default";
}
编译结果:
meng-yue@ubuntu:~/MengYue/c++/object_class/04$ g++ -o object_array03 object_array03.cpp
object_array03.cpp: In function ‘int main(int, char**)’:
object_array03.cpp:43:13: error: call of overloaded ‘Demo()’ is ambiguous
Demo d[SIZE];
^
object_array03.cpp:21:1: note: candidate: Demo::Demo(int, const string&)
Demo::Demo(int iDemo = 3, const string &str = "meng-yue")
^~~~
object_array03.cpp:27:1: note: candidate: Demo::Demo()
Demo::Demo()
^~~~
meng-yue@ubuntu:~/MengYue/c++/object_class/04$
编译报错了,因为这两个构造函数都可以被调用,编译器不知道调用哪一个,所以报错了!
4. 构造函数来初始化数组元素,在这种情况下,必须为每个元素调用构造函数。
object_array04.cpp
#include <iostream>
#include <string>
/*
author:梦悦foundation
公众号:梦悦foundation
可以在公众号获得源码和详细的图文笔记
*/
using namespace std;
const int SIZE = 3;
class Demo {
private:
int iDemo;
string str;
public:
Demo();
Demo(int iDemo, const string & str);
void Show();
};
Demo::Demo(int iDemo, const string &str = "meng-yue")
{
cout << "Demo(int iDemo = " << iDemo << ", string = " << str << ")" << endl;
this->iDemo = iDemo;
this->str = str;
}
Demo::Demo()
{
cout << "Demo()" << endl;
this->iDemo = 1;
this->str = "default";
}
void Demo::Show()
{
cout << "iDemo:" << iDemo << ", str:" << str << endl;
}
int main(int argc, char *argv[])
{
int i = 0;
cout << "---------------开始--->公众号:梦悦foundation---------------" << endl;
Demo d[SIZE] = {
Demo(7, "1-meng-yue"),
Demo(),
Demo(8, "2-meng-yue")
};
for (i = 0; i < SIZE; i++) {
cout << "d[" << i << "]:";
d[i].Show();
}
cout << "---------------结束--->公众号:梦悦foundation---------------" << endl;
return 0;
}
运行结果!
meng-yue@ubuntu:~/MengYue/c++/object_class/04$ g++ -o object_array04 object_array04.cpp
meng-yue@ubuntu:~/MengYue/c++/object_class/04$ ./object_array04
---------------开始--->公众号:梦悦foundation---------------
Demo(int iDemo = 7, string = 1-meng-yue)
Demo()
Demo(int iDemo = 8, string = 2-meng-yue)
d[0]:iDemo:7, str:1-meng-yue
d[1]:iDemo:1, str:default
d[2]:iDemo:8, str:2-meng-yue
---------------结束--->公众号:梦悦foundation---------------
meng-yue@ubuntu:~/MengYue/c++/object_class/04$
上面的代码就相当于
d[0] = Demo(7, "1-meng-yue");
d[1] = Demo();
d[2] = Demo(8, "2-meng-yue");