定义一个字符串类CStrOne,包含一个存放字符串的数据成员,能够通过构造函数初始化字符串,通过成员函数显示字符串的内容;
在此基础上派生出CStrTwo类,增加一个存放字符串的数据成员,并能通过派生类的构造函数传递参数,初始化两个字符串;
在CStrTwo中定义一个成员函数进行两个字符串的合并以及输出。(字符串合并可使用标准库函数strcat,需要包含头文件string.h);
主要就是这个两个字符串的合并以及输出不会写
谢谢大家!
#include<bits/stdc++.h>
using namespace std;
class CStrOne{
protected:
char str[1024];
public:
CStrOne(const char *str_){strcpy(str,str_);}
~CStrOne();
void print(){
cout<<str<<endl;
}
};
class CStrTwo:public CStrOne{
private:
char str1[1024];
public:
CStrTwo(const char *str_,const char *str_1):CStrOne(str_)//这样叫初始化吗
{
strcpy(str1,str_1);
}
~CStrTwo();
void print(){
CStrOne::print();
cout<<str1<<endl;
}
void connect(const char *str,const char *str1){
strcat(str,str1);
}
};
int main(){
CStrTwo *pC = new CStrTwo("Hello","World");
pC->print();
pC->connect("Hello","world");
return 0;
}