结构体与类的区别

有关指针数组和数组指针,请点击

有关类和结构体的区别,请点击

结构体与类都是各种数据类型的集合,区别是结构体里只能是各种变量类型的集合,而类除了各种变量类型外,还可以有各种函数

结构体举例:

#include "stdio.h"
#include "stdlib.h"

struct Student{
	char *name;
	int age;
	int score;
};

void display(struct Student stu){
	printf("%s,%d,%d", stu.name, stu.age, stu.score);
}

int main(){
	struct Student stu1;
	stu1.name = "csdn";
	stu1.age = 20;
	stu1.score = 80;
	display(stu1);
	system("pause");
}

类举例:

#include "stdio.h"
#include "stdlib.h"

class Student{
public:
	char *name;
	int age;
	int score;

	void display(){
		printf("%s,%d,%d", name, age, score);
	}
};
int main(){
	Student stu1;
	stu1.name = "csdn";
	stu1.age = 20;
	stu1.score = 80;
	stu1.display();
	system("pause");
}

这里我们注意display这个函数,在结构体例子里,由于name,age,score都是结构体之外的变量,所以display形参是定义了一个结构体,内部使用时也在前面加上了stu.

但在类的例子里,我们看到在类的定义中,display()直接使用三个参数

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值