Last active
April 26, 2016 14:48
-
-
Save sword-jin/42eceb50b4233fad575c0687350ae921 to your computer and use it in GitHub Desktop.
c++ basic templates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Enumerations | |
enum Day { SUN, MON, TUE, WED, THU, FRI, SAT }; | |
enum Mood { HAPPY = 3, SAD = 1, ANXIOUS = 4, SLEEPY = 2 }; | |
Day today = THU; // today may be any of MON . . . SAT | |
Mood myMood = SLEEPY; // myMood may be HAPPY, . . ., SLEEPY | |
// pointers | |
char ch = ’Q’; | |
char* p = &ch; // p holds the address of ch | |
cout << *p; // outputs the character ’Q’ | |
ch = ’Z’; // ch now holds ’Z’ | |
cout << *p; // outputs the character ’Z’ | |
*p = ’X’; // ch now holds ’X’ | |
cout << ch; // outputs the character ’X’ | |
// note | |
int* x, y, z; // same as: int* x; int y; int z; | |
// Arrays | |
double f[5]; // array of 5 doubles: f[0], . . ., f[4] | |
int m[10]; // array of 10 ints: m[0], . . ., m[9] | |
f[4] = 2.5; | |
m[2] = 4; | |
cout << f[m[2]]; // outputs f[4], which is 2.5 | |
int a[ ] = {10, 11, 12, 13}; // declares and initializes a[4] | |
bool b[ ] = {false, true}; // declares and initializes b[2] | |
char c[ ] = {’c’, ’a’, ’t’}; // declares and initializes c[3] | |
// Arrays and pointers | |
char c[ ] = {’c’, ’a’, ’t’}; | |
char* p = c; // p points to c[0] | |
char* q = &c[0]; // q also points to c[0] | |
cout << c[2] << p[2] << q[2]; // outputs “ttt” | |
// C-style structure | |
enum MealType { NO_PREF, REGULAR, LOW_FAT, VEGETARIAN }; | |
struct Passenger { | |
string name; // passenger name | |
MealType mealPref; // meal preference | |
bool isFreqFlyer; // in the frequent flyer program? | |
string freqFlyerNo; // the passenger’s freq. flyer number | |
}; | |
Passenger pass = { "John Smith", VEGETARIAN, true, "293145" }; | |
// change | |
pass.name = "Pocahontas"; // change name | |
pass.mealPref = REGULAR; // change meal preference | |
// Reference | |
string author = "Samuel Clemens"; | |
string& penName = author; // penName is an **alias** for author | |
penName = "Mark Twain"; // now author = “Mark Twain” | |
cout << author; // outputs “Mark Twain” | |
// Traditional C-Style Casting | |
int i1 = 18; | |
int i2 = 16; | |
double dv1 = i1 / i2; // dv1 has value 1.0 | |
double dv2 = double(i1) / double(i2); // dv2 has value 1.125 | |
double dv3 = double( i1 / i2 ); // dv3 has value 1.0 | |
// Static Casting | |
double d1 = 3.2; | |
double d2 = 3.9999; | |
int i1 = static_cast<int>(d1); // i1 has value 3 | |
int i2 = static_cast<int>(d2); // i2 has value 3 | |
// Function overriding | |
void print(int x) | |
{ | |
cout << x << endl; | |
} | |
void print(const Passenger& passenger) | |
{ | |
cout << passenger.name << " " << passenger.mealPref; | |
if (passenger.isFreqFlyer) | |
{ | |
cout << " " << passenger.freqFlyerNo; | |
} | |
cout << endl; | |
} | |
// operator overriding | |
bool operator==(const Passenger& x, const Passenger& y) | |
{ | |
return x.name == y.name && | |
x.mealPref == y.mealPref && | |
x.isFreqFlyer == y.isFreqFlyer && | |
x.freqFlyerNo == y.freqFlyerNo; | |
} | |
ostream& operator<<(ostream& out, const Passenger& passenger) | |
{ | |
out << passenger.name << " " << passenger.mealPref; | |
if (passenger.isFreqFlyer) { | |
cout << " " << passenger.freqFlyerNo; | |
} | |
out << endl; | |
return out; | |
} | |
// inline function | |
inline int min(int x, int y) { return (x < y ? x : y); } | |
// class | |
class Passenger { | |
private: | |
// . . . | |
public: | |
Passenger(); // default constructor | |
Passenger(const string& nm, MealType mp, const string& ffn = "NONE"); | |
Passenger(const Passenger& pass); // copy constructor | |
// . . . | |
}; | |
Passenger::Passenger() { // default constructor | |
name = "--NO NAME--"; | |
mealPref = NO PREF; | |
isFreqFlyer = false; | |
freqFlyerNo = "NONE"; | |
} | |
// constructor given member values | |
Passenger::Passenger(const string& nm, MealType mp, const string& ffn) { | |
name = nm; | |
mealPref = mp; | |
isFreqFlyer = (ffn != "NONE"); // true only if ffn given | |
freqFlyerNo = ffn; | |
} | |
// copy constructor | |
Passenger::Passenger(const Passenger& pass) { | |
name = pass.name; | |
mealPref = pass.mealPref; | |
isFreqFlyer = pass.isFreqFlyer; | |
freqFlyerNo = pass.freqFlyerNo; | |
} | |
// constructor using an initializer list | |
Passenger::Passenger(const string& nm, MealType mp, string ffn) | |
: name(nm), mealPref(mp), isFreqFlyer(ffn != "NONE") | |
{ freqFlyerNo = ffn; } | |
// class note | |
Every class that allocates its own objects using new should: | |
• Define a destructor to free any allocated objects. | |
• Define a copy constructor, which allocates its own new member storage | |
and copies the contents of member variables. | |
• Define an assignment operator, which deallocates old storage, allocates | |
new storage, and copies all member variables. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment