var_name.method_name(/* parameters */);where varname is an instance of a class
class Xyz { int a; // private by default public: int b; int foo() { return a + b + c + bar(); } private: int c; int bar() { return a * b * c; } }; int test() { Xyz x; return x.b + x.foo() // ok + x.a // NOT allowed + x.c // NOT allowed + x.bar(); // NOT allowed }
#ifndef FILENAME_H # define FILENAME_H // Declaration of simple types // Definition of symbolic constants // Forward declaration of classes # include "others.h" // Definition of classes // Global variables // Function prototypes #endif /* FILENAME_H */Goal is to avoid problems caused by multiple inclusions, and to deal with circular dependencies.
#ifndef GROW_H # define GROW_H class Growing_buffer { enum { INITIAL_SIZE = 16 }; char *buf; int n_used; int size; public: // Default constructor Growing_buffer(); // Specifies initial size of buffer Growing_buffer(int isize); // Specifies initial contents of buffer Growing_buffer(const char *str); // Add a character to the buffer void addc(char c); // Empty the buffer void reset(); // Return buffer length int getlen() const { return n_used; } // Return buffer contents const char * getbuf() const { return buf; } ~Growing_buffer(); }; #endif /* GROW_H */
#include <string.h> #include "grow-3.h" Growing_buffer::Growing_buffer() { size = INITIAL_SIZE; n_used = 0; buf = new char[size]; } Growing_buffer::Growing_buffer(int isize) { size = isize; n_used = 0; buf = new char[size]; } Growing_buffer::Growing_buffer(const char *str) { size = strlen(str) + 1; n_used = size; buf = new char[size]; strcpy(buf, str); } void Growing_buffer::addc(char c) { if (n_used >= size) { char *nbuf; size *= 2; nbuf = new char[size]; memcpy(nbuf, buf, size / 2); delete [] buf; buf = nbuf; } buf[n_used++] = c; } void Growing_buffer::reset() { n_used = 0; } Growing_buffer::~Growing_buffer() { delete [] buf; }
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <string.h> #include "grow-3.h" int readline(FILE *fp, Growing_buffer *gb) { int c; gb->reset(); while ((c = fgetc(fp)) != EOF) { gb->addc(c); if (c == '\n') break; } gb->addc('\0'); return gb->getlen() - 1; } int main(int argc, char **argv) { int i; int lineno; int ecode = 0; FILE *fp; Growing_buffer gbuf; for (i = 1; i < argc; i++) { fp = fopen(argv[i], "r"); if (!fp) { fprintf(stderr, "can't open %s - %s\n", argv[i], strerror(errno)); ecode = 1; continue; } lineno = 1; while (readline(fp, &gbuf) > 0) { printf("%d %s", lineno, gbuf.getbuf()); lineno++; } fclose(fp); } return ecode; }
// a comment
void doNothing() { bool var = true; var = false; }
int i; float f; char c; char buf[1024]; cin >> i; cin >> f; cin >> c; cin.get(c); c = cin.peek(); cin.get(buf, sizeof(buf), '\n'); if (cin.fail()) { // or if (!cin) { cerr << "Couldn't read buffer\n"; exit(1); }
int i; cout << "Processing output"; for (i = 0; i < 8291; i++) { // a slow function process_data(i); cout << '.'; cout.flush(); } cout << "done\n"; char *s = "hi there folks"; // print whole string cout << s; // print first n characters cout.write(s, 8); if (!cout) cerr << "problem writing to output\n";
/* References */ /* Pointers */ int i = 10; int i = 10; int &iref = i; int *iref = &i; /* this changes i */ /* this changes i */ iref = -3; *iref = -3; printf("%d\n", i); printf("%d\n", i);
double Point::distanceTo(const Point &p) const { double dx = x - p.getX(); double dx = y - p.getY(); return sqrt(dx * dx + dy * dy); } /* ... */ int main() { Point a(2, -6), b(4, 9); double d = a.distanceTo(b); /* ... */ } void halfIt(Point &p) { p.move(-p.getX() / 2, -p.getY() / 2); }
Point & minPoint(Point &p1, Point &p2) { Point zero(0, 0); if (zero.distanceTo(p1) < zero.distanceTo(p2)) return p1; else return p2; } int main() { Point a(3,4), b(5, 2); /* ... */ minPoint(a, b).move(0, -1); /* ... */ }