C# difference between member variables and arguments passed to a method











up vote
0
down vote

favorite












I am working on an assignment with very specific instructions, which, if it is of any intrest, I will post below the code I have created. But, in short, I am to create a BasicShape abstract class, as well as a Circle and Rectangle subclasses. Each subclass has a method for calculating the area of the shape. the area is calculated using member variables. However, in my code below, these member variables are never assigned a value. I am confused as to how to assign it to them, because Circle and Rectangle methods also require arguments to be passed to them. These arguments x, y, r for Circle and w, l for Rectangle are specified in the main program when a new instance of a shape is made, but these values also seem to do nothing, since the output is always 0. What relationship do the arguments passed into the methods have to the member variables? How is it that the member variables should be assigned values when the values are given via arguments set when a Circle or Rectangle instance is called?



Here is my code:



abstract class BasicShape
{
protected double area;

public double getArea()
{
Console.WriteLine("Area: {0}", area);
return area;
}

public virtual void calcArea()
{

}
}

class Circle : BasicShape
{
private int centerX;
private int centerY;
private double radius;

public Circle(int x, int y, double r)
{
calcArea();
}


public int genCenterX()
{
return centerX;
}

public int genCenterY()
{
return centerY;
}

public override void calcArea()
{
area = 3.14159 * radius * radius;
Console.WriteLine("The area of the circle is: {0}", area);
}

}

class Rectangle : BasicShape
{
private int width;
private int length;

public Rectangle(int w, int l)
{
calcArea();
}

public int getWidth()
{
return width;
}

public int getLength()
{
return length;
}

public override void calcArea()
{
area = length * width;
Console.WriteLine("The area of the rectangle is: {0}", area);
}
}

public class TestShapes
{
static void Main(string args)
{
Circle circle1 = new Circle(2, 2, 5);
Rectangle rectangle = new Rectangle(6, 7);

Console.ReadLine();
}
}


Here are the instructions for the assignment:



Define a pure abstract base class called BasicShape.
The BasicShape class should have the following members:



Private Member Variable:
area, a double used to hold the shape’s area.



Public Member Methods:
getArea(): This method should return the value in the member variable area.
calcArea(): This method should be a pure virtual method.



Next, define a class named Circle. It should be derived from the BasicShape class. It should have the following members:



Private Member Variable:
centerX, an integer used to hold the x coordinate of the circle’s center.
centerY, an integer used to hold the y coordinate of the circle’s center.
radius, a double used to hold the circle’s radius.



Public Member Methods:



Circle(int x, int y, int r): accepts values for centerX, centerY, and radius. Should call the overridden calcArea
method described below.



genCenterX: returned the value in centerX



genCenterY: returned the value in centerY



calcArea(): calculates the area of the circle (area = 3.14159 * radius * radius) and stored the result in the inherited member area.



Next, define a class named Rectangle. It should be derived from the BasicShape class. It should have the following members:
Private Member Variable:



width, an integer used to hold the width of the rectangle
length, an integer used to hold the length of the rectangle



Public Member Methods:



Rectangle(int w, int l): accepts values for the width and length. Should call the overridden calcArea method described below.



getWidth(): returns the value in width.
getLength(): returns the value in length
calcArea(): calculates the area of the circle (area = length * width) and stored the result in the inherited member area.



After you have created these classes, create a main program that defined a Circle object and a Rectangle object.



Demonstrate that each object properly calculates and reports its area.










share|improve this question






















  • You're not assigning the parameters from the constructor to any class variable...
    – Adrian
    Nov 11 at 16:52















up vote
0
down vote

favorite












I am working on an assignment with very specific instructions, which, if it is of any intrest, I will post below the code I have created. But, in short, I am to create a BasicShape abstract class, as well as a Circle and Rectangle subclasses. Each subclass has a method for calculating the area of the shape. the area is calculated using member variables. However, in my code below, these member variables are never assigned a value. I am confused as to how to assign it to them, because Circle and Rectangle methods also require arguments to be passed to them. These arguments x, y, r for Circle and w, l for Rectangle are specified in the main program when a new instance of a shape is made, but these values also seem to do nothing, since the output is always 0. What relationship do the arguments passed into the methods have to the member variables? How is it that the member variables should be assigned values when the values are given via arguments set when a Circle or Rectangle instance is called?



Here is my code:



abstract class BasicShape
{
protected double area;

public double getArea()
{
Console.WriteLine("Area: {0}", area);
return area;
}

public virtual void calcArea()
{

}
}

class Circle : BasicShape
{
private int centerX;
private int centerY;
private double radius;

public Circle(int x, int y, double r)
{
calcArea();
}


public int genCenterX()
{
return centerX;
}

public int genCenterY()
{
return centerY;
}

public override void calcArea()
{
area = 3.14159 * radius * radius;
Console.WriteLine("The area of the circle is: {0}", area);
}

}

class Rectangle : BasicShape
{
private int width;
private int length;

public Rectangle(int w, int l)
{
calcArea();
}

public int getWidth()
{
return width;
}

public int getLength()
{
return length;
}

public override void calcArea()
{
area = length * width;
Console.WriteLine("The area of the rectangle is: {0}", area);
}
}

public class TestShapes
{
static void Main(string args)
{
Circle circle1 = new Circle(2, 2, 5);
Rectangle rectangle = new Rectangle(6, 7);

Console.ReadLine();
}
}


Here are the instructions for the assignment:



Define a pure abstract base class called BasicShape.
The BasicShape class should have the following members:



Private Member Variable:
area, a double used to hold the shape’s area.



Public Member Methods:
getArea(): This method should return the value in the member variable area.
calcArea(): This method should be a pure virtual method.



Next, define a class named Circle. It should be derived from the BasicShape class. It should have the following members:



Private Member Variable:
centerX, an integer used to hold the x coordinate of the circle’s center.
centerY, an integer used to hold the y coordinate of the circle’s center.
radius, a double used to hold the circle’s radius.



Public Member Methods:



Circle(int x, int y, int r): accepts values for centerX, centerY, and radius. Should call the overridden calcArea
method described below.



genCenterX: returned the value in centerX



genCenterY: returned the value in centerY



calcArea(): calculates the area of the circle (area = 3.14159 * radius * radius) and stored the result in the inherited member area.



Next, define a class named Rectangle. It should be derived from the BasicShape class. It should have the following members:
Private Member Variable:



width, an integer used to hold the width of the rectangle
length, an integer used to hold the length of the rectangle



Public Member Methods:



Rectangle(int w, int l): accepts values for the width and length. Should call the overridden calcArea method described below.



getWidth(): returns the value in width.
getLength(): returns the value in length
calcArea(): calculates the area of the circle (area = length * width) and stored the result in the inherited member area.



After you have created these classes, create a main program that defined a Circle object and a Rectangle object.



Demonstrate that each object properly calculates and reports its area.










share|improve this question






















  • You're not assigning the parameters from the constructor to any class variable...
    – Adrian
    Nov 11 at 16:52













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am working on an assignment with very specific instructions, which, if it is of any intrest, I will post below the code I have created. But, in short, I am to create a BasicShape abstract class, as well as a Circle and Rectangle subclasses. Each subclass has a method for calculating the area of the shape. the area is calculated using member variables. However, in my code below, these member variables are never assigned a value. I am confused as to how to assign it to them, because Circle and Rectangle methods also require arguments to be passed to them. These arguments x, y, r for Circle and w, l for Rectangle are specified in the main program when a new instance of a shape is made, but these values also seem to do nothing, since the output is always 0. What relationship do the arguments passed into the methods have to the member variables? How is it that the member variables should be assigned values when the values are given via arguments set when a Circle or Rectangle instance is called?



Here is my code:



abstract class BasicShape
{
protected double area;

public double getArea()
{
Console.WriteLine("Area: {0}", area);
return area;
}

public virtual void calcArea()
{

}
}

class Circle : BasicShape
{
private int centerX;
private int centerY;
private double radius;

public Circle(int x, int y, double r)
{
calcArea();
}


public int genCenterX()
{
return centerX;
}

public int genCenterY()
{
return centerY;
}

public override void calcArea()
{
area = 3.14159 * radius * radius;
Console.WriteLine("The area of the circle is: {0}", area);
}

}

class Rectangle : BasicShape
{
private int width;
private int length;

public Rectangle(int w, int l)
{
calcArea();
}

public int getWidth()
{
return width;
}

public int getLength()
{
return length;
}

public override void calcArea()
{
area = length * width;
Console.WriteLine("The area of the rectangle is: {0}", area);
}
}

public class TestShapes
{
static void Main(string args)
{
Circle circle1 = new Circle(2, 2, 5);
Rectangle rectangle = new Rectangle(6, 7);

Console.ReadLine();
}
}


Here are the instructions for the assignment:



Define a pure abstract base class called BasicShape.
The BasicShape class should have the following members:



Private Member Variable:
area, a double used to hold the shape’s area.



Public Member Methods:
getArea(): This method should return the value in the member variable area.
calcArea(): This method should be a pure virtual method.



Next, define a class named Circle. It should be derived from the BasicShape class. It should have the following members:



Private Member Variable:
centerX, an integer used to hold the x coordinate of the circle’s center.
centerY, an integer used to hold the y coordinate of the circle’s center.
radius, a double used to hold the circle’s radius.



Public Member Methods:



Circle(int x, int y, int r): accepts values for centerX, centerY, and radius. Should call the overridden calcArea
method described below.



genCenterX: returned the value in centerX



genCenterY: returned the value in centerY



calcArea(): calculates the area of the circle (area = 3.14159 * radius * radius) and stored the result in the inherited member area.



Next, define a class named Rectangle. It should be derived from the BasicShape class. It should have the following members:
Private Member Variable:



width, an integer used to hold the width of the rectangle
length, an integer used to hold the length of the rectangle



Public Member Methods:



Rectangle(int w, int l): accepts values for the width and length. Should call the overridden calcArea method described below.



getWidth(): returns the value in width.
getLength(): returns the value in length
calcArea(): calculates the area of the circle (area = length * width) and stored the result in the inherited member area.



After you have created these classes, create a main program that defined a Circle object and a Rectangle object.



Demonstrate that each object properly calculates and reports its area.










share|improve this question













I am working on an assignment with very specific instructions, which, if it is of any intrest, I will post below the code I have created. But, in short, I am to create a BasicShape abstract class, as well as a Circle and Rectangle subclasses. Each subclass has a method for calculating the area of the shape. the area is calculated using member variables. However, in my code below, these member variables are never assigned a value. I am confused as to how to assign it to them, because Circle and Rectangle methods also require arguments to be passed to them. These arguments x, y, r for Circle and w, l for Rectangle are specified in the main program when a new instance of a shape is made, but these values also seem to do nothing, since the output is always 0. What relationship do the arguments passed into the methods have to the member variables? How is it that the member variables should be assigned values when the values are given via arguments set when a Circle or Rectangle instance is called?



Here is my code:



abstract class BasicShape
{
protected double area;

public double getArea()
{
Console.WriteLine("Area: {0}", area);
return area;
}

public virtual void calcArea()
{

}
}

class Circle : BasicShape
{
private int centerX;
private int centerY;
private double radius;

public Circle(int x, int y, double r)
{
calcArea();
}


public int genCenterX()
{
return centerX;
}

public int genCenterY()
{
return centerY;
}

public override void calcArea()
{
area = 3.14159 * radius * radius;
Console.WriteLine("The area of the circle is: {0}", area);
}

}

class Rectangle : BasicShape
{
private int width;
private int length;

public Rectangle(int w, int l)
{
calcArea();
}

public int getWidth()
{
return width;
}

public int getLength()
{
return length;
}

public override void calcArea()
{
area = length * width;
Console.WriteLine("The area of the rectangle is: {0}", area);
}
}

public class TestShapes
{
static void Main(string args)
{
Circle circle1 = new Circle(2, 2, 5);
Rectangle rectangle = new Rectangle(6, 7);

Console.ReadLine();
}
}


Here are the instructions for the assignment:



Define a pure abstract base class called BasicShape.
The BasicShape class should have the following members:



Private Member Variable:
area, a double used to hold the shape’s area.



Public Member Methods:
getArea(): This method should return the value in the member variable area.
calcArea(): This method should be a pure virtual method.



Next, define a class named Circle. It should be derived from the BasicShape class. It should have the following members:



Private Member Variable:
centerX, an integer used to hold the x coordinate of the circle’s center.
centerY, an integer used to hold the y coordinate of the circle’s center.
radius, a double used to hold the circle’s radius.



Public Member Methods:



Circle(int x, int y, int r): accepts values for centerX, centerY, and radius. Should call the overridden calcArea
method described below.



genCenterX: returned the value in centerX



genCenterY: returned the value in centerY



calcArea(): calculates the area of the circle (area = 3.14159 * radius * radius) and stored the result in the inherited member area.



Next, define a class named Rectangle. It should be derived from the BasicShape class. It should have the following members:
Private Member Variable:



width, an integer used to hold the width of the rectangle
length, an integer used to hold the length of the rectangle



Public Member Methods:



Rectangle(int w, int l): accepts values for the width and length. Should call the overridden calcArea method described below.



getWidth(): returns the value in width.
getLength(): returns the value in length
calcArea(): calculates the area of the circle (area = length * width) and stored the result in the inherited member area.



After you have created these classes, create a main program that defined a Circle object and a Rectangle object.



Demonstrate that each object properly calculates and reports its area.







c# inheritance methods arguments member






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 16:49









ZL4892

495




495












  • You're not assigning the parameters from the constructor to any class variable...
    – Adrian
    Nov 11 at 16:52


















  • You're not assigning the parameters from the constructor to any class variable...
    – Adrian
    Nov 11 at 16:52
















You're not assigning the parameters from the constructor to any class variable...
– Adrian
Nov 11 at 16:52




You're not assigning the parameters from the constructor to any class variable...
– Adrian
Nov 11 at 16:52












1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










You don't assign the values passed from the constructor to your member variables. So when you call calcArea you execute it using the default values for the types int or double (which is zero)



class Circle : BasicShape
{
private int centerX;
private int centerY;
private double radius;

public Circle(int x, int y, double r)
{
radius = r;
// Now you are executing the calcArea using the value passed in
calcArea();
}
....
}

class Rectangle : BasicShape
{
private int width;
private int length;

public Rectangle(int w, int l)
{
width = w;
length = l;
calcArea();
}
....
}


The override of calcArea needs the member variables to be set to something otherwise these member variables are initialized with their default values (zero in both integer and double) and thus the method cannot produce a meaningful result.






share|improve this answer





















  • Ahh ok. So you use, for example, radius = r to assign the value passed to the variable. I didn't know that could be done. Thank you.
    – ZL4892
    Nov 11 at 17:07










  • private member variables are just variables that are used internally in your class for various purposes (like keeping the state of the class instance) and are hidden from the outside of the class. They can be used in the same way you use any other variable.
    – Steve
    Nov 11 at 18:16











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250968%2fc-sharp-difference-between-member-variables-and-arguments-passed-to-a-method%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










You don't assign the values passed from the constructor to your member variables. So when you call calcArea you execute it using the default values for the types int or double (which is zero)



class Circle : BasicShape
{
private int centerX;
private int centerY;
private double radius;

public Circle(int x, int y, double r)
{
radius = r;
// Now you are executing the calcArea using the value passed in
calcArea();
}
....
}

class Rectangle : BasicShape
{
private int width;
private int length;

public Rectangle(int w, int l)
{
width = w;
length = l;
calcArea();
}
....
}


The override of calcArea needs the member variables to be set to something otherwise these member variables are initialized with their default values (zero in both integer and double) and thus the method cannot produce a meaningful result.






share|improve this answer





















  • Ahh ok. So you use, for example, radius = r to assign the value passed to the variable. I didn't know that could be done. Thank you.
    – ZL4892
    Nov 11 at 17:07










  • private member variables are just variables that are used internally in your class for various purposes (like keeping the state of the class instance) and are hidden from the outside of the class. They can be used in the same way you use any other variable.
    – Steve
    Nov 11 at 18:16















up vote
3
down vote



accepted










You don't assign the values passed from the constructor to your member variables. So when you call calcArea you execute it using the default values for the types int or double (which is zero)



class Circle : BasicShape
{
private int centerX;
private int centerY;
private double radius;

public Circle(int x, int y, double r)
{
radius = r;
// Now you are executing the calcArea using the value passed in
calcArea();
}
....
}

class Rectangle : BasicShape
{
private int width;
private int length;

public Rectangle(int w, int l)
{
width = w;
length = l;
calcArea();
}
....
}


The override of calcArea needs the member variables to be set to something otherwise these member variables are initialized with their default values (zero in both integer and double) and thus the method cannot produce a meaningful result.






share|improve this answer





















  • Ahh ok. So you use, for example, radius = r to assign the value passed to the variable. I didn't know that could be done. Thank you.
    – ZL4892
    Nov 11 at 17:07










  • private member variables are just variables that are used internally in your class for various purposes (like keeping the state of the class instance) and are hidden from the outside of the class. They can be used in the same way you use any other variable.
    – Steve
    Nov 11 at 18:16













up vote
3
down vote



accepted







up vote
3
down vote



accepted






You don't assign the values passed from the constructor to your member variables. So when you call calcArea you execute it using the default values for the types int or double (which is zero)



class Circle : BasicShape
{
private int centerX;
private int centerY;
private double radius;

public Circle(int x, int y, double r)
{
radius = r;
// Now you are executing the calcArea using the value passed in
calcArea();
}
....
}

class Rectangle : BasicShape
{
private int width;
private int length;

public Rectangle(int w, int l)
{
width = w;
length = l;
calcArea();
}
....
}


The override of calcArea needs the member variables to be set to something otherwise these member variables are initialized with their default values (zero in both integer and double) and thus the method cannot produce a meaningful result.






share|improve this answer












You don't assign the values passed from the constructor to your member variables. So when you call calcArea you execute it using the default values for the types int or double (which is zero)



class Circle : BasicShape
{
private int centerX;
private int centerY;
private double radius;

public Circle(int x, int y, double r)
{
radius = r;
// Now you are executing the calcArea using the value passed in
calcArea();
}
....
}

class Rectangle : BasicShape
{
private int width;
private int length;

public Rectangle(int w, int l)
{
width = w;
length = l;
calcArea();
}
....
}


The override of calcArea needs the member variables to be set to something otherwise these member variables are initialized with their default values (zero in both integer and double) and thus the method cannot produce a meaningful result.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 16:55









Steve

178k16152216




178k16152216












  • Ahh ok. So you use, for example, radius = r to assign the value passed to the variable. I didn't know that could be done. Thank you.
    – ZL4892
    Nov 11 at 17:07










  • private member variables are just variables that are used internally in your class for various purposes (like keeping the state of the class instance) and are hidden from the outside of the class. They can be used in the same way you use any other variable.
    – Steve
    Nov 11 at 18:16


















  • Ahh ok. So you use, for example, radius = r to assign the value passed to the variable. I didn't know that could be done. Thank you.
    – ZL4892
    Nov 11 at 17:07










  • private member variables are just variables that are used internally in your class for various purposes (like keeping the state of the class instance) and are hidden from the outside of the class. They can be used in the same way you use any other variable.
    – Steve
    Nov 11 at 18:16
















Ahh ok. So you use, for example, radius = r to assign the value passed to the variable. I didn't know that could be done. Thank you.
– ZL4892
Nov 11 at 17:07




Ahh ok. So you use, for example, radius = r to assign the value passed to the variable. I didn't know that could be done. Thank you.
– ZL4892
Nov 11 at 17:07












private member variables are just variables that are used internally in your class for various purposes (like keeping the state of the class instance) and are hidden from the outside of the class. They can be used in the same way you use any other variable.
– Steve
Nov 11 at 18:16




private member variables are just variables that are used internally in your class for various purposes (like keeping the state of the class instance) and are hidden from the outside of the class. They can be used in the same way you use any other variable.
– Steve
Nov 11 at 18:16


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250968%2fc-sharp-difference-between-member-variables-and-arguments-passed-to-a-method%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

Bicuculline

さくらももこ