posted:
Hi there,
I am looking to design a project using C++
The main objective of the project is to display details of periodic
table elements such as periodic element name, properties(such as atomic
number and atomic mass) for each periodic number entered by user. I am
thinking to input all the data regarding each periodic element number
in form of class.
Please give your opinion on the correct design method for the same.
Thanks
Nishant
Here's how I'd go about it:
First of all, the Periodic Table is composed of Elements, so it'd be handy
if we had an object type called "Element". What info do we need about an
element? Just the amount of protons in the nucleus of an atom of the
element, therefore:
class Element
{
private:
unsigned const amount_protons;
const char* const full_name;
const char* const short_name;
public:
Element( unsigned const a, const char* const b, const char* const c)
: protons_in_nucleus(a), full_name(b), short_name(c) {}
unsigned AtomicNumber() const { return amount_protons; }
};
From here, there's two alternative routes you can take. We could make
"Carbon" as follows:
Element carbon(6, "Carbon", "C");
In the above case, "carbon" is a single object. This would probably make
sense for your particular project. Also, you may want to specify the average
atomic weight of an atom of the element.
Or... if it were a more elaborate Chemistry program, we could make "Carbon"
a type, as follows:
class Carbon : public Element
{
public:
Carbon() : Element(6, "Carbon", "C") {}
};
From here, we could go on to define the concept of Carbon-12. There's
another two alternatives... we could make the Carbon-12 isotope as an
object:
Carbon carbon_twelve(6);
//Let's pretend its constructor takes the quantity of neutrons
Or, we could make it a class:
class Carbon_Twelve : public Carbon
{
//...
}
but before we got that far, we could make an "Isotope":
class Isotope : public Element
{
public:
unsigned GetQuantityNeutrons() const;
};
And then have:
class Carbon_Twelve : public Isotope
{
//...
};
And finally we can make an individual atom:
class Atom
{
private:
Element element;
unsigned amount_neutrons;
//...
};
And also go on to define an "Ion":
class Ion : public Atom
{
public:
//...
}
Best thing to do is sit down with a pencil and a lots of sheets of paper and
try to come up with some good relationships, meaningful reasons for deriving
"Atom" from "Ion", or vice versa.
There's more than one way to solve this problem -- you just have to find a
way you like.
-Tomás