C++类继承:子类调用父类的构造函数
程序员文章站
2022-05-14 09:06:39
...
父类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)
{ .....
}
下一篇: c++ 子类父类 的构造函数调用顺序