Things to note about C++ class, and if you are coding a C++ class ...
If you code a C++ class, the standard (class guarantee) expectation is that
all data members will be constructed, initialed to known pre-defined values.
See examples ...
One ...
class PairInt
{
int a, b; // private in class by default here, since was a 'class' //
public:
// ctor with default values 0,0 //
PairInt( int a=0, int b=0 ) : a(a), b(b) {} //
int get_a() const { return a; }
int get_b() const { return b; }
} ;
// later ...
PairInt pr1; // call s default ctor //
cout << pr1.get_a(); // prints 0 //
Example two ...
class Student
{
public:
// ctor with default values //
Student( int i=0, string n="" ) : id(i), name(n) {}
int get_id() const { return id; }
string get_name() const { return name; }
private:
int id;
string name;
} ;
// later ...
Student s; // calls default ctor //
cout << s.get_id(); // prints 0 //
Example three ...
// if you declare a C++ string ...
string s; // calls default ctor//
// note default is s = "" //
cout << s; // will print nothing //
Example four ( and mostly why I am doing this: 'things to note' )
// if you declare a C++ vector
vector< int > v; // calls default ctor
// note, a size=0 v is created //
v.resize(5); // calls resize method
// note now v[0].. v[4] all are created, *each initialed to 0* //
// If you now call v.size();
//*** it would return 5 ***
//if you now call ...
cout << v[4]; // prints 0 //
So ...
// if you call ...
vector< vector < int > > matrix; // default size of matrix is 0 //
// then after the above, you call ...
const int rows = 3, cols = 5;
matrix.resize( rows ); // size of matrix is now 3 ...
// and matrix[0] .. matrix[2] are all created, each with size 0 //
// now ... can do this ...
for( size_t i = 0; i < matrix.size(); ++ i )
{
matrix[i].resize( cols );
}
// NOW ... the whole 3 rows by 5 columns matrix is created with ALL 15 elements initialed to 0 //
Try also, and see the comments in the next three example programs ...
(1)
// about_class_1.cpp // // 2016-02-04 //
#include <iostream>
#include <string>
using namespace std;
// This next class will create for you (at compile time) ...
// 1) a default ctor..
// 2) a default copy ctor
// 3) a default overloaded = (assignment operartor)
// 4) a default destructor
class Student
{
// recall that these are private (by default here) inside a class //
string id,
name;
public:
// mutators/setters //
void set_id( const string& i )
{
id = i;
}
void set_name(const string& n )
{
name = n;
}
// accessors/getters //
string get_id() const
{
return id;
}
string get_name() const
{
return name;
}
} ;
// here ... we define an overload of operator << for above 'Student' objects //
ostream& operator << ( ostream& os, const Student& stud )
{
return os << stud.get_id() << ", " << stud.get_name();
}
int main()
{
Student stud, stud2; // call default ctor //
cout << "stud = '" << stud << "'\n";
stud.set_id( "123_456_789" );
stud.set_name( "Sam Smith" );
cout << "stud = '" << stud << "'\n";
stud2 = stud; // call to default assignment //
cout << "stud2 = '" << stud2 << "'\n";
Student stud3(stud2); // call to default copy ctor... //
cout << "stud3 = '" << stud2 << "'\n";
cout << "Press 'Enter' to continue/exit ... ";
cin.get();
}
(2)
// about_class_2.cpp // // 2016-02-04 //
#include <iostream>
#include <string>
using namespace std;
// This next class will create for you (at compile time) ...
// 1) a VALUES PASSED IN ctor... (but NO default ctor...)
// 2) a default copy ctor
// 3) a default overloaded = (assignment operartor)
// 4) a default destructor
class Student
{
// recall that these are private (by default here) inside a class //
string id,
name;
public:
// ctor with values ...
Student( const string& id, const string name )
: id(id), name(name) {}
// accessors/getters //
string get_id() const
{
return id;
}
string get_name() const
{
return name;
}
} ;
// here ... we define an overload of operator << for above 'Student' objects //
ostream& operator << ( ostream& os, const Student& stud )
{
return os << stud.get_id() << ", " << stud.get_name();
}
int main()
{
//Student stud; // HERE, a call to default ctor wiil NOT compile, since now, NO default ctor was created //
Student stud( "123_456_789", "Sam Smith" );
cout << "stud = '" << stud << "'\n";
Student stud2 = stud; // OK ... SINCE here is REALLY a call to the default copy ctor... //
cout << "stud2 = '" << stud2 << "'\n";
Student stud3(stud2); // call to default copy ctor... //
cout << "stud3 = '" << stud2 << "'\n";
cout << "Press 'Enter' to continue/exit ... ";
cin.get();
}
(3)
// about_class_3.cpp // // 2016-02-04 //
#include <iostream>
#include <string>
using namespace std;
// This next class will create for you (at compile time) ...
// 1) a VALUES PASSED IN ctor... AND it also 'doubles as' the default ctor...
// 2) a default copy ctor
// 3) a default overloaded = (assignment operartor)
// 4) a default destructor
class Student
{
// recall that these are private (by default here) inside a class //
string id,
name;
public:
// ctor with values ... amd ALSO defaults ... AND using an initilization list in ctor... //
Student( const string& id = "", const string name = "" )
: id(id), name(name) {}
// accessors/getters //
string get_id() const
{
return id;
}
string get_name() const
{
return name;
}
} ;
// here ... we define an overload of operator << for above 'Student' objects //
ostream& operator << ( ostream& os, const Student& stud )
{
return os << stud.get_id() << ", " << stud.get_name();
}
int main()
{
Student stud; // HERE, a call to default ctor wiil NOW compile, since NOW, default ctor IS created //
cout << "stud = '" << stud << "'\n";
Student stud2( "123_456_789", "Sam Smith" );
cout << "stud2 = '" << stud2 << "'\n";
Student stud3(stud2); // call to default copy ctor... //
cout << "stud3 = '" << stud2 << "'\n";
cout << "Press 'Enter' to continue/exit ... ";
cin.get();
}