/Weekly Assignment-4
Due date: 20th June 2020 – [12:00AM] Total marks: 100
Carefully watch the following C++ program.
1. Your task is to type the above program in your system and run it in your system. Add
output of the above program and also draw UML- Class diagram.
SOLUTION:
****************************************
******************************************
*/
#include <iostream>
using namespace std;
//Rectangle class declaration.
class Rectangle
{
private:
double width;
double length;
public:
void setWidth(double);
void setLength(double);
double getWidth() const;
double getLength() const;
double getArea() const;
};
//*******************************************************
// setWidth assigns a value to the width member. *
//********************************************************
void Rectangle :: setWidth (double w)
{
width = w;
}
//*********************************************************
// setLength assigns a value to the Length member. *
//*********************************************************
void Rectangle :: setLength (double len)
{
length = len;
}
//************************************************************
// getWidth returns the value in the width member. *
//************************************************************
double Rectangle :: getWidth() const
{
return width;
}
//************************************************************
// getLength returns the value in the Length member. *
//************************************************************
double Rectangle :: getLength() const
{
return length;
}
//*************************************************************
// getArea returns the product of width times Length *
//*************************************************************
double Rectangle :: getArea() const
{
return width * length;
}
//*************************************************************
// Function main *
//*************************************************************
int main()
{
Rectangle box; //Define an instance of the Rectangle class
double rectWidth; //Local variable for width
double rectLength; //Local variable for Length
// Get the rectangle's width and Length from the user.
cout <<" This program will calculate the area of a\n";
cout <<"rectangle. What is the width? ";
cin >> rectWidth ;
cout <<" What is the length? ";
cin >> rectLength;
// Store the width and Length of the rextangle
// in the box object.
[Link](rectWidth);
[Link](rectLength);
// Display the rectangle's data.
cout << "Here is the rectangle's data: \n";
cout << "Width: " << [Link]() << endl;
cout << "Length: " << [Link]() <<endl;
cout << "Area: " << [Link]() <<endl;
return 0;
}
OUTPUT:
UML-class diagram:
2. Watch carefully the style of commenting done in the above program.
a. Follow the same commenting style for the following programs taught in the
specific dates mentioned.
b. Your answer should also include output of every program in your system.
c. Also draw the UML – class diagram of each program.
List of programs are as follows:
1. [Link] of 10th June online class
SOLUTION:
/**************************************
*/
#include<iostream>
#include<string.h>
using namespace std;
//Employee class declaration
class Employee
{
int empID;
string empName;
string dept;
float salary;
public:
//*******************************************************
// getId returns the value in the empID member. *
//*******************************************************
int getID() // accessors or getters
{
return empID;
}
//*******************************************************
// setTD assigns a value to the empID member. *
//*******************************************************
void setID(int id) // mutators or setters
{
empID = id;
}
};
//************************************************************
// Function main *
//************************************************************
int main()
{
Employee e;
// Store the ID of the Employee in the e object.
[Link](3000);
// display employee ID data
cout << "employee ID = " << [Link]() << endl;
return 0;
}
OUTPUT:
UML-class diagram:
2. [Link] of 17th June online class
SOLUTION:
#include<iostream>
#include<string.h>
using namespace std;
//Student class declaration
class Student
{
int roll;
string name;
public:
// parameterized constructor
Student(int roll = 100, string name = "TBC")
{
Student::roll = roll;
Student::name = name;
}
// copy constructor
Student(Student &std)
{
roll = [Link];
name = [Link];
}
void display();
};
//************************************************************
// display() displays the values of roll and name members *
//************************************************************
void Student :: display()
{
cout << "Roll no = " << roll << endl;
cout << "Name = " << name << endl;
}
//************************************************************
// Function main * *
//************************************************************
int main()
{
Student s1;
// calling default constructor
[Link]();
// calling parameterized constructor
Student s2(1008,"Ramesh");
[Link]();
//passing the value of roll and name members in s3 object
Student s3(2000, "Ram");
// calling the copy constructor
Student s4(s3);
// display the details of student s4
cout << "\nDetails of student S4 :\n";
[Link]();
return 0;
}
OUTPUT:
UML-class diagram:
TASK-2 : Answer the following MCQs.
============
ANSWERS:
1. B
2. C
3. C
4. D
5. A
6. A
7. B
8. B
9. A
10. B
11. B
12. C
13. B
14. A
15. B