0% found this document useful (0 votes)
16 views

Scs202 Group a Assignment Doc.

The document outlines a C++ programming course at Murang'a University, detailing various programming concepts such as message vs. arguments, pass by value vs. pass by reference, and examples of simple programs for checking odd/even numbers and converting digits to words. It also explains global vs. local variables, arrays vs. pointers, and includes a checkout program that calculates totals and discounts. Each section provides code snippets and explanations to illustrate the concepts discussed.

Uploaded by

obotemiltone4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Scs202 Group a Assignment Doc.

The document outlines a C++ programming course at Murang'a University, detailing various programming concepts such as message vs. arguments, pass by value vs. pass by reference, and examples of simple programs for checking odd/even numbers and converting digits to words. It also explains global vs. local variables, arrays vs. pointers, and includes a checkout program that calculates totals and discounts. Each section provides code snippets and explanations to illustrate the concepts discussed.

Uploaded by

obotemiltone4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

MURANG`A UNIVERSITY OF TECHNOLOGY

SCHOOL OF EDUCATION
PROGRAMME: BED SCIENCE
DEPARTMENT OF EDUCATION AND TECHNOLOGY
Course Code: SCS202
Course Title: OBJECT ORIENTED PROGRAMMING
Academic Year: Year 2 Semester 2, 2025/2026Academic Year

DATE:21/3/2025.

GROUP MEMBERS: REGISTRATION NUMBER


MUNYI ROSE NGENDO EH200/1169/2022
KENNEDY MBAU EH200/1281/2023
NAOM MORANG’A EH200/1437/2023
DAVID LELEI EH200/3200/2023
NKAYIA DAVID EH200/3324/2023
1.Differentiate message and arguments in C++
Message:
In C++, a message is typically associated with object-oriented programming. It
refers to a request for an object to perform a particular action or behavior. In
other words, it’s a way of telling an object to execute a method or function. This
concept is more abstract and commonly used in the context of method calls in
OOP.
When you call a function or method on an object, you are essentially sending a
message to that object. The message is an instruction that tells the object to
invoke a specific behavior or action.
WHILE
Arguments are the values passed to a function or method when it is called. They
provide the necessary input data for the function or method to perform its
operation. In C++, arguments are specified within the parentheses of a function
call and can be of any data type.
When calling a function or method, you can pass arguments to it, which it uses
to compute or carry out the desired behavior.

2.Using an example of a program, differentiate between pass by value and pass


by reference with regards to method calling in C++
[20/03, 10:37 pm] Venus: In C++, pass by value and pass by reference are two
ways of passing arguments to functions. The key difference is how the function
handles the argument:

Pass by Value: A copy of the argument is made, and modifications inside the
function do not affect the original variable.
Pass by Reference: The function receives a reference to the actual variable, so
modifications affect the original variable.

Example Program:
#include <iostream>

using namespace std;

// Function that uses pass by value


void passByValue(int x) {
x = x + 10; // This change is local to the function
cout << "Inside passByValue, x = " << x << endl;
}

// Function that uses pass by reference


void passByReference(int &x) {
x = x + 10; // This change affects the original variable
cout << "Inside passByReference, x = " << x << endl;
}

int main() {
int a = 5, b = 5;

cout << "Before passByValue, a = " << a << endl;


passByValue(a);
cout << "After passByValue, a = " << a << endl; // a remains unchanged

cout << "\nBefore passByReference, b = " << b << endl;


passByReference(b);
cout << "After passByReference, b = " << b << endl; // b is changed

return 0;
}
Output:

Before passByValue, a = 5
Inside passByValue, x = 15
After passByValue, a = 5

Before passByReference, b = 5
Inside passByReference, x = 15
After passByReference, b = 15

Explanation:

Pass by Value: In passByValue(a), a copy of a is created inside the


function. Any changes to x inside the function do not affect a in main().

Pass by Reference: In passByReference(b), b is passed as a reference (&x).


Changes made to x inside the function affect b in main().
Thus, pass by reference is useful when you want to modify the original
variable, while pass by value keeps the original variable unchanged.

3.Write a C++ program that accepts an integer and then determines if it is


odd or even number.
#include <iostream>

int main() {
int number;

// Asking user for input


std::cout << "Enter an integer: ";
std::cin >> number;

// Checking if the number is even or odd


if (number % 2 == 0) {
std::cout << number << " is an even number." << std::endl;
} else {
std::cout << number << " is an odd number." << std::endl;
}

return 0;
}
4.As a software developer at ABC academy, you are required to develop a simple
App to be used by grade one pupils. The aim of the app is to help the pupils learn
about naming digits. The app should accept values from 0 to 9 and then display
the name on the screen based on the number entered. E.g., if a user keys in 5,
then the program will output Five. Write a C++ program to implement the app.

#include <iostream>
using namespace std;

// Function to convert a number to its word representation


string numberToWord(int num) {
switch(num) {
case 0: return "Zero";
case 1: return "One";
case 2: return "Two";
case 3: return "Three";
case 4: return "Four";
case 5: return "Five";
case 6: return "Six";
case 7: return "Seven";
case 8: return "Eight";
case 9: return "Nine";
default: return "Invalid number"; // In case the user enters a
number outside the range 0-9
}
}

int main() {
int digit;

// Introduction message
cout << "Welcome to the Number to Word App!" << endl;
cout << "Please enter a digit between 0 and 9: ";
cin >> digit;

// Check if the input is within the range 0-9


if(digit >= 0 && digit <= 9) {
cout << "The number you entered is: " << numberToWord(digit) <<
endl;
} else {
cout << "Invalid input! Please enter a digit between 0 and 9." << endl;
}
return 0;
}

5.Explain the differences between global and local variable. Give an example in
each case.
Global Variable:
A global variable is declared outside any function, typically at the top of a
program or file, and is accessible by all functions within the same file or program.
Its scope is global, meaning it can be accessed and modified by any part of the
code after its declaration.
The lifetime of a global variable is for the entire duration of the program. It is
created when the program starts and destroyed when the program terminates.
If a global variable is not initialized, it is automatically initialized to zero (or
NULL for pointers).
EXAMPLE:
#include <iostream>
using namespace std;

int globalVar = 10; // This is a global variable

void printGlobal() {
cout << "Global Variable: " << globalVar << endl; // Accessing global variable
}

int main() {
printGlobal(); // Global variable is accessible here
globalVar = 20; // Modifying global variable
printGlobal(); // Accessing modified global variable
return 0;
}
Local Variable:
A local variable is declared inside a function or block, and its scope is limited to
the block or function in which it is declared. This means it cannot be accessed
from outside the function or block.
The lifetime of a local variable is limited to the execution of the block or function
in which it is declared. Once the function or block finishes executing, the local
variable is destroyed.
A local variable does not have a default value. It must be explicitly initialized
before use, otherwise, it may contain garbage data.
Example of Local Variable:
#include <iostream>
using namespace std;

void myFunction() {
int localVar = 5; // This is a local variable
cout << "Local Variable: " << localVar << endl;
}

int main() {
myFunction(); // localVar is accessible inside myFunction() only
// cout << localVar; // Error! localVar is not accessible here
return 0;

6.Giving an example, explain the difference between an array and a pointer as


used in C++.
Example Program:

#include <iostream>

using namespace std;

int main() {
// Array declaration
int arr[3] = {10, 20, 30};

// Pointer declaration
int* ptr = arr; // Pointer points to the first element of the array

// Accessing elements using an array


cout << "Using array indexing: " << arr[0] << ", " << arr[1] << ", " << arr[2]
<< endl;

// Accessing elements using a pointer


cout << "Using pointer arithmetic: " << *ptr << ", " << *(ptr + 1) << ", " <<
*(ptr + 2) << endl;

// Changing the pointer to point to a different location


ptr = &arr[1]; // Now points to the second element
cout << "Pointer now points to arr[1]: " << *ptr << endl;

return 0;
}

Output:

Using array indexing: 10, 20, 30


Using pointer arithmetic: 10, 20, 30
Pointer now points to arr[1]: 20

Explanation:

1. Array (arr[3]): A fixed-size collection of elements stored in contiguous


memory.

2. Pointer (ptr): Initially points to the first element of arr. It can be moved
to different memory locations (ptr = &arr[1])

Arrays cannot change their base address, while pointers can be


reassigned.

sizeof(arr) gives total size of the array, but sizeof(ptr) only gives size of the
pointer (usually 4 or 8 bytes).

Both can be used with indexing and pointer arithmetic.


This distinction is important for dynamic memory allocation using new
and delete

7.As a software developer at ABC academy, you are required to develop a C++
program to take the price and quantity of items as an input for a given customer
during checking out. The program should use four functions as follows:
i. Function called compute(), to calculate the total of the prices.
ii. Function called calcDiscount(), to calculate the discount according
to the following rules:
• Total less than 1000, No discount.
• Total more than 1000 but less than 10,000, discount 6%
• Total more than 10,000, discount 10%
iii. Function called Display(), to print the individual item prices, sub-
total, discount and the grad total.
iv. main() function where program execution begins.

#include <iostream>
using namespace std;

// Function to compute total price


double compute(double price, int quantity) {
return price * quantity;
}

// Function to calculate discount


double calcDiscount(double total) {
if (total < 1000)
return 0;
else if (total >= 1000 && total < 10000)
return total * 0.06;
else
return total * 0.10;
}

// Function to display details


void Display(double price, int quantity, double subtotal, double
discount, double grandTotal) {
cout << "Price per item: " << price << endl;
cout << "Quantity: " << quantity << endl;
cout << "Subtotal: " << subtotal << endl;
cout << "Discount: " << discount << endl;
cout << "Grand Total: " << grandTotal << endl;
}
// Main function
int main() {
double price;
int quantity;

// User input
cout << "Enter price of the item: ";
cin >> price;
cout << "Enter quantity: ";
cin >> quantity;

// Compute total and discount


double subtotal = compute(price, quantity);
double discount = calcDiscount(subtotal);
double grandTotal = subtotal - discount;

// Display the results


Display(price, quantity, subtotal, discount, grandTotal);

return 0;
}

Explanation:

1. compute() calculates the total price.

2. calcDiscount() determines the discount based on the given rules.

3. Display() prints the price, quantity, subtotal, discount, and grand


total.

4. main() takes user input, processes it, and displays the output.

You might also like