Hi guys !
I am trying to find out how to do this problem using construction with useful value , here is

Can I use cons traction with

public :
            void set_value (int e, int r)
           
               {
                           q=e;
                           w=r;
               }

My code

 
/*
Q: Write a C++ program to print the area of rectangle using class constructor?

*/
#include<iostream>
using namespace std;
 
class rectangle
{
     int q,w ;
     char o;
     public :
            void set_value (int e, int r)
           
               {
                           q=e;
                           w=r;
               }
               rectangle();    // my construction         
             int area()
             
               {
                       return q*w ;
                       }
                       
                       };
 Rectangle::rectangle ()// I could not do it with useful values so I use this sentence     
{
                     cout<<"Welcome to Area Calulater \n\n";                    
                     
                     
                     }
int main()
{
    int t,y,u;
    char i;
    rectangle rect;
    cout << "Enter a length:\n";
    cin>>t;
    cout << "Enter a width:\n "  ;
    cin >> y;
    rect.set_value(t,y);
    u = rect.area();
    cout<< "area of the rectangle ="<<u;
    cin>>i;
    return 0;
    }
/*
Q: Write a C++ program to print the area of rectangle using class constructor?
 
*/
#include<iostream>
using namespace std;
 
class rectangle
{
     int q,w ;
     char o;
     public :
               rectangle(int e=0, int r=0)
               {
                           q=e;
                           w=r;
               }         
             int area()
             
               {
                       return q*w ;
               }
                       
};

int main()
{
    int t,y,u;
    char i;
    ;
    cout << "Enter a length:\n";
    cin>>t;
    cout << "Enter a width:\n "  ;
    cin >> y;
    rectangle rect(t,y);
    u = rect.area();
    cout<< "area of the rectangle ="<<u;
    cin>>i;
    return 0;
    }

why did you give 0s?

public :               rectangle(int e=0, int r=0)               {                           q=e;                           w=r;               }
rectangle(int e=0, int r=0)

Means default value of e and r are 0 and 0. So you can call same constructor in number of ways.

rectangle r1(); // q=0,w=0
// or
rectangle r2(4); // q=4,w=0
// or
rectangle r3(4,3); // q=4,w=3

i got it , thanks a lot

or you can use a version similar to this one :

class Rectange
{
    private:
    int q,w;
    char o;
    void ToString();
    public:
     Rectange(int,int);
};

void Rectange::ToString()
{
    cout << "Q >> " << q << " W >> "  << w << endl;
}

Rectange::Rectange(int qq=0,int ww=0)
{
    q = qq;
    w = ww;
    ToString();
}
/*
Q: Write a C++ program to print the area of rectangle using class constructor?
 
*/
#include<iostream>
using namespace std;
 
class rectangle
{
     int q,w ;
     char o;
     public :
               rectangle(int e=0, int r=0)
               {
                           q=e;
                           w=r;
               }         
             int area()
             
               {
                       return q*w ;
               }
                       
};

int main()
{
    int t,y,u;
    char i;
    ;
    cout << "Enter a length:\n";
    cin>>t;
    cout << "Enter a width:\n "  ;
    cin >> y;
    rectangle rect(t,y);
    u = rect.area();
    cout<< "area of the rectangle ="<<u;
    cin>>i;
    return 0;
    }

what is this ?

int main()
{
    int t,y,u;
    char i;
    ;
    cout << "Enter a length:\n";
    cin>>t;
    cout << "Enter a width:\n "  ;
    cin >> y;
    rectangle rect(t,y);    /* what is this line? creating object rect? if so where is       calling the  constructore? ?  like object_name. function_name*/
    u = rect.area();
    cout<< "area of the rectangle ="<<u;
    cin>>i;
    return 0;
    }

Line 10 create a object of "rectangle" and will call constructor internally.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.