實訓總結報告
----《面向對象程序設計》
實訓的目的與系統概述
1、目的:
(1)掌握面向對象設計開發的基本思想和方法,培養學生在已有的知識基礎上進一步獲取新知識的能力,提高分析問題和解決問題的能力。
(2)進一步鞏固《面向對象程序設計》課程中所學到的知識,熟練掌握C++語言程序設計,加強對VC++編程環境的使用能力,掌握VC++編程環境中的調試功能,增強實際編程能力。
(3)培養和提高邏輯思維、抽象思維和統籌規劃能力,培養獨立完成程序設計開發的工作能力。為今后從事專業性軟件開發工作打下基礎。
2、 功能
通過將不同類型的人員設計成相應的類,完成的學校人員的管理。
3、 系統概述
學校的人員包括學生、職工;職工又分為教師和行政人員;在職進修教師又具備學生和教師的屬性和行為。
a) 人員具有姓名、年齡、性別、地址和人員類別等屬性,有顯示屬性的功能和設置屬性的功能。
b) 學生具有人員的屬性和行為,此外,添加學號、成績屬性。具有的行為有:設置屬性的函數:讓用戶能夠通過鍵盤的輸入設置除了成績之外的基本屬性;錄入成績:輸入學生的各門成績;顯示:顯示基本屬性和他的平均成績。
c) 職工具有人員的屬性和行為,此外,添加職工號、職稱和工資屬性。
d) 教師具有職工的屬性和行為,此外,添加教研室、教授的課程屬性。具有的行為有:設置屬性的函數:讓用戶能夠通過鍵盤的輸入設置除了課程信息之外的基本屬性;錄入課程:輸入教師所教的各門成績;顯示:顯示基本屬性。
e) 行政人員具有職工的屬性和行為,此外,添加辦公室屬性。
f) 在職進修教師具有教師與學生的屬性和行為,此外,添加進修時間,工作單位屬性。
通過實現此系統,讓我綜合運用所學知識,掌握面向對象設計開發的基本思想和方法和C++的編程思想來完成簡單的面向對象的程序設計。讓我在已有的知識基礎上進一步獲取新知識的能力,提高分析問題、解決問題和獨立完成程序設計開發的工作能力。為今后從事專業性軟件開發工作打下基礎。
類的實現
編程實現上面類的設計。
主程序中的驗證
創建對象,檢驗類的功能是否能夠實現。
1、遇到的困難、解決辦法及收獲:
①各類屬性的實現。
利用對象數組即方便又能固定調用哪個屬性。在構造函數中定義屬性類型,利用對象數組調用。
②對象數組中字符串的賦值。
分配(new實現)一個char型變量長度加一的字符串,再利用拷貝函數拷貝原變量并放在name里,如:
name=new char[strlen(na)+1];strcpy(name,na);
③因為沒有給原帶參構造函數的形參賦值,主函數中不能聲明類的無參對象。
在定義一個無參構造函數,如:
person(){}
④在派生類帶參構造函數與基類帶參構造函數所帶參數不同時沒有對基類構造函數的參數初始化。
構造函數無法被繼承,當派生類帶參構造函數與基類帶參構造函數所帶參數不同時,要在派生類構造函數中對基類構造函數的參數進行初始化,如:
worker(int num,char *v,int p,char *na,int a,char *s,char *ad,char *l):person(na,a,s,ad,l){}
⑤平均分的實現。
利用對象數組正確調用分數屬性再利用返回值為float的成員函數實現平均分,如:
float getaverage(student st[],int i){
int a=st[i].mathscore;
int b=st[i].cscore;
return st[i].average=(a+b)/2;}
⑥正確輸入和調用屬性。
利用p[n].屬性名實現屬性的各種行為,如:
cin>>p[n+1].age;
cout<<"姓名:"<<p[n].name<<endl;
⑦switch語句總執行完一個case后還繼續執行。
加入break語句跳出switch語句。
2、 程序中還需要改進的地方。
姓名,年齡,地址等屬性限制的實現。
附件:源程序
#include <iostream.h>
#include <string>
using namespace std;
class person{
protected:
char *name;
int age;
char *sex;
char *address;
char *lb;
public:
person(char *na,int a,char *s,char *ad,char *l){//對象數組
name=new char[strlen(na)+1];
strcpy(name,na);
age=a;
sex=new char[strlen(s)+1];
strcpy(sex,s);
address=new char[strlen(ad)+1];
strcpy(address,ad);
lb=new char[strlen(l)+1];
strcpy(lb,l);}
person(){}//無參構造函數,方便聲明無參對象
virtual void getinf(person p[],int n){
char na1[128];
cout<<"姓名:";
cin>>na1;
p[n].name=new char[strlen(na1)+1];//分配na1長度+1的字符串
strcpy(p[n].name,na1);//拷貝字符串
cout<<endl;
cout<<"年齡:";
cin>>p[n+1].age;
cout<<endl;
char s1[2];
cout<<"性別:(n:男,w:女)";
cin>>s1;
p[n+2].sex=new char[strlen(s1)+1];
strcpy(p[n+2].sex,s1);
cout<<endl;
char ad1[128];
cout<<"地址:";
cin>>ad1;
p[n+3].address=new char[strlen(ad1)+1];
strcpy(p[n+3].address,ad1);
cout<<endl;
}
virtual void disp(person p[],int n){//有參虛函數,對象數組輸出的事現
cout<<"姓名:"<<p[n].name<<endl;
cout<<"年齡:"<<p[n+1].age<<endl;
cout<<"性別:"<<p[n+2].sex<<endl;
cout<<"地址:"<<p[n+3].address<<endl;}
};
class student:public virtual person
{
protected:
int number;
int mathscore,cscore;
float average;
public:
student(int nu,int ms,int cs,float av,char *na,int a,char *s,char *ad,char *l):person(na,a,s,ad,l){//構造函數無法被繼承,在派生類構造函數帶參數與基類構造函數所帶參數不同時要先對基類構造函數初始化
number=nu;mathscore=ms;cscore=cs;average=av;}
student(){}
virtual void getvalue(student st[],int i){
cout<<"學號:";
cin>>st[i].number;
cout<<endl;}
void getscore(student st[],int i){
cout<<"數學成績:";
cin>>st[i].mathscore;
cout<<endl;
cout<<"C++成績:";
cin>>st[i].cscore;
cout<<endl;}
float getaverage(student st[],int i){//平均分的實現
int a=st[i].mathscore;
int b=st[i].cscore;
return st[i].average=(a+b)/2;}
void show(student st[],int i){
cout<<"學號:"<<st[i].number<<endl;
}
virtual void show1(student st[],int i){
st[i].show(st,i);
cout<<"數學成績:"<<st[i].mathscore<<endl;
cout<<"C++成績:"<<st[i].cscore<<endl;
st[i].getaverage(st,i);
cout<<"平均成績:"<<st[i].average<<endl;
}
};
class worker:public virtual person
{
protected:
int number;
float pay;
char *value;
public:
worker(int num,char *v,int p,char *na,int a,char *s,char *ad,char *l):person(na,a,s,ad,l){
number=num;
value=new char[strlen(v)+1];
strcpy(value,v);
pay=p;}
worker(){}
void getvalue(worker w[],int i){
cout<<"職工號:";
cin>>w[i].number;
cout<<endl;
char v1[128];
cout<<"職稱:";
cin>>v1;
w[i].value=new char[strlen(v1)+1];
strcpy(w[i].value,v1);
cout<<endl;
cout<<"工資:";
cin>>w[i].pay;
cout<<endl;
}
void show(worker w[],int i){
cout<<"職工號:"<<w[i].number<<endl;
cout<<"職稱:"<<w[i].value<<endl;
cout<<"工資:"<<w[i].pay<<endl;
}
};
class teacher:public virtual worker,public virtual person{
protected:
char *kemu;
int chengji;
char *office;
public:
teacher(char *of,char *ke,int ch,int num,char *v,int p,char *na,int a,char *s,char *ad,char *l):worker(num,v,p,na,a,s,ad,l),person(na,a,s,ad,l){
office=new char[strlen(of)+1];
strcpy(office,of);
kemu=new char[strlen(ke)+1];
strcpy(kemu,ke);
chengji=ch;}
teacher(){}
void getmessage(teacher t[],int i) {
char of1[128];
cout<<"教研室:";
cin>>of1;
t[i].office=new char[strlen(of1)+1];
strcpy(t[i].office,of1);
cout<<endl;
char ke1[128];
cout<<"科目:";
cin>>ke1;
t[i].kemu=new char[strlen(ke1)+1];
strcpy(t[i].kemu,ke1);
cout<<endl;
cout<<"成績:";
cin>>t[i].chengji;
cout<<endl;
}
void show1(teacher t[],int i){
cout<<"教研室:"<<t[i].office<<endl;
cout<<"科目:"<<t[i].kemu<<endl;
cout<<"成績:"<<t[i].chengji<<endl;
}
};
class employee:public virtual worker,public virtual person{
private:
char *office;
public:
employee(char *of,int num,char *v,int p,char *na,int a,char *s,char *ad,char *l):worker(num,v,p,na,a,s,ad,l),person(na,a,s,ad,l){
office=new char[strlen(of)+1];
strcpy(office,of);
}
employee(){}
void getmessage(employee e[],int i) {
char of1[128];
cout<<"教研室:";
cin>>of1;
e[i].office=new char[strlen(of1)+1];
strcpy(e[i].office,of1);
cout<<endl;
}
void show1(employee e[],int i){
cout<<"教研室:"<<e[i].office<<endl;
}
};
class jingx:public virtual student,public virtual teacher,public virtual worker,public virtual person{
private:
int time;
char *danwei;
public:
jingx(int ti,char *da,int nu,int ms,int cs,float av,char *of,char *ke,int ch,int num,char *v,int p,char *na,int a,char *s,char *ad,char *l):student(nu,ms,cs,av,na,a,s,ad,l),teacher(of,ke,ch,num,v,p,na,a,s,ad,l),worker(num,v,p,na,a,s,ad,l),person(na,a,s,ad,l){
time=ti;
danwei=new char[strlen(da)+1];
strcpy(danwei,da);}
jingx(){}
void getmessage1(jingx j[],int i){
cout<<"進修時間:";
cin>>j[i].time;
cout<<endl;
char da1[128];
cout<<"單位屬性:";
cin>>da1;
j[i].danwei=new char[strlen(da1)+1];
strcpy(j[i].danwei,da1);
cout<<endl;
}
void show2(jingx j[],int i) {
cout<<"進修時間:"<<j[i].time<<endl;
cout<<"單位屬性:"<<j[i].danwei<<endl;
}
};
int i;
person p[1]; student st[1]; worker wo[1]; teacher te[1]; employee em[1]; jingx jx[1];
void main(){
cout<<"請選擇人員類別:"<<endl;
cout<<"1、普通人 2、學生 3、學生及成績 4、員工 5、教師 6、行政人員 7、在職進修教師"<<endl;
cin>>i;
cout<<"請依次錄入信息"<<endl;
switch(i){//錄入信息種類選擇的實現
case 1:{
p[1].getinf(p,1);
cout<<"*************************************"<<endl;
p[1].disp(p,1);
break;}//要有break語句否則將執行case2
case 2:{
st[1].getvalue(st,1);
p[1].getinf(p,1);
cout<<"*************************************"<<endl;
st[1].show(st,1);
p[1].disp(p,1);
break;}
case 3:{
st[1].getvalue(st,1);
p[1].getinf(p,1);
st[1].getscore(st,1);
cout<<"*************************************"<<endl;
st[1].show1(st,1);
p[1].disp(p,1);
break;}
case 4:{
wo[1].getvalue(wo,1);
p[1].getinf(p,1);
cout<<"*************************************"<<endl;
wo[1].show(wo,1);
p[1].disp(p,1);
break;}
case 5:{
te[1].getmessage(te,1);
p[1].getinf(p,1);
wo[1].getvalue(wo,1);
cout<<"*************************************"<<endl;
te[1].show1(te,1);
p[1].disp(p,1);
wo[1].show(wo,1);
break;}
case 6:{
em[1].getmessage(em,1);
p[1].getinf(p,1);
wo[1].getvalue(wo,1);
cout<<"*************************************"<<endl;
em[1].show1(em,1);
p[1].disp(p,1);
wo[1].show(wo,1);
break;}
case 7:{
jx[1].getmessage1(jx,1);
te[1].getmessage(te,1);
st[1].getvalue(st,1);
wo[1].getvalue(wo,1);
p[1].getinf(p,1);
st[1].getscore(st,1);
cout<<"*************************************"<<endl;
jx[1].show2(jx,1);
p[1].disp(p,1);
te[1].show1(te,1);
wo[1].show(wo,1);
st[1].show1(st,1);
break;}
default :
cout<<"輸入錯誤!"<<endl;
}
}
《實訓總結報告》出自:百味書屋
鏈接地址:http://www.alexanderday.net/news/182677.html
轉載請保留,謝謝!