Professional Documents
Culture Documents
GROUP MEMBERS:
DIFFERENCES
d) By using suitable example, explain how a class can be declared in the C++ language
A class is declared by using the keyword class followed by an opening brace and list the date members
of that class and ends with a semi colon.
1. #include <iostream>
2. using namespace std;
3. class politician
4. {
5. public:
6. int id;//data member
7. string name;//data member
8. };
9. int main()
10. {
11. politician pol;
12. pol.id = 001;
13. pol.name = "Mburu Muturi";
14. cout<<pol.name<<endl;
15. cout<<pol.id<<endl;
16. return 0;
17. }
e) Write C++ segment to output the hours worked and rate of pay of class Employee with objects
such as Engineer and Accountant
1. #include <iostream>
2. using namespace std;
3. class Employee
4. {
5. private:
6. float salary;
7. public:
8. int Hours, Rateofpay;
9. };
10. int main ()
11. {
12. int Hours, Rateofpay;
13. Hours=10;
14. Rateofpay=6000;
15. string object ="Accountant";
16. cout<<"The Hours worked by the " << object << " is " << Hours <<endl;
17. cout<<"The Rate of pay of the " << object << " is " << Rateofpay<<endl;
18. system ("pause");
19. object ="Engineer";
20. Hours = 7;
21. Rateofpay = 4000;
22. cout<<"The Hours worked by the " << object << " is " << Hours <<endl;
23. cout<<"The Rate of pay of the " << object << " is " << Rateofpay<<endl;
24. return 0;}