面向对象 结构体

本文详细介绍了C++中结构体的概念、声明与内存分配,以及如何访问结构变量、给结构体赋值、使用结构指针、结构与数组的交互、传递结构参数、返回结构和链表结构的创建与遍历。重点展示了结构体在组织和操作不同类型数据时的灵活性。
摘要由CSDN通过智能技术生成

结构概述

为什么要用结构体

结构体的存在是为了把不同类型的数据信息存放在一起。

结构的概念

声明一个结构并不分配内存,内存分配发生在定义这个新数据类型的变量中。

#include <iostream> 
struct employee
{
	char name[20];
	long code;
	float salary;
	char address[50];
	char phone[11];
};//分号是必须的

int main()
{
	employee person;//定义一个employee结构的变量,分配变量空间;
	//使用这个结构变量; 
} 

 访问结构变量

结构中的结构分量称为成员。用点操作符“.”,结构类型变量·结构中的成员。

int main()
{
	employee person;//定义一个employee结构的变量,分配变量空间;
	//使用这个结构变量; 
	employee.code =45.5; 
} 

给结构体赋值

数组不能彼此赋值,但是结构体可以。

不过不同结构名的变量不允许互相赋值。

结构和指针

  1. 通过箭头操作符“->",结构指针->结构成员;这个更直观一些。
  2. 通过点操作符(*结构指针).结构成员;
strucpy((*prPtr).name,"David Marat");
    (*prPtr).id =987654321;
    (*prPtr).salary =335.0;
    
    cout<<(*prPtr).name<<" "
        <<(*prPtr).id <<" "
        <<(*prPtr).salary<<endl;
#include <iostream>
#include <string>
using namespace std;
struct Person 
{
	char name[20];
	unsigned long id;
	float salary;
};
int main()
{
	Person pr1;
	Person *prPtr;
	prPtr=&pr1;
	
	strcpy(prPtr->name,"David Marat");
	
	prPtr->id =987654321;
	prPtr->salary =335.0;
	
	cout<<prPtr->name<<" "
        <<prPtr->id <<" "
        <<prPtr->salary<<endl;
}

结构与数组

#include <iostream>
#include <string>
using namespace std;
struct Person 
{
	char name[20];
	unsigned long id;
	float salary;
};
Person allone[2]={"jone",12345,339.0},
				 {"david",13916,449.0};

也可以建立结构指针数组来进行变量交换

#include <iostream>
#include <string>
using namespace std;
struct Person 
{
	char name[20];
	unsigned long id;
	float salary;
};

Person allone[2]={"jone",12345,339.0},
				 {"david",13916,449.0};


int main()
{
	Person *pA[2]={&allone[0],&allone[1]};
	
}

传递结构参数

传递结构值

#include <iostream>
#include <string>
using namespace std;
struct Person 
{
	char name[20];
	unsigned long id;
	float salary;
};

void Print(Person pr)
{
	cout<<pr.name <<" "
	    <<pr.id <<" "
	    <<pr.salary <<endl;
}

Person allone[2]={"jone",12345,339.0},
				 {"david",13916,449.0};

int main()
{
	for(int i=0;i<4;i++)
	{
		Print(allone[i]);
	}
	
}

传递结构的引用

这种方法效率更高,而函数实现和值传递相同,所以一般采用这种方法。

#include <iostream>
#include <string>
using namespace std;
struct Person 
{
	char name[20];
	unsigned long id;
	float salary;
};

void Print(Person &pr)
{
	cout<<pr.name <<" "
	    <<pr.id <<" "
	    <<pr.salary <<endl;
}

Person allone[2]={"jone",12345,339.0},
				 {"david",13916,449.0};

int main()
{
	for(int i=0;i<4;i++)
	{
		Print(allone[i]);
	}
}

返回结构

结构的引用参数返回

#include <iostream>
#include <string>
using namespace std;
struct Person 
{
	
    char name[20];
	unsigned long id;
	float salary;

};

void GetPerson(Person&p) // 结构参数引用传递的函数 
{
	cout<<"please enter a name for one person:\n";
	cin>>p.name;

	cout<<"please enter one's id number and hos salary:\n";
	cin>>p.id>>p.salary;

}

void Print(Person &pr)
{
	cout<<pr.name <<" "
	    <<pr.id <<" "
	    <<pr.salary <<endl;
}

int main()
{
	Person employee[3];

	for(int i=0;i<3;i++)
	{
		GetPerson(employee[i]); // 调用后,employee[i]被赋值; 
		Print(employee[i]);
	}
	
}

链表结构

结构的嵌套

结构可以嵌套,但结构成员不能是自身的结构变量,但可以用自身结构指针作为指针。

struct List
{
	char name[20];
	List *pN; //List结构指针作为成员 
	List m; // error 不可含有自身结构变量 
} 

创建与遍历链表

#include <iostream>
using namespace std;
#include <iomanip>

struct student{

	long number;
	float score;
	student * next;
};

student * head;//链首指针 

student * getnode()
{
    int num;
    float sc;

    cin>>num>>sc;

    if(num==0)
    {
    	return NULL;//结点无效 ,结束创建过程 
	}

	student *p=new student;

	p->number =num;
	p->score=sc;
	p->next=0;

	return p;
}

void create()
{
	if((head=getnode())==0)   //新建第一个结点,挂入链首 
		return;               //放回空链表 

	for(student *pe=head,*ps;ps=getnode();pe=ps) //pe指向尾结点 
		pe->next=ps;

}

void showlist()
{
	cout<<"now the items of list are\n";

	for(student *p=head;p;p=p->next)
	{
		cout<<p->number<<","<<p->score<<endl;
	}
}

int main()
{
	cout<<fixed<<setprecision(1);
	create();
	showlist();
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值