Question

In: Computer Science

Write anoyher overloaded constructor for this class. Thr constructor should accept an argumrny for each field....

Write anoyher overloaded constructor for this class. Thr constructor should accept an argumrny for each field. C++ I need help thanks.

Solutions

Expert Solution

I have taken example of employee.

emp.h


#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;

class employee
{
private: // private data members
   int id;
   char name[20];
   int age;
   char gen[5];
   int sal;

public:
   employee(); //default constructor
   employee(int,const char*,int,const char*,int); //parameterized constructor
   employee(int,int,const char*,const char*,int); //overloaded parameterized constructor
   employee(int,int,int,const char*,const char*); //overloaded parameterized constructor

   void display(); //display function to display all attributes

};

emp.cpp

#include"emp.h"

employee:: employee()
{
this->id=0;
strcpy(this-> name,"\0");
this->age=0;
strcpy(this->gen,"\0");
this->sal=0;


}

employee::employee(int id,const char* name,int age,const char* gen,int sal)

{
this->id=id;
   strcpy(this-> name,name);
   this->age=age;
strcpy(this->gen,gen);
this->sal=sal;

}
employee::employee(int id,int age,const char* name,const char* gen,int sal)

{
this->id=id;
this->age=age;
   strcpy(this-> name,name);
   strcpy(this->gen,gen);
this->sal=sal;

}

employee::employee(int id,int age,int sal,const char* name,const char* gen)
{
this->id=id;
this->age=age;
this->sal=sal;
   strcpy(this-> name,name);
   strcpy(this->gen,gen);


}

void employee::display()
{
cout<<"id is: "<<id<<endl;
cout<<"name is:"<<name<<endl;
   cout<<"age is: "<<age<<endl;
   cout<<"gender is: "<<gen<<endl;
cout<<"salary is: "<<sal<<endl;

}

main.cpp

#include"emp.h"

int main()
{
   employee empl1(123,"vishal",24,"M",45000); //parameterized constructor
   cout<<"Costructor 1: "<<endl;
empl1.display();
cout<<endl;
   employee empl2(124,24,"mohit","M",55000); //overloaded constructor
   cout<<"Overloaded Costructor called : "<<endl;
   empl2.display();
cout<<endl;
   employee empl3(125,24,45000,"sanjana","F"); //overloaded constructor
   cout<<"Overloaded Costructor called : "<<endl;
   empl3.display();

   _getch();

   return 0;
}


======================OUTPUT==================

//Please upvote!!!


Related Solutions

In C++ Write a class named TestScores. The class constructor should accept an array of test...
In C++ Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a member function that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an exception. Demonstrate the class in program.
Write a class named TestScores. The class constructor should accept an array of test scores as...
Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program. Use TestScoresDemo.java to test your code public class TestScoresDemo { public static void main(String[] args) { // An array with test scores. //...
Write a class "car" with data fields "make" and "speed." The constructor should accept the "make"...
Write a class "car" with data fields "make" and "speed." The constructor should accept the "make" parameter. Be sure to use the "this" reference when setting the "make" parameter. The class should contain a static field for defaultSpeed set to 50. The class "car" should have a method "speed." The method should return the defaultSpeed. There should be an overloaded method for speed that takes a speed parameter. Finally, this class should take a getSpeed method that returns the speed....
Write a class named UpperCaseFile. The class's constructor should accept two file names as arguments. The...
Write a class named UpperCaseFile. The class's constructor should accept two file names as arguments. The first ofile should be opened for reading and the second file should be opened for writing. The class should read the contents of the first file, change all characters to uppercase, and store the results in the second file. The second file will be a copy of the first file, excpet all the characters will be uppercase. Use notepad or another text editor to...
Create a class called Cipher. Make the constructor accept some text and a key. Encrypt the...
Create a class called Cipher. Make the constructor accept some text and a key. Encrypt the given text using the key. Use the following cipher: Take the key and mod it by 26. Example: a key of 30 becomes 4. If the character is a letter, shift it by the key, but 'wrap around' the alphabet if necessary. If the character is not a letter, then shift it by the key but do not wrap. Check the test cases for...
Write a Java class called Person. The class should have the following fields: A field for...
Write a Java class called Person. The class should have the following fields: A field for the person’s name. A field for the person’s SSN. A field for the person’s taxable income. A (Boolean) field for the person’s marital status. The Person class should have a getter and setter for each field. The Person class should have a constructor that takes no parameters and sets the fields to the following values: The name field should be set to “unknown”. The...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor...
- Create a java class named SaveFile in which write the following: Constructor: The class's constructor should take the name of a file as an argument A method save (String line): This method should open the file defined by the constructor, save the string value of line at the end of the file, and then close the file. - In the same package create a new Java class and it DisplayFile in which write the following: Constructor: The class's constructor...
In Angel, you will find a class called Employee. This class should include a constructor which...
In Angel, you will find a class called Employee. This class should include a constructor which sets name to blanks and salary to $0.00 and a constructor which sets name to a starting name and salary to a set amount. Additionally, the class should include methods to set the name and salary and return the name and salary. Create another method to return the name and salary nicely formatted as a string (hint – research the toString method). You will...
In Angel, you will find a class called Employee. This class should include a constructor which...
In Angel, you will find a class called Employee. This class should include a constructor which sets name to blanks and salary to $0.00 and a constructor which sets name to a starting name and salary to a set amount. Additionally, the class should include methods to set the name and salary and return the name and salary. Create another method to return the name and salary nicely formatted as a string (hint – research the toString method). You will...
In Angel, you will find a class called Employee. This class should include a constructor which...
In Angel, you will find a class called Employee. This class should include a constructor which sets name to blanks and salary to $0.00 and a constructor which sets name to a starting name and salary to a set amount. Additionally, the class should include methods to set the name and salary and return the name and salary. Create another method to return the name and salary nicely formatted as a string (hint – research the toString method). You will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT