-1

I started learning C++ and now I am lost, I don't see the logic in this. simply does not make sense to me how it is possible that I can add arguments to an object and then those arguments are used by the program. Sure I can memorize this feature, but can someone please explain the logic behind this? Everything else in C++ makes sense, I guess this probably does too, only I don't see it.

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

class Person{
  private:
     string name;
     int age;

  public:
     Person(){
       name = "undefined";
       age = 0;
     };
     Person(string newname){
       name = newname;
       age = 0;
     };
     Person(string newname, int newage){
       name = newname;
       age = newage;
     };

     string toString(){
       stringstream ss;
       ss << "Name is: " << name << " Age is : " << age;
       return ss.str();
    };

 };


int main()
{
  Person person1;
  Person person2("David");  // I don't get this ???
  Person person3("Mia", 35); // // I don't get this ???

  cout << person1.toString() << endl;
  cout << person2.toString() << endl;
  cout << person3.toString() << endl;

  return 0;
};
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • See Constructors in C++ https://www.tutorialspoint.com/cplusplus/cpp_constructor_destructor.htm – Rohith R Sep 22 '16 at 15:45
  • 7
    You really should be using a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to learn C++ from. It will explain things like this. – NathanOliver Sep 22 '16 at 15:45
  • Put breakpoints on the three constructors and run it. – ClickRick Sep 22 '16 at 15:46
  • I'm voting to close this question as off-topic because Stack Overflow is not a tutorial site that teaches programming. – IInspectable Sep 22 '16 at 15:49
  • The thing I don't understand is how these values ("Mia", 35) are passed from object to a constructor, I don't see the logic behind it... – Rajesh Muntari Sep 23 '16 at 13:05

3 Answers3

3

You are actually calling the constructors when you say you're passing arguments to objects. Constructors with matching signatures are called.

When you write Person person1;, default constructor which is

Person(){
    name = "undefined";
    age = 0;
};

is called.
When you write Person person2("David");,

Person(string newname){
    name = newname;
    age = 0;
};

is called.
And finally, when you do Person person3("Mia", 35);,

Person(string newname, int newage){
    name = newname;
    age = newage;
};

is called.

nishantsingh
  • 4,537
  • 5
  • 25
  • 51
1

When you define a new C++ class, with its members as your choice, each one of these functions as a regular variable (or pointers, or instances of other classes, etc.) which is a concept you are already familiar with.

So when you instantiate a new instance of the class, the compiler knows exactly how much memory it should allocate, and what is the inner structure of it - how to divide that memory into smaller pieces corresponding to the class members. And that is how, when you call a constructor, it knows how to "add the arguments" to your instance.

proton
  • 393
  • 6
  • 31
1

This statement: Person person1; is calling this constructor:

Person();

This Person person2("David"); is calling this constructor:

Person(string newname);

And this Person person3("Mia", 35); is calling this constructor:

Person(string newname, int newage);

Since constructors are functions that initialize the object of class Person, they can receive arguments, like any function.

M.M
  • 2,254
  • 1
  • 20
  • 33