C++實驗 實驗3 類和對象 2


定義一個學生類,其中有3個數據成員有學號、姓名、年齡,以及若干成員函數。同時編寫主函數使用這個類,實現對學生數據的賦值和輸出。

要求:

(1)使用成員函數實現對數據的輸入、輸出;

(2)使用構造函數和析構函數實現對數據的輸入、輸出。


#include<iostream>
using namespace std;

class student
{
public:
	student();
	void data_Input();
	void data_Output();
	~student();
private:
	char name[20];
	int num;
	int age;
};

student::student()
{
    cout << "默認構造函數執行輸入" << endl;
    cout << "請輸入學生的姓名、學號、年齡:"; 
	cin >> name >> num >> age;
}

void student::data_Input()
{
    cout << "成員函數執行輸入" << endl;
    cout << "請輸入學生的姓名、學號、年齡:";
	cin >> name >> num >> age;
}

void student::data_Output()
{
    cout << "成員函數執行輸出" << endl;
	cout << "Name: " << name << endl;
	cout << "Number: " << num << endl;
	cout << "Age: " << age << endl;
}

student::~student()
{
    cout << "析構函數執行輸出" << endl;
	cout << "Name: " << name << endl;
	cout << "Number: " << num << endl;
	cout << "Age: " << age << endl;
}

int main(void)
{
	student stud;
	
	stud.data_Input();
	
	stud.data_Output();
	
	return 0;
}


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