Skip to content

Instantly share code, notes, and snippets.

@congdanhqx-zz
Last active August 29, 2015 13:57
Show Gist options
  • Save congdanhqx-zz/9523623 to your computer and use it in GitHub Desktop.
Save congdanhqx-zz/9523623 to your computer and use it in GitHub Desktop.
Example for C/C++ keywords
#include <iostream>
using namespace std;
struct first {
char first_char;
int an_int;
char second_char;
};
struct second {
char first_char;
char second_char;
int an_int;
};
int main() {
// your code goes here
cout << "sizeof(first) = " << sizeof(first) << endl;
cout << "sizeof(second) = " << sizeof(second) << endl;
return 0;
}
#include <iostream>
using namespace std;
template<class T>
auto doWork(T builder) -> decltype(builder.getNumber()) {
return builder.getNumber() * 2;
}
template <class T, class U>
auto add(T t, U u) -> decltype(t + u);
struct IntBuilder{
int getNumber() {
return 1;
}
enum Color {
BLACK,
WHITE
}
Color getColor();
};
struct FloatBuilder{
float getNumber() {
return 1.2;
}
};
int main() {
// your code goes here
cout << doWork(IntBuilder()) << endl;
cout << doWork(FloatBuilder()) << endl;
return 0;
}
auto IntBuilder::getColor() -> Color {
return IntBuilder::BLACK;
}
enum Color {
BLACK, // BLACK will be 0
BLUE, // BLUE will be 1
GREEN = 0x10, // GREEN is 0x10 = 16
RED = 0x100, // RED is 0x100 = 256
PURPLE, // PURPLE is 0x101 = 257
WHITE = 0x111 // WHITE is 0x111 = 273
};
// In C++ we can omit the enum keyword in this below declaration.
void setColor(enum Color color) {
enum Color c = BLACK; // Always valid
Color c1 = BLUE; // Valid in C++ only
Color c2 = Color::GREEN; // Valid in C++11 only
switch(color) {
case BLACK: // doWork();
break;
case BLUE:
// do work
break;
case Color::GREEN: // this LOC is only valid in C++11
// do work
break;
//....
}
// do work.
}
struct A
{
A(int) {}
operator int() const { return 0; }
};
struct B
{
explicit B(int) {}
explicit operator int() const { return 0; }
};
int main()
{
// A has no explicit ctor / conversion, everything is fine
A a1 = 1;
A a2 ( 2 );
A a3 { 3 };
int na1 = a1;
int na2 = static_cast<int>( a1 );
B b1 = 1; // Error: implicit conversion from int to B
B b2 ( 2 ); // OK: explicit constructor call
B b3 { 3 }; // OK: explicit constructor call
int nb1 = b2; // Error: implicit conversion from B to int
int nb2 = static_cast<int>( b2 ); // OK: explicit cast
}
class PoweredDevice
{
public:
PoweredDevice(int nPower)
{
cout << "PoweredDevice: " << nPower << endl;
}
};
class Scanner: public PoweredDevice
{
public:
Scanner(int nScanner, int nPower)
: PoweredDevice(nPower)
{
cout << "Scanner: " << nScanner << endl;
}
};
class Printer: public PoweredDevice
{
public:
Printer(int nPrinter, int nPower)
: PoweredDevice(nPower)
{
cout << "Printer: " << nPrinter << endl;
}
};
class Copier: public Scanner, public Printer
{
public:
Copier(int nScanner, int nPrinter, int nPower)
: Scanner(nScanner, nPower), Printer(nPrinter, nPower)
{
}
};
int doWork(int i) {
std::cout << i << std::endl;
}
int doWork(int* i) {
if (!i) {
std::cout << "nullptr" << std::endl;
} else {
std::cout << *i << std::endl;
}
}
enum class Color {
BLACK,
WHITE
};
enum struct Unit {
FSU1,
FSU3,
FSU11,
FSU15,
FSU17
};
int main() {
// your code goes here
Color color = Color::BLACK;
Unit unit = Unit::FSU1;
return 0;
}
#include <iostream>
int main() {
char static_array[10];
char* dynamic_array = new char[10];
std::cout << sizeof(static_array) << std::endl;
std::cout << sizeof(dynamic_array) << std::endl;
delete[] dynamic_array;
return 0;
}
namespace Example {
enum Season {
SPRING,
SUMMER,
FALL,
WINTER
};
enum Gender {
MALE,
FEMALE,
LESBIAN,
GAY,
BISEXUAL,
TRANSGENDER,
QUESTIONABLE
};
}
int main() {
// your code goes here
Example::Season season = Example::SPRING;
Example::Gender gender = Example::MALE;
// Should we allow this.
cout << (season == gender) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment