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