C++ 程式:在結構中儲存學生資訊
結構是不同資料型別的專案集合。它非常有助於生成具有不同資料型別記錄的複雜資料結構。結構使用 struct 關鍵字定義。
結構的示例如下。
struct employee {
int empID;
char name[50];
float salary;
};一個在結構中儲存學生資訊的程式如下。
示例
#include <iostream>
using namespace std;
struct student {
int rollNo;
char name[50];
float marks;
char grade;
};
int main() {
struct student s = { 12 , "Harry" , 90 , 'A' };
cout<<"The student information is given as follows:"<<endl;
cout<<endl;
cout<<"Roll Number: "<<s.rollNo<<endl;
cout<<"Name: "<<s.name<<endl;
cout<<"Marks: "<<s.marks<<endl;
cout<<"Grade: "<<s.grade<<endl;
return 0;
}輸出
The student information is given as follows: Roll Number: 12 Name: Harry Marks: 90 Grade: A
在上述程式中,結構在 main() 函式之前定義。此結構包含學生的學號、姓名、成績和班級。這在以下程式碼段中得到演示。
struct student {
int rollNo;
char name[50];
float marks;
char grade;
};在 main() 函式中,定義了型別為 struct student 的一個物件。此物件包含學號、姓名、成績和班級值。演示如下。
struct student s = { 12 , "Harry" , 90 , 'A' };結構值按以下方式顯示。
cout<<"The student information is given as follows:"<<endl; cout<<endl; cout<<"Roll Number: "<<s.rollNo<<endl; cout<<"Name: "<<s.name<<endl; cout<<"Marks: "<<s.marks<<endl; cout<<"Grade: "<<s.grade<<endl;
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP