What is the single "m" in main? How that works? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the single "m" in main? How that works?

#include <iostream> using namespace std; class Mother { public: Mother() { cout <<"Mother ctor"<<endl; } ~Mother() { cout <<"Mother dtor"<<endl; } }; class Daughter: public Mother { public: Daughter() { cout <<"Daughter ctor"<<endl; } ~Daughter() { cout <<"Daughter dtor"<<endl; } }; int main() { Daughter m; }

10th Aug 2019, 4:56 PM
Jesús Lopez
Jesús Lopez - avatar
4 Answers
+ 7
Jesus López you can name it anything you want! The naming rules for object are same as variable. As I said the name of object can be anything you want. Just some rules must be followed : 1.Name should not start with number '1object' [INVALID] 2.It should not contain special charecters 'MyObj
#x27; [INVALID] 3.A keyword can't be used as variable/object name 'int' [INVALID] 'for' [INVALID] 4. It should not contain white spaces. 'student obj' [INVALID] you can name it anything you want . Just remember some rules And giving name as per context would be good programming practice.
10th Aug 2019, 5:53 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 5
The single 'm' you are having in main method is an object of class 'Daughter'. In your program you have two classes. 1.Mother 2.Daughter Mother class has a constructor and a destructor. Similarly Daughter class also has a constructor and a destructor. Look at definition of Daughter... It is derived publicly from Mother class. //Inheritance ^_^ Basically assuming that you are asking why the output is so... See when object of a class is created (instantiated) it's constructor is called and when object goes out of scope it's destructor gets called. //automatically When object of derived(Daughter) class is created constructor of Base (Mother) class is called first and then derived class constructor is called and hence the output ... Mother ctor Daughter ctor Now about destructors. ... The derived class(Daughter) destructor is called first and then Base class(Mother) destructor is called! Hence the output : Daughter dtor Mother dtor I hope this is what you asked. clarify question. Happy to help;
10th Aug 2019, 5:13 PM
🇮🇳Omkar🕉
🇮🇳Omkar🕉 - avatar
+ 3
It's a variable of type Daughter which means that it can contain an object of subclass daughter. when you create an object of Daughter which extends Mother the object will contain all the methods and fields of Mother which are not private. once you have created an object the refrenece is stored in the variable of type daughter.
10th Aug 2019, 5:09 PM
D_Stark
D_Stark - avatar
0
Why is possible to replace the "m" with any other letter and keep the code working?
10th Aug 2019, 5:41 PM
Jesús Lopez
Jesús Lopez - avatar