CIS-255 Home http://www.c-jump.com/bcc/c255c/c255syllabus.htm

C++ you already know


  1. CIS-255 Object Oriented Programming in C++
  2. C++ you already know
  3. Stream I/O
  4. Enumerations
  5. User-defined types
  6. Using existing classes
  7. Adding functions to structs
  8. A Bit of Advice
  9. C++ Comments

Object Oriented Programming in C++



// My first C++ program
#include <iostream>
int main()
{
    std::cout << "Hello, World" << std::endl;
    return 0;
}

C++ you already know


Stream I/O


Enumerations



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!!

User-defined types


Using existing classes



#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;
}

Adding functions to structs



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
}

A Bit of Advice


C++ Comments