C++ Define and implement the overloaded constructors that support the following
ID: 2246286 • Letter: C
Question
C++
Define and implement the overloaded constructors that support the following test function for a StudentEmployee class. The data members for the StudentEmployee class are:
• age - integer number
• name - String
• DSI - String
• salary - floating point number
int main()
{ //s1 will take all default value
StudentEmployee s1();
//s2 will take all supplied values
StudentEmployee s2 (50, "Jackie Chan", "D87654321", 10000000.99);
//s3 will take supplied age value, name, DSI and salary will take default values
StudentEmployee s3 (25);
//s4 will take supplied age and name value, DSI and salary will take default values
StudentEmployee s4 (35, "Jenny");
//s5 will take supplied age, name and DSI value, salary will take default value
StudentEmployee s5 (45, "Jack", "D11223344");
//s6 will take supplied age, name and salary value, DSI will take default value
StudentEmployee s6 (60, "James Bond", 65000.0);
//s7 will take the same value of s2
StudentEmployee s7=s2;
//the rest of the code
}
Explanation / Answer
#include<iostream>
#include<string>
using namespace std;
class StudentEmployee {
private:
int age;
string name;
string DSI;
double salary;
public:
StudentEmployee(int ag = 0, string nm = "", string dsi = "", double sal = 0.0);
StudentEmployee(int ag , string nm, double sal);
StudentEmployee(const StudentEmployee &s2);
void getData();
};
StudentEmployee::StudentEmployee(int ag, string nm , string dsi, double sal){
age = ag;
name = nm;
DSI = dsi;
salary = sal;
}
StudentEmployee::StudentEmployee(int ag, string nm , double sal){
age = ag;
name = nm;
DSI = "";
salary = sal;
}
StudentEmployee::StudentEmployee(const StudentEmployee &s2){
age = s2.age;
name = s2.name;
DSI = s2.DSI;
salary = s2.salary;
}
void StudentEmployee::getData(){
cout << age << " " << name << " " << DSI << " " << salary << endl;
}
int main()
{ //s1 will take all default value
StudentEmployee s1; // StudentEmployee s1() is an errornous statement so it has been modified with "()" removed.This will call constructow with all default values
s1.getData();
//s2 will take all supplied values
StudentEmployee s2 (50, "Jackie Chan", "D87654321", 10000000.99);
s2.getData();
//s3 will take supplied age value, name, DSI and salary will take default values
StudentEmployee s3 (25);
s3.getData();
//s4 will take supplied age and name value, DSI and salary will take default values
StudentEmployee s4 (35, "Jenny");
s4.getData();
//s5 will take supplied age, name and DSI value, salary will take default value
StudentEmployee s5 (45, "Jack", "D11223344");
s5.getData();
//s6 will take supplied age, name and salary value, DSI will take default value
StudentEmployee s6 (60, "James Bond", 65000.0);
s6.getData();
//s7 will take the same value of s2
StudentEmployee s7=s2;
s7.getData();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.