#include <iostream> #include <iomanip> using namespace std; /* a class named Employee */ class Employee{ private : int empID; /* Employee ID. */ string empName; /* Employee Name */ string dept; /* Department */ float sal; /* Employee Salary */ public: static int emp_num; /* Static data member that hold value of total no. of employees */ Employee(){ /*Default constructor for Employee class*/ empID = 0; empName = ""; dept = ""; sal = 0.0; emp_num++; } Employee(int id, string name, string d, double s){ /*Parametrized constructor for Employee class*/ empID = id; empName = name; dept = d; sal = s; emp_num++; } Employee(Employee &e){ /*Copy constructor */ empID = e.empID; empName = e.empName; dept = e.dept; sal = e.sal; emp_num++; } /*Setter functions*/ void setID(int id) { empID = id; } void setName(string n) { empName = n; } void setDept(string d) { dept = d; } void setSal(float s) { sal = s; } /*Getter functions*/ int getID() { return empID; } string getName() { return empName; } string getDept() { return dept; } float getSal() { return sal; } /* function to calculate the net pay of an employee based on his salary*/ float calcNetPay(){ float net_pay; if(sal<=10000) net_pay=sal; else if(sal>10000 && sal<=20000) net_pay=sal-sal*0.05; else if(sal>20000 && sal<=30000) net_pay=sal-sal*0.07; else net_pay=sal-sal*0.1; return net_pay; } /*function to display the values of all data members of class*/ void display(){ cout<<empID<<"\t"<<empName<<"\t"<<dept<<"\t"<<calcNetPay()<<endl; } ~Employee(){ /*Destructor of Employee class*/ } }; int Employee::emp_num = 0; int main() { Employee emp[5]; /*create an array of 5 objects of class Employee*/ emp[0]=Employee (5, "Ahmad", "Accounts", 20000 ); emp[1]=emp[0]; emp[2]=Employee (10, "Ayesha", "Accounts", 25000); emp[3]=Employee (3, "Hassan", "Administration", 10000); emp[4]=Employee (6, "Arsalan", "Administration", 35000); cout<<"ID"<<"\t"<<"Name"<<"\t"<<"Department"<<"\t"<<"NetPay"<<endl; for(int i=0;i<5;i++){ /*loop to call calcNetPay() and display() functions for each object*/ emp[i].calcNetPay(); emp[i].display(); } return 0; }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}