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

Lab No 2 Os Submitted

The document describes a lab assignment on executing C++ programs via shell commands and makefiles. The objectives of the lab are to install a C++ compiler in Ubuntu, execute a C++ program using shell commands, and execute a C++ program using a Makefile. The document provides instructions on completing tasks to meet the objectives, including writing and compiling simple C++ programs, and creating Makefiles to automate compilation. It also includes sample code and Makefiles as examples. The tasks require writing C++ programs to calculate the area and perimeter of a rectangle, and calculate the total cost of purchases based on quantity discounts.

Uploaded by

Mubashir Hussain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Lab No 2 Os Submitted

The document describes a lab assignment on executing C++ programs via shell commands and makefiles. The objectives of the lab are to install a C++ compiler in Ubuntu, execute a C++ program using shell commands, and execute a C++ program using a Makefile. The document provides instructions on completing tasks to meet the objectives, including writing and compiling simple C++ programs, and creating Makefiles to automate compilation. It also includes sample code and Makefiles as examples. The tasks require writing C++ programs to calculate the area and perimeter of a rectangle, and calculate the total cost of purchases based on quantity discounts.

Uploaded by

Mubashir Hussain
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Submitted By : Mubashir Hussain 4979

LAB NO. 02
EXECUTING C++ PROGRAM VIA SHELL
COMMANDS
Following are the lab objectives:

1. Installing C++ compiler in Ubuntu


Lab 2. Executing C++ program using shell commands

Objectives 3. Executing C++ program using Makefile

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

Lab Objectives and CLOs Mapping

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

INSTALLING C++ COMPILER IN UBUNTU


Installing g++ Compiler
G++, the GNU C++ Compiler is a compiler in Linux which was developed to compile C++
programs. The file extensions that can be compiled with G++ are .c and .cpp. This will be
achieved by installing the build-essential package.

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.

Syntax to install the package build-essential:

$ sudo apt install build-essential

Check C++ compiler version:

$ g++ --version

EXECUTE C++ PROGRAM VIA SHELL COMMANDS


Create a basic C++ code source using any editor in Ubuntu. For example we are going to use the
gedit command to open a cpp file in the built-in editor.

Step 1: Open a cpp file using gedit by running the following command at Terminal.

$ gedit helloworld.cpp

Step 2: Save the following code in helloworld.cpp text file.


#include <iostream>
using namespace std;

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.

Following command is used to compile the source code.

$ g++ -c helloworld.cpp

Following command is used to make an executable file from cpp file.

$ g++ -o helloworld helloworld.cpp

Following command is used to run/execute the executable file.

$ ./helloworld

Table 1: Summary of G++ Compilation and Execution Commands


Command Description
This command is used to compile the C++ code to
$ g++ -c helloworld.cpp
check the syntax errors.
This command is used to make an executable file
$ g++ -o helloworld helloworld.cpp
from C++ code.
This command is used to execute/run the program
$ ./helloworld
using a generated executable file.

EXECUTE C++ PROGRAM USING MAKEFILE


Compiling the C++ source code files can be tedious, especially when we have to include several
source files and type the compiling command every time we need to compile. What is the
alternative? Makefiles are a simple way to organize code compilation procedure.
Makefiles are special format files that together with the make utility will help us to
automatically build and manage our projects (having multiple source files).
For example, let’s assume we have the following source files.
Submitted By : Mubashir Hussain 4979

▪ 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"

using namespace std;

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"

using namespace std;

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"

int factorial(int n){

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

$ g++ main.cpp hello.cpp factorial.cpp –o hello

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

double perimeter(double w, double l){


return (w*2)+(l*2);
}
int main(){
double w,l;
cout<<”Enter length : “;cin>>l;
cout<<”Enter Width : “;cin>>w;

cout<<endl<<”Area : “<<area(w,l);
cout<<endl<<”Perimeter : “<<perimeter(w,l)<<endl;
}
Makefile

all:

g++ task1.cpp -o output

Output:
Submitted By : Mubashir Hussain 4979
Submitted By : Mubashir Hussain 4979

Output:

Task 2 (10 marks)


A software company sells a package that retails for $199. Quantity discounts are given according
to the following table.

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>

using namespace std;

int compute(int units){

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<<"Enter number of purchased units : ";cin>>units;

cout<<endl<<"Price : "<<compute(units)<<endl;

return 0;

Makefile

all:

g++ task2.cpp -o output

Output
Submitted By : Mubashir Hussain 4979

Output:

Task 3 (20 marks)

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>

using namespace std;

class Date{

int month,day,year;

public:

Date(){

month=day=year='\0';

Date(int month,int day,int year){

if(month>0 && month<13){

this->month=month;

else {

this->month=1;
Submitted By : Mubashir Hussain 4979

this->day=day;

this->year=year;

Date(Date & obj){

month=obj.month;

day=obj.day;

year=obj.year;

void setm(int month){

this->month=month;

void setd(int day){

this->day=day;

void sety(int year){

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>

using namespace std;

int main(){

cout<<"With setter, Getter and Default construtor"<<endl;

Date obj;

obj.setd(2);
Submitted By : Mubashir Hussain 4979

obj.setm(1);

obj.sety(2000);

obj.displayDate();

cout<<endl<<"With parameterized constructor \n";

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

You might also like