---
title: "Triangle perimeter with constructors"
subject: "Object Oriented Programming"
chapter: 4
key: "semester_3/object_oriented_programming:triangle-perimeter-constructors"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/triangle-perimeter-constructors
site: BCT Engineering
---

# Triangle perimeter with constructors

Object Oriented Programming · Chapter 4: Objects and Classes

- Asked in: 2073 Shrawan, 3 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/triangle-perimeter-constructors
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-4

## Question

Write a program to calculate the Perimeter of Triangle using Default and Parameterized constructors.

## Short answer

A Triangle class defines a default constructor that sets all sides to 1 and a parameterized constructor accepting three side lengths. Perimeter is the sum of the sides. Example: default object perimeter = 3 units; object with sides 3,4,5 perimeter = 12 units.

## Answer

The class has a default constructor (sets all sides to a default of 1) and a parameterized constructor (sets the three sides). The perimeter is the sum of the three sides.

```cpp
#include <iostream>
using namespace std;

class Triangle
{
    float a, b, c;
public:
    Triangle()                          // default constructor
    {
        a = b = c = 1;
    }
    Triangle(float x, float y, float z) // parameterized constructor
    {
        a = x;
        b = y;
        c = z;
    }
    float perimeter()
    {
        return a + b + c;
    }
};

int main()
{
    Triangle t1;                        // default
    Triangle t2(3, 4, 5);               // parameterized

    cout << "Perimeter of t1 = " << t1.perimeter() << endl;
    cout << "Perimeter of t2 = " << t2.perimeter() << endl;
    return 0;
}
```

#### Output

```text
Perimeter of t1 = 3
Perimeter of t2 = 12
```

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What does the default constructor set the sides to?
   A. 0
   B. 1
   C. 3
   D. Garbage
   Answer: B. It sets a = b = c = 1. (Key point: Default constructor)

2. What is the perimeter of Triangle t2(3,4,5)?
   A. 60
   B. 12
   C. 3
   D. 5
   Answer: B. Perimeter is a+b+c = 12. (Key point: Perimeter)

3. Which line creates a triangle using the parameterized constructor?
   A. Triangle t1;
   B. Triangle t2(3, 4, 5);
   C. Triangle t3();
   D. Triangle t4 = 0;
   Answer: B. Passing arguments calls the parameterized constructor. (Key point: Parameterized constructor)

## Related in Object Oriented Programming

- [Area of triangle and rectangle program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aarea-triangle-rectangle-overload) — Object Oriented Programming, chapter 3
- [Default argument and overloading program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Adefault-arg-overloading-program) — Object Oriented Programming, chapter 3
- [Display N characters using default arguments](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3An-chars-default-args-program) — Object Oriented Programming, chapter 3
- [Constructors: need and types](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aconstructor-types) — Object Oriented Programming, chapter 4, asked 2×
- [Function overloading; new/delete average program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-overloading-new-delete-average) — Object Oriented Programming, chapter 3
- [Protected specifier; student/test/result program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprotected-three-class-program) — Object Oriented Programming, chapter 6

## Related in other subjects

- [Principal MI and axes of quarter-circle/triangle section (2078 Bhadra)](https://www.bctengineer.com/question.md?key=semester_3%2Fstrength_of_materials%3Aprincipal-mi-2078b) — Strength of Materials, chapter 2
- [Principal MI of 15 cm right triangle](https://www.bctengineer.com/question.md?key=semester_3%2Fstrength_of_materials%3Aprincipal-mi-right-triangle-15x15) — Strength of Materials, chapter 2
- [Resection point O inside triangle N, P, G](https://www.bctengineer.com/question.md?key=semester_4%2Fsurveying_ii%3Aresection-nag-triangle-2078c) — Surveying II, chapter 5
- [Timing diagram of STA, program time](https://www.bctengineer.com/question.md?key=semester_4%2Fmicroprocessor%3Atiming-sta-program-time) — Microprocessor, chapter 2
