#include <iostream.h> main() { cout << "hello, world\n"; }
Σε εγκαταστάσεις Linux το πρόγραμμα πρέπει να έχει επίθεμα .C. Για να μεταγλωττιστεί χρησιμοποιούμε την εντολή g++.
#include <iostream.h> main() { int i = 3; cout << i; for (int j = 0; j < 10; j++) cout << j; }Καλό είναι οι μεταβλητές να ορίζονται ακριβώς πριν από το πρώτο σημείο όπου χρησιμοποιούνται.
int i; // This is a line comment; set i to 8. i = 8;
int y = 8; int &x = y; x = 3; // Set y to 3
#include <iostream.h> double square(double x); int square(int x); double square(double x) { return (x * x); } int square(int x) { return (x * x); } main() { int i = 3; double d = 1.4142; cout << "square(" << i << ")=" << square(i) << "\n"; cout << "square(" << d << ")=" << square(d) << "\n"; }
struct point { int x, y; }; point *p; main() { p = new point; delete p; }
main() { int *ip = new int[10]; ip[3] = 8; delete[] ip; }
class point { int x, y; };
Παράδειγμα:
class point { private: int x, y; public: int getx(); int gety(); void setpos(int sx, int sy); void display(); };Στο παραπάνω παράδειγμα τα μέλη (ιδιότητες) της κλάσης x, y δεν είναι ορατά και προσβάσιμα παρά μόνο από τις συναρτήσεις (μεθόδους) της κλάσης getx, gety, setpos και display.
int point::getx() { return (x); } int point::gety() { return (y); } void point::display() { cout << "(" << x << "," << y << ")\n"; } void point::setpos(int sx, int sy) { x = sx; y = sy; }
#include <iostream.h> #include "point.h" main() { point a; point b, *c; c = new point; b.setpos(6, 6); cout << b.getx(); a.display(); b.display(); c->display(); }
void point::setpos(int sx, int sy) { this->x = sx; point::y = sy; }
class stack { private: int *values; int size; int sp; public: stack(int size); // Constructor ~stack(); // Destructor }; stack::stack(int size) { stack::size = size; values = new int[size]; } stack::~stack() { delete[] values; }
class point { private: int x, y; static int numpoints; public: // ... static int points_used(); }; int point::numpoints = 0; // Constructors point::point(int sx, int sy) { x = sx; y = sy; numpoints++; } point::point() { x = y = 0; numpoints++; } // Destructor point::~point() { numpoints--; } // Access function int point::points_used() { return (numpoints); }
class point { private: int x, y; static int numpoints; public: // ... friend void display(point& p); // Display friend function }; // Friend function; used as display(a) void display(point& p) { cout << "(" << p.x << "," << p.y << ")\n"; } main() { point b = point(1, 2); display(b); // Friend function }
class point { private: int x, y; public: int getx() const; // Access functions int gety() const; void display(); // Display member function // ... }; int point::getx() const { return (x); }Ο προσδιορισμός αυτός αναγκάζει το μεταγλωττιστή να ελέγχει αν η δέσμευση αυτή τηρείται μέσα στο σώμα της συνάρτησης.
#include <iostream.h> class point { private: int x, y; static int numpoints; public: point(); // Default contructor point(int sx, int sy); // Other constructor ~point(); // Destructor int getx() const; // Access functions int gety() const; void display(); // Display member function void setpos(int sx, int sy); // Set position static int points_used(); // Return number of points friend void display(point& p); // Display friend function }; int point::numpoints = 0; point::point(int sx, int sy) { x = sx; y = sy; numpoints++; } point::point() { x = y = 0; numpoints++; } point::~point() { numpoints--; } int point::getx() const { return (x); } int point::gety() const { return (y); } // Member function; used as a.display(); void point::display() { cout << "(" << x << "," << y << ")\n"; } // Friend function; used as display(a) void display(point& p) { cout << "(" << p.x << "," << p.y << ")\n"; } void point::setpos(int sx, int sy) { this->x = sx; point::y = sy; } int point::points_used() { return (numpoints); } main() { point a(1, 2); point b, *c; c = new point(5, 5); b.setpos(6, 6); a.display(); // Member function display(b); // Friend function c->display(); cout << "points used = " << point::points_used() << "\n"; delete(c); cout << "points used = " << point::points_used() << "\n"; }
#include <iostream.h> class point { private: int x, y; public: point(int sx, int sy); // Constructor point operator+(point b); // Overload the + operator void display(); // Display member function }; point::point(int sx, int sy) { x = sx; y = sy; } void point::display() { cout << "(" << x << "," << y << ")\n"; } point point::operator+(point b) { point p(x, y); // p is a new point at x, y p.x += b.x; p.y += b.y; return (p); } main() { point p1 = point(1, 2); point p2 = point(10, 20); point p3 = point(0, 0); p3 = p1 + p2; p1.display(); p2.display(); p3.display(); }
int_queue(int size);
void put_int_queue(int i);
int get_int_queue();
int isempty_int_queue();
I 1 I 2 I 3 O 1 I 4 O 2 ...Παρατήρηση: Το πρόγραμμα μπορεί να δομηθεί γύρω από τον παρακάτω κώδικα εισόδου:
#include <iostream.h> main() { char c; // Initializations here for (;;) { cin >> c; switch (c) { case 'I': // Process input here break; case 'O': // Process output here break; } } }