int myInt = 101; // Integer
float myFloat = 45.81; // Float
bool positive = true; // Boolean
string message = "Hello World!"; // String
char letter; // Declaration
letter = 'a'; // Initialization
int foo = 5;
foo++;
int bar = 10;
foo = foo + bar;
/*
+ : Addition - : Subtraction
* : Multiplication / : Division
% : Modulus Division
++ : Increase by 1 -- : Decrease by 1
*/
foo += 12; // Addition assignment operator
foo -= 35; // Subtraction assignment operator
year = 2018;
if (year > 2018) {
message = "After 2018";
}
else if (year < 2018) {
message = "Before 2018";
}
else {
message = "The year is 2018!";
}
/*
== : Equal to != : Not Equal to
> : Greater than < : Less than
>= : Greater than or equal to <= : Less than or equal to
*/
int foo = 0;
// While Loop
while (foo < 100) {
foo++;
}
int sum = 0;
// For Loop
for (int i = 0; i < 100; i++) {
sum += i; // adds all values from 0 to 99
}
// Declares the array "points" that can store 9 integers
int points[9];
points[0] = 716;
points[1] = 482;
cout << points[1]; // Prints 482
// Initialize at declaration
// Size of array automatically matches number of items
char grades[] = {'A', 'C', 'A', 'B'};
bool isNegative(int n) {
return n < 0;
}
bool answer = isNegative(n)
if (answer) {
cout << "Number is negative!";
}
class Person {
private:
// Only accessible from class instance
int ssn;
public:
// Publicly accessible
string name;
int age;
// Constructor
Person(string n, int a, int s) {
name = n;
age = a;
ssn = s;
}
}
// Creates a Person object called "man"
Person man("Bob", 30, 123456789);
// Prints "Bob"
cout << man.name;