Okay my code worked but I have a question so I may understand the whole class and object thing ..
I first wrote it like that:
Cat catx = new Cat();
catx.name = "bob";
but that gave me just errors.
but if I write it like that:
Cat cat1 = new Cat("bob",2,3,4);
it works.
Can you explain to me why you have to put the value directly while creating it and not define it later?
Can someone explain?
Resolved
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
21 January 2019, 19:04solution
So a default constructor is used in your first example, but the cat class that is provided in the task has a non-default constructor (this means it has values passed in). When no constructors are present, you can use a default constructor without problem. However, when constructors are present, you can only use the constructors that are present. In this case, because there is no default constructor present, your first example would error out. You could add a default constructor to the cat class and make that code work, but it would probably fail the task.
+5
MP
22 January 2019, 12:03
okay I got it thank you, still a lot of questions left but it gets better :D
+1