我正在尝试实现从简单到复杂的继承对象的层次结构,以这样的方式执行它,即对象具有尽可能多的面向对象的功能,但我填写说,这种努力可以通过多种方式得到改进。任何消化都是非常受欢迎的。特别是我不知道如何使用继承功能,这是一种安全的方法来解决以下问题:
此时我将展示代码:
#include <iostream>
#include <armadillo>
using namespace std;
using namespace arma;
class Contraction{
protected:
vector<double> zeta;
vector<double> c;
vec A;
public:
Contraction(){} /*contructor*/
Contraction(vector<double> Zeta,vector<double> C, vec a):zeta(Zeta),c(C),A(a){}
/*contructor*/
~Contraction(){} /*destructor*/
bool deepcopy(const Contraction& rhs) {
bool bResult = false;
if(&rhs != this) {
this->zeta=rhs.zeta;
this->c=rhs.c;
this->A=rhs.A;
bResult = true;
}
return bResult;
}
public:
Contraction(const Contraction& rhs) { deepcopy(rhs); }
Contraction& operator=(const Contraction& rhs) { deepcopy(rhs); return *this; }
};
class BasisFunction: public Contraction{
protected:
vector<int> n;
vector<int> l;
vector<int> m;
bool deepcopy(const BasisFunction& rhs) {
bool bResult = false;
if(&rhs != this) {
this->zeta=rhs.zeta;
this->c=rhs.c;
this->A=rhs.A;
this->n=rhs.n;
this->l=rhs.l;
this->m=rhs.m;
bResult = true;
}
return bResult;
}
public:
BasisFunction(){};/*How to define this constructor to initialize the inherited elements too?*/
~BasisFunction(){};
};
class Atom{
protected:
int Z;
vec R; ///Position
vec P; ///Momentum
vec F; ///Force
double mass;
vector<BasisFunction> basis;
public:
/*Here I need to define a function that uses the information in vectors c_i and zeta_i of the vector basis, how could it be achieved?*/
};
vector<Atom> Molecule; /*I nedd transform this in a singleton, how?*/
提前致谢。
答案 0 :(得分:0)
Contraction
的受保护字段成为公共字段,或者在Contraction
中创建Getter / Setter方法来访问此文件,或者重写所有类的层次结构,以使这些字段完全位于需要它们的位置。
class Molecule
{
public:
static Molecule & Instance() { static Molecule instance; return instance; }
const vector<Atom> & GetMyData() const { return data; }
vector<Atom> & GetMyData() { return data; }
private:
Molecule() {};
Molecule(const Molecule & rhs);
const Molecule & operator=(const Molecule & rhs);
vector<Atom> data;
};