简体   繁体   中英

A bit of help understanding constructors, operator overloads, destructors, etc

so I'm a C++ beginner and I have a question.

Let's say we have a class Grades.

From what I've learned so far a destructor would look like

~Grades();

a copy constructor:

Grades(const Grades & );

an << operator:

ostream & operator << (ostream & os, const Grades & g);

Are these correct?

How would a regular constructor look like? What about a conversion constructor ?

Regular default constructor, provided by compiler, will look like below and it will internally call the base class constructors if it is derived from any class. And later for data members having user defined type, it will call their respective default constructors in the order of their declaration.

Grades();

The conversion constructors are something you will have to define, and they will look like

Grade(const T&)

If you want conversion function,

 Grade operator=(const T&)

The only destructor you could have is suppose to de-initialize the object by calling respective destructors if user defined data members and then for base classes, exactly in reverse order what default compiler provided constructor would provide.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM