Classes

Hi everyone, I need help with some coding:

Given a class Window, with integer data members width, height, xPos, and yPos, write the following two constructors: - a constructor accepting 4 integer arguments: width, height, horizontal position, and vertical position (in that order), that are used to initialize the corresponding members. - a constructor accepting 2 integer arguments: width and height (in that order), that are used to initialize the corresponding members. The xPos and yPos members should be initialized to 0.


I only need help with the interface for public, this is my code so far:

Window (int width1, int height1, int horizontal1, int vertical1)
{
}

Window (int width1, int height1)
{
width=width1;
height=height1;
xPos=0;
yPos=0;
}
what exactly do wou want to initialise with horisontal1 and horisontal2?


it might look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Class Window
{
private:
       int width;
       int height;
       int xPos;
       int yPos;
public:
       // constructors
       Window(int inWidth, int inHeight, int inXpos, int inYPos);
       Window(int inWidth, int inHeight);

       ~Window();  // destructor.
};

window::Window(int inWidth, int inHeight, int inXpos, int inYPos)
{
       width = inWidth;
       height = inHeight;
       xPos = inXpos;
       yPos = inYpos;
}

Window::Window(int inWidth, int inHeight)
{
       width = inWidth;
       height = inHeight;
       xPos = 0;
       yPos = 0;
}


I don't know if this was homework. It doesn't look like something a class assignment though to me.

this isn't the way I would implement this though.
Last edited on
initialized to 0, I need to overload the constructor.
Last edited on
Reading what you posted that is what you wanted. One constructor that accepts 4 parameters, and one that accepts two. If I am to use default parameters nothing was stated.

And the constructor are overloaded the way you specified. If you don't know what overloading in C++ means learn a little more
Last edited on
Yea it asks for a partial coding not the whole program, I know what overloading means, the submission keeps marking it wrong, and I cant think of any other way to write the code for it.
Last edited on
That is because that code is not 'initializing' the members, it is assigning the member after they have already been initialized. If you want to initialize you have to use the member initialization list, I wont post the code unless you get stuck but look up 'member initialization list'.
thats alright ill figure it out.
Last edited on
closed account (zb0S216C)
Louflow, if you believe that you've received your answer, please mark the thread as solved. If not, could you please post the relevant code so that we can offer you further advice. No rudeness intended.

Azagaros, the code you posted doesn't require a destructor. The default destructor would do just fine. Just thought you'd like to know.

Wazzak
//Here is the code:

Window (int width1, int height1, int horizontal1, int vertical1)
{
width=width1;
height=height1;
xPos=horizontal1;
yPos=vertical1;
}

Window (int width1, int height1)
{
width=width1;
height=height1;
xPos=0;
yPos=0;
}

Last edited on
closed account (zb0S216C)
Based on the code you gave, I can only suggest that you use the initialisation list, but that's already been suggested. Can you post the full code, because it's obviously not an issue with the constructors.

Wazzak
its solved with the code i just posted.
Topic archived. No new replies allowed.