You are on page 1of 6

SCHOOL: BUSINESS

UNIT CODE: BMS 201

UNIT TITLE: COMPUTER PROGRAMING

DEPARTMENT: MANAGEMENT SCIENCE

TASK: BMS 201 ASSIGNMENT

GROUP MEMBERS:

NAME : REG.NUMBER: SIGN:

1. MBURU PETER MUTURI D33S/14211/2019

2. SABRINA ABDULATIF MWANYALI D33EA/13393/2019

3. PROVIDENCE VALIKWIRIMA D33F/25024/2019

4. KEZIA MUTHONI MWANGI D33S/14324/2019

5. IAN NGUNU NJOROGE D33S/14607/2019


a) Write some C++ code demonstrating how switch statement can be used when evaluating a range
of values.
1. #include<iostream>
2. using namespace std;
3. int main ()
4. {
5. int score;
6. cout<<"Enter your score"<<endl;
7. cin>>score;
8. switch (score)
9. {
10. case 70 ... 100 :
11. cout <<"Excellent"<<endl;
12. cout <<"You have an A"<<endl;
13. break;
14. case 60 ... 69 :
15. cout <<"Well done"<<endl;
16. cout<<" You have a B"<<endl;
17. default:
18. cout <<"Invalid score "<<endl;
19. cout <<"Obtained score "<<score <<endl;
20. }
21. return 0;
22. }

b) Demonstrate different ways of calling functions using illustrative examples


1) CALL BY VALUE: In call by value, value being passed to the function is locally stored by the
function parameter in stack memory location.
1. #include <iostream>
2. using namespace std;
3. int main(){
4. int x =7, y=6;
5. cout<<"The value of x is "<<x<<" and the value of y is "<<y<<endl;
6. swap(x, y);
7. cout<<"The value of x is "<<x<<" and the value of y is "<<y<<endl;
8. return 0;
9. }
2) CALL BY REFERENCE:- In call by reference, original value is modified because we pass reference (address).
1. #include <iostream>
2. using namespace std;
3. void swapReferenceVar(int &a, int &b)
4. {
5. int temp = a;
6. a = b;
7. b = temp;
8. }
9. int main(){
10. int x =7, y=6;
11. cout<<"The value of x is "<<x<<" and the value of y is "<<y<<endl;
12. swapReferenceVar(x, y); //This will swap a and b using reference variables
13. cout<<"The value of x is "<<x<<" and the value of y is "<<y<<endl;
14. return 0;
15. }
c) Compare and contrast object-oriented programming technique and procedural oriented
programming technique
SIMILARITIES
I. Both can take an input and produce a desired output.
II. Both utilize modular concepts to break down complex programs into several smaller program
units.
III. Both require rudimentary understanding of programming concepts and basic control flow.

DIFFERENCES

Procedural Programming Object-oriented Programming

 It focuses on the process and  It focuses on the data and


functions. classes.

 It is not easy to maintain.  It is easy to maintain.


 In this paradigm, if a sub-
 In this paradigm, it is easy to
procedure has to be modified, it
maintain code and modify
becomes difficult to find and
existing code.
maintain it.

 Due to its complexity,  Due to easy maintenance,


development time increases. development time reduces.

 Procedural programming  The object-oriented


languages are not as faster as programming languages are
object-oriented. faster and more effective.

 uses procedures, modules,


 uses objects, classes, messages.
procedure calls.

 It focuses on procedure rather


 It focuses on data rather than
data which has priority in data-
procedures.
driven systems.

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;}

You might also like