Capital University of Science and Technology, Islamabad Department of Computer Science, Faculty of Computing
Capital University of Science and Technology, Islamabad Department of Computer Science, Faculty of Computing
Table of Contents
1. Introduction 3
2. Activity Time-boxing 3
4. Concept Map 4
4.1 Reusability/ Modularity of Code 4
4.2 Function Prototype and Function Definition 4
4.3 Function Arguments and Parameters 4
4.4 Returning a Value from a Function 5
4.5 Pass by Value and Pass by Reference 5
4.6 Pointers 6
4.7 Structures 7
4.7.1 How to define a structure in C++ programming? 8
4.7.2 How to define a structure variable? 8
4.7.3 How to access members of a structure? 8
4.7.4 Structure Example 9
7. Practice Tasks 2
7.1 Practice Task 1 [Expected time = 10 mins] 2
7.2 Practice Task 2 [Expected time = 10 mins] 2
7.3 Practice Task 3 [Expected time = 10 mins] 2
7.4 Out comes 3
7.5 Testing 3
9. Evaluation Criteria 3
Lectures: 1, 2
Textbook: Object-Oriented Programming Using C++, Fourth edition, Robert Lafore.
o Chapters: 5, 10
2. Activity Time-boxing
Table 1: Activity Time Boxing
Task No. Activity Name Activity time Total Time
5.1 Evaluation of Design 15 mins 15 mins
6.2 Setting-up Visual Studio 5 mins 5 mins
6.3 Walkthrough Task 25 mins 25 mins
7 Practice tasks 15 + 15 + 20 + 35 (mins) 85 mins
8 Evaluation Task 40 min for two task 50 mins
Total Time 170 Mins
Understanding of structure
4. Concept Map
Computer programmers have always devised mechanisms through which they can make their code more
understandable and then reusable. In this regard functions offer a very popular and easy mechanism of
introducing the above goals. Function handling operates on the concept of provision of service/
functionality. We can call the services of a function by providing the required data and in result getting the
promised service from the function.
Reusability of code means devising methods through which you can use code again and again without
having to copy-paste in the source file. Modularity of code means dividing the code into small, manageable
and easy to understand segments.
Now suppose we are calling the same function from within the main().
void main (){
int x=3, y=4;
addTow(x, y);
}
For example the following function does not return a value hence the void keyword is used
void addtwo (int a, int b);{
int c=a+b;
cout<<c;
}
The following function returns an integer value hence the keyword int is used.
The value being returned can be displayed by using the following statement from where the function is being c
cout<< addrow(x, y);
On the other hand pass by reference is an argument passing technique in which the function works with the exact
variable that is being passed as an argument. This means that even the smallest change in a parameter will be exactly
reflected in the arguments. This further implies that the arguments and the parameters are tightly coupled.
4.6 Pointers
Pointers are special kind of variables that are allowed to hold the address of another variable. Because of their pointing
ability pointers are considered very powerful in C++. Pointers can hold the address of another variable and this is
called referencing the memory location. When we attempt to extract values from a memory location then this is called
dereferencing a pointer
Capital University of Science and Technology, Islamabad
Department of Computer Science,
Faculty of Computing
Figure 2: The various operations that can be performed with a pointer. Output is also provided. Memory addresses
may be different depending on the hardware environment
In the code above first a simple integer variable is created. Then an integer pointer is created because we intend to
point to an integer variable. The pointer is then given the address of variable x by using the address operator. Then
we use the dereference operator (*) that directs us to the memory location where the pointer is pointing to.
4.7 Structures
Structure is the collection of variables of different types under a single name for better visualization of
problem. Arrays is also collection of data but arrays can hold data of only one type whereas structure can
hold data of one or more types.
Capital University of Science and Technology, Islamabad
Department of Computer Science,
Faculty of Computing
Here a structure person is defined which has three members: name, age and salary.
person bill;
The members of structure variable is accessed using dot operator. Suppose, you want to access ageof
structure variable bill and assign it 50 to it. You can perform this task by using following code below:
bill.age = 50;
Capital University of Science and Technology, Islamabad
Department of Computer Science,
Faculty of Computing
Output:
Capital University of Science and Technology, Islamabad
Department of Computer Science,
Faculty of Computing
Write pseudo-code for a program that will find the factorial of a number. Your task is to also perform checks on the
number entered by the user because the factorial can only be found of an integer and a positive number. Incorporate
these two conditions in you pseudo-code
Capital University of Science and Technology, Islamabad
Department of Computer Science,
Faculty of Computing
5.2.1 Task 1
In The field of Physics you are aware of the concept of force and acceleration. Your task is to write a program to
find the force if the mass and the acceleration of a body is given. Write a function to perform the calculation. Your
task is to provide the mass and acceleration as arguments to the function and then display the force without returning a
value.
5.2.2 Task 2
Write a program that creates a function to find the area of a cylinder if the radius and height of the cylinder is provided.
The function should return the value of area. The area of a cylinder is 𝜋 𝑟2ℎ. Consider the figure below for further
clarification.
6.1 Tools
Visual Studio 2008.
In the code above note that the two functions use different mechanisms for passing/ using an array. You can
choose the method that suits your code/ situation. The size is passed in both functions because without the size
the functions can easily cross the array bounds. Hence passing the array size is a good practice.
6.3.2 Compilation
Write a program to find the power of a number if the base and exponent is provided in the main function. Use pass
by value mechanism to compute the power and return the results back to the main function then display the result in
main function.
7. Practice Tasks
This section will provide more practice exercises which you need to finish during the lab. You need to finish
the tasks in the required time. When you finish them, put these tasks in the following folder:
\\dataserver\assignments$\OOP\Lab01.
After that create array of 5 Students in main function. Create a menu in main function to enable user to select and
perform the operations we created above. Program must not exit until and unless user wants to do so.
Sample Menu:
Main Menu
---------------
7.5 Testing
Test Cases for Practice Task-1 and Task-2
The lab instructor will give you unseen task depending upon the progress of the class.
9. Evaluation Criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each task is
assigned the marks percentage which will be evaluated by the instructor in the lab whether the student has
finished the complete/partial task(s).
10.1 Books
Text Book:
Object-Oriented Programming Using C++, Fourth edition, Joyce Farrell
10.2 Slides
The slides and reading material can be accessed from the folder of the class instructor available at
\\dataserver\jinnah$\