How do you make a constructor? (Novice Level)?
-
I am currently working on this problem for a class and i can't get passed the part where i have to make a constructor. Can anybody show me how to do this problem or explain to me how to make a constructor? Thank you! Write the definition of a class Counter containing: An instance variable named counter of type int . A constructor that takes one int argument and assigns its value to counter A method named increment that adds one to counter . It does not take parameters or return a value. A method named decrement that subtracts one from counter . It also does not take parameters or return a value. A method named getValue that returns the value of the instance variable counter .
-
Answer:
class Counter { int counter ; void increment() { counter++ ; } void decrement() { counter-- ; } int getValue() { return counter ; } public: Counter(int x) // constructor has same name as that of class and NO return type { counter=x; } } ; Hope it helps....
Chris at Yahoo! Answers Visit the source
Other answers
copy and paste this and it will work perfect public class Counter { private int counter; public Counter(int x) { counter=x; } public void increment() { counter++ ; } public void decrement() { counter-- ; } public int getValue() { return counter ; } }
David
Related Q & A:
- How do I make a python web program that is on a ubuntu server allow access to the server?Best solution by Yahoo! Answers
- How do i make a my yahoo but not a home page?Best solution by Yahoo! Answers
- How do you make a link into a clickable picture?Best solution by Yahoo! Answers
- How do you make a book for a kindergarten describing how a burrito gets digested?Best solution by answers.yahoo.com
- How can anybody make a liven on a $8.00 an hour job?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.