追回C++(2)

基本類型的理解,對引用和指針的理解

using namespace std;
//學生實體
struct StudentDto
{
	//名稱
	string CName;
	//英文名
	char EName [50];
	//年齡
	int Age;
	//性別
	char Sex;
	//是否繳費
	bool IsFee;
	//分數
	double FenShu;

};

#include <iostream>
#include "Student.h"
using std::cin;
using std::cout;
using std::endl;

int main()
{
	cout << "基本類型、引用、指針!" << endl;
	//學生1
	StudentDto stu1;
	//學生2
	StudentDto stu2;
	//學生3
	StudentDto stu3;
	cout << "請依次錄入學生1的姓名、英文名、年齡、性別、是否繳費、分數信息" << endl;
	cin >> stu1.CName >> stu1.EName >> stu1.Age >> stu1.Sex >> stu1.IsFee>>stu1.FenShu;
	cout << "請依次錄入學生2的姓名、英文名、年齡、性別、是否繳費、分數信息" << endl;
	cin >> stu2.CName >> stu2.EName >> stu2.Age >> stu2.Sex >> stu2.IsFee >> stu2.FenShu;
	cout << "請依次錄入學生3的姓名、英文名、年齡、性別、是否繳費、分數信息" << endl;
	cin >> stu3.CName >> stu3.EName >> stu3.Age >> stu3.Sex >> stu3.IsFee >> stu3.FenShu;
	cout << "兩個學生平均分是:" << ((stu1.FenShu + stu2.FenShu) / 2) << endl;
	cout << "下面測試引用和指針*****************************" << endl;
	//學生1的引用
	StudentDto& stu1Alias = stu1;
	//學生2的指針
	StudentDto* stu2Point = &stu2;
	cout << "請修改學生1的分數" << endl;
	cin >> stu1Alias.FenShu;
	cout << "請修改學生2的分數" << endl;
	cin >> (*stu2Point).FenShu;
	//輸出全用原始對象
	cout << "兩個學生修改後平均分是:" << ((stu1.FenShu + stu2.FenShu) / 2) << endl;
	cout << "下面測試指針傳遞********************************" << endl;
	//指針指向引用
	StudentDto* stu1AliasP = &stu1Alias;
	//指針指向指針
	StudentDto** stu2PP = &stu2Point;
	cout << "請修改學生1的分數" << endl;
	cin >> (*stu1AliasP).FenShu;
	cout << "請修改學生2的分數" << endl;
	cin >> (**stu2PP).FenShu;
	//輸出全用原始對象
	cout << "兩個學生修改後平均分是:" << ((stu1.FenShu + stu2.FenShu) / 2) << endl;
	cout << "下面測試指針傳切換指向*****************************" << endl;
	//學生2指針指向學生3,應該操作到三上去了
	stu2Point = &stu3;
	//一級指針應該輸出學生三姓名
	cout << "一級指針輸出,我是:" << (*stu2Point).CName << endl;
	//二級指針也應該輸出學生三姓名
	cout << "二級指針輸出我是:" << (**stu2PP).CName << endl;
	return 0;
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章