| 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
|
That should be fairly simple. Start by creating a class that stores one
element with the appropriate components required.
i) class might be simply called Element with the following components:
a) name could be a std::string
b) atomic number could be an unsigned integer
c) atomic mass could be a double
Then determine what constructors and operators as well as member functions
you'll need. Then test it. If you cannot store a single Element, how the
hell are you going to store an entire periodic table?
#include <iostream>
#include <ostream>
#include <deque> // an STL container - double ended queue
#include <iterator> // for std:

stream_iterator
#include <algorithm> // for std::copy
class Element
{
std::string name; // why not symbol too
unsigned number;
double mass;
public:
// parametized ctor
Element(std::string s, unsigned n, double m)
: name(s), number(n), mass (m) { }
// copy ctor
Element(const Element& copy)
{
name = copy.name;
number = copy.number;
mass = copy.mass;
}
// d~tor
~Element() { }
// assignment operator
Element& operator=(const Element& rhv)
{
if(&rhv == this) return *this; // paranoia check
name = rhv.name;
number = rhv.number;
mass = rhv.mass;
return *this;
}
// friend operator<<
friend
std:

stream&
operator<<(std:

stream& os, const Element& e)
{
os << e.number << "\t";
os << e.name.c_str() << ", mass = ";
os << e.mass << std::endl;
return os;
}
}; // class Element
int main()
{
std::deque< Element > de;
de.push_back(Element("Hydrogen", 1, 1.00794)); // copies
de.push_back(Element("Helium", 2, 4.00260));
de.push_back(Element("Lithium", 3, 6.941));
std::copy( de.begin(),
de.end(),
std:

stream_iterator< Element >(std::cout) );
return 0;
}
/*
1 Hydrogen, mass = 1.00794
2 Helium, mass = 4.0026
3 Lithium, mass = 6.941
*/
Once that is achieved, what container would you want the periodic table to
be composed of? A std::map? How will you provide the dataset? a file stream?
etc... The possibilities are endless.
The step-by-step approach at developing this specific project (the periodic
table) is simple enough to help you gain the knowledge you seek and then
some.
If your knowledge of C++ is not quite at par: Consider researching a few
topics.
Topics:
a) encapsulation
b) constructors and initialisation lists
c) references
d) the rule of three
e) templates
f) streams (like std:

stream)
g) iterators
h) algorithms
i) sequential STL containers (vector, list, deque, etc)
j) associative STL containers (map, set, multimap, etc)
There is no need to delve into inheritance yet. At least not until some
smart-a** starts arguing that some elements are non-metallic, some noble
gasses, then metals and yet others radio-active. hmmm.