C++学习笔记(三):自定义数据结构

本文详细介绍了C++中结构体的定义和使用,包括如何定义结构体类型,如何声明和初始化结构体变量,以及如何访问结构体成员。示例展示了如何创建一个书籍信息的结构体,并输出其详细信息。此外,还讨论了struct与class在C++中的区别,指出尽管struct在C++中具备类的特性,但推荐在定义数据结构时使用struct,而在定义类时使用class。
摘要由CSDN通过智能技术生成

1.定义结构

数据结构是把一组相关的数据元素组织起来然后使用他们的策略和方法。
C++语言允许用户以类的形式自定义数据类型,而库类型string、istream、ostream等也都是以类的形式定义的。

定义结构的格式如下:

struct type_name {
	member_type1 member_name1;
	member_type2 member_name2;
	member_type3 member_name3;
	.
	.
} object_names;

type_name 是结构体类型的名称,member_type1 member_name1 是标准的变量定义,比如 int i; 或者 float f; 或者其他有效的变量定义。

在结构定义的末尾,最后一个分号之前,您可以指定一个或多个结构变量,这是可选的。

下面是声明一个结构体类型 Books,变量为 book

struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} book;

注意在结构定义的末尾,一定要加上分号。这是因为后面可以紧跟变量名以示对该类型对象的定义,所以分号必不可少:

//用法1:直接在花括号后添加变量名,不用typedef 定义结构体,后跟的是结构体变量(类的对象)。
struct Student{/*...*/ } student1, student2, *student3;
//用法2:分成两行来写,与上一条语句等价,但可能更好一些
struct Student{ /*...*/ } ;
Student student1, student2, *student3;
//用法3:使用typedef定义结构体,后跟的是结构体的别名(相当于类的另一个名字)。其中,Stu是别名。
typedef struct Student{/*...*/}Stu;
Student student1, student2, *student3;//可以用原类型定义
Stu student4, student5, *student6;//也可以用别名定义

2.访问结构成员

#include <iostream>
#include <cstring>
 
using namespace std;
 
// 声明一个结构体类型 Books 
struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
};
 
int main( )
{
   Books Book1;        // 定义结构体类型 Books 的变量 Book1
   Books Book2;        // 定义结构体类型 Books 的变量 Book2
 
   // Book1 详述
   strcpy( Book1.title, "C++ 教程");
   strcpy( Book1.author, "Runoob"); 
   strcpy( Book1.subject, "编程语言");
   Book1.book_id = 12345;
 
   // Book2 详述
   strcpy( Book2.title, "CSS 教程");
   strcpy( Book2.author, "Runoob");
   strcpy( Book2.subject, "前端技术");
   Book2.book_id = 12346;
 
   // 输出 Book1 信息
   cout << "第一本书标题 : " << Book1.title <<endl;
   cout << "第一本书作者 : " << Book1.author <<endl;
   cout << "第一本书类目 : " << Book1.subject <<endl;
   cout << "第一本书 ID : " << Book1.book_id <<endl;
 
   // 输出 Book2 信息
   cout << "第二本书标题 : " << Book2.title <<endl;
   cout << "第二本书作者 : " << Book2.author <<endl;
   cout << "第二本书类目 : " << Book2.subject <<endl;
   cout << "第二本书 ID : " << Book2.book_id <<endl;
 
   return 0;
}

在这里插入图片描述

3.struct与class的区别

struct是C语言的关键字,但在C++中为了保持对C程序的兼容也引入了struct关键字,并在引入之后对其进行了扩充。C语言是面向过程的语言,C++是面向对象的语言。

因此,在C++中struct也可以拥有静态成员、成员数据可进行初始化、拥有函数、也可以继承、甚至多态也支持。

当然,根据google代码规范,struct一般只用来存储结构化数据,不应该有类继承于结构体,或结构体之间互相继承。

在编写C++代码时,强烈建议使用 class 来定义类,而使用 struct 来定义结构体,这样做语义更加明确。

#include<iostream>
#include<string>
using namespace std;
 
struct Student
{
	//可以拥有构造函数(非必须)
	Student(string name, int age) :m_name(name), m_age(age) {};
	//可以拥有函数(非必须)
	void show() { cout << "name: " << m_name << ", age: " << m_age << endl; };
 
	string m_name;
	int m_age;
};
 
//可以继承
struct Child_Student :public Student
{
	Child_Student(string name, int age, float score) :Student(name, age), m_score(score) {};
	void show()
	{
		cout << "This is a Child_Student." << endl;
		//可以使用基结构体中的成员
		cout << "name: " << m_name << ", age: " << m_age << ", score: " << m_score << endl;
	}
	float m_score;
};
 
int main(void)
{
	Student stu1("Alice", 23);
	stu1.show();
	cout << "访问结构体成员: " << endl;
	cout << "name: " << stu1.m_name << ", age: " << stu1.m_age << endl;
 
	Child_Student stu2("Ben", 20, 99.9);
	stu2.show();
	system("pause");
	return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值