C++类继承之子类调用父类的构造函数的实例详解

父类HttpUtil:

#pragma once

#include <windows.h>
#include <string>
using namespace std;
class HttpUtil
{
private:
  LPVOID hInternet;
  LPVOID hConnect;
  LPVOID hRequest;

protected:
 wchar_t * mHostName;
 short mPort;
 string sendRequest(WCHAR * pvHostName, short pvPort, WCHAR * pvUrl, WCHAR * pvMethod, char * pvPostData, int pvPostDataLen);

public:
  HttpUtil(wchar_t * pvHostName, short pvPort);
  ~HttpUtil();
  void reset();

};

构造函数有两个参数,host和port , 子类BmsNetUtil继承它, 并将host/port封装在里面, 主程序调用BmsNetUtil时无需再指定host/port参数 , 这时应该这样写:

#pragma once

#include <windows.h>
#include <string>
#include "HttpUtil.h"
using namespace std;

class BmsNetUtil :public HttpUtil
{
protected:
public:
  BmsNetUtil();
  ~BmsNetUtil();
  bool login();
};

BmsNetUtil 构造函数的实现:

BmsNetUtil::BmsNetUtil():HttpUtil(TEXT(C_SITE),C_PORT)
{ .....
}

在构造函数后面加上:父类的构造语句就可以了...

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

02-06 00:38