/*
* This program makes use of a trivial C++ class in order to point out
* some basic syntax and other issues.
*/
#include <iostream>
#include <string>
using namespace std;
/*
* This class represents an integer value with a label for printing.
*/
class LabeledInt {
public:
// Create an object.
LabeledInt(string label, int value = 0) {
m_label = label;
m_value = value;
}
// Get the labeled value for easy printing
string to_string() const {
return m_label + ": " + std::to_string(m_value);
}
// Change the label.
void relabel(string label) { m_label = label; }
// Get or set the value.
int get() const { return m_value; }
void set(int value) { m_value = value; }
private:
string m_label;
int m_value;
};
void munge(LabeledInt ll)
{
ll.relabel("Nothing");
ll.set(0);
}
void set(LabeledInt & ll)
{
ll.relabel("something");
ll.set(99);
}
int main()
{
// Make some.
LabeledInt li1("Peanuts", 487);
LabeledInt li2("Acorns", 9871);
LabeledInt li3("Marbles");
// Print them.
cout << "A " << li1.to_string() << ", " << li2.to_string() << ", "
<< li3.to_string() << endl;
// Some pretty random changes.
li1.set(li1.get() + 13);
li3.set(17);
li2.set(li3.get() * li1.get());
// Print them.
cout << "B " << li1.to_string() << ", " << li2.to_string() << ", "
<< li3.to_string() << endl;
// Compare to Java.
LabeledInt cpy = li1;
li1.set(3);
li1.relabel("Pebbles");
li2 = li3;
li2.set(3*li2.get());
// Print them.
cout << "C " << li1.to_string() << ", " << li2.to_string() << ", "
<< li3.to_string() << ", " << cpy.to_string() << endl;
// Note that each function uses a different parameter passing method.
munge(li1);
set(li2);
// Print them.
cout << "D " << li1.to_string() << ", " << li2.to_string() << ", "
<< li3.to_string() << endl;
}
This is the first example of a C++ class definition. This
not-too-exciting program shows some differences between C++
and Java classes. To note:
- The class is declared in the same program as main and couple
other functions. In C++, classes aren't required to be in a file by
themselves, or a file with any particular name. It just needs to be defined
before it is used, like anything else. Smaller classes are often
placed in code with other things.
- The class definitnion is followed by a the semicolon.
- The public and private controls mean the same as
in Java, but the syntax differs. The keywords are followed by a
colon, and introduce sections. The classification applies until
changed, and may be changed (or changed back) at any time.
- The constructor uses a default parameter. These are allowed in
any function or method, but are often
particularly convenient in constructors.
- The const after the parameter list declares that the method will not
change any object variables. That is, a call to x.f() will not change x.
- There are two top-level functions, munge and set,
which are sent an object as a parameter. When sent without marks,
objects are passed by value like other parameters. Objects are
often passed by reference (with &).
- Note that the variables declared as type Point actually contain the
objects, not references. Likewise, assignment copies objects; it does
not just copy a reference as in Java.