C++与C语言常用的语法对比

 更新时间:2022年04月15日 14:55:14   作者:浅若清风cyf   
这篇文章主要介绍了C++与C语言常用的语法对比,文章基于c++和C语言的相关资料展开两者的语法相互对比,需要的小伙伴可以参考一下,希望对你的学习有所帮助

前言

本人在校学习的第一门语言是C++,由于操作系统这门课程实验的需要,要求在linux下使用GCC编译器编译C程序代码,为了写代码的方便,本人先采用VS2017写了C++版本的代码,再根据C++和C语言两个语法的不同将程序进行修改成C程序。由于本人没有学过C语言,对C语言的语法也不是很熟悉,写本文的目的是记录下修改过程的遇到的几个注意点,方面以后参考,

1.头文件

c++

#include <iostream>
#include <ctime>
#include <stdlib.h>
#include <random>

C

#include <time.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
#include <malloc.h>

注:以上两种语言的头文件没有给出直接对应的关系,在使用时若不知道需要哪些头文件可以直接全部复制使用。

2.结构体struct

C++ 说明: C++的struct成员包含成员函数

struct Hello{
	void sayHello(char *name){
		printf("你好,%s\n",name);
	}
};

int main()
{
	Hello hello;
	hello.sayHello();
	exit(0);
}

说明: C语言的struct的成员只能是数据,不支持函数,因此若要定义结构体的函数需要使用函数指针实现

struct Hello{
	void (*sayHello)(char* name);   //函数名为指针类型
};
void sayHello(char* name){
	printf("你好,%s\n",name);
}
int main(){
	struct Hello hello;  //声明结构体的变量,需要加struct
	hello.sayHello=sayHello;  //C语言需要使用函数指针指向函数的声明!!!
	hello.sayHello("浅若清风");
	return 0;
}

3.动态数组的创建与删除

以一维和二维动态数组为例: C++ 创建: C++使用new自动为动态数组分配空间 删除: C++使用delete为动态数组释放内存

void f()
{
	int n;
	int m;
	int *Array1;  //声明一维动态数组
	Array1=new int(n);  //为一维动态数组分配空间,元素个数为n
	int **Array2;  //声明二维动态数组
	Array2=new int*[n];  //为二维动态数组分配空间(n个指针空间),n行
	for(int i;i<n;++i)
	{
		Array2[i]=new int[m];  //为每一行分配内存空间(m个整数空间),m列
	}
	//释放内存
	delete []Array1;
	for(int i=0;i<n;++i){
		delete[]Array2[i];
	}
	delete[]Array2;
}

创建: C使用calloc为动态数组分配空间 删除: C使用free()为动态数组释放内存

void f()
{
	int n;
	int m;
	int *Array1;  //声明一维动态数组
	Array1=(int *)calloc(n,sizeof(int));  //为一维动态数组分配空间,元素个数为n
	int **Array2;  //声明二维动态数组
	Array2=(int **)calloc(n,sizeof(int*));  //为二维动态数组分配空间(n个指针空间),n行
	for(int i;i<n;++i)
	{
		Array2[i]=(int*)calloc(m,sizeof(int));  //为每一行分配内存空间(m个整数空间),m列
	}
	//释放内存
	free(Array1);
	for(int i=0;i<n;++i){
		free(Array2[i]);
	}
	free(Array2);
}

malloc函数与calloc函数的区别:

  • malloc函数:不能初始化所分配的内存空间,在动态分配完内存后,里边数据是随机的垃圾数据
  • calloc函数:能初始化所分配的内存空间,在动态分配完内存后,自动初始化该内存空间为零

4.函数顺序问题

  • C++中,写在前面的函数可以直接调用写在后面的函数
  • C中,被调用的函数需要写在调用函数之前

5.类(class)

类是C++引入的类型,可以包含数据,也可以包含函数,解决了C语言struct只能包含数据的缺陷。 另外一个不同点是struct是默认public,而class默认private

  • 下面以一个链队的例子,来体现C++的 class的用法,并与C语言用struct实现进行对比

C++

#include<iostream>
using namespace std;

class QueueNode  //队列结点
{
public:
	QueueNode() :page(-1), next(NULL) {};
	QueueNode(int m_page, QueueNode* m_next = NULL) :page(m_page), next(m_next) {};
	~QueueNode() { next = NULL; }
public:
	int page;
	QueueNode* next;  //储存下一个结点的指针
};

class LinkQueue  //队列
{
public:
	LinkQueue() { front = rear = new QueueNode(); count = 0; }
	void push(int m_page);  //加到队尾
	int pop();  //取出队首结点(此例是包含头指针的链队,头指针的下一个结点才是队首结点),并返回该结点值
	bool exist(int page);  //判断队列中是否有结点的值为page
	void print(int page);  //打印队列

	QueueNode* front;  //队首指针
	QueueNode* rear;  //队尾指针
	int count;  //统计结点数量
};

void LinkQueue::push(int m_page)
{
	QueueNode* p = new QueueNode(m_page);
	p->next = NULL;
	rear->next = p;
	rear = p;
	count++;
}

int LinkQueue::pop()
{
	if (front == rear) {
		printf("队列为空!\n");
		return 0;
	}
	int top = front->next->page;  //记录队首,作为返回值
	QueueNode* tmp = front->next;
	front->next = tmp->next;
	if (rear == tmp)  //只剩下一个元素,此题不会出现这种情况
	{
		rear = front;
	}
	delete tmp;
	count--;
	return top;
}

void LinkQueue::print()  //打印队列
{
	QueueNode* p = front;
	while (p->next != NULL)
	{
			printf("%d ", p->next->page);
			p = p->next;
	}
	printf("\n");
}

bool LinkQueue::exist(int page) {  //判断队列中是否存在元素page
	QueueNode*p = front;
	while (p->next != NULL) {
		if (p->next->page == page) {
			return true;
		}
		else {
			p = p->next;
		}
	}
	return false;
}

int main()
{
	LinkQueue queue;
	for(int i=0;i<5;++i)
	{
		queue.push(i);
	}
	queue.print();
	if(queue.exit(6)) printf("yes!\n");
	else printf("no!\n");
	int top=queue.pop();  //获得队首元素值
	printf("队首元素的值为%d\n",top);
}
# include<stdio.h>
# include<stdlib.h>
# include<stdbool.h>

struct QueueNode
{
	int page;
	struct QueueNode *next;
};

typedef struct LinkQueue {
	struct QueueNode* front;
	struct QueueNode* rear;
	int count;
};

void initQueue(struct LinkQueue *q)
{
	q->front = q->rear=(struct QueueNode *)malloc(sizeof(struct QueueNode));
	q->front->next = NULL;
	q->count=0;
}

void push(struct LinkQueue *q, int page)
{
	struct QueueNode* node = (struct QueueNode*)malloc(sizeof(struct QueueNode));
	node->next = NULL;
	node->page = page;
	q->rear->next = node;  //与新增结点链接
	q->rear=node;  //尾指针后移
	q->count++;
}

int pop(struct LinkQueue*q)  //队首元素出队,并返回数值
{
	int top = q->front->next->page;
	struct QueueNode *node = q->front->next;
	q->front->next = q->front->next->next;
	free(node);
	return top;
}

bool exist(struct LinkQueue*q, int page)  //判断队列是否有结点的值等于page
{
	struct QueueNode *node = q->front;
	while (node->next != NULL)
	{
		if (node->next->page == page) return true;
		else node = node->next;
	}
	return false;
}

void print(struct LinkQueue *q)
{	
	struct QueueNode *node =q->front;
	while (node->next != NULL)
	{
		printf("%d ", node->next->page);
		node = node->next;
	}
	printf("\n");
}

int main()
{
	struct LinkQueue queue;
	initQueue(&queue);
	for (int i = 1; i < 5; ++i)
	{
		push(&queue, i);
	}
	print(&queue);
	if (exist(&queue, 3)) printf("yes!\n");
	printf("======\n");
	if (exist(&queue, 6)) printf("yes!\n");
	else printf("no!\n");
	int top = pop(&queue);
	printf("队首元素的值为%d\n", top);
}

到此这篇关于C++与C语言常用的语法对比的文章就介绍到这了,更多相关C++与C语言语法对比内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Cocos2dx实现数字跳动效果

    Cocos2dx实现数字跳动效果

    这篇文章主要为大家详细介绍了Cocos2dx实现数字跳动效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-09-09
  • C++ OpenGL实现旋转立方体的绘制

    C++ OpenGL实现旋转立方体的绘制

    这篇文章主要主要为大家详细介绍了如何利用C++和OpenGL实现旋转立方体的绘制,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起动手尝试一下
    2022-07-07
  • 深度剖析C语言结构体

    深度剖析C语言结构体

    今天小编就为大家分享一篇关于深度剖析C语言结构体,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • C语言结构体超详细讲解

    C语言结构体超详细讲解

    C语言结构体(Struct)从本质上讲是一种自定义的数据类型,只不过这种数据类型比较复杂,是由 int、char、float 等基本类型组成的。你可以认为结构体是一种聚合类型
    2022-04-04
  • C语言中结构体与内存对齐实例解析

    C语言中结构体与内存对齐实例解析

    C语言结构体对齐也是老生常谈的话题了,基本上是面试题的必考题,这篇文章主要给大家介绍了关于C语言中结构体与内存对齐的相关资料,需要的朋友可以参考下
    2021-07-07
  • Qt+QWidget实现简约美观的加载动画

    Qt+QWidget实现简约美观的加载动画

    这篇文章主要为大家详细介绍了Qt如何结合QWidget实现简约美观的加载动画,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-02-02
  • 深入了解C++中常用的三个智能指针

    深入了解C++中常用的三个智能指针

    C++是一门强大的编程语言,但是在内存管理方面却存在着一些问题。手动管理内存不仅费时费力,而且容易出错。因此,C++中引入了智能指针这一概念,以更好地管理内存,下面就来详细讲讲C++中常用的三个智能指针吧
    2023-05-05
  • C++实现设计模式之装饰者模式详解

    C++实现设计模式之装饰者模式详解

    这篇文章主要介绍了C++设计模式之装饰模式,装饰模式能够实现动态的为对象添加功能,是从一个对象外部来给对象添加功能,需要的朋友可以参考下
    2021-09-09
  • C语言循环队列与用队列实现栈问题解析

    C语言循环队列与用队列实现栈问题解析

    循环队列又叫环形队列,是一种特殊的队列。循环队列解决了队列出队时需要将所有数据前移一位的问题,本篇带你一起看看循环队列的问题和怎样用队列实现栈
    2022-04-04
  • C++中关于constexpr函数使用及说明

    C++中关于constexpr函数使用及说明

    这篇文章主要介绍了C++中关于constexpr函数使用及说明,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11

最新评论