mardi 26 janvier 2021

Why is this happening exacly? i know it works, but i dont know the reason

i have this block : Student s1; Student s2(s1); Student s3; Student s4 = s1; why is Student s4 = s1; going in the second constructor like Student s2(s1) ?

my code is this :

#include <iostream>
using namespace std;

class Date { int day, month, year;
   public:
       Date(int d=0, int m=0, int y=0)
           :day(d), month(m), year(y)
           {
               cout << " i am creating a date!" << endl;
           }
       Date( const Date& anotherd)
           : day(anotherd.day), month(anotherd.month), year(anotherd.year)
           {
               cout<< " i am creating a date by COPYING" << endl;
           }
};

class Student{
   Date registrationdate;
   public:
       Student(){
           cout << "i am creating a student" << endl;
       }
       Student(const Student& s){
           cout << "just copied" << endl;
       }
};


int main(){
   Student s1;
   Student s2(s1);
   Student s3;
   Student s4 = s1;
   cout <<"ok"<<endl;
   return 0;
   
}

Aucun commentaire:

Enregistrer un commentaire