Area of Rectangle using Constructor in C++


Example Program:

Here is the simple program for printing the area of rectangle using constructor in C++.

Source Code

#include<iostream>
using namespace std;
class Area
{
  private:
    int length;
    int breadth;
  public:
    Area(): length(7), breadth(4){ }
    void GetLength()
    {
      cout<<"\nEnter length of the Rectangle: ";
      cin>>length;
      cout<<"\nEnter breadth of the Rectangle: ";
      cin>>breadth;
    }
    int Calculation()
    {
      return (length*breadth);
    }
    void Display(int temp)
    {
      cout<<"Area: "<<temp;
    }
};
int main()
{
  Area A1,A2;
  int temp;
  A1.GetLength();
  temp=A1.Calculation();
  A1.Display(temp);
  cout<<endl<<"\nDefault Area when value is not taken from user"<<endl;
  temp=A2.Calculation();
  A2.Display(temp);
  return 0;
}
To download raw file Click Here

Output

Enter length of the Rectangle: 20
Enter breadth of the Rectangle: 10
Area: 200
Default Area when value is not taken from user
Area: 28

Basic Programs


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples



Learn All in Tamil © Designed & Developed By Tutor Joes