Hello,

I am currently a sophomore CS major and have a C++ assignment I need help with. I have browsed the boards to see if asking for hw help was appropriate, and I did not find anything saying that it was not. If this is not appropriate, please let mek now and I will remove the post.


Add the following constructors to your application:

Constructor Parameter Instance Variables Value
Default None x 1
y 1
height 10
width 10
Three Member newX x newX
newY y newY
size height size
width size
Four Member newX x newX
newY y newY
newHeight height newHeight
newWidth width newWidth

In each constructor, add cout statements that indicate which constructor is executing.


<my program so far>

//  Lab 6
// Class header
// Rectangle.h

class Rectangle {
public:
	Rectangle(double x=1, double y=1, double Height=10, double Width=10);
	
	double getArea();
	double getPerimeter();
	void draw();
	
	double getX();
	double getY();
	double getHeight();
	double getWidth();
	
	void setX(int a);
	void setY(int a);
	void setHeight(int a);
	void setWidth(int a);

private:
	double x;
	double y;
	double Height;
	double Width;
};

// Lab 6
// Class implementation
// Rectangle.cpp

#include "Rectangle.h"
#include <iostream>
using namespace std;

double Rectangle::getArea() {
	return Height*Width;
}
double Rectangle::getPerimeter(){
	return 2*Height+2*Width;
}
void Rectangle::draw(){
	cout << "Rectangle\n";
}	
double Rectangle::getX(){
	return x;
}
double Rectangle::getY(){
	return y;
}
double Rectangle::getHeight(){
	return Height;
}
double Rectangle::getWidth(){
	return Width;
}
void Rectangle::setX(int a){
	if(a>0) {
		x=a;
	}
}
void Rectangle::setY(int a){
	if(a>0) {
		y=a;
	}
}
void Rectangle::setHeight(double a){
	double a;
	if(a >0) {
		Height=a;
	}
}
void Rectangle::setWidth(double a){
	double a;
	if(a>0) {
		Width=a;
	}
}

//  Lab 6
// Main function
// TestRectangle.cpp


#include <iostream>
#include "rectangle.h"
using namespace std;

void main() {
	Rectangle r1;

	r1.setHeight(30);
	r1.setWidth(10);
	r1.setX(100);
	r1.setY(150);

	cout << "Rectangle Settings" << endl;
	cout << "X: " << r1.getX();
	cout << "\tY: " << r1.getY();
	cout << "\tHeight: " << r1.getHeight();
	cout << "\tWidth: " << r1.getWidth();
	cout << "\tArea: " << r1.getArea();
	cout << "\tPerimeter: " << r1.getPerimeter();
	cout << endl << endl;

}

I'm not looking for someone to do it all for me, but some advice would be great on how to implement the constructors. I am clueless on this atm.

Thanks in advance!

This is a declaration.

Rectangle(double x=1, double y=1, double Height=10, double Width=10);

The constructor still needs to be defined, just like any other method/function does. I would put the defaults in an initialization list and define it inline within the body of the declaration given the body of the constructor will be a simple cout statement. You could try something like this :

Rectangle() : x(1.0), y(1.0), Height(10.0), Width(10.0)
{ cout << "default constructor called" << endl;}

The four member constructor seems relatively straightforward. Place four parameters of type double with the parameter list, naming them NewX, NewY, NewHeight, NewWidth and then withing the initialization list place NewX in the parenthesis after x, NewY in the parenthesis after y, etc., with an appropriate output statement in the body of the constructor.

I'm not sure what the parameters for the three parameter version would be as it looks like you have all four member variables listed in the instructions you have posted.

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.