Computer Programming Paradigm Lab
Computer Programming Paradigm Lab
LAB EXPERIMENT NO 1
Aim: Write a C++ program to implement Triangle class which has the following members -
three sides, four constructors ( one with no parameter, one with single parameter (equilateral
triangle), one with two parameters (isosceles triangle), one with three parameters (scalene
triangle), a destructor, methods to read data and display data along with area of respective
triangles.
Theory:
1) What are the advantages of Encapsulation?
Ans. Encapsulation is defined as the wrapping up of data under a single unit. It is the
mechanism that binds together code and the data it manipulates. Other way to think about
encapsulation is, it is a protective shield that prevents the data from being accessed by the
code outside this shield.
Advantages of Encapsulation:
Data Hiding: The user will have no idea about the inner implementation of the class. It
will not be visible to the user that how the class is storing values in the variables. He
only knows that we are passing the values to a setter method and variables are getting
initialized with that value.
Increased Flexibility: We can make the variables of the class as read-only or write-
only depending on our requirement. If we wish to make the variables as read-only then
we have to omit the setter methods like setName(), setAge() etc. from the above
program or if we wish to make the variables as write-only then we have to omit the get
methods like getName(), getAge() etc. from the above program
Reusability: Encapsulation also improves the re-usability and easy to change with new
requirements.
Testing code is easy: Encapsulated code is easy to test for unit testing.
2) Differentiate between object and class.
Ans.
OBJECT CLASS
}
....
ABC obj;
....
2. Parameterized Constructor: A constructor that has parameters is known as
parameterized constructor. If we want to initialize fields of the class with your own values,
then use a parameterized constructor.
E.g. ABC(int a, int b){
}
….
ABC obj(20,30);
4) When are constructors invoked? How are they different from functions?
Ans. Constructor is a special member function of a class that initializes the object of the
class. Constructor name is same as class name and it doesn’t have a return type.
In C++, Constructor is automatically called when object (instance of class) create. At the
time of calling constructor, memory for the object is allocated in the memory. It is a special
type of method which is used to initialize the object.
1) Constructor doesn’t have a return type. Member function has a return type.
2) Constructor is automatically called when we create the object of the class. Member
function needs to be called explicitly using object of class.
3) When we do not create any constructor in our class, C++ compiler generates a default
constructor and insert it into our code. The same does not apply to member functions.
Program:
/* Write a C++ program to implement Triangle class which has the following members -
three sides, four constructors ( one with no parameter, one with single parameter
(equilateral triangle), one with two parameters (isosceles triangle), one with three
parameters (scalene triangle), a destructor, methods to read data and display data along
with area of respective triangles.*/
#include <iostream>
#include <math.h>
using namespace std;
class Triangle
{
private :
double s1,s2,s3;
public :
Triangle()
{
s1=0.0;
s2=0.0;
s3=0.0;
}
Triangle(double side)
{
s1=s2=s3=side;
}
Triangle(double same, double diff)
{
s1=s2=same;
s3=diff;
}
Triangle(double side1, double side2, double side3)
{
s1=side1;
s2=side2;
s3=side3;
}
double area()
{
double s=(s1+s2+s3)/2;
return (sqrt(s*(s-s1)*(s-s2)*(s-s3)));
}
display_area()
{
cout<<"Sides : "<<s1<<", "<<s2<<" and "<<s3<<endl;
cout<<"Area : "<<area()<<endl;
}
~Triangle()
{
cout<<"End of the program"<<endl;
}
};
int main()
{
double a,b,c,x,y,z;
cout<<"Enter a side of an equilateral triangle : ";
cin>>a;
Triangle triangle1(a);
cout<<endl;
triangle1.display_area();
cout<<endl;
cout<<"Enter the Equal sides of an isosceles triangle : ";
cin>>b;
cout<<"Enter the different side of an isosceles triangle : ";
cin>>c;
Triangle triangle2(b,c);
cout<<endl;
triangle2.display_area();
cout<<endl;
cout<<"Enter first side of a scalene triangle : ";
cin>>x;
cout<<"Enter second side of a scalene triangle : ";
cin>>y;
cout<<"Enter third side of a scalene triangle : ";
cin>>z;
Triangle triangle3(x,y,z);
cout<<endl;
triangle3.display_area();
cout<<endl;
return 0;
}
Output: