Displaying the instance name of a struct / class

So I'm getting into objects and I have a situation where i want to display the name of the object.

So let's say i have a critter named stan:
1
2
3
4
5
6
7
8
9
10
11
struct critter
{
    int health = 10;
    
} stan;

int main()
{
    cout<<"Your Critter's health is "<<stan.health;
    cout<<"...and his name is "<< stan.name;
}


I know the "stan.name" is not correct, but I'm not sure how to display that, if its even possible.....
yup.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
#include<string>

struct critter
{
	int health = 10;
	std::string name = "Hanibal Lector";
} stan;

int main()
{
	std::cout << "Your Critter's health is " << stan.health;
	std::cout << "...and his name is " << stan.name;

	return 0;
}
}


although you wouldn't "hard code" variable values like 10 and "hanibal" like that normally. you'd have member variables to store them.
http://www.cplusplus.com/doc/tutorial/classes/
Last edited on
derp.....
thanks!
Topic archived. No new replies allowed.