Lab Solution
Lab Solution
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
Step 1:
Step 2:
Step 3:
Step 5:
Step 7:
Your Dev C++ 4.9.9.2 has been installed successfully. Now start writing
compiling and running programs and enjoy programming.
Step 1:
Step 2:
Step 3:
Step 5:
Step 7:
Your Dev C++ 4.9.9.2 has been installed successfully. Now start writing
compiling and running programs and enjoy programming.
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/
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.
You are required to extract the Class Name, attributes and behaviours
(operations) from the above scenario.
Solution:
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
Solution:
Lab 3: Constructor overloading and copy constructor
Objective:
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
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:
#include <iostream>
#include <string.h>
class Employee
{
private:
int empId ;
char *empName ;
float empSalary ;
public:
void setId(int id){
empId = id ;
}
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");
}