C++面向物件程式設計50道程式設計題(第18題)
阿新 • • 發佈:2018-12-09
C++面向物件程式設計50道程式設計題(第18題)
摘要:C++程式設計實習是為學生提供了一個既動手又動腦,獨立實踐的機會,將課本上的理論知識和實際有機的結合起來,鍛鍊學生的分析問題和解決問題的能力,提高學生運用所學知識解決實際問題的能力。
本專輯為程式設計入門者、高校計算機軟體專業學習或複習提供C++程式設計題庫。
讀者請先獨立思考哦,再與參考程式進行比對檢查。
一、問題描述
二、考察內容
基本面向物件概念,如何建立類、物件,對類私有資料成員和公有成員函式的理解,簡單的字串處理。
三、難度等級
難度等級:★★★☆☆
四、參考程式
#include <iostream.h> #include <string.h> class Modistr{ char *str; public: Modistr(char *s=0); void print(){cout<<str<<endl;} ~Modistr(){delete []str;} void Modify(char *); }; Modistr::Modistr(char *s) { str=new char[strlen(s)+1]; strcpy(str,s); } void Modistr::Modify(char *wordp) { char p1[100],p2[20]; strcpy(p1,str); strcpy(p2,wordp); int i=0,n; for(;p1[i++];) { for(int j=i,k=0;p1[j]==p2[k]&&p2[k]!='\0';k++,j++); if(p2[k]=='\0'){ n=j; while(p1[n++]); for(int m=n;m>=j;m--) p1[m+1]=p1[m]; p1[j]='s'; i=j; } } str=new char[i]; strcpy(str,p1); } void main() { char *s="We are student ,you are student ,too."; char *p="student"; Modistr mys(s); mys.print(); mys.Modify(p); mys.print(); }
五、心得感受
可以在評論處寫下思考和程式設計此題的感受