64,652
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
Student(string,string,int);
~Student();
Student(const Student&obj);
void display();
public:
string name;
string sex;
int age;
};
Student::Student(string n,string s,int a)
{
cout<<"construct"<<endl;
name=n; //学生姓名
sex=s; //学生性别
age=a; //学生年龄
}
Student::Student(const Student& obj)
{
cout<<"copy construct"<<endl;
}
Student::~Student()
{
cout<<"调用析构函数"<<endl;
}
void Student::display()
{
cout<<name<<"//"<<sex<<"//"<<age<<endl;
}
int main()
{
Student s[3]={Student("a","b",1),Student("a1","b",10),Student("a3","b",100)};
return 0;
}
char a[] = "12345";
是栈里分配一个char[6]的数组,把字符串"12345"从常量数据区拷贝过来f(int a){};//定义
f(int(14));//调用
Student s[3]={Student("a","b",1),Student("a1","b",10),Student("a3","b",100)};
Student s0=Student("a","b",1);
Student s1=Student("a1","b",10);
Student s2=Student("a3","b",100);
#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
Student(string ,string ,int);
Student(const Student& s);
~Student();
void display();
public:
string name;
string sex;
int age;
};
Student::Student(string n,string s,int a)
{
name=n; //学生姓名
sex=s; //学生性别
age=a; //学生年龄
cout << "调用构造函数" << endl ;
}
Student::Student(const Student& s)
{
name = s.name;
sex = s.sex;
age = s.age;
cout << "调用拷贝构造函数" << endl ;
}
Student::~Student()
{
cout << "调用析构函数" << endl ;
}
void Student::display()
{
cout << name << "//" << sex << "//" << age << endl ;
}
int main()
{
Student s[3] = {Student("a","b",1), Student("a1","b",10), Student("a3","b",100)};
s[0].display();
s[1].display();
s[2].display();
return 0;
}