Lab No 2 Os Submitted
Lab No 2 Os Submitted
LAB NO. 02
EXECUTING C++ PROGRAM VIA SHELL
COMMANDS
Following are the lab objectives:
Student
Mubashir Hussain
Roll No. 4979 Name
Obtained
Marks Comments
Marks
Task 1 10
Task 2 10
Task 3 20
Total
40
Marks
Sir Kaleemullah
Lab Instructor
Submitted By : Mubashir Hussain 4979
CLOs
Lab Objectives
a b c
1
2
3
Instructions
▪ This is individual Lab work/task.
▪ Complete this lab work within lab timing.
▪ Discussion with peers is not allowed.
▪ You can consult any book, notes & Internet.
▪ Copy paste from the Internet will give you negative marks.
▪ Lab work is divided into small tasks, completing all tasks sequentially.
▪ Show the solution of each lab task to your Lab Instructor.
▪ In-Lab Exercises/Tasks
▪ Write your code at provided space after each question
▪ You need to upload code for all tasks at LMS.
Submitted By : Mubashir Hussain 4979
Although we can install the C++ compiler separately by installation of the gcc package, the
recommended way to install the C++ compiler on Ubuntu 20.04 is by installation of the entire
development package build-essential.
$ g++ --version
Step 1: Open a cpp file using gedit by running the following command at Terminal.
$ gedit helloworld.cpp
int main()
{
cout << "Hello, World!";
return 0;
Submitted By : Mubashir Hussain 4979
Step 3: Compile and execute the C++ code by running the following command at Terminal.
Note: Before running the following commands, make sure your working directory is the same
where the cpp code file is stored.
$ g++ -c helloworld.cpp
$ ./helloworld
▪ main.cpp
▪ hello.cpp
▪ factorial.cpp
▪ functions.h
Write the given C++ code in respective files using gedit editor.
main.cpp
The following is the code for main.cpp source file
#include <iostream>
#include "functions.h"
int main(){
print_hello();
cout << endl;
cout << "The factorial of 5 is " << factorial(5) << endl;
return 0;
}
hello.cpp
The following is the code for hello.cpp source file
#include <iostream>
#include "functions.h"
void print_hello(){
cout << "Hello World!";
}
Submitted By : Mubashir Hussain 4979
factorial.cpp
The following is the code for factorial.cpp source file.
#include "functions.h"
if(n!=1){
return(n * factorial(n-1));
} else return 1;
}
functions.h
The following is the code for functions.h source file.
void print_hello();
int factorial(int n);
The trivial way to compile the files and obtain an executable, is by running the command
Warning: But we’ll NOT use this process of executable file generation.
Makefile
The make command allows us to manage large programs (with multiple files) or groups of
programs. As we begin to write large programs, we notice that re-compiling large programs takes
longer time than re-compiling short programs.
Create a Makefile listing the rules for building the executable, the file should be named
‘Makefile’ or ‘makefile’. This only has to be done once, except when new modules are added to
the program, the Makefile must be updated to add new module dependencies to existing rules
and to add new rules to build the new modules.
If we run :
$ make
This program will look for a file named Makefile in your directory, and then execute it.
Submitted By : Mubashir Hussain 4979
Makefile Composition
The basic Makefile is composed of:
target: dependencies
[tab] system command
First Makefile
Create a file with the name Makefile, and write following commands in the file.
all:
g++ main.cpp hello.cpp factorial.cpp –o output
In this first example we see that our target is called all. This is the default target for Makefiles.
The make utility will execute this target if no other one is specified.
We also see that there are no dependencies for target all, so make safely executes the system
commands specified.
Finally, make compiles the program according to the command line we gave it. Run following
commands at Terminal.
$ make
g++ main.cpp hello.cpp factorial.cpp –o output
$ ./output
Second Makefile
Create a second file with the name Makefile-2, and write following commands in the file.
all: make-2
make-2: main.o factorial.o hello.o
g++ main.o factorial.o hello.o –o output-2
main.o: main.cpp
g++ -c main.cpp
factorial.o: factorial.cpp
g++ -c factorial.cpp
Submitted By : Mubashir Hussain 4979
hello.o: hello.cpp
g++ -c hello.cpp
clean:
rm –rf *o output-2
Sometimes it is useful to use different targets. This is because if you modify a single file in your
project, you don't have to recompile everything, only what you modified.
In this example we see a target called clean. It is useful to have such target if you want to have
a fast way to get rid of all the object files and executables. Run the following commands at
Terminal.
$ make –f Makefile-2
g++ -c main.cpp
g++ -c factorial.cpp
g++ -c hello.cpp
g++ main.o factorial.o hello.o –o output-2
$ ./output-2
LAB TASKS
Task 1 (10 marks)
Write a C++ program which takes the width & length of Rectangle from the user. Display area
and perimeter of Rectangle. Use Makefile to execute your code via Terminal.
Code:
#include<iostream>
using namespace std;
double area(double w, double l){
return w*l;
}
Submitted By : Mubashir Hussain 4979
cout<<endl<<”Area : “<<area(w,l);
cout<<endl<<”Perimeter : “<<perimeter(w,l)<<endl;
}
Makefile
all:
Output:
Submitted By : Mubashir Hussain 4979
Submitted By : Mubashir Hussain 4979
Output:
Quantity Discount
1-50 No discount
Otherwise 20%
Write a program that asks for the number of units purchased and computes the total cost of the
purchase. Use Makefile to execute your code via Terminal.
Code:
#include<iostream>
int price;
if(units<51){
Submitted By : Mubashir Hussain 4979
price=units*199;
else if(units>50){
price= ((units*199)-(((units*199)*20)/100));
return price;
int main(){
int units;
cout<<endl<<"Price : "<<compute(units)<<endl;
return 0;
Makefile
all:
Output
Submitted By : Mubashir Hussain 4979
Output:
Create a class called Date that includes three pieces of information as data members; a month
(type int), a day (type int), and a year (type int). Your class should have three constructors that
initialize the three data members. For the purpose of this exercise, assume that the values
Submitted By : Mubashir Hussain 4979
provided for the year and day are correct, but ensure that the month value is in the range 1-12; if
it isn’t, set the month to 1.
Provide setter and getter methods for each data member. Provide a member function
displayDate() that displays the month, day and year in the provided format, i.e. day – month –
year.
You need to create a header file, i.e. Date.h, which contains specification of class, creates
another file Data.cpp which contains all the implementations/definitions of methods available in
the class. Write a test program that demonstrates class Date’s capabilities. Use Makefile to
execute your code via Terminal.
Code:
Date.h
#include <iostream>
class Date{
int month,day,year;
public:
Date(){
month=day=year='\0';
this->month=month;
else {
this->month=1;
Submitted By : Mubashir Hussain 4979
this->day=day;
this->year=year;
month=obj.month;
day=obj.day;
year=obj.year;
this->month=month;
this->day=day;
this->year=year;
int getm(){
return month;
int getd(){
return day;
int gety(){
Submitted By : Mubashir Hussain 4979
return year;
void displayDate(){
cout<<endl<<day<<"-"<<month<<"-"<<year<<endl;
};
Data.cpp
#include "Date.h"
#include<iostream>
int main(){
Date obj;
obj.setd(2);
Submitted By : Mubashir Hussain 4979
obj.setm(1);
obj.sety(2000);
obj.displayDate();
Date obj1(3,1,2001);
obj1.displayDate();
cout<<endl<<"Copy Constructor\n";
Date obj2(obj1);
obj2.displayDate();
cout<<endl;
Makefile
all:
g++ data.cpp -o output
Output:
Submitted By : Mubashir Hussain 4979
The End