C++與C語言常用的語法對比
前言
本人在校學(xué)習(xí)的第一門語言是C++,由于操作系統(tǒng)這門課程實驗的需要,要求在linux下使用GCC編譯器編譯C程序代碼,為了寫代碼的方便,本人先采用VS2017寫了C++版本的代碼,再根據(jù)C++和C語言兩個語法的不同將程序進行修改成C程序。由于本人沒有學(xué)過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>
注:以上兩種語言的頭文件沒有給出直接對應(yīng)的關(guān)系,在使用時若不知道需要哪些頭文件可以直接全部復(fù)制使用。
2.結(jié)構(gòu)體struct
C++ 說明: C++的struct成員包含成員函數(shù)
struct Hello{
void sayHello(char *name){
printf("你好,%s\n",name);
}
};
int main()
{
Hello hello;
hello.sayHello();
exit(0);
}
C 說明: C語言的struct的成員只能是數(shù)據(jù),不支持函數(shù),因此若要定義結(jié)構(gòu)體的函數(shù)需要使用函數(shù)指針實現(xiàn)
struct Hello{
void (*sayHello)(char* name); //函數(shù)名為指針類型
};
void sayHello(char* name){
printf("你好,%s\n",name);
}
int main(){
struct Hello hello; //聲明結(jié)構(gòu)體的變量,需要加struct
hello.sayHello=sayHello; //C語言需要使用函數(shù)指針指向函數(shù)的聲明?。?!
hello.sayHello("淺若清風(fēng)");
return 0;
}
3.動態(tài)數(shù)組的創(chuàng)建與刪除
以一維和二維動態(tài)數(shù)組為例: C++ 創(chuàng)建: C++使用new自動為動態(tài)數(shù)組分配空間 刪除: C++使用delete為動態(tài)數(shù)組釋放內(nèi)存
void f()
{
int n;
int m;
int *Array1; //聲明一維動態(tài)數(shù)組
Array1=new int(n); //為一維動態(tài)數(shù)組分配空間,元素個數(shù)為n
int **Array2; //聲明二維動態(tài)數(shù)組
Array2=new int*[n]; //為二維動態(tài)數(shù)組分配空間(n個指針空間),n行
for(int i;i<n;++i)
{
Array2[i]=new int[m]; //為每一行分配內(nèi)存空間(m個整數(shù)空間),m列
}
//釋放內(nèi)存
delete []Array1;
for(int i=0;i<n;++i){
delete[]Array2[i];
}
delete[]Array2;
}
C 創(chuàng)建: C使用calloc為動態(tài)數(shù)組分配空間 刪除: C使用free()為動態(tài)數(shù)組釋放內(nèi)存
void f()
{
int n;
int m;
int *Array1; //聲明一維動態(tài)數(shù)組
Array1=(int *)calloc(n,sizeof(int)); //為一維動態(tài)數(shù)組分配空間,元素個數(shù)為n
int **Array2; //聲明二維動態(tài)數(shù)組
Array2=(int **)calloc(n,sizeof(int*)); //為二維動態(tài)數(shù)組分配空間(n個指針空間),n行
for(int i;i<n;++i)
{
Array2[i]=(int*)calloc(m,sizeof(int)); //為每一行分配內(nèi)存空間(m個整數(shù)空間),m列
}
//釋放內(nèi)存
free(Array1);
for(int i=0;i<n;++i){
free(Array2[i]);
}
free(Array2);
}
malloc函數(shù)與calloc函數(shù)的區(qū)別:
- malloc函數(shù):不能初始化所分配的內(nèi)存空間,在動態(tài)分配完內(nèi)存后,里邊數(shù)據(jù)是隨機的垃圾數(shù)據(jù)。
- calloc函數(shù):能初始化所分配的內(nèi)存空間,在動態(tài)分配完內(nèi)存后,自動初始化該內(nèi)存空間為零。
4.函數(shù)順序問題
- C++中,寫在前面的函數(shù)可以直接調(diào)用寫在后面的函數(shù)
- C中,被調(diào)用的函數(shù)需要寫在調(diào)用函數(shù)之前
5.類(class)
類是C++引入的類型,可以包含數(shù)據(jù),也可以包含函數(shù),解決了C語言struct只能包含數(shù)據(jù)的缺陷。 另外一個不同點是struct是默認public,而class默認private
- 下面以一個鏈隊的例子,來體現(xiàn)C++的 class的用法,并與C語言用struct實現(xiàn)進行對比
C++
#include<iostream>
using namespace std;
class QueueNode //隊列結(jié)點
{
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; //儲存下一個結(jié)點的指針
};
class LinkQueue //隊列
{
public:
LinkQueue() { front = rear = new QueueNode(); count = 0; }
void push(int m_page); //加到隊尾
int pop(); //取出隊首結(jié)點(此例是包含頭指針的鏈隊,頭指針的下一個結(jié)點才是隊首結(jié)點),并返回該結(jié)點值
bool exist(int page); //判斷隊列中是否有結(jié)點的值為page
void print(int page); //打印隊列
QueueNode* front; //隊首指針
QueueNode* rear; //隊尾指針
int count; //統(tǒng)計結(jié)點數(shù)量
};
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) //只剩下一個元素,此題不會出現(xiàn)這種情況
{
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; //與新增結(jié)點鏈接
q->rear=node; //尾指針后移
q->count++;
}
int pop(struct LinkQueue*q) //隊首元素出隊,并返回數(shù)值
{
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) //判斷隊列是否有結(jié)點的值等于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);
}到此這篇關(guān)于C++與C語言常用的語法對比的文章就介紹到這了,更多相關(guān)C++與C語言語法對比內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++?OpenGL實現(xiàn)旋轉(zhuǎn)立方體的繪制
這篇文章主要主要為大家詳細介紹了如何利用C++和OpenGL實現(xiàn)旋轉(zhuǎn)立方體的繪制,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起動手嘗試一下2022-07-07
C語言中結(jié)構(gòu)體與內(nèi)存對齊實例解析
C語言結(jié)構(gòu)體對齊也是老生常談的話題了,基本上是面試題的必考題,這篇文章主要給大家介紹了關(guān)于C語言中結(jié)構(gòu)體與內(nèi)存對齊的相關(guān)資料,需要的朋友可以參考下2021-07-07
C語言循環(huán)隊列與用隊列實現(xiàn)棧問題解析
循環(huán)隊列又叫環(huán)形隊列,是一種特殊的隊列。循環(huán)隊列解決了隊列出隊時需要將所有數(shù)據(jù)前移一位的問題,本篇帶你一起看看循環(huán)隊列的問題和怎樣用隊列實現(xiàn)棧2022-04-04
C++中關(guān)于constexpr函數(shù)使用及說明
這篇文章主要介紹了C++中關(guān)于constexpr函數(shù)使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11

