You are on page 1of 6

COMPUTER PROGRAMMING PARADIGM LAB

LAB EXPERIMENT NO 1

Roll No. 47 Batch B

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

1) Object is an instance of a class. 1) Class is a blueprint or template from


which objects are created.

2) Object is a real world entity such as 2) Class is a group of similar objects.


pen, laptop, mobile, bed, keyboard,
mouse, chair etc.

3) Object is a physical entity. 3) Class is a logical entity.

4)Object is created through new 4) Class is declared using class


keyword mainly e.g. keyword.

Student s1=new Student(); e.g. class Student{}

5) Object is created many times as per 6) Class is declared once.


requirements.

3) What is a constructor, what are the types of constructor?


Ans. A Constructor is a special type of method having no return types. Constructor(s) of a
class must has same name as the class name in which it resides. Access modifiers can be used
in constructor declaration to control its access i.e which other class can call the constructor.

There are two type of constructor:


1. No-argument constructor: A constructor that has no parameter is known as default
constructor. If we don’t define a constructor in a class, then compiler creates default
constructor (with no arguments) for the class. And if we write a constructor with
arguments or no-arguments then the compiler does not create a default constructor.
Default constructor provides the default values to the object like 0, null, etc. depending on the
type.
E.g. ABC(){

}
....
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.

5) Explain the concepts of constructor overloading?


Ans. Prerequisites: Constructors in C++.
In C++, We can have more than one constructor in a class with same name, as long as each
has a different list of arguments. This concept is known as Constructor Overloading and is
quite similar to function Overloading.
1) Overloaded constructors essentially have the same name (name of the class) and different
number of arguments.
2) A constructor is called depending upon the number and type of arguments passed.
3) While creating the object, arguments must be passed to let compiler know, which
constructor needs to be called.

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:

You might also like