// demo_cin.cpp // // 2016-01-26 @ 19:35 //
// a demo of using C++ cin //
#include <iostream>
#include <sstream> // re. stringstream objects //
#include <string>
using namespace std;
char takeInChr( const string& msg )
{
cout << msg;
string reply;
getline( cin, reply );
if( reply.size() )
return reply[0];
// else ...
return 0;
}
bool more( const string& text = "" )
{
char reply = takeInChr( "More " + text + "(y/n) ? " );
if( reply == 'n' || reply == 'N' )
return false;
// else ...
return true;
}
int main()
{
string dummy;
do
{
cout << "Enter a char: ";
char c;
cin >> c; // extract first char //
/*
above, skips over any leading whitespace and reads first (non-whitespace) char entered.
note: leaves any next char like the '\n' char still in the cin (in) stream
*/
cout << "You entered: '" << c << "'\n";
getline( cin, dummy ); // clean up any stuff and '\n' whitespace left over //
if( dummy.size() != 0 ) cout << "You also entered '" << dummy << "'\n";
}
while( more( "characters " ) ) ;
do
{
cout << "Enter an integer: ";
int val;
cin >> val;
/*
above, skips over any leading whitespace and expects first non-whitespace entered to be a 0..9 or a + or -
reads all next 0..9's and stops at first non-0..9
Note: cin flags are set if good data or bad data encountered.
Note also: cin leaves all next char like the '\n' char still in the cin (in) stream
Thus if you entered -123abc 456
above and then pressed the Enter key,
val holds -123 and calling cin.good() would then return true,
but abc 456 and the '\n' whitespace char would still all be in the cin stream
*/
if( cin.good() )
{
cout << "You entered: '" << val << "'\n";
getline( cin, dummy ); // 'eat' whitespace '\n' left in cin stream //
if( dummy.size() != 0 ) cout << "You also entered: '" << dummy << "'\n";
}
else
{
cin.clear(); // firstly, clear (all) cin error flags //
getline( cin, dummy );
cout << "Error! Was expecting a *valid* integer, but '" << dummy << "' was encountered.\n";
}
}
while( more( "integers " ) );
while( true )
{
// Safe data entry ... uses ... as per the following ...
cout << "Enter your ID number, a space and then your name(s): ";
string line;
// get the whole line into 'line' and 'eat' the '\n' char(s) at the end //
getline( cin, line );
istringstream iss( line ); // form 'iss' object from string obj. 'line' //
// then parse out the data you expect from the iss object ...
int id;
string name;
if( iss >> id && id > 0 ) // accept ONLY positive ID's here //
{
string word;
while( iss >> word )
name += word + ' ';
if( name.size() != 0 )
{
name.erase( name.size()-1 ) ; // erase last ' ' char added //
cout << "You entered '" << id << ' ' << name << "'\n";
if( !more( "lines to parse " ) ) break;
}
else
cout << "You forgot to enter any names ... try again.\n";
}
else
cout << "Error! Was expecting a *valid* positive integer ... followed by name(s).\n"
<< "Try again ...\n";
}
}
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 //
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 //
// if you declare a C++ string ...
string s; // calls default ctor//
// note default is s = "" //
cout << s; // will print nothing //
// 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 //
// 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 //
// 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();
}
// 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();
}
// 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();
}