C++ Parent Class with Abstracted Function that Uses Functions in a Child Class - class

I am trying to create a C++ parent class that has two functions, f1 and f2, to be implemented in the child class. This parent class has a function, abstractedFunction that abstracts how f1 and f2 should be used together. Both f1 and f2 are implemented in the child class as shown in the code below.
#include <iostream>
class Parent
{
public:
int f1(); // To be implemented in the derived class
void f2(int i); // To be implemented in the derived class
void abstractedFunction() { // Abstracted in the parant class
auto r = f1();
f2(r);
}
};
class Child : public Parent
{
public:
int f1() {
std::cout << "f1 is implemented in the child class\n";
return 1;
}
void f2(int i) {
std::cout << "f2 is implemented in the child class\n";
std::cout << "Return value for f1 = " << i << "\n";
}
};
int main() {
Child ch;
ch.abstractedFunction();
return 0;
}
Is such a concept implementable in C++?

Yes, You can do something like this. You need to make the functions defined in base class as pure virtual : Follow this link to know more about them and then you can create an object of derived class and assign it to the base pointer to make a required function call
#include <iostream>
using namespace std;
class Parent
{
public:
virtual int f1()=0; // To be implemented in the derived class
virtual void f2(int i)=0; // To be implemented in the derived class
void abstractedFunction() { // Abstracted in the parant class
auto r = f1();
f2(r);
}
};
class Child : public Parent
{
public:
int f1() {
std::cout << "f1 is implemented in the child class\n";
return 1;
}
void f2(int i) {
std::cout << "f2 is implemented in the child class\n";
std::cout << "Return value for f1 = " << i << "\n";
}
};
int main() {
Parent *ptr;
Child c;
ptr=&c;
ptr->abstractedFunction();
return 0;
}

Related

Why does vscode tell constructor defined outside class is inaccesible?

I have defined a constructor and then tried initializing an object but vscode tells me that the constructor is inaccessible. I don't understand what the problem is
this is my code
using namespace std;
#include<iostream>
class player{
// attributes
int xp{0};
string name;
int health{0};
float avg_score{0};
int tot{0};
int c{0};
// methods
void add_score(int score){
tot += score;
c++;
};
void display_avg_score(){
avg_score = tot/c;
cout << avg_score << endl;
};
void player_is_perfect(){
if((xp > 5) && (avg_score > 23)){
cout << "Perfect"<< endl;
}
};
// defining a constructor
player(int exp,float avg);
};
player::player(int exp,float avg){
xp = exp;
avg_score = avg;
};
int main(){
player frank{23,45.6};
};
As Raymond specified, classes have default access specifier "private". Anything you want to access outside the class should be preceded by the "public" statement like so
class player{
// attributes
int xp{0};
string name;
int health{0};
float avg_score{0};
int tot{0};
int c{0};
public:
// methods
void add_score(int score){
tot += score;
c++;
}
// rest of class
};

Classes in class in c++

I have this block of code below, and I cant find out what that class ContractB : public: ContractA means?
#include
using namespace std;
class ContractA
{
unsigned int ether = 0;
public:
ContractA(unsigned int e) :ether(e) {}
auto sendEther() { return ether; }
};
class ContractB : public ContractA
{
unsigned int wei = 1;
public:
ContractB(unsigned int w) :wei(w) {}
auto sendWei() { return wei; }
};
int main()
{
ContractB b(0);
cout << b.sendEther() << " " << b.sendWei();
return 0;
}
It represents inheritance. 'public' is the access specifier that limits the most accessible level for the members inherited from the base class (ContractA).
You can read more about it here.

Robust type caster for STL-vector-like classes

I have a class that is quite similar to an STL-vector (the differences are not important for the pybind11 type caster, so I will ignore them here). I have written a type caster for this class. A minimal working example of my code is given below. An example showing the problem is included below the code.
The problem is that my caster is quite limited (because I have used py::array_t). In principle the interface does accept tuples, lists, and numpy-arrays. However, when I overload based on typename, the interface fails for inputted tuples and lists (simply the first overload is selected even though it is the incorrect type).
My question is: How can I make the type caster more robust? Is there an effective way to re-use as much as possible existing type casters for STL-vector-like classes?
C++ code (including pybind11 interface)
#include <iostream>
#include <vector>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
namespace py = pybind11;
// class definition
// ----------------
template<typename T>
class Vector
{
private:
std::vector<T> mData;
public:
Vector(){};
Vector(size_t N) { mData.resize(N); };
auto data () { return mData.data (); };
auto data () const { return mData.data (); };
auto begin() { return mData.begin(); };
auto begin() const { return mData.begin(); };
auto end () { return mData.end (); };
auto end () const { return mData.end (); };
size_t size () const { return mData.size (); };
std::vector<size_t> shape() const { return std::vector<size_t>(1, mData.size()); }
std::vector<size_t> strides() const { return std::vector<size_t>(1, sizeof(T) ); }
template<typename It> static Vector<T> Copy(It first, It last) {
Vector out(last-first);
std::copy(first, last, out.begin());
return out;
}
};
// C++ functions: overload based on type
// -------------------------------------
Vector<int> foo(const Vector<int> &A){ std::cout << "int" << std::endl; return A; }
Vector<double> foo(const Vector<double> &A){ std::cout << "double" << std::endl; return A; }
// pybind11 type caster
// --------------------
namespace pybind11 {
namespace detail {
template<typename T> struct type_caster<Vector<T>>
{
public:
PYBIND11_TYPE_CASTER(Vector<T>, _("Vector<T>"));
bool load(py::handle src, bool convert)
{
if ( !convert && !py::array_t<T>::check_(src) ) return false;
auto buf = py::array_t<T, py::array::c_style | py::array::forcecast>::ensure(src);
if ( !buf ) return false;
auto rank = buf.ndim();
if ( rank != 1 ) return false;
value = Vector<T>::Copy(buf.data(), buf.data()+buf.size());
return true;
}
static py::handle cast(const Vector<T>& src, py::return_value_policy policy, py::handle parent)
{
py::array a(std::move(src.shape()), std::move(src.strides()), src.data());
return a.release();
}
};
}} // namespace pybind11::detail
// Python interface
// ----------------
PYBIND11_MODULE(example,m)
{
m.doc() = "pybind11 example plugin";
m.def("foo", py::overload_cast<const Vector<int > &>(&foo));
m.def("foo", py::overload_cast<const Vector<double> &>(&foo));
}
Example
import numpy as np
import example
print(example.foo((1,2,3)))
print(example.foo((1.5,2.5,3.5)))
print(example.foo(np.array([1,2,3])))
print(example.foo(np.array([1.5,2.5,3.5])))
Output:
int
[1 2 3]
int
[1 2 3]
int
[1 2 3]
double
[1.5 2.5 3.5]
A very easy solution is to specialise pybind11::detail::list_caster. The type caster now becomes as easy as
namespace pybind11 {
namespace detail {
template <typename Type> struct type_caster<Vector<Type>> : list_caster<Vector<Type>, Type> { };
}} // namespace pybind11::detail
Note that this does require Vector to have the methods:
clear()
push_back(const Type &value)
reserve(size_t n) (seems optional in testing)
Complete example
#include <iostream>
#include <vector>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>
namespace py = pybind11;
// class definition
// ----------------
template<typename T>
class Vector
{
private:
std::vector<T> mData;
public:
Vector(){};
Vector(size_t N) { mData.resize(N); };
auto data () { return mData.data (); };
auto data () const { return mData.data (); };
auto begin() { return mData.begin(); };
auto begin() const { return mData.begin(); };
auto end () { return mData.end (); };
auto end () const { return mData.end (); };
size_t size () const { return mData.size (); };
void push_back(const T &value) { mData.push_back(value); }
void clear() { mData.clear(); }
void reserve(size_t n) { mData.reserve(n); }
std::vector<size_t> shape() const { return std::vector<size_t>(1, mData.size()); }
std::vector<size_t> strides() const { return std::vector<size_t>(1, sizeof(T) ); }
template<typename It> static Vector<T> Copy(It first, It last) {
printf("Vector<T>::Copy %s\n", __PRETTY_FUNCTION__);
Vector out(last-first);
std::copy(first, last, out.begin());
return out;
}
};
// C++ functions: overload based on type
// -------------------------------------
Vector<int> foo(const Vector<int> &A){ std::cout << "int" << std::endl; return A; }
Vector<double> foo(const Vector<double> &A){ std::cout << "double" << std::endl; return A; }
// pybind11 type caster
// --------------------
namespace pybind11 {
namespace detail {
template <typename Type> struct type_caster<Vector<Type>> : list_caster<Vector<Type>, Type> { };
}} // namespace pybind11::detail
// Python interface
// ----------------
PYBIND11_MODULE(example,m)
{
m.doc() = "pybind11 example plugin";
m.def("foo", py::overload_cast<const Vector<double> &>(&foo));
m.def("foo", py::overload_cast<const Vector<int > &>(&foo));
}

Want to reset integers using virtual class function

The function I want works within the class butt won't apply to main. Must maintain the initial (Entity *entity = new Nummchange(flarb);)
#include <iostream>
using namespace std;
class Entity
{
public:
Entity(){}
~Entity(){}
virtual int reset(int NUMM) = NULL;
protected:
private:
};
class Nummchange : public Entity
{
public:
Nummchange(int NUMM);
~Nummchange();
int reset(int NUMM);
protected:
private:
int numm;
};
Nummchange::Nummchange(int NUMM)
{
}
Nummchange::~Nummchange()
{
}
int Nummchange::reset(int NUMM)
{
numm = 50;
NUMM = numm;
std::cout << "\nnumm+++++++"<< numm << "\n" << std::endl;
return numm;
}
int main()
{
int flarb = 50;
Entity *entity = new Nummchange(flarb);
while (flarb >= 0)
{
flarb--;
cout << flarb;
if(flarb == 0)
{
entity->reset(flarb);
std::cout << "flarb+++++++"<< flarb << "\n" << std::endl;
}
}
system("pause");
return 0;
}
Success is if the while loop continues perpetually.
int reset(int NUMM); method should take reference of integer type as below.
int reset(int &NUMM);
Make sure that you change the parameter to be reference of integer in all the three places of the method use in the program.

Could somebody override this callBack()?

Usually, callBack() gotta be overridden in Child class.
but it doesn't. When the thread calls callBack(), it runs original method.
Is there any way to right this?
I compiled it with "g++ -o file source.cpp -lpthread"
I'm sure it is not about a compiler.
#include <iostream>
#include <unistd.h>
#include <pthread.h>
using namespace std;
class Parent
{
public:
virtual void callBack()
{
cout << "Original callBack() reported this: " << this << endl;
}
private:
pthread_t th = 0;
static void *th_func(void *arg)
{
Parent *p = (Parent*)arg;
cout << "*th_func() reported *arg: " << arg << endl;
p->callBack();
}
public:
Parent()
{
if(pthread_create(&th, NULL, th_func, (void*)this) < 0)
cerr << "thread not born." << endl;
else
cout << "thread has born." << endl;
}
~Parent()
{
if(th!=0)
pthread_join(th, NULL);
cout << "joined. Parent leaving." << endl;
}
};
class Child : public Parent
{
public:
void callBack()
{
cout << "child overridden." << endl;
}
Child() : Parent(){}
};
int main()
{
Child *ch = new Child();
delete ch;
return 0;
}
The problem with your code is that you are calling the thread function from inside the parent constructor. At that point, the Child object is not constructed yet (look up object initialisation order in C++), thus the only virtual function that it can call is the parent's.
From a C++ point of view, it's doing the right thing :).
In order to get your code to work, you have to separate the thread creation from the object creation, otherwise you'll never be able to call a function in a derived class.
Here's some more info from the C++ FAQ. And here's what Scott Meyers has to say about this topic.