Question

Using C++ Complete the class below that stores information about chemical elements. Your class should implement...

Using C++

Complete the class below that stores information about chemical elements. Your class should implement 5 functions:

  1. void SetName( string n ); that initializes the name data member and sets it equal to n (completed for you)
  2. void SetAtomicWeight( double z ); that initializes the atomicWeight data member and sets it equal to Z
  3. void SetValenceElectrons( int e ); that initializes the valenceElectrons data member and sets it equal to e
  4. void SetSymbol( string s ); that initializes the symbol data member and sets it equal to s
  5. void PrintElement(); that prints out the data stored in the object in the following manner given the information for the chemical element of oxygen:

Output example: (No space between each line)

Name: Oxygen

Symbol: O

Atomic weight: 15.999

# valence electrons: 6

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hello, here is the completed code you wanted. Every important statement is explained using comments. Please check and let me know if you have any doubts. Thanks.

Class name is not specified in the question. I have given it as "Element". If student want to change it please specify it in the comment.

CODE

#include<iostream>

using namespace std;

//class Element
//name for class is not specified in the question
//if student want to change the name please comment
class Element
{
   //private section
   private:
       //variable to store name of element
       string name;
      
       //variable to store atomic weight
       double atomicWeight;
      
       //variable to store valence electrons
       int valenceElectrons;
      
       //variable to store symbol
       string symbol;
      
   //public section
   public:
       //setter for set name data member
       void SetName(string n)
       {
           //sets it equal to n
           name = n;
       }
       //setter for set atomicWeight data member
       void SetAtomicWeight(double z)
       {
           //sets it equal to z
           atomicWeight = z;
       }
       //setter for set valenceElectrons data member
       void SetValenceElectrons(int e)
       {
           //sets it equal to e
           valenceElectrons = e;
       }
       //setter for set symbol data member
       void SetSymbol(string s)
       {
           //sets it equal to s
           symbol = s;
       }
       //prints out the data stored in the object
       void PrintElement()
       {
           //printing information about chemical element
           cout << "Name: " << name << "\n";
           cout << "Symbol: " << symbol << "\n";
           cout << "Atomic weight: " << atomicWeight << "\n";
           cout << "# valence electrons: : " << valenceElectrons;
       }
};
//main function
int main()
{
   //creating object for class Element
   Element obj;
  
   //calling SetName
   obj.SetName("Oxygen");
  
   //calling SetSymbol
   obj.SetSymbol("O");
  
   //calling SetAtomicWeight
   obj.SetAtomicWeight(15.999);
  
   //calling SetValenceElectrons
   obj.SetValenceElectrons(6);
  
   //calling PrintElement
   obj.PrintElement();
}

OUTPUT

CODE SCREEN SHOT

Add a comment
Know the answer?
Add Answer to:
Using C++ Complete the class below that stores information about chemical elements. Your class should implement...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT