};
编写构造函数,析构函数及其它函数,实现动态分配内存,释放内存和数据输入输出等功能。并进行调试。
#include<iostream> using namespace std; class Myarray { int *alist; int length; public: Myarray(); Myarray( int leng); ~Myarray(); void input(); void display(); }; Myarray::Myarray() { length=0; alist=NULL; } Myarray::Myarray(int leng) { cout<<"Constructing"<<endl; length=leng; alist=new int[length+1]; } Myarray::~Myarray() { if(alist!=NULL) { delete []alist; length=0; cout<<"Destructing"<<endl; } } void Myarray::input() { if(alist!=NULL) { cout<<"Input"<<length<<" numbers for Myarray"<<endl; for(int i=0; i<length; i++) { cin>>alist[i]; } } } void Myarray::display() { cout<<"Display elements of Myarray"<<endl; for(int i=0; i<length; i++) { cout<<alist[i]<<" "; } cout<<endl; } int main() { Myarray a(10); a.input(); a.display(); return 0; }
4. 定义一个类Nawarray继承自averarray和revarray。在继承过程中声明为虚基类,体会虚基类在解决二义性中的问题中的作用。调试中可以试一试不用虚基类出现的问题。
#include<iostream> using namespace std; class Myarray//定义一个基类MyArray,基类中可以存放一组数组 { public: int *alist; int length; Myarray(); Myarray( int leng); ~Myarray(); void input(); void display(); }; Myarray::Myarray() { length=0; alist=NULL; } Myarray::Myarray(int leng) { cout<<"Constructing"<<endl; length=leng; alist=new int[length+1]; } Myarray::~Myarray() { if(alist!=NULL) { delete []alist; length=0; cout<<"Destructing"<<endl; } } void Myarray::input() { if(alist!=NULL) { cout<<"Input"<<length<<" numbers for Myarray"<<endl; for(int i=0; i<length; i++) { cin>>alist[i]; } } } void Myarray::display() { cout<<"Display elements of Myarray"<<endl; for(int i=0; i<length; i++) { cout<<alist[i]<<" "; } cout<<endl; } class Averarray:virtual public Myarray//类averarray继承自myarray, { public: double aver; Averarray(int n):Myarray(n) { cout<<"Averarray Constructing"<<endl; aver=0; } ~Averarray() { cout<<"Averarray Destructing"<<endl; } void getAver() { for(int i=0; i<length; i++) { aver+=alist[i]; } cout<<"Aver="<<aver/length<<endl; } }; class Revarray:virtual public Myarray//类Revarray继承自myarray, { public: Revarray(int n):Myarray(n) { cout<<"Revarray Constructing"<<endl; } ~Revarray() { cout<<"Revarray Destructing"<<endl; } void Reverse() { cout<<"The original Revarray"<<endl; display(); for(int i=0; i<(length)/2; i++) { int temp=alist[i]; alist[i]=alist[length-1-i]; alist[length-1-i]=temp; } cout<<"After reverse"<<endl; display(); } void display() { cout<<"Display elements of Nawarray"<<endl; for(int i=0; i<length; i++) cout<<alist[i]<<" "; cout<<endl; } }; class Nawarray:public Averarray,public Revarray { public: Nawarray(int length):Averarray(length),Revarray(length),Myarray(length) { cout<<"Nawarray Constructing"<<endl; } ~Nawarray() { cout<<"Nawarray Destructing"<<endl; } void display() { cout<<"Display elements of Nawarray"<<endl; for(int i=0; i<length; i++) cout<<alist[i]<<" "; cout<<endl; } }; int main() { Myarray m(5); m.input(); m.display(); Averarray a(5); a.input(); a.getAver(); Revarray r(5); r.input(); r.Reverse(); Nawarray n(5); n.input(); n.display(); return 0; }
#include <iostream> using namespace std; class A { public: void f1(); A() { i1=10; j1=11; } protected: int j1; private: int i1; }; class B:private A { public: void f2(); B() { i2=20; j2=21; } protected: int j2; private: int i2; }; class C: public B { public: void f3(); C() { i3=30; j3=31; } protected: int j3; private: int i3; }; int main() { return 0; }
一个简单串类string,包含输入字符串、返回字符串长度及内容等功能;另有一个具有编辑功能的串类edit_string,它继承string类,在其中设置一个光标,使其能支持在光标处的插入、替换和删除等编辑功能。
#include<iostream> #include<cstring> using namespace std; class String { public: int length; char *s; String(char *str=NULL); //普通构造函数 String(const String &other); //拷贝构造函数 ~ String(void); // 析构函数 void print(); int getLen(); }; String::String(char *str) { if(str) { this->length=strlen(str); s= new char[length+1]; strcpy(s,str); } } String::String(const String &other) { length=strlen(other.s); s= new char[length+1]; strcpy(s,other.s); } String::~String() { cout<<"Destructing String "<<s<<endl; if(s) delete []s; length=0; } void String::print() { cout<<s<<endl; } int String::getLen() { return length; } class edit_String:public String { public: edit_String(char *s):String(s) { cout<<"Constructing edit_String"<<endl; } void Delete(int p); //实现删除功能 void Insert(int p,char c); //实现插入功能 bool Replace(int p,char c); //实现替换功能 }; void edit_String::Delete(int p) //实现删除功能 { if(s==NULL) { cout<<"Fail to delete,s is empty"<<endl; return; } if(p>=length) { cout<<"p is too big"<<endl; return; } for(int i=0; i<length-1; i++) { if(i>=p-1) { s[i]=s[i+1]; } } length--; } bool edit_String::Replace(int p,char c)//实现替换功能 { if(s==NULL) { cout<<"Fail to replace,s is empty"<<endl; return false; } if(p>=length) { cout<<"p is too big"<<endl; return false; } for(int i=0; i<length; i++) { if(i==p) { s[i]=c; return true; } } return false; } void edit_String::Insert(int p,char c)//实现插入功能 { char* temp=new char[length+1]; for(int i=0; i<=length; i++) { if(i<p) temp[i]=s[i]; else if(i==p) { temp[i]=c; } else { temp[i]=s[i-1]; } } if(s!=NULL) delete s; s= new char[strlen(temp)+1]; length++; strcpy(s,temp); delete []temp; } int main() { char s1[9]="dutclass"; edit_String es(s1); es.print(); es.Delete(3); es.print(); es.Replace(1,'m'); es.print(); es.Insert(3,'w'); es.print(); return 0; }