constructor

stackoverflow error in class constructor

↘锁芯ラ 提交于 2019-12-17 06:18:50
问题 Please excuse what is probably a very basic question, but I am writing a program to store employee info and it works fine until it tries to set the info inside my employee class. It gives a stackoverflow error and I cannot figure out why. Thanks for any help. Main class: import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner Input = new Scanner(System.in); System.out.println("Enter the number of employees to enter."); int employeeCount = Input.nextInt()

Why won't my public void Constructor {} compile?

痞子三分冷 提交于 2019-12-17 06:18:09
问题 I have an assignment that requires a bank account to be able to transfer funds from a checking and savings account. The transactions are stored in an ArrayList and set up for the user to specify when to transfer the funds. The bank account class for checking and savings works OK, but the TransferService class I created isn't compiling properly in NetBeans. The hints don't seem to be fixing the errors. I get the error: Transaction is abstract and cannot be instantiated. How can I fix this

Why is protected constructor raising an error this this code?

ぐ巨炮叔叔 提交于 2019-12-17 06:13:32
问题 One question about protected constructor. I learnt that the protected constructor can be used in the derived class. How ever, I found the code below has an error. Why does it happen like this? class A { protected: A(){} }; class B: public A { public: B() { A* f=new A(); // Why it is not working here } }; 回答1: This has nothing to do with constructors specifically. This is just how protected access works. The way protected access specifier works, it allows the derived class B to access the

What's the most reliable way to prohibit a copy constructor in C++?

半腔热情 提交于 2019-12-17 06:10:01
问题 Sometimes it's necessary to prohibit a copy constructor in a C++ class so that class becomes "non-copyable". Of course, operator= should be prohibited at the same time. So far I've seen two ways to do that. Way 1 is to declare the method private and give it no implementation: class Class { //useful stuff, then private: Class( const Class& ); //not implemented anywhere void operator=( const Class& ); //not implemented anywhere }; Way 2 is to declare the method private and give it "empty"

How much work should be done in a constructor?

烂漫一生 提交于 2019-12-17 05:01:09
问题 Should operations that could take some time be performed in a constructor or should the object be constructed and then initialised later. For example when constructing an object that represents a directory structure should the population of the object and its children be done in the constructor. Clearly, a directory can contain directories and which in turn can contain directories and so on. What is the elegant solution to this? 回答1: Historically, I have coded my constructors so that the

Scope of variables in if statements

佐手、 提交于 2019-12-17 04:30:35
问题 I have a class that has no default constructor or assignment operator so it is declared and initialized within an if/else statement depending on the result of another function. But then it says that it is out of scope later even though both routes of the conditional will create an instance. Consider the following example (done with int just to illustrate the point): #include <iostream> int main() { if(1) { int i = 5; } else { int i = 0; } std::cout << i << std::endl; return 0; } Do variables

In Ruby, what's the relationship between 'new' and 'initialize'? How to return nil while initializing?

烈酒焚心 提交于 2019-12-17 04:18:51
问题 What I want is: obj = Foo.new(0) # => nil or false This doesn't work: class Foo def initialize(val) return nil if val == 0 end end I know in C/C++/Java/C#, we cant return a value in a constructor. But I'm wondering whether it is possible in Ruby. 回答1: In Ruby, what's the relationship between ' new ' and ' initialize '? new typically calls initialize . The default implementation of new is something like: class Class def new(*args, &block) obj = allocate obj.initialize(*args, &block) # actually

In Ruby, what's the relationship between 'new' and 'initialize'? How to return nil while initializing?

拈花ヽ惹草 提交于 2019-12-17 04:17:34
问题 What I want is: obj = Foo.new(0) # => nil or false This doesn't work: class Foo def initialize(val) return nil if val == 0 end end I know in C/C++/Java/C#, we cant return a value in a constructor. But I'm wondering whether it is possible in Ruby. 回答1: In Ruby, what's the relationship between ' new ' and ' initialize '? new typically calls initialize . The default implementation of new is something like: class Class def new(*args, &block) obj = allocate obj.initialize(*args, &block) # actually

Is std::cout guaranteed to be initialized?

你说的曾经没有我的故事 提交于 2019-12-17 04:07:25
问题 What I know about C++ is that the order of the constructions (and destructions) of global instances should not be assumed. While I'm writing code with a global instance which uses std::cout in the constructor & destructor, I got a question. std::cout is also a global instance of iostream. Is std::cout guaranteed to be initialized before any other global instances? I wrote a simple test code and it works perfectly, but still I don't know why. #include <iostream> struct test { test() { std:

Setter methods or constructors

 ̄綄美尐妖づ 提交于 2019-12-17 03:55:35
问题 so far I have seen two approaches of setting a variable's value in Java. Sometimes a constructor with arguments is used, others setter methods are used to set the value of each variable. I know that a constructor initialises an instance variable inside a class once a class is instantiated using the "new" Keyword. But when do we use constructors and when do we use setters? 回答1: You should use the constructor approach, when you want to create a new instance of the object, with the values