【发布时间】:2014-12-17 09:37:47
【问题描述】:
我正在尝试实现从简单到复杂的继承对象的层次结构,这样做的方式是使对象具有尽可能多的面向对象的功能,但我认为这项工作可以在很多方面得到改进。任何建议都非常受欢迎。特别是我不知道如何使用继承功能来安全地解决以下问题:
- 对象 Atom 包含一个对象向量 BasisFunction,该向量继承自更简单的对象 Contraction。但在 Atom 中,我需要访问每个 BasisFunction 的向量 zeta 和 C 中包含的信息。如何修改它以使其成为可能?
- 层次结构中的所有对象都可复制吗?如何引入面向对象的功能?
- 最后我想要一个分子单例。如何定义?
- 我关心这种实施方法的性能。哪里可以改进?
此时我将展示代码:
#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?*/
提前致谢。
【问题讨论】:
-
最好在codereview.stackexchange.com问这个问题
标签: c++ oop inheritance operator-overloading