C++的探索路13继承与派生之练习篇(需重新学习)
Posted Guerrouj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++的探索路13继承与派生之练习篇(需重新学习)相关的知识,希望对你有一定的参考价值。
由程序结果填空
输出:4,6
填空:
class A
int val;
public:
A(int n)
val = n;
int GetVal()
return val;
;
class B :public A
private:
int val;
public:
B(int n) :____
int GetVal()
return val;
;
int main()
B b1(2);
cout << b1.GetVal() << "," << b1.A::GetVal() << endl;
return 0;
输出4,6则说明
b1.GetVal()输出4,而b1.A::GetVal()输出6
A类包含成员变量val,以及返回val值的成员函数GetVal()
B类继承A类的衣钵,并同样拥有GetVal函数返回val。
先看一下缺失部分:B类对A的初始化。
再看下输出:4,6;恰好为2的两倍与三倍。
因此填空可以填为:
B(int n) : A(3*n)
val = 2 * n;
写出程序的输出结果
class Base
public:
int val;
Base()
cout << "Base Constructor" << endl;
~Base()
cout << "Base Destructor" << endl;
;
class Base1 :virtual public Base ;
class Base2 :virtual public Base ;
class Derived:public Base1,public Base2;
int main()
Derived d;
return 0;
由于采用了虚继承,因此只会构造与析构一次。
输出结果应当为
Base Constructor
Base Destructor
重新编写MyString类
这是一道复合题
第一问来自于运算符重载:
编写一个MyString类,输出结果为:
1.abcd-efgh-abcd-
2.abcd-
3.
4.abcd-efgh-
5.efgh-
6.c
7.abcd-
8.ijAl-
9.ijAl-mnop
10.qrst-abcd
11.abcd-qrst-abcd-uvw xyz
about
big
me
take
abcd
qrst-abcd-
第二问是
MyString类必须从string类派生而来。
提示1,如果MyString由string替换,除了最后两行无法编译通过,其他都可以。也就是扩充体现在最后两条语句上面。
提示2,string类有一个成员函数string substr(int start,int length),能够求从start开始,长度为length的子串
不要问我为什么这么长,我也很绝望啊~~
程序如下
#include<cstring>
#include<cstdlib>
using namespace std;
int CompareString(const void*e1, const void*e2)
MyString*s1 = (MyString*)e1;
MyString*s2 = (MyString*)e2;
if (*s1 < *s2) return -1;
else if (*s1 == *s2)return 0;
else if (*s1 > *s2)return 1;
int main()
MyString s1("abcd-"), s2,
s3("efgh-"), s4(s1);
MyString SArray[4] =
"big","me","about","take"
;
cout << "1." << s1 << s2 << s3 << s4 << endl;
s4 = s3; s3 = s1 + s3;
cout << "2. " << s1 << endl;
cout << "3. " << s2 << endl;
cout << "4. " << s3 << endl;
cout << "5. " << s4 << endl;
cout << "6. " << s1[2] << endl;
s2 = s1;
s1 = "ijkl-";
s1[2] = 'A';
cout << "7. " << s2 << endl;
cout << "8. " << s1 << endl;
s1 += "mnop";
cout << "9. " << s1 << endl;
s4 = "qrst-" + s2;
cout << "10. " << s4 << endl;
s1 = s2 + s4 + "uvw" + "xyz";
cout << "11. " << s1 << endl;
qsort(SArray, 4, sizeof(MyString), CompareString);
for (int i = 0; i < 4; ++i)
cout << SArray[i] << endl;
cout << s1(0, 4) << endl;//输出s1从下标0开始长度为4的子串
cout << s1(5, 10) << endl;
return 0;
emmm。。。智商有点不够用了,直接贴答案,后续将对这部分进行再次练习
原文链接 程序设计实习MOOC / 继承和派生——编程作业 第五周程序填空题1
参考coding
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class MyString : public string
public:
MyString() ;
//1.0继承类继承父类所有的成员变量和成员函数,但不继承构造函数和析构函数
//1.1继承类的无参构造函数,会隐式调用父类的无参构造函数
MyString(const char * st) :string(st) ;
//1.2继承类的有参构造函数,如果父类也有有参构造函数,则必须显示调用它
//2.0这里的参数根据reference有两种选择,此处必须用const char*,"xxx"的类型是const char*
MyString(const MyString& s) :string(s)
//1.3继承类的复制构造函数必须要显示的调用父类的复制构造函数,不然就会默认调用父类的无参构造函数
MyString operator +(MyString & op2)
string s1 = *this;
string s2 = op2;
string s = s1 + s2;
return *new MyString(s.c_str());
MyString & operator +(const char * cs2)
string str1 = *this;
string s = str1 + cs2;
return *new MyString(s.c_str());
MyString & operator()(int s, int l)
string str = substr(s, l);
return *new MyString(str.c_str());
;
MyString operator+(const char * op1, MyString & op2)
string st2 = op2;
string s = op1 + st2;
return *new MyString(s.c_str());
int CompareString(const void * e1, const void * e2)
MyString * s1 = (MyString *)e1;
MyString * s2 = (MyString *)e2;
if (*s1 < *s2) return -1;
else if (*s1 == *s2) return 0;
else if (*s1 > *s2) return 1;
int main()
MyString s1("abcd-"), s2, s3("efgh-");
MyString s4(s1);
MyString SArray[4] = "big","me","about","take" ;
//这里等号右边的赋值操作相当于调用了MyString的转换构造函数,其实就是单一非const classname&参数的构造函数可以直接接受参数类型的变量
cout << "1. " << s1 << s2 << s3 << s4 << endl;
s4 = s3;
//3.0 operator=可以直接用string类里面的
s3 = s1 + s3;
s1 + s3;
cout << "2. " << s1 << endl;
cout << "3. " << s2 << endl;
cout << "4. " << s3 << endl;
cout << "5. " << s4 << endl;
cout << "6. " << s1[2] << endl;
s2 = s1;
s1 = "ijkl-";
s1[2] = 'A';
cout << "7. " << s2 << endl;
cout << "8. " << s1 << endl;
s1 += "mnop";
cout << "9. " << s1 << endl;
s4 = "qrst-" + s2;
cout << "10. " << s4 << endl;
s1 = s2 + s4 + " uvw " + "xyz";
cout << "11. " << s1 << endl;
qsort(SArray, 4, sizeof(MyString), CompareString);
for (int i = 0; i < 4; ++i)
cout << SArray[i] << endl;
cout << s1(0, 4) << endl;
cout << s1(5, 10) << endl;
return 0;
以上是关于C++的探索路13继承与派生之练习篇(需重新学习)的主要内容,如果未能解决你的问题,请参考以下文章