C++

An example of a constructor called by a subclass of the C++ class


An example of an C++ class subclass calling the constructor of a parent class

The parent class 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();

};

The constructor has two parameters,host and port. The subclass BmsNetUtil inherits it and encapsulates host/port inside. When the main program calls BmsNetUtil, it does not need to specify 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();
};

Implementation of BmsNetUtil constructor:

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

After the constructor, add: parent class construction statement.

If you have any questions, please leave a message or come to the site community to exchange discussion, thank you for reading, hope to help you, thank you for your support of the site!