0
#include <iostream>
#include <math.h>
#include <string>
using namespace std;

class People{
public:
    string name;
    int age;
    bool educated;
    People(){
        cout << [name] << "class people is initialised" << endl;
    }
    ~People(){
        cout << [name]  << "class people is destroyed" << endl;
    }
private:
    double worth;
};


int main(){
    People Joe;
}

how do i display that class name in the constructor and destructor? I saw another method was calling the function specifically in main() however that is not what i want. I want to try to display the class name upon its creation and destruction

1 Answers1

-1

Welcome to stackoverflow and C++!

I tool the liberty of fixing some smaller issues with the code:

<cmath> is the proper C++ header, use that instead of <math.h> (the C header. C and C++ are different).

Do not build an early habit of using namespace std. Yes, it seems convenient, but read here why you should not do it.

Using std::endl will likely drop your performance. Read here about the differences. using \n works just as good, is cross-platform-compatible and even less to type.

Does this achieve what you want?

#include <cmath>
#include <iostream>
#include <string>

class People{
public:
    std::string name;
    int age;
    bool educated;
    People(){
        std::cout << typeid(*this).name() << "class people is initialised\n";
    }
    ~People(){
        std::cout << typeid(*this).name()  << "class people is destroyed\n";
    }
private:
    double worth;
};


int main(){
    People Joe;
}

Responding to the comment:

People(std::string const& str)
: name(str)
{
        std::cout << name << " class people is initialised\n";
    
}

////

int main(){
    Person Joe("Joe");
}

Note this important difference: People(std::string str) will create a copy of the string (which is usually expensive).

People(std::string const& str) will create a constant reference. Constant means it can not be changed and since it is a reference, it will not be copied here (it will be copied into the class member though).

infinitezero
  • 1,610
  • 3
  • 14
  • 29
  • Thanks for the helpful response! what if i want to display object name? and what does the number in front of the class name means when i used your code?? – francescacodesleep Jul 02 '22 at 05:21
  • @ididnotsignupforthis you can not print the object name. That is reflection and works for example in Java. If you want to print the name of the person, you can write a constructor that takes the name as an argument. I'll edit this into the post. Also, if you find answers helpful, you can acknowledge this by using the arrows to the left to upvote and click the check mark, once you found the answer you were looking for. – infinitezero Jul 02 '22 at 05:23
  • 1
    `typeid(*this).name()` does not necessarily print the class name. It does on some compilers, it does not on others. – john Jul 02 '22 at 05:38
  • Added bonus from `const &`: It accepts and keeps alive temporary variables. Normally you can't take a reference to a temporary, partly because it's not going to be alive long enough to be useful. – user4581301 Jul 02 '22 at 07:19