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" ?

17th Nov 2017, 2:51 PM
thong nguyen
thong nguyen - avatar
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.
17th Nov 2017, 3:29 PM
Gao Xiangshuai
Gao Xiangshuai - avatar
0
Oops my bad i wanted to make a notice xD. What will happen if i chane Animal to int ?
17th Nov 2017, 3:49 PM
thong nguyen
thong nguyen - avatar
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.
18th Nov 2017, 5:27 AM
Gao Xiangshuai
Gao Xiangshuai - avatar
0
e.g. if checkAge returns age of the animal: *Animal animal = new Animal(); int animalAge = animal -> checkAge(); BUT *Animal checkAge(); makes no sense
18th Nov 2017, 5:28 AM
Gao Xiangshuai
Gao Xiangshuai - avatar