Creating class object c++ in if-statement - class

I actually have a small question. I want to create an attribute "function" which should be from the class function1, function2 or function3. Is there a way I can do that?
Here is the code:
double Uppersum::evalIntegral(double p_) {
if (functiontype == FUNKTION1){
Function1 function;
}
else if (functiontype == FUNKTION2) {
Function2 function;
}
else if (functiontype == FUNKTION3){
Function3 function;
}
function.setParameterP(p_);
double increment_h = (boundary_b - boundary_a)/num_subintervalls_m;
double sum = 0;
for (int index_i = 0; index_i < num_subintervalls_m -1; index_i++){
double x_1 = index_i * increment_h;
double x_2 = (index_i+1) * increment_h;
double y_1, y_2;
y_1 = function.evalFunctionValue(x_1);
y_2 = function.evalFunctionValue(x_2);
sum += increment_h * std::max(y_1, y_2);
}
}
class Function {
protected:
double parameter_p;
public:
void setParameterP(double p_);
virtual double evalFunctionValue(double x_)=0;
};
class Function1 : public Function {
public:
double evalFunctionValue(double x_);
};

Why not use inheritance, superclass has the virtual functions setParameterP and evalFunctionValue. and in subclasses, override those virtual functions.
here is the test code:
test.cpp
#include <iostream>
typedef enum{
FUNCTION1,
FUNCTION2,
FUNCTION3
}FunctionType;
using namespace std;
class super
{
public:
super(){}
~super(){}
virtual void setParameterP() = 0;
virtual void evalFunctionValue() = 0;
};
class func1:public super
{
public:
func1(){}
virtual void setParameterP(){cout<<"call setParameterP In func1"<<endl;}
virtual void evalFunctionValue(){cout<<"call evalFunctionValue In func1"<<endl;}
};
class func2:public super
{
public:
func2(){}
virtual void setParameterP(){cout<<"call setParameterP In func2"<<endl;}
virtual void evalFunctionValue(){cout<<"call evalFunctionValue In func2"<<endl;}
};
class func3:public super
{
public:
func3(){}
virtual void setParameterP(){cout<<"call setParameterP In func3"<<endl;}
virtual void evalFunctionValue(){cout<<"call evalFunctionValue In func3"<<endl;}
};
class FuncFactory
{
public:
static super* create(FunctionType var)
{
super* ret = nullptr;
switch (var)
{
case FUNCTION1:
ret = new func1();
break;
case FUNCTION2:
ret = new func2();
break;
case FUNCTION3:
ret = new func3();
break;
default:
cout <<"invalid FunctionType" << endl;
}
return ret;
}
};
int main(int argc, char** argv)
{
super* pFunc = FuncFactory::create(FUNCTION1);
pFunc->setParameterP();
pFunc->evalFunctionValue();
delete pFunc;
pFunc = FuncFactory::create(FUNCTION2);
pFunc->setParameterP();
pFunc->evalFunctionValue();
delete pFunc;
pFunc = FuncFactory::create(FUNCTION3);
pFunc->setParameterP();
pFunc->evalFunctionValue();
delete pFunc;
return 0;
}
here is the process result:
result

Related

How to define a static private method outside the class in c++

#include <iostream>
class BST {
private:
struct Bst_node
{
int data;
struct Bst_node* left;
struct Bst_node* right;
}*root;
// static Bst_node* get_inorder_predecessor(Bst_node* ptr);
static BST::Bst_node* get_inorder_predecessor(Bst_node* ptr)
{
ptr = ptr->left;
if (ptr == NULL) return NULL;
while (ptr->right != NULL)
{
ptr = ptr->right;
}
return ptr;
}
public:
void create()
{
root = new Bst_node;
root->data = 8;
root->right = NULL;
root->left = new Bst_node;
root->left->data = 10;
root->left->left = NULL;
root->left->right = NULL;
}
void print()
{
Bst_node * ptr = get_inorder_predecessor(this->root);
if(ptr == NULL) std::cout<<"NULL\n";
else std::cout<<ptr->data<<std::endl;
}
};
int main()
{
BST obj;
obj.create();
obj.print();
return 0;
}
The above code is working fine but when i define 'get_inorder_predecessor()' function outside the class it gives error see the below code.
#include <iostream>
class BST {
private:
struct Bst_node
{
int data;
struct Bst_node* left;
struct Bst_node* right;
}*root;
static Bst_node* get_inorder_predecessor(Bst_node* ptr);
public:
void create()
{`
root = new Bst_node;
root->data = 8;
root->right = NULL;
root->left = new Bst_node;
root->left->data = 10;
root->left->left = NULL;
root->left->right = NULL;
}
void print()
{
Bst_node * ptr = get_inorder_predecessor(this->root);
if(ptr == NULL) std::cout<<"NULL\n";
else std::cout<<ptr->data<<std::endl;
}
};
static BST::Bst_node* get_inorder_predecessor(Bst_node* ptr)
{
ptr = ptr->left;
if (ptr == NULL) return NULL;
while (ptr->right != NULL)
{
ptr = ptr->right;
}
return ptr;
}
int main()
{
BST obj;
obj.create();
obj.print();
return 0;
}
See the ERRORS:
'''binary.cpp:33:13: error: 'struct BST::Bst_node' is private within this context
static BST::Bst_node* get_inorder_predecessor(Bst_node* ptr)
^~~~~~~~
binary.cpp:4:12: note: declared private here
struct Bst_node
^~~~~~~~
binary.cpp:33:47: error: 'Bst_node' was not declared in this scope
static BST::Bst_node* get_inorder_predecessor(Bst_node* ptr)
^~~~~~~~
binary.cpp:33:57: error: 'ptr' was not declared in this scope
static BST::Bst_node* get_inorder_predecessor(Bst_node* ptr)
^~~
'''

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.

Member function doesn't see the private member of the class

Member function (reverseString()) has to turn the string but when I create and try to initialize a new pointer in the member function (char *temporary = stringo;), it aparently doesn't see the member of the class (That's not the whole code though).
#include"Header.h"
#include<iostream>
using namespace std;
Stringfuncs::Stringfuncs(char *string)
{
this->stringo = string;
}
int Stringfuncs::lengthString()
{
int counter = 0;
for (int i = 0; stringo[i]; i++) { counter++; }
return counter;
}
char Stringfuncs::*reverseString() //this function doesn't see the member of the class
{
char *temporary = stringo; //identifier "stringo" isn't defined
}

Replacing dynamic polymorphy with static polymorphy in C++

Motivated due to the fact, that the avr-g++ places the vtables in RAM, I wrote a replacement using static polymorphy.
Consider the following example:
volatile uint8_t x;
struct IX {
virtual void f() const = 0;
// virtual ~IX() = default; // need delete
};
struct A : public IX {
const uint8_t v = 0;
void f() const override {
x = v;
}
};
struct B : public IX {
const uint8_t v = 1;
void f() const override {
x = v;
}
};
struct C : public IX {
const uint8_t v = 2;
void f() const override {
x = v;
}
};
volatile uint8_t index = 2;
int main() {
A a;
B b;
C c;
const std::array<const IX*, 3> cc{&a, &b, &c};
cc[index]->f();
while(true) {}
}
Here we have some types A, B and C implementing an interface IX and placing pointers in the array cc. Then we call the virtual function f() for a specific instance. (Using this on a small µC like the AVRs, there is a "waste" of RAM, since the vtables are placed in RAM and each object contains a vptr, and a performance penalty due to the indirect call of f().
So I looked for an alternative solution in this case: the simplest way is to use an heterogenous container like std::tuple and write a switch-statement:
const std::tuple<A, B, C> t;
auto f = [](const auto& v) {
v.f();
};
switch (index) {
case 0:
f(std::get<0>(t));
break;
case 1:
f(std::get<1>(t));
break;
case 2:
f(std::get<2>(t));
break;
default:
assert(false);
break;
}
This yields to optimale machine-code but it is an unflexible solution. So I wrote a metafunction to call f() for a specific element of the tuple:
const std::tuple<A, B, C> t;
Meta::visitAt(t, index, [](const auto& v){v.f();});
And the implementation looks like:
namespace Meta {
namespace detail {
template<uint8_t N>
struct visit {
template<typename T, typename F>
static void at(T& tuple, uint8_t index, const F& f) {
if (index == (N - 1)) {
f(std::get<N - 1>(tuple));
}
else {
visit<N - 1>::at(tuple, index, f);
}
}
};
template<>
struct visit<0> {
template<typename T, typename F>
static void at(T&, uint8_t , const F&) {
assert(false);
}
};
template<typename T, typename F, size_t... I>
void all(const T& tuple, const F& f, std::index_sequence<I...>) {
(f(std::get<I>(tuple)), ...);
}
}
template<typename... T, typename F>
void visitAt(const std::tuple<T...>& tuple, uint8_t index, const F& f) {
detail::visit<sizeof...(T)>::at(tuple, index, f);
}
template<typename... T, typename F>
void visitAt(std::tuple<T...>& tuple, uint8_t index, const F& f) {
detail::visit<sizeof...(T)>::at(tuple, index, f);
}
template<typename... T, typename F>
void visit(const std::tuple<T...>& tuple, const F& f) {
detail::all(tuple, f, std::make_index_sequence<sizeof...(T)>{});
}
}
This works very well in my scenarios, yet is obviously limited to static containers (like std::tuple). There is also a for-each-like iteration Meta::visit().
My question is: are there any other drawbacks / limitations with this approach?
Are there any improvements?

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.