CS253: Software Development with C++

Fall 2018

Struct And Class

See this page as a slide show

CS253 Struct And Class

made at imgflip.com

struct

struct Point {
    int x, y;
};
Point p;             // Note lack of new
cout << p.x << '\n'; // uninitialized value
p.x = 8;
p.y = 9;
cout << p.x << ',' << p.y << '\n';
c.cc:5: warning: 'p.main()::Point::x' is used uninitialized in this function
0
8,9

class

class Point {
  public:
    Point(int a, int b) { x = a; y = b; }   // constructor (alias ctor)
    int get_x() const { return x; }         // accessor
    int get_y() const { return y; }         // accessor
    void go_right() { x++; }                // mutator
  private:
    int x, y;                               // Hands off!
};

Output

To make a class work for output, define operator<< appropriately:

class Point {
  public:
    Point(int a, int b) { x = a; y = b; }   // ConstrucTOR
    int get_x() const { return x; }         // accessor
    int get_y() const { return y; }         // accessor
    void go_right() { x++; }                // mutator
  private:
    int x, y;                               // Hands off!
};

ostream &operator<<(ostream &out, const Point &p) {
    return out << p.get_x() << ',' << p.get_y();
}

int main() {
    Point p(12, 34);
    cout << p << '\n';      // invoke operator<<
}
12,34

Example

class Quoted {
    string s;
  public:
    Quoted(const string &word) : s(word) { }
    string get() const { return "“" + s + "”"; }
};

ostream &operator<<(ostream &os, const Quoted &rhs) {
    return os << rhs.get();
}

int main() {
    Quoted name("Robert Bruce Banner");
    cout << "I am " << name << ".\n";
}
I am “Robert Bruce Banner”.
  • s is private
  • parameter passing by const reference
  • member initialization
  • operator<< is not a method
  • half-indent for private: & public:
  • Return output operation?

methods: in-line and not

Methods can be defined inside or outside of the class.

class Quoted {
    string s;
  public:
    Quoted(const string &word) : s(word) { }
    string get() const; // declaration only
};
string Quoted::get() const {
    return "“" + s + "”";
}
ostream &operator<<(ostream &os, const Quoted &rhs) {
    return os << rhs.get();
}

int main() {
    Quoted name("Slartibartfast");
    cout << "I am " << name << ".\n";
}
I am “Slartibartfast”.

Protection

Protection

class Foo {     // A class, with a variable of each type
  public:
    int pub;    // I’m public!
  private:
    int priv;   // I’m private!
  protected:
    int prot;   // I’m a little teapot, short & stout.
};

int main() {
    Foo f;

    f.pub = 1;          // great
    // f.priv = 2;      // nope
    // f.prot = 2;      // nope

    return f.pub;
}

Friends

Friend Declarations

One class can declare another class/method/function to be its friend:

class Foo {
    friend class Bar;
    friend double Zip::zulu(int) const;
    friend int alpha();
    friend std::ostream &operator<<(std::ostream &os, const Foo &);
  private:
    int x,y;
};

Restrictions

Don’t go nuts

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-06-25T11:47

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2018 Colorado State University
CS Building