0
Can someone help me explain ?
Can someone explain for me the small code under: struct Animal { int age; } class Pet { public: Animal* CheckAge(int); } *what happen if i change Animal to some variable type what will happen ? // i used Visual Studio 2017 and create a function body in other .cpp file this is what i get Animal Pet::CheckAge(int x) { //some code } In conclusion, my question is what happen if i use int for "CheckAge" instead of struct name "Animal" ?
4 Answers
0
your constructor should be named pet instead of animal. from your code, Animal* is a pointer and should be a variable referencing(contains memory) an animal object. not sure why you have a method call after animal.
0
Oops my bad i wanted to make a notice xD. What will happen if i chane Animal to int ?
0
you can't because animal is a custom data type of the class animal.
your code won't even run because syntax is all wrong and complier will give you an error.
what does checkAge return? if it is an int, it can be assign to an int variable, if it is an animal object, it can be assign to an animal variable, if it is void then it can't be assign to any variables.
0
e.g. if checkAge returns age of the animal:
*Animal animal = new Animal();
int animalAge = animal -> checkAge();
BUT
*Animal checkAge(); makes no sense