平台:VC++ 2005 測試通過!
.vcproj
這是使用應用程式嚮導產生的 VC++ 項目的主專案檔案。
它包含產生該檔案的 Visual C++ 的版本資訊,以及有關使用應用程式嚮導選擇的平台、配置和項目功能的資訊。
StdAfx.h, StdAfx.cpp
這些檔案用於產生名為 twod.pch 的先行編譯頭 (PCH) 檔案和名為 StdAfx.obj 的先行編譯類型檔案。
這些都是使用應用程式嚮導產生的 VC++ 檔案故不列出
我只列出程式主要部分!
/*
本例展示了兩個抽象資料類型CStudent、CGradstu的簡單
繼承關係, 作為子類的CGradstu繼承了父類的屬性和方法,
體現資料和代碼的重用.此外,子類根據自己的需要
"重寫(overwrite)"了父類的同名方法.
*/
#include <iostream>
#include <string>
#include <list>
using namespace std;
class CStudent {
public:
CStudent(long stuid, string nm, string year, string dept);
virtual void print() const;
protected:
/*If it is protected , its name can be used only
1. by member functions and friends of the class in which
it is declared
2. and by member functions and friends of classes derived
from this class*/
long _stuid;
string _year; string _name; string _dept;
};
class CGradstu : public CStudent {
/*puclic inheritance means that the protected and public
members of CStudent are to be inherited as protected
and public member of CGradstu*/
public:
CGradstu( long stuid, string nm, string year,
string dept, string sp, string th);
virtual void print() const;
protected:
string _support;
string _thesis;
};
CStudent::CStudent(long stuid, string nm, string year, string dept)
:_name(nm), _stuid(stuid), _year(year), _dept(dept){}
CGradstu::CGradstu (long stuid, string nm, string year,
string dept, string sp, string th):
CStudent(stuid, nm, year, dept), _support(sp), _thesis(th){}
void CStudent::print() const {
cout << _stuid << " , " << _name
<< " , " << _year << " , " << _dept << endl;
}
/*子類根據自己的需要overwrite從父類繼承來的方法*/
void CGradstu::print() const {
CStudent::print(); //base class info is printed
cout << _support << " , " << _thesis << endl;
}
// StuPro.cpp : 定義控制台應用程式的進入點。
//
#include "stdafx.h"
#include "CStudent.h"
/*使用者的global function*/
void output(list<CStudent> stulist){
list<CStudent>::iterator iter = stulist.begin();
list<CStudent>::iterator end = stulist.end();
while( iter != end )
(*iter++).print();
}
int _tmain(int argc, _TCHAR* argv[])
{
CStudent stu1(2004112001, "張三", "三年級", "軟體工程");
CStudent stu2(2004112002, "李四", "三年級", "網路工程");
CGradstu grad1(10112001, "王五" , "研究生", "計算數學",
"TA", "求解博弈nash均衡");
list<CStudent> StuList;
StuList.push_back(stu1);
StuList.push_back(stu2);
StuList.push_back(grad1);
output(StuList);
//grad1.print();
return 0;
}