實訓C++語言設計——Student學生類設計、繼承、重寫

來源:互聯網
上載者:User

平台: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;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.