继承派生类的构造函数+继承string(POJ C++ 第5周)

子类可以继承父类所有的成员变量和成员方法,但不继承父类的构造方法。因此,在创建子类对象时,为了初始化从父类继承来的数据成员,系统需要调用其父类的构造方法。

构造原则如下:

    1. 如果子类没有定义构造方法,则调用父类的无参数的构造方法。

   2. 如果子类定义了构造方法,不论是无参数还是带参数,在创建子类的对象的时候,首先执行父类无参数的构造方法,然后执行自己的构造方法

#include <iostream.h>

  class animal
  {
  public:
    animal(int height, int weight)
    {
      cout<<"animal construct"<<endl;
    }
    …
  };

  class fish:public animal
  {
  public:
    fish():animal(400,300)
    {
      cout<<"fish construct"<<endl;
    }
    …
  };
  void main()
  {
    fish fh;
  }

在fish类的构造函数后,加一个冒号(:),然后加上父类的带参数的构造函数。

这样,在子类的构造函数被调用时,系统就会去调用父类的带参数的构造函数去构造对象。

3w6描述

下面程序输出的结果是:

4,6

#include <iostream>
using namespace std;

class A 
{
	int val;
public:
	A(int n) { val = n; }//构造函数
	int GetVal() { return val; }//成员函数
};

class B : public A //B继承A
{
private:
	int val;
public:
	 //填充代码
         //构造函数初始值列表
         //继承类的构造函数,先初始化基类部分再初始化派生类成员
	B(int n) : A(n+4), val(n+2) {}	
        int GetVal() { return val; }//成员函数
};
int main() 
{
	B b1(2);
	cout << b1.GetVal() << "," << b1.A::GetVal() << endl;
	return 0;
}

5W5

描述

写一个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类必须是从C++的标准类string类派生而来。提示1:如果将程序中所有 "MyString" 用"string" 替换,那么题目的程序中除了最后两条语句编译无法通过外,其他语句都没有问题,而且输出和前面给的结果吻合。也就是说,MyString类对 string类的功能扩充只体现在最后两条语句上面。提示2: string类有一个成员函数 string substr(int start,int length); 能够求从 start位置开始,长度为length的子串

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
using namespace std;
// 在此处补充你的代码
class MyString:public string
{
public:
	//默认构造函数
	MyString():string(){}
	//初始化参数构造函数
	MyString(const char* a):string(a){}
	//复制构造函数
	MyString(const MyString& a):string(a){}
	//类型转换构造函数,
	//!!!把string对象转换成MyString 使得string 的+和最后的()可以用
	MyString(const string& a) :string(a){}
	//重载函数调用运算符operator()
	MyString operator()(int start, int length)
	{
		return substr(start, length);
	}

};

//全局函数
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);//3个构造函数
	MyString SArray[4] = { "big", "me", "about", "take" };//数组初始化
	cout << "1. " << s1 << s2 << s3 << s4 << endl;

	s4 = s3;   //重载赋值运算符需要吗?
	s3 = s1 + s3;//两个string可以相加
	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;
	//输出s1从下标0开始长度为4的子串
	cout << s1(0, 4) << endl;
	//输出s1从下标为5开始长度为10的子串
	cout << s1(5, 10) << endl;
	return 0;
}

    //类型转换构造函数,
    //!!!把string对象转换成MyString 使得string 的+和最后的()可以用
    MyString(const string& a) :string(a) { }

 s3 = s1 + s3;  //s3是MyString类型,等号右边相加后为string,因为+ 号是string的成员函数。父类的对象是无法赋值到子类上去

猜你喜欢

转载自blog.csdn.net/try_again_later/article/details/81214894