在c++中如何调用数组对象的构造函数

 我来答
ziguowen
推荐于2017-09-27 · TA获得超过2723个赞
知道小有建树答主
回答量:785
采纳率:35%
帮助的人:337万
展开全部
#include<iostream>
#include<string>
using namespace std;

//对象数组的初始化请看该函数
class student
{
public:
student(string name)
{
m_strname=name;
}
student(){}
void display()
{
cout<<"student name:"<<m_strname<<endl;
}
private:
string m_strname;
};

void main()
{
student stu[3]={student("zhangsan"),student("lisi")};
stu[0].display();
stu[1].display();
}

//深入了解构造和析构函数过程参看该函数!
#include<iostream>
using namespace std;

class study
{
private:
int num;
public:
study()
{
cout << "study default constructing^" << endl;
}
study(int a)
{
num = a;
cout << "study constructing^ " << num << endl;
}
~study()
{
cout << "study desctructing^" << endl;
}
};

void main()
{
study example[5] = {study(1),study(2),study()};
}
贸一谷梁初曼
2019-03-28 · TA获得超过3804个赞
知道大有可为答主
回答量:3200
采纳率:34%
帮助的人:252万
展开全部
#include<iostream>
#include<string>
using
namespace
std;
//对象数组的初始化请看该函数
class
student
{
public:
student(string
name)
{
m_strname=name;
}
student(){}
void
display()
{
cout<<"student
name:"<<m_strname<<endl;
}
private:
string
m_strname;
};
void
main()
{
student
stu[3]={student("zhangsan"),student("lisi")};
stu[0].display();
stu[1].display();
}
//深入了解构造和析构函数过程参看该函数!
#include<iostream>
using
namespace
std;
class
study
{
private:
int
num;
public:
study()
{
cout
<<
"study
default
constructing^"
<<
endl;
}
study(int
a)
{
num
=
a;
cout
<<
"study
constructing^
"
<<
num
<<
endl;
}
~study()
{
cout
<<
"study
desctructing^"
<<
endl;
}
};
void
main()
{
study
example[5]
=
{study(1),study(2),study()};
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
liujq007
2008-12-22 · TA获得超过942个赞
知道大有可为答主
回答量:1035
采纳率:0%
帮助的人:1068万
展开全部
book books[2]={book(1001,"C++面向对象程序设计","谭浩强"),book(1002,"数据结构","严蔚敏")};
其中book是类
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式