结构体:定义一组特征的对象
#include <iostream>
#include <cstring>
using namespace std;
// 定义结构体成员
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main(void) {
Books book1; // 定义结构体类型Books 的变量book1
Books book2; // 定义结构体类型Books 的变量book2
// book1
strcpy_s(book1.title, "C++ tutor");
strcpy_s(book1.author, "Runoob");
strcpy_s(book1.subject, "code language");
book1.book_id = 1234;
// book2
strcpy_s(book2.title, "CSS ");
strcpy_s(book2.author, "Oliva");
strcpy_s(book2.subject, "code");
book2.book_id = 6789;
//输出信息
cout << "book1.title:" << book1.title << endl;
cout << "book1.author:" << book1.author << endl;
cout << "book1.subject:" << book1.subject << endl;
cout << "book1.book_id:" << book1.book_id << endl;
cout << "book2.title:" << book2.title << endl;
cout << "book2.author:" << book2.author << endl;
cout << "book2.subject:" << book2.subject << endl;
cout << "book2.book_id:" << book2.book_id << endl;
system("pause");
return 0;
}
结果: