C++ Program to find Area of Rectangle using constructor

By | 31.12.2016

Area of Rectangle using constructor


Write a C++ Program to find Area of Rectangle using constructor. Here’s a Simple Program to find Area of Rectangle using constructor in C++ Programming Language.


What is Class and Objects in C++?


  • Class is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating instance of that class.
  • The variables inside class definition are called as data members and the functions are called member functions.
  • Class is just a blue print, which declares and defines characteristics and behavior, namely data members and member functions respectively. And all objects of this class will share these characteristics and behavior.

  • Objects are instances of class, which holds the data variables declared in class and the member functions work on these class objects.
  • Each object has different data variables. Objects are initialized using special class functions called Constructors.

Below is the source code for C++ Program to find Area of Rectangle using constructor which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C++ Program to find Area of Rectangle using constructor  */

#include <iostream>
using namespace std;

class CRectArea
{
    private:
        int length;
        int breadth;
    public:
        CRectArea (int,int);

        int areaofrect ()
        {
            return (length * breadth);
        }
        int length1()
        {
            return length;
        }

        int breadth1()
        {
            return breadth;
        }
};


CRectArea::CRectArea(int x, int y)
{
    length = x;
    breadth = y;
}


int main ()
{
    CRectArea myrectangle (2,2);

    cout<<"The Length of Rectangle :: "<<myrectangle.length1()<<"\n";
    cout<<"\nThe Breadth of Rectangle :: "<<myrectangle.breadth1()<<"\n";
    cout << "\nThe area of rectangle is :: " << myrectangle.areaofrect()<< endl;
    return 0;
}

OUTPUT : :


/*  C++ Program to find Area of Rectangle using constructor  */

The Length of Rectangle :: 2

The Breadth of Rectangle :: 2

The area of rectangle is :: 4

Process returned 0

Above is the source code and output for C++ Program to find Area of Rectangle using constructor which is successfully compiled and run on Windows System to produce desired output.

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post….

3.6 16 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mani Sai

I need area of rectangle in destrutor by c++