By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,631 Members | 892 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,631 IT Pros & Developers. It's quick & easy.

Design of C++ Periodic table project

P: n/a
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

Mar 16 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi,

I thiink you already made your design yourself:

You need a map from periodic number to element class so a stl map from
unsigned long to a class 'Element'

CElement
{
private:
// Your element data
public
// Bunch of get and set functions
Get();
};
std::map<unsigned long, CElement> periodiclist;

Or

CPeriodicNumber
{
...
operator <( const CPeriodicNumber& PeriodicNumber )const;
{
//...
}
};

std::map<CPeriodicNumber, CElement> PeriodicList;

Fill in the details,et voila.
Regards, Ron AF Greve

http://moonlit.xs4all.nl

<ni*******@rediffmail.com> wrote in message
news:11*********************@e56g2000cwe.googlegro ups.com...
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

Mar 17 '06 #2

P: n/a

<ni*******@rediffmail.com> wrote in message
news:11*********************@e56g2000cwe.googlegro ups.com...
| 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::ostream_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::ostream&
operator<<(std::ostream& 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::ostream_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::ostream)
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.

Mar 17 '06 #3

P: n/a

"Peter_Julian" <pj@antispam.codigo.ca> wrote in message
news:Rb*******************@news20.bellglobal.com.. .
|
| <ni*******@rediffmail.com> wrote in message
| news:11*********************@e56g2000cwe.googlegro ups.com...
| | 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 <string> // duh !!

| #include <deque> // an STL container - double ended queue
| #include <iterator> // for std::ostream_iterator
| #include <algorithm> // for std::copy
|
<snip>

Mar 17 '06 #4

P: n/a
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

Mar 17 '06 #5

P: n/a
ni*******@rediffmail.com wrote:
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


Whatever you do with the rest of the program, I definitely recommend
NOT to hardcode the numerical values (of the elements in the periodic
table) in your code. If you store all of those values in a seperate
file, you will be able to edit/append things easily without recompiling
the code all the time. You just have to make sure that that file is
not tampered with by the user.

Mahurshi Akilla

Mar 17 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.