i have a question about constructor here is an example
struct time
{
int min
int hour;
int seconds;
};
struct person
{
string lastName;
string firstName;
person born
person()//constructor
};
person::person()
{
lastName ="";
firstName="";
};
i dont get how to initialize the constructor of a struct with the struct
for example "born" how do you initialize that data could someone explain it to me.
struct time
{
time();
time(int min, int hour, int seconds);
int min
int hour;
int seconds;
};
struct person
{
string lastName;
string firstName;
time born;
person();
};
person::person()
{
// No need, strings are initialized with empty strings
// lastName ="";
// firstName="";
}
time::time(int min, int hour, int seconds)
{
this->min = min;
time::hour = hour;
this->seconds = seconds;
}
time::time()
{
min = hour = seconds = 0;
}
time::time()
{
min = hour = seconds = 0;
}
//instead this can be done directly as part of the struct declaration:
#include <iostream>
struct time
{
time(){}
time(int min, int hour, int seconds);
int min = 8;
int hour = 11;
int seconds = 45;
};
int main()
{
time t;
std::cout << t.min << " " << t.hour;
}
/*Program Output:
8 11 */
> i dont get how to initialize the constructor of a struct with the struct
> for example "born" how do you initialize that data could someone explain it to me.
#include <string>
struct A { int i ; double d ; std::string s ; };
struct B
{
A a { 12, 34.56, "hello world" } ; // in-class member initialiser
};
struct C
{
A a {} ; // in-class member initialiser: value initialised
// (a.i = 0 ; a.d = 0 ; a.s is default constructed)
};
struct D
{
// constructor with member initialiser list
D( int ii, double dd, std::string ss ) : a{ ii, dd, ss } {}
A a ;
};
> prefer member-initialization to assignment statements
Debatable at best when the members involved are scalars.
If there must be a guideline/rule for non-const scalars, it should be:
favour assignment within the body of the constructor.
1 2 3 4 5 6 7 8 9 10 11 12
#include <cstdlib>
struct A { int rp1 ; int r ; A() : r( std::rand() % 100 ), rp1(r+1) {} }; // dependancy on member declaration order
struct B { int rp1 ; int r ; B() { r = std::rand() % 100 ; rp1 = r+1; } }; // no dependancy on member declaration order
int main()
{
A a ; // undefined behaviour
B b ; // fine
}
struct time
{
int min
int hour;
int seconds;
time();
};
struct person
{
string lastName;
string firstName;
int weight//for the example
person born
person()//constructor
};
time::time()
{
min =0;
hour=0;
seconds=0;
}
person::person()
{
weight =0;
}
struct time
{
int min ;
int hour ;
int seconds ;
time() { min = hour = seconds = 0 ; }
};
struct person
{
// ...
int weight ;
time time_of_birth ;
person() { weight = 0 ; }
};
In this case, since the constructor of person does not explicitly initialise time_of_birth and time has a non-trivial default constructor, the object would be default constructed.
(The default constructor would set the members min, hour and seconds to zero.)
struct time
{
int min ;
int hour ;
int seconds ;
time() { min = hour = seconds = 0 ; }//initialize
};
struct person
{
// ...
int weight ;
time time_of_birth ;
person() { weight = 0 ; }//initialize
};
time::time()
{
//do i need to initialize it here also
}
would i still need to initialize it here were i comment at