#include<iostream>
using namespace std;
#include<string.h>
class Book
{
private:
char name[20];
int number;
public:
Book()
{
strcpy(name, "no name");
number = 0;
cout << "调用无参构造函数" << endl;
}
Book(char* newname, int number)
{
strcpy(name, newname);
this->number = number;
cout << "调用有参构造函数" << endl;
}
void Show();
Book(Book& b)
{
name = b.name;
number = b.number;
cout << "执行拷贝构造函数" << endl;
}
};
void Book::Show()
{
cout << "书名和number分别为:" << endl;
cout << name << endl;
cout << number << endl;
}
int main()
{
Book b1;
b1.Show();
}
字符串不能直接赋值哦,我改了一下
#include<iostream>
using namespace std;
#include<string.h>
class Book
{
private:
char name[20];
int number;
public:
Book()
{
strcpy(name, "no name");
number = 0;
cout << "调用无参构造函数" << endl;
}
Book(char* newname, int number)
{
strcpy(name, newname);
this->number = number;
cout << "调用有参构造函数" << endl;
}
void Show();
Book(Book& b)
{
strcpy(name, b.name);
number = b.number;
cout << "执行拷贝构造函数" << endl;
}
};
void Book::Show()
{
cout << "书名和number分别为:" << endl;
cout << name << endl;
cout << number << endl;
}
int main()
{
Book b1;
b1.Show();
}
数组名是个地址,是常量