Create a class named Triangle having the following attributes: base (double) , height(double) oop programming-allpakhub

Create a class named Triangle having the following attributes: base (double) , height(double) oop programming-allpakhub


We are learning about the following things by this example:

  • Classes in C++
  • Constructors
  • Parameterized Constructors
  • Generic Data Input

 

Task Example:

QUESTION:

Create a class named Triangle having the following attributes:

1.      base (double)

2.      height(double)

Now,

       Write an overloaded6 function input () that takes input from the user.

       Write a non-returning display () function to print.

       Write a function calculateArea() which calculates the area of the triangle.


Now write a program to create two objects of Triangle in main (). Take input and then calculate the area of both of the objects. Display the complete information including the area on the console.

 

Solution

#include <iostream>

using namespace std;

class AreaTriangle

{

private:

            double base, height;

public:

            AreaTriangle()

            {

                        base = 0.0;

                        height = 0.0;

            }

            AreaTriangle(double bs, double ht)

            {

                        base = bs;

                        height = ht;

            }

 

            void Setbase(double bs)

            {

                        base = bs;

            }

            void Setbase(int bs)                // Function Overloading to change datatype by writing one more setter

            {

                        base = bs;

            }

            void Setheight(double ht)

            {

                        height = ht;

            }

            void Setheight(int ht)                          // Function Overloading to change datatype by writing one more setter

            {                                                                                               // But output will be in origional datatype...like in double

                        height = ht;

            }

            double Getbase()

            {

                        return base;

            }

            double Getheight()

            {

                        return height;

            }

 

            inline void display()  //Function Overloading

            {

                        double area;

                        area = 0.5* (base*height);

                        cout << "Area of Triangle is: " << area << endl << endl;

            }

            ~AreaTriangle() // destructor

            {

 

            }

};

int main()

{

            double a, b, base, height;

            cout << "Enter base in float: ";

            cin >> a;

            cout << "Enter height in float: ";

            cin >> b;

            AreaTriangle obj;

            obj.Setbase(a);

            obj.Setheight(b);

            obj.display();  //Function Overloading

 

            system("pause");

            return 0;

}



 

That's exactly what today's post is all about. We hope you'll like it. You can also message us to see more posts, and if you like it, be sure to share it with your friends. Thank you very much for reading.

 

-----------------------------------------------------------------------------------

Post a Comment

0 Comments