### Rectangle Class

This program, when calculating the perimeter and area of a given size rectangle, always returns a value of 1 for perimeter and a value of 1 for area.

ex09_11
MAIN

#include "stdafx.h"
#include "Rectangle.h"
int main()
{

double l;
double w;
std::cin>>l;
std::cin>>w;
Rectangle printPerimeter(double);
Rectangle printArea (double);
std::cout<<"The perimeter of the rectangle is:"<<&Rectangle::printPerimeter;
std::cout<<"The area of the rectangle is:" <<&Rectangle::printArea;
std::cout<<std::endl;
std::cin>>l;
std::cin>>w;
return 0;
}
double Rectangle::getl()
{
return l;
}
double Rectangle::getw()
{
return w;
}
double Rectangle::printArea(double l, double w)
{
A=l*w;

cout<<"length* "<<l<<"width* "<<w<<"=area"<<A;
return w*l;
}
double Rectangle::printPerimeter(double w, double l)
{
//P=2*(l+w);
//cout<<2*l+2*w<<"=perimeter"<<P;
return 2*(w+l);
}

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
#include <iostream>

#include <iomanip>
using std::cout;
using std::cin;
using std::endl;

Rectangle.h
class declaration

#pragma once

#ifndef Rectangle_h
#define Rectangle_h

class Rectangle
{
public:
//constructor

Rectangle(void);
//default constructor

//set functions
void setl(double l);
void setw(double w);
//get functions
//Rectangle(double=1.0, double=1.0)
//{
//
double getl();
double getw();
double l;
double w;
double printPerimeter(double l, double w);
double A;
double P;
double printArea(double l, double w);

};

#endif
I thought that such code would not compile. You don't seem to create any Rectangles in main().
In these statements

std::cout<<"The perimeter of the rectangle is:"<<&Rectangle::printPerimeter;
std::cout<<"The area of the rectangle is:" <<&Rectangle::printArea;

you are trying to output address of member functions that are implicitly converted to bool values. As the addresses are not equal to zero then true (that is 1) is displayed for both the statements.
Here's what came from my new version:

1>ex09_11.obj : error LNK2019: unresolved external symbol "public: __thiscall Rectangle::Rectangle(double,double)" (??0Rectangle@@QAE@NN@Z) referenced in function _main
1>C:\Users\Jonathan\Documents\Visual Studio 2010\Projects\Rectangleclass\Debug\Rectangleclass.exe : fatal error LNK1120: 1 unresolved externals

#include "stdafx.h"

//In these statements

//std::cout<<"The perimeter of the rectangle is:"<<&Rectangle::printPerimeter;
//std::cout<<"The area of the rectangle is:" <<&Rectangle::printArea;

//you are trying to output address of member functions that are implicitly converted to bool values. As the addresses are not equal to zero then true (that is 1) is displayed for both the statements.

#include "Rectangle.h"
int main()
{

double l;
double w;
std::cin>>l;
std::cin>>w;
Rectangle perimeter;//create two Rectangle class objects
Rectangle area;//
Rectangle printPerimeter(double);
Rectangle printArea (double);
std::cout<<"The perimeter of the rectangle is:"<<perimeter.printPerimeter(l,w);
std::cout<<"The area of the rectangle is:" <<area.printArea(l,w);
std::cout<<std::endl;
std::cin>>l;
std::cin>>w;
return 0;
}
double Rectangle::getl()
{
return l;
}
double Rectangle::getw()
{
return w;
}

double Rectangle::printArea(double l, double w)
{
A=l*w;

/*cout<<"length* "<<l<<"width* "<<w<<"=area"<<A;*/
return w*l;
}
double Rectangle::printPerimeter(double w, double l)
{
//P=2*(l+w);
//cout<<2*l+2*w<<"=perimeter"<<P;
return 2*(w+l);
}

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#ifndef Rectangle_h
#define Rectangle_h

class Rectangle
{
public:
//constructor
Rectangle::Rectangle(double length =1, double width =1);

/*Rectangle(void);*/
//default constructor

//set functions
void setl(double l){
l=(l>0 && l<20)?l:0;
}
void setw(double w){
w=(w>0 && w<20)?w:0;
}
//get functions
//Rectangle(double=1.0, double=1.0)
//{
//
double getl();
double getw();
double l;
double w;
double printPerimeter(double l, double w);
double A;
double P;
double printArea(double l, double w);
};

#endif

You did not define the constructor with two parameters.
I get unresolved external symbol error LNK2019 after my last compile.

1>ex09_11.obj : error LNK2019: unresolved external symbol "public: __thiscall Rectangle::Rectangle(double,double)" (??0Rectangle@@QAE@NN@Z) referenced in function _main
1>ex09_11.obj : error LNK2001: unresolved external symbol "public: static double Rectangle::l" (?l@Rectangle@@2NA)
1>ex09_11.obj : error LNK2001: unresolved external symbol "public: static double Rectangle::w" (?w@Rectangle@@2NA)
I do no know how your upsated code looks. But in any case you should compare function declarations with their definitions. They must coinside.
When compiling this newer version of the application, I get syntax errors regarding identifiers 'l' and 'w':

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#ifndef Rectangle_h
#define Rectangle_h

class Rectangle
{
public:
//constructor
//Rectangle();
Rectangle(double length =l, double width =w);

/*Rectangle(void);*/
//default constructor

//set functions
double static l;
double static w;
void setl(l){
l=(l>0 && l<20)?l:0;
}
void setw( w){
w=(w>0 && w<20)?w:0;
}
//get functions
//Rectangle(double=1.0, double=1.0)
//{
//
double getl();
double getw();

double printPerimeter(double l, double w);
double A;
double P;
double printArea(double l, double w);
};

#endif

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
#include <iostream>

#include <iomanip>
using std::cout;
using std::cin;
using std::endl;

int main()
{

double l;
double w;
std::cin>>l;
std::cin>>w;
Rectangle perimeter;//create two Rectangle class objects
Rectangle area;//
Rectangle printPerimeter(double);
Rectangle printArea (double);
std::cout<<"The perimeter of the rectangle is:"<<perimeter.printPerimeter(l,w);
std::cout<<"The area of the rectangle is:" <<area.printArea(l,w);
std::cout<<std::endl;
std::cin>>l;
std::cin>>w;
return 0;
}
double Rectangle::getl()
{
return l;
}
double Rectangle::getw()
{
return w;
}

double Rectangle::printArea(double l, double w)
{
A=l*w;

/*cout<<"length* "<<l<<"width* "<<w<<"=area"<<A;*/
return w*l;
}
double Rectangle::printPerimeter(double w, double l)
{
//P=2*(l+w);
//cout<<2*l+2*w<<"=perimeter"<<P;
return 2*(w+l);
}
Topic archived. No new replies allowed.