Calling a Base Class Construtor in a function of Derived Class - function

I am trying to call a constructor of a Base Class in a function of Derived Class. Here is the code:
Classes:
#pragma once
#include "CAR_TYRE_DOOR.h"
#include <string>
using namespace std;
//#ifndef 1_A_H_B
//#define 1_A_H_B
class Honda_Civic: public Car
{
private:
string CNG_y_n;
public:
Honda_Civic();
Honda_Civic(string CNG);
Honda_Civic(Honda_Civic& H1);
void set_CNG_y_n(string S);
string get_CNG_y_n();
void print();
};
class BMW: public Car
{
private:
string conv_y_n;
public:
BMW();
BMW(string S);
BMW(BMW& BMW1);
void set_conv_y_n(string S);
string get_conv_y_n();
void print();
};
class Mercedes: public Car
{
private:
int no_WS;
string SGR_y_n;
public:
Mercedes();
Mercedes(int no_WS, string SGR_y_n);
Mercedes(Mercedes& Merc);
//::Car( Merc1);
void set_no_WS(int n);
void set_SGR(string SGR);
int get_no_WS();
string get_SGR();
void print();
};
//#endif
The BaseClass functions:
//#include "BMW+MERC.h"
#include "CAR_TYRE_DOOR.h"
#include "Honda.h"
#include "S_R.h"
#include <iostream>
#include <string>
using namespace std;
void Car::set_color(string S)
{
S = this->color;
}
void Car::set_model(string S)
{
S = this->model;
}
void Car::set_cost(float x)
{
x = this->cost;
}
string Car::get_color()
{
return this->color;
}
string Car::get_model()
{
return this->model;
}
float Car::get_cost()
{
return this->cost;
}
Car::Car()
{
}
Car::Car(string color, string model, float cost)
{
this->color = "white";
this->model = "2011";
this->cost = 1000000;
}
Car::Car(Car& C1)
{
this->color = C1.color;
this->model = C1.model;
this->cost = C1.cost;
for(int i=0; i<4; i++)
{
DX[i] = C1.DX[i];
}
for(int i=0; i<4; i++)
{
TX[i] = C1.TX[i];
}
}
void Car::print_car()
{
cout <<"Car color: "<<get_color()<<endl;
cout <<"Car model: "<<get_model()<<endl;
cout <<"Car door color: "<<DX[0].get_color()<<endl;
cout <<"Car door vendor: "<<DX[0].get_vendor()<<endl;
cout <<"Tyre vendor: "<<TX[0].get_vendor()<<endl;
for(int i=0; i<4; i++)
{
cout <<"Tyre"<< i+1 <<"type: "<<TX[i].get_rubber_type()<<endl;
}
}
The Derived Class:
#include "Honda.h"
#include <iostream>
#include <string>
using namespace std;
Mercedes::Mercedes()
{
}
Mercedes::Mercedes(int no_WS, string SGR_y_n)
{
this->no_WS = 4;
this->SGR_y_n = "Yes";
}
Mercedes::Mercedes(Mercedes& Merc)
{
Mercedes::Car( Merc);
this->no_WS = Merc.no_WS;
this->SGR_y_n = Merc.SGR_y_n;
}
void Mercedes::set_no_WS(int n)
{
this->no_WS = n;
}
void Mercedes::set_SGR(string SGR)
{
this->SGR_y_n = SGR;
}
int Mercedes::get_no_WS()
{
return this->no_WS;
}
string Mercedes::get_SGR()
{
return this->SGR_y_n;
}
void Mercedes::print()
{
Mercedes.print_car();
cout <<"Number of Woofer Speakers: "<<get_no_WS()<<endl;
cout <<"Sunglass Roof: "<<get_SGR()<<endl;
}
Now in the copy constructor of the derivedclass, i am trying to call the copy constructor of the base class using:
Mercedes::Mercedes(Mercedes& Merc)
{
Mercedes::Car( Merc);
this->no_WS = Merc.no_WS;
this->SGR_y_n = Merc.SGR_y_n;
}
See this: Mercedes::Car( Merc);
to implement this, please tell me the syntax.

The proper way to call constructor hierarchies is like this:
class Car
{
public:
Car () { }
Car (cosnt Car &) {}
};
class Mercedes : public Car
{
public:
Mercedes () { }
Mercedes (const Mercedes &) {}
};
Mercedes :: Mercedes () : Car() { }
Mercedes :: Mercedes (const Mercedes &car) : Car(car) { }
A copy constructor looks like this (notice the const):
Class :: Class (const Class &)

Related

How to use boolean function inside class in Arduino?

I am making a library for the MPR121 sensor and in that I want to use a bool function inside a class in Arduino IDE but it is not working.
Here's my code.
#include <Wire.h>
#include "Adafruit_MPR121.h"
#ifndef _BV
#define _BV(bit) (1 << (bit))
#endif
Adafruit_MPR121 cap = Adafruit_MPR121();
uint16_t lasttouched = 0;
uint16_t currtouched = 0;
class PaperTron {
private:
uint8_t i;
uint8_t a;
public:
PaperTron(){
}
void touchBegin(){
Serial.begin(9600);
Serial.println("Hi! I'm PaperTron.");
if (!cap.begin(0x5A)) {
Serial.println("Error! TOUCH pins not found. Please check wiring.");
while (1);
}
Serial.println("Hurray! Now you can use my TOUCH pins.");
}
**//This is the boolean function which is not working**
bool isHigh(uint8_t touchpin) {
currtouched = cap.touched();
for (i=0; i<12; i++) {
if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
if(i==touchpin) return true;
}
if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) {
if(i==touchpin) return false;
}
}
lasttouched = currtouched;
}
};
PaperTron papertron;
void setup() {
papertron.Begin();
papertron.touchBegin();
}
void loop() {
if(papertron.isHigh(2)){Serial.println("Hello");}
}
The serial monitor is showing "Hello" continuously whereas it should print "Hello" only when I press the touch pin on MPR121.

Serialize and deserialize objects in C ++

I'm new to C ++, I can not find a detailed example of how to serialize an object in binary so I can save it to the mysql server and then be deserialized.
The object looks like this:
class house{
public:
int houseNumber;
bool isEmpty;
Dweller *dweller;
};
class Dweller{
public:
int phoneNumber;
bool man;
Backpack backpack;
};
class Backpack{
public:
int item1;
int item2;
int item3;
int item4;
float weight;
bool empty;
};
I have to serialize the house object, which owns the other objects.
I was able to solve the problem using:
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/vector.hpp>
See I used #include "boost/serialization/vector.hpp" because now the 'dweller' are in a vector to make things easier
Then the code stayed like this:
class house{
public:
int houseNumber;
bool isEmpty;
std::vector<Dweller> dweller;
private:
friend class boost::serialization::access;
template <class archive>
void serialize(archive &ar, const unsigned int version)
{
ar &houseNumber;
ar &isEmpty;
ar &dweller;
}
};
class Dweller{
public:
int phoneNumber;
bool man;
Backpack backpack;
private:
friend class boost::serialization::access;
template <class archive>
void serialize(archive &ar, const unsigned int version)
{
ar &phoneNumber;
ar &man;
ar &backpack;
}
};
class Backpack{
public:
int item1;
int item2;
int item3;
int item4;
float weight;
bool empty;
private:
friend class boost::serialization::access;
template <class archive>
void serialize(archive &ar, const unsigned int version)
{
ar &item1;
ar &item2;
ar &item3;
ar &item4;
ar &weight;
ar ∅
}
};
So to serialize I used:
#include <iostream>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/serialization.hpp>
#include <sstream>
int main()
{
Backpack _backpack;
_backpack.item1 = 0;
_backpack.item1 = 1;
_backpack.item1 = 2;
_backpack.item1 = 3;
_backpack.item1 = 4;
_backpack.weight = 3.21;
_backpack.empty = false;
Dweller _dweller1;
_dweller.phoneNumber = 1234567;
_dweller.man = false;
_dweller.backpack = _backpack;
Dweller _dweller2;
_dweller.phoneNumber = 7654321;
_dweller.man = true;
_dweller.backpack = _backpack;
std::vector<Dweller> _dwellers;
_dwellers.push_back(_dweller1);
_dwellers.push_back(_dweller2);
house _house;
_house.houseNumber = 4;
_house.isEmpty = false;
_house.dweller = _dwellers;
/*Serializing*/
std::stringstream ss;
boost::archive::binary_oarchive oa(ss);
oa << _house;
/*Deserializing*/
boost::archive::binary_iarchive ia(ss);
house _house2;
ia >> _house2;
}

Passing class function using function pointer to external library

I have a class that uses a preexisting library. There is a function call that needs a function pointer, and I am trying to pass in the function that is in my class. It doesn't compile though. What can I do to fix this? (Also, I'm sure this was asked before in a much clearer way. I'm out of my element with this, so my apologies).
Note: This is for an arduino.
In my main program I have the following code...
#include "CM.h"
CM cm;
void setup() {
cm.setup();
}
CM.h
class CM {
private:
LibClass *lib;
void onInit();
public:
void setup();
};
CM.cpp
#include "CM.h"
void CM::setup() {
lib->attach(onInit); // <-- this isn't working.
}
void CM::onInit() {
Serial.println("HERE I AM");
}
To pass a member function, you need to make it "static" and then pass it with a full scope qualifier:
#include <iostream>
void attach( void (*func)(void) );
class CM {
private:
static void onInit();
public:
void setup();
};
void CM::setup()
{
attach(CM::onInit);
}
void CM::onInit(void)
{
std::cout << "HERE I AM";
}
// a global function pointer for this example
void (*p_func)(void);
// a "library" attach function
void attach( void (*func)(void) )
{
p_func = func;
}
int main(int argc, const char * argv[]) {
CM my;
my.setup();
p_func(); // like the library call
return 0;
}

How is WaitForSingleObjectEx supposed to be implemented when using source voice callbacks with XAudio2 for Windows Runtime Components?

I am receiving a system error code x05 error_access_denied when I try to use the WaitForSingleObjectEx function to signal that an Xaudio2 source voice has finished processing all of the data in its buffer.
I want to use this as an event handler to know when to submit another buffer of audio data to the source voice, so it can continue playing until my stop() function/method is called.
How should the WaitForSingleObjectEx function be propely implemented so that I do not receive the error_access_denied system error code?
This is for a Windows Phone 8 C++/CX Windows Runtime Component...
TestTone.h file:
#pragma once
#include <client.h> /* ComPtr */
#include <winnt.h> /* HRESULT */
#include <xaudio2.h> /* IXAudio2 */
#include <ppltasks.h> /* task .then */
#include "XAudio2VoiceBufferCallback.h"
namespace PhoneDirect3DXamlAppComponent
{
public ref class TestTone sealed
{
public:
void Initialize();
void Play();
void Stop();
private:
Microsoft::WRL::ComPtr<IXAudio2> pXAudio2;
IXAudio2MasteringVoice *pMasteringVoice;
IXAudio2SourceVoice * pSourceVoice;
byte soundData[2*44100];
XAUDIO2_BUFFER buffer;
WAVEFORMATEX waveformat;
VoiceCallback *vcb;
void CreateSourceVoice();
void FillSoundArrayWithData();
void getSourceVoiceCallbacks(VoiceCallback *vcb,IXAudio2SourceVoice *pSourceVoice,XAUDIO2_BUFFER *buffer);
};
}
TestTone.cpp file:
// TestTone.cpp
#include "pch.h"
#include "TestTone.h"
#include "XAudio2VoiceBufferCallback.h"
#include <client.h> /* ComPtr */
#include <winnt.h> /* HRESULT */
#include <XAudio2.h> /* IXAudio2 */
#include <ppltasks.h> /* task .then */
#include <math.h> /* sine */
#define PI 3.14159265
using namespace PhoneDirect3DXamlAppComponent;
using namespace concurrency; // tasks
void TestTone::Initialize()
{
XAudio2Create(&pXAudio2,0,XAUDIO2_DEFAULT_PROCESSOR);
pXAudio2->CreateMasteringVoice(&pMasteringVoice);
vcb = new VoiceCallback();
CreateSourceVoice();
buffer.AudioBytes = 2 * 44100;
buffer.pAudioData = soundData;
buffer.PlayBegin = 0;
buffer.PlayLength = 44100;
}
void TestTone::Play()
{
pSourceVoice->Start();
FillSoundArrayWithData();
buffer.pAudioData = soundData;
pSourceVoice->SubmitSourceBuffer(&buffer);
getSourceVoiceCallbacks(vcb, pSourceVoice, &buffer);
}
void TestTone::Stop()
{
pSourceVoice->Stop();
CreateSourceVoice();
}
void TestTone::CreateSourceVoice()
{
waveformat.wFormatTag = WAVE_FORMAT_PCM;
waveformat.nChannels = 1;
waveformat.nSamplesPerSec = 44100;
waveformat.nAvgBytesPerSec = waveformat.nSamplesPerSec * waveformat.nChannels;
waveformat.nBlockAlign = waveformat.nChannels;
waveformat.wBitsPerSample = 8;
waveformat.cbSize = 0;
hrXAudio2Create = pXAudio2->CreateSourceVoice(&pSourceVoice,&waveformat,0,XAUDIO2_DEFAULT_FREQ_RATIO,vcb,NULL,NULL);
}
void TestTone::FillSoundArrayWithData()
{
for (int index = 0, second = 0; second < 1; second++)
{
for (int sample = 0; sample < waveformat.nSamplesPerSec; sample++)
{
soundData[index++] = 128+(127*sin((500*sample*PI)/(waveformat.nSamplesPerSec)));
}
}
}
void TestTone::getSourceVoiceCallbacks(VoiceCallback *vcb,IXAudio2SourceVoice *pSourceVoice,XAUDIO2_BUFFER *buffer)
{
DWORD dw = NULL;
dw = WaitForSingleObjectEx( vcb->hBufferEndEvent,0,//INFINITE,TRUE );
DWORD err = NULL;
err = GetLastError();
while (WAIT_OBJECT_0 == (WaitForSingleObjectEx(vcb->hBufferEndEvent,INFINITE,TRUE)))
{
pSourceVoice->SubmitSourceBuffer(buffer);
}
return;
}
XAudio2VoiceBufferCallback.h file:
#pragma once
#include <client.h> /* ComPtr */
#include <winnt.h> /* HRESULT */
#include <xaudio2.h> /* IXAudio2 */
namespace PhoneDirect3DXamlAppComponent
{
class VoiceCallback : public IXAudio2VoiceCallback
{
public:
HANDLE hBufferEndEvent;
VoiceCallback();//: hBufferEndEvent( CreateEventEx( NULL, FALSE, FALSE, NULL ) ){};
~VoiceCallback();//{ CloseHandle( hBufferEndEvent ); }
//Called when the voice has just finished playing a contiguous audio stream.
virtual void __stdcall OnStreamEnd(); //{ SetEvent( hBufferEndEvent ); }
//Unused methods are stubs
void __stdcall OnVoiceProcessingPassEnd();
void __stdcall OnVoiceProcessingPassStart(UINT32 SamplesRequired);
void __stdcall OnBufferEnd(void * pBufferContext);
void __stdcall OnBufferStart(void * pBufferContext);
void __stdcall OnLoopEnd(void * pBufferContext);
void __stdcall OnVoiceError(void * pBufferContext, HRESULT Error);
};
}
XAudio2VoiceBufferCallback.cpp file:
// XAudio2VoiceBufferCallback.cpp
#include "pch.h"
#include "XAudio2VoiceBufferCallback.h"
#include <client.h> /* ComPtr */
#include <winnt.h> /* HRESULT */
#include <XAudio2.h> /* IXAudio2 */
using namespace PhoneDirect3DXamlAppComponent;
VoiceCallback::VoiceCallback()//:hBufferEndEvent( CreateEventEx( NULL, FALSE, FALSE, NULL ) )
{
hBufferEndEvent = (CreateEventEx(NULL,FALSE,FALSE,NULL));
}
VoiceCallback::~VoiceCallback()
{
CloseHandle( hBufferEndEvent );
}
void VoiceCallback::OnStreamEnd()
{
SetEvent( hBufferEndEvent );
}
void VoiceCallback::OnVoiceProcessingPassEnd() { }
void VoiceCallback::OnVoiceProcessingPassStart(UINT32 SamplesRequired) { }
void VoiceCallback::OnBufferEnd(void * pBufferContext) { }
void VoiceCallback::OnBufferStart(void * pBufferContext) { }
void VoiceCallback::OnLoopEnd(void * pBufferContext) { }
void VoiceCallback::OnVoiceError(void * pBufferContext, HRESULT Error) { }

call an object's member function in std::algorithms (note: this is a different object, not in containers)

My code:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
class myClass {
public:
myClass() {
init();
}
void init() {
_myVector.push_back("Hello");
_myVector.push_back("World");
_myVector.push_back("Bye!");
for_each (_myVector.begin(), _myVector.end(), &myClass::print);
}
void print(string &myStr) {
cout << myStr << "." << endl;
}
private:
vector<string> _myVector;
};
int main() {
myClass myObj;
return 0;
}
If _myVector contained myClass objects or pointers, I could use std::mem_fun_ref or std::mem_fun. Is there any way to do the above? And yes, I do not want myClass::print to be static.
for_each (_myVector.begin(), _myVector.end(), &myClass::print);
to be replaced with
for_each (_myVector.begin(), _myVector.end(), bind1st(mem_fun(&myClass::print), this));