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

Lab Solution

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

Lab Solution

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

Solution

Lab 1-1: Installation and usage of Dev-C++ and online


compilers

Objective:

The objective of this lab is to teach students how to install and use the
Dev-C++ and online compilers.

Learning Outcomes:

After this lab, the students will be able to run the C++ code in Dev-C++
and the online compilers.

Problem Statement

You are required to install any version of Dev-C++ in your system


according to your system and operating system requirements.

Dev C++ 4.9.9.2 Installation Guide


Installation Steps:

Step 1:

Double-click on the file devcpp_setup.exe and Click on the “Run” button.

Step 2:

Click on the “OK” button.

Step 3:

Click on the “OK” button.


Step 4:

Click on the “I Agree ” button.

Step 5:

Click on the “Next” button.


Step 6:

Click on the “Install” button.

Step 7:

Wait until the green bar completed


Step 8:

Click on the “Finish” button.

Your Dev C++ 4.9.9.2 has been installed successfully. Now start writing
compiling and running programs and enjoy programming.

Dev C++ 5.9 Installation Guide


Installation Steps:

Step 1:

Double-click on the file devcpp_setup.exe and Click on the “Run” button.

Step 2:

Click on the “OK” button.

Step 3:

Click on the “OK” button.


Step 4:

Click on the “I Agree ” button.

Step 5:

Click on “Next” button.


Step 6:

Click on “Install” button.

Step 7:

Wait until the green bar completed


Step 8:

Click on “Finish” button.

Your Dev C++ 4.9.9.2 has been installed successfully. Now start writing
compiling and running programs and enjoy programming.

Guidelines on how to use Dev-C++


Following are the steps to write, save, compile, and run a program, using
Dev-C++:

How to open Dev-C++:

Main interface of Dev-C++:


Write a new Program:
Where to write the code:
After writing your code:
Save the Program:

1st Step:
2nd Step:
How to compile the program:

1st Step:
2nd and 3rd step will be performed automatically

2nd Step:
3rd Step:
How to run the program:
Output will be displayed as:
Guidelines on how to use online C++ compiler
Following are the steps to write, compile, and run a program using an
online compiler:

Open a browser and type the following address in the address bar.

https://www.programiz.com/cpp-programming/online-compiler/

Now, write the C++ code in the highlighted area.


Write your C++ code in the highlighted area.

After writing the code, click on the ‘Run’ button.


The output of the code will be shown in the highlighted area.

If the program contains syntax errors, then errors will be shown along with
the line number in the highlighted area.
After removing the syntax errors, click on the ‘Run’ button again to see
the output.
Lab 1-2: Object Orientation

Objective:

The objective of this lab is to teach students how to identify objects, their
attributes and behaviors from the real-world scenario.

Learning Outcomes:

After this lab, the students will be able to identify objects, attributes and
behaviors from a given scenario.

Problem Statement

Consider the following scenario and identify all objects, their attributes,
and behaviors.

In the Virtual University bookshop, the handouts of all courses are


provided at reasonable prices. The handouts are made in accordance with
the video lectures recorded by famous professors. It is a step to facilitate
students to easily digest the course contents.

You are required to extract the Class Name, attributes and behaviours
(operations) from the above scenario.

Class Attributes Behaviors (operations)


Name (characteristics)

Solution:

Class Attributes Behaviors (operations)


(characteristics)
Booksho Name Set Order
p Address (location) Get Order
Phone number Confirm Order
list of books Deliver order
Update Inventory (update
order)
Payment receive/ collect
Handout Title Upload
No. of topics /chapters Download
Page number Edit
Author Print
Publisher Read
Creation date Write
Modify date Share
Edition View
Course Course code Add information
Course name View Information
Course instructor Edit information
Credit hour Read
Total marks Write
Grading scheme
No. of lectures
Video Video number View / watch
Lectures Name Record
Duration Upload
Video quality Download
Type Stop
Video URL Share
Pause
Forward
Reverse
Increase speed
Decrease speed
Increase volume
Decrease volume
playback
Professor Employee ID Teach
Name Develop assignment
Age Solve student’s problems
Gender Develop Quiz
Qualification Develop GDB
Specialization Prepare Exam
Designation Take Exam
Experience Invigilate
Salary Mark papers
No. of taught subjects Mark activities
Give grades
Prepare result
Student Student ID / Roll Study
number Give exam
Name Attempt activities
Age Taking lectures
Gender Write notes
Student program Making time table
Semester
CGPA
No. of courses
Course No. of topics Upload
content Names Edit
Topic number View
Topic Description Share
Composed by Write
Publish by Download
Written by
Lab 2: Class Diagram

Objective:

The objective of this lab is to teach students how to identify classes and
relationships between classes and draw a UML class diagram by applying
the concepts of inheritance, aggregation or composition.

Learning Outcomes:

After this lab, the students will be able to draw a UML class diagram for a
given scenario.

Problem Statement

Consider a scenario of an Employee Management System that is used to


manage employee records and departments. The system handles various
types of employees and departments. Employees in the organization can
be of different types, such as Full-Time and Part-Time. Full-time employees
get a salary according to their designation whereas part-time employees
get a salary based on working hours. A Department contains multiple
employees, but an employee is associated with only one department at a
time.

You are required to apply the concepts of inheritance, aggregation or


composition to the given scenario and draw a UML class diagram.

Solution:
Lab 3: Constructor overloading and copy constructor
Objective:

The objective of this lab is to teach students how to overload constructors,


deep copy constructors, and destructors in a class.

Learning Outcomes:

After this lab, the students will be able to overload constructors, copy
constructors, and destructors.

Problem Statement

Write a C++ program that consists of a class named Employee having the
following data members:
 Employee id
 Employee name
 Employee salary

The class should have the following member functions:


 Setters and getters functions
 Default constructor
 Parameterized constructor
 Deep copy constructor
 Destructor to deallocate the memory
 Display () function

Within the main () function, create an object emp1 of class Employee and
initialize its data members using a parameterized constructor. Within the
constructor, you have to call the setters functions to set the values of
these data members.

Now, create another object emp2 and initialize it with emp1 using the
deep copy constructor.
After that, you have to call the display () function to show the data
members of both objects.

Sample Output:

After running your program, the following screen should display.


Solution:

#include <iostream>
#include <string.h>

using namespace std;

class Employee
{
private:
int empId ;
char *empName ;
float empSalary ;

public:
void setId(int id){
empId = id ;
}

void setName(char *name){


empName = new char[strlen(name)+1];
strcpy(empName , name) ;
}

void setSalary( float salary){


empSalary = salary ;
}

int getId(){
return empId ;
}
const char *getName(){
return empName ;
}
float getSalary(){
return empSalary ;
}
// default construcor
Employee(){
empId = 0 ;
empName = NULL ;
empSalary = 0.0 ;
}
// parameterized construcor
Employee(int id, char *name, float salary){
setId(id);
setName(name);
setSalary(salary);
}

// copy constructor
Employee(const Employee & e){
empId = e.empId ;
empName = new char[strlen(e.empName)+1];
strcpy(empName , e.empName) ;
empSalary = e.empSalary;
}
void display(){
cout << "Employee Id: " << getId() << endl ;
cout << "Employee Name: " << getName() << endl;
cout << "Employee Salary: " << getSalary() << endl << endl;
}

~Employee(){
delete []empName ;
}
};

int main()
{
Employee emp1 (10, "Ali", 50000);
Employee emp2 = emp1 ; // copy constructor will be called here
cout << "The data members of object 1:" << endl ;
emp1.display();
cout << "The data members of object 2:" << endl ;
emp2.display();
system("pause");
}

You might also like