C++ program to calculate area of rectangle using constructor


Asked by admin @ in Computer Science viewed by 346 People


C++ program to calculate area of traingle circle and rectangle using constructor overloading

Answered by admin @



Q. Write a C++ program to calculate the area of triangle, rectangle and circle using constructor overloading. The program should be menu driven.

Answer:

Constructors have same name of the class but with number of arguments. Constructors can be overloaded.

Following program is displaying the work of overloaded constructors.

#include<iostream>

#include<math.h>

#include<cstdlib>

using namespace std;

class area

{

float ar;

public:

area(float r)

{

ar=3.14*r*r;

}

area(float l, float b)

{

ar=l*b;

}

area(float a, float b, float c)

{

float s;

s=(a+b+c)/2;

ar=s*(s-a)*(s-b)*(s-c);

ar=pow(ar,0.5);

}

void display()

{

cout<<"\n Area : "<<ar;

}

};

int main()

{

int ch;

float x, y, z;

do

{

<<"\n\n 1. Area of Circle";

cout<<"\n 2. Area of Rectangle";

cout<<"\n 3. Area of Triangle";

cout<<"\n 4. Exit";

cout<<"\n\n Enter Your Choice : ";

cin>>ch;

switch(ch)

{

case 1 :

{

cout<<"\n Enter Radius of the Circle : ";

cin>>x;

area a1(x); //Class area, object is created : a1

a1.display();

}

break;

case 2 :

{

cout<<"\n Enter Length and Breadth of the Rectangle : ";

cin>>x>>y;

area a2(x,y);

a2.display();

}

break;

case 3 :

{

cout<<"\n Enter Sides of the Triangle : ";

cin>>x>>y>>z;

area a3(x,y,z);

a3.display();

}

break;

case 4 :

exit(0);

default :

cout<<"\n\n Invalid Choice ...";

}

} while(ch!=4);

return 0;

}


Similar Questions

Write a program to calculate area of circle in c++

Asked by admin @ in Computers and Technology viewed by 310 persons

I want to write a C++ program to read the diameter of a circle. The program should find and display the area and circumference of the circle. Define a constant …

Write a c++ program to calculate gpa in a semester

Asked by admin @ in Computers and Technology viewed by 351 persons

Write a C++ program that opens and reads file question2.txt. File question1.txt has 3 students and 3 grades of each. You program should read values from file and assign values …

C++ program to add two complex numbers using class

Asked by admin @ in Computer Science viewed by 398 persons

Write a c++ program to perform addition of two complex numbers using classes​

C program to print number in words using switch case

Asked by admin @ in Computer Science viewed by 357 persons

C program to display the number in words using switch case where a sample input and output is given

C++ program to perform arithmetic operations using objects and classes

Asked by admin @ in General viewed by 413 persons

How do you write a C++ program to find the arithmetic operations on complex numbers using operator overloading using the C++ language?

Which of the following could be used to calculate the area of the sector in the circle shown above?

Asked by maham237 @ in Mathematics viewed by 339 persons

Circle A is shown with a central angle marked 40 degrees and the radius marked 15 inches.

C programing

Asked by pranav94 @ in Engineering viewed by 469 persons

How to calculate the surface area of a hexagonal prism

Asked by admin @ in Mathematics viewed by 370 persons

The figure shows a regular hexagonal prism. The figure shows a regular hexagonal prism. The side of the hexagonal face is labeled as 4 centimeters, the length of the rectangular …

How to find the lateral area of a prism calculator

Asked by admin @ in Mathematics viewed by 393 persons

1. use formulas to find the lateral area and surface area of the given prism. Round your answer to the nearest whole number. Use the large 12 * 7 m …

How to find the surface area of a prism calculator

Asked by admin @ in Mathematics viewed by 464 persons

Surface area of the semicircular right prism is _____inches squared

How do u calculate the surface area of a cylinder

Asked by admin @ in Mathematics viewed by 288 persons

How do you find the surface area of a cylinder

How do you calculate the surface area of a sphere

Asked by admin @ in Mathematics viewed by 277 persons

3. The formula to find the surface area of a sphere is to take the radius of the sphere and square it and multiply that amount by pi and then …

How to calculate the area and perimeter of a circle

Asked by admin @ in Physics viewed by 283 persons

Write an algorithm and draw the flowchart of finding the area and perimeter of a circle. The radius (float) need to be entered by the user and if the user …

Which program is used to create a presentation

Asked by admin @ in Computer Science viewed by 330 persons

Which application software is used to create presentation? (a). OpenOffice impress(b). OpenOffice calc(c). OpenOffice writer(d). None of these​

What are variables used for in javascript programs

Asked by admin @ in Computer Science viewed by 333 persons

What are variables used for in javascript programs mcq?

Most viewed questions in Computer Science


A programmer mistakenly writes gor instead of for

Asked by admin @ in Computer Science viewed by 19016 persons


Domestic data entry operator ncert book class 9 solutions

Asked by admin @ in Computer Science viewed by 15794 persons


Information technology code 402 class 9 solutions chapter 1

Asked by admin @ in Computer Science viewed by 10964 persons



Ncert solutions for class 8 computer chapter 1 computer system

Asked by admin @ in Computer Science viewed by 10397 persons


A scrum team works on a 4 weeks sprint

Asked by admin @ in Computer Science viewed by 8716 persons


Which of the following occupies more memory in c

Asked by admin @ in Computer Science viewed by 8127 persons



A database of motor vehicles has the base entity

Asked by admin @ in Computer Science viewed by 7837 persons


The network architecture can be termed as a mcq

Asked by admin @ in Computer Science viewed by 6480 persons


Cat5 and cat6 cabling refer to which reference model layer

Asked by admin @ in Computer Science viewed by 5785 persons



Class 7 computer chapter 3 questions and answers

Asked by admin @ in Computer Science viewed by 5684 persons


Choose the correct options about usability and user experience

Asked by admin @ in Computer Science viewed by 5471 persons


Which advocates daily team meetings for coordination and integration

Asked by admin @ in Computer Science viewed by 4977 persons



Which three security features match the database security level

Asked by admin @ in Computer Science viewed by 4967 persons


Which of the dbms package is not available

Asked by admin @ in Computer Science viewed by 4680 persons


________ aid a business grow and attain its objectives

Asked by admin @ in Computer Science viewed by 4483 persons