1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Free_Kalibri [48]
3 years ago
10

Write a program that create Employee class with fields id,name and sal and create Employee object and store data and display tha

t data.

Computers and Technology
1 answer:
sashaice [31]3 years ago
5 0

Answer:

Here is the C++ program for Employee class with fields id,name and sal.

#include <iostream>  // to use input output functions

#include <string>  //to manipulate and use strings

using namespace std;   // to access objects like cin cout

class Employee {  //class Employee

private:  

/* the following data members are declared as private which means they can only be accessed by the functions within Employee class */

  string name;  //name field

  int id; //id field

  double sal;   //salary field

public:    

  Employee();  // constructor that initializes an object when it is created

/* setName, setID and setSalary are the mutators which are the methods used to change data members. This means they set the values of a private fields i.e. name, id and sal */

  void setName(string n)  //mutator for name field

     { name = n; }        

  void setId(int i)  //mutator for id field

     { id = i; }        

  void setSalary(double d)  //mutator for sal field

     { sal = d; }  

/* getName, getID and getSalary are the accessors which are the methods used to read data members. This means they get or access the values of a private fields i.e. name, id and sal */

  string getName()  //accessor for name field

     { return name; }        

  int getId()  //accessor for id field

     { return id; }        

  double getSalary()  //accessor for sal field

     { return sal; }  };  

Employee::Employee() {  //default constructor where the fields are initialized

  name = "";  // name field initialized

  id = 0;  // id field initialized to 0

  sal = 0;   }   // sal field initialized to 0

void display(Employee);  

// prototype of the method display() to display the data of Employee

int main() {  //start of the main() function body

  Employee emp;  //creates an object emp of Employee class

/*set the name field to Abc Xyz which means set the value of Employee class name field to Abc Xyz  through setName() method and object emp */

  emp.setName("Abc Xyz");  

/*set the id field to 1234 which means set the value of Employee class id field to 1234  through setId() method and object emp */

  emp.setId(1234);

/*set the sal field to 1000 which means set the value of Employee class sal field to 1000  through setSalary() method and object emp */

  emp.setSalary(1000);    

  display(emp);  }   //calls display() method to display the Employee data

void display(Employee e) {  // this method displays the data in the Employee //class object passed as a parameter.

/*displays the name of the Employee . This name is read or accessed through accessor method getName() and object e of Employee class */

  cout << "Name: " << e.getName() << endl;  

/*displays the id of the Employee . This id is read or accessed by accessor method getId() and object e */

  cout << "ID: " << e.getId() << endl;

/*displays the salary of the Employee . This sal field is read or accessed by accessor method getSalary() and object e */

  cout << "Salary: " << e.getSalary() << endl;  }

Explanation:

The program is well explained in the comments mentioned with each statement of the program.

The program has a class Employee which has private data members id, name and sal, a simple default constructor Employee(), mutatator methods setName, setId and setSalary to set the fields, acccessor method getName, getId and getSalary to get the fields values.

A function display( ) is used to display the Employee data i.e. name id and salary of Employee.

main() has an object emp of Employee class in order to use data fields and access functions defined in Employee class.

The output of the program is:

Name: Abc Xyz                                                                                                      

ID: 1234                                                                                                                  

Salary: 1000

The program and its output are attached.

You might be interested in
A year in the modern Gregorian Calendar consists of 365 days. In reality, the earth takes longer to rotate around the sun. To ac
denpristay [2]

Answer:

import java.util.Scanner;

public class LabProgram {

   public static void main(String[] args) {

       Scanner scnr = new Scanner(System.in);

       int inputYear;

       boolean isLeapYear;

       isLeapYear = false;

       inputYear = scnr.nextInt();

       // If a year is divisible by 400,  then it is a leap year

       if (inputYear % 400 == 0)

           isLeapYear = true;

       // If a year is divisible by 100,  then it is not a leap year

       if (inputYear % 100 == 0)

           isLeapYear = false;

       // If a year is divisible by 4,  then it is a leap year

       if (inputYear % 4 == 0)

           isLeapYear = true;

       if(isLeapYear)

           System.out.println(inputYear + " is a leap year.");

       else

           System.out.println(inputYear + " is not a leap year.");

   }

}

Explanation:

If a year is divisible by 400,  then set the boolean isLeapYear to true. If a year is divisible by 100,  then set the boolean isLeapYear to false. If a year is divisible by 4,  then set the boolean isLeapYear to true.

Check if isLeapYear is true, then print that it is a leap year. Otherwise, print that it is not a leap year.

Output:

1712

1712 is a leap year.

5 0
3 years ago
Read 2 more answers
I want to know why almost every single "expert answer verified" thing I come across is wrong. If it's wrong, why the h is it exp
Studentka2010 [4]

Answer:

Because people just give out the verified things to the first person to answer their question

Explanation:

3 0
3 years ago
Read 2 more answers
1. Provide Java code for a simple class of your choice. Be sure to include at least one constructor, two methods and two fields.
Ivanshal [37]

Answer:

Explanation:

public class Main

{

public static void main(String[] args) {

 System.out.println("Test Class:");

 Name name = new Name("Dayanand","Ghelaro");

 System.out.println("First Name : "+name.getFirstName());

 System.out.println("Last Name : "+name.getLastName());

}

}

class Name{

   private String firstName;   // first name field

   private String lastName;    // last name field

   public Name(String firstName, String lastName){

       this.firstName = firstName;

       this.lastName = lastName;

   }// end of constructor

   public String getFirstName(){

       return this.firstName;

   } // end of method

   public String getLastName(){

       return this.lastName;

   }// end of method

}// end of Name class

8 0
3 years ago
Which Boolean operator enables you to exclude a search term?
Lisa [10]

It is letter D. but i might be incorrect, please contact your teacher for further information. sorry if I got your answer wrong... :(

4 0
3 years ago
Read 2 more answers
When the computer is turned on, the operating system is loaded from the hard disk to the _________________. *​
Pavlova-9 [17]

Answer:

The main memory (ROM)

7 0
3 years ago
Other questions:
  • What is the portion of the PowerPoint window that contains the current slide, total slides, zoom options, and various shortcuts
    8·1 answer
  • The following function open_file() opens a file called 'example.txt' and returns the file pointer. This function is called withi
    12·1 answer
  • You open a link in a new tab by holding the _______ button while clicking the link
    8·1 answer
  • Calculate the data rate capacity for a 2400 baud signal where there are M=8 levels per symbol. a. 2400 bps b. 4800 bps c. 7200 b
    12·1 answer
  • Write a structured algorithm that prompts the user to input the name and price of an item and the quantity purchased. It should
    10·1 answer
  • What makes a computer a computer?​
    12·2 answers
  • Read the Python program below: num1 = int(input()) num2 = 10 + num1 * 2 print(num2) num1 = 20 print(num1) Question 1 When this p
    6·1 answer
  • _____ is confirming the significance of what another person is feeling.
    9·2 answers
  • Re:
    15·1 answer
  • onsider a file system on a disk that has both logical and physical block sizes of 512 bytes. assume that the information about e
    9·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!