CIS-255 Home http://www.c-jump.com/bcc/c255c/c255syllabus.htm
// My first C++ program #include <iostream> int main() { std::cout << "Hello, World" << std::endl; return 0; }
for, while, if, switch, etc.
int, float, double, char, void, bool b = true;
int arr[100]; float* pf; unsigned char uch; long int num;
#include <iostream>
std::cout, std::cin, std::cerrThese correspond to stdout, stdin, and stderr in C
std::endl
enum
enum Suit { CLUB, DIAMOND, HEART, SPADE }; Suit s; // enum keyword here not required s = CLUB; // of course s = (Suit) 1; // Ok, not recommended s = 1; // ERROR enum PrintFlags {COLOR=1, LANDSCAPE=2, TWOSIDE=4}; // Range is [0:7] PrintFlags pf = COLOR | TWOSIDE; pf = (PrintFlags) 6; pf = (PrintFlags) 11; // Undefined!! switch ( s ) { // use a default label!!
struct and class
struct point { int x; int y; }; point p1; // Note: struct keyword not needed p1.x = 9; // and so on...
#include <iostream> #include <string> using std::string; int main() { string s1 = "This is a string"; std::cout << "There are " << s1.size() << " chars in " << s1 << std::endl ; return 0; }
struct point { int x; // member variables int y; void adjust( int x_, int y_ ) // member function { x += x_; y += y_; } }; // don't forget this semicolon void main() { // Now we can: point p; p.x = p.y = 4; // access to member variables p.adjust( 1, 1 ); // member function call }
// this --> /* is not effective so this becomes an error */ /* this --> // is not effective */ so this is an error