Classes and Objects

Tom Kelliher, CS18

Mar. 15, 1996

The Entity-Relationship Model

Entity:

A living being, object, or abstraction that can be described in terms of certain characteristics, and is similar to, but distinct from, entities of the same type.

Examples:

Entity Set:

A collection of entities of the same type.

Distinguished by attributes.

Relationship:

An association among members of one or more entity sets.

Relationship set:

A set of similar but distinct relationships of the same type.

An Example from the Registrar

Note: differs from the example in the text.

The Student Entity Set

Attributes:

  1. ID number.
  2. Name.
  3. Age.
  4. Address.
  5. Class standing.
  6. ...

The Class Entity Set

Attributes:

  1. Department.
  2. Number.
  3. Title.
  4. Meeting time.
  5. Meeting place.
  6. Enrollment limit.
  7. ...

The Enrollment Relationship Set

Attributes:

  1. Student ID number.
  2. Department.
  3. Number.

Classes and Objects

Structure is a class with public access:

struct sample
{
   int a;
};

int main()
{
   sample s;

   s.a = 12;

   return 0;
}

Class is a structure with private access:

class sample
{
   int a;
};

int main()
{
   sample s;

   // error message: "member `a' is a private member of class `sample'"
   s.a = 12;

   return 0;
}

Declaring Classes

Elements of a class declaration:

  1. The reserved word class and a class name.
  2. A compound block containing: concluded by a semicolon.

Class members have access rights associated:

  1. Private --- The default for a class. Only member functions of the class may access private class members.
  2. Protected --- Only member functions of the class and derived classes may access protected class members.
  3. Public --- The default for a structure. Any function may access public class members.

``Access'' means:

Complex Number Example

Class declaration:

class Complex
{
 private:
   double real;
   double imag;

 public:
   // constructor declaration:
   Complex(double re, double im);

   // destructor definition:
   ~Complex() {}

   // other methods' declarations:
   void Add(Complex x, Complex y);
   void Mult(Complex x, Complex y);
   void Conj(void);
};

Method definitions:

Complex::Complex(double re, double im)
{
   real = re;
   imag = im;
}

void Complex::Add(Complex x, Complex y)
{
   real = x.real + y.real;
}

void Complex::Mult(Complex x, Complex y)
{
   real = x.real * y.real - x.imag * y.imag;
   imag = x.real * y.imag + x.imag * y.real;
}

void Complex::Conj(void)
{
   imag = -imag;
}

Client function:

int main()
{
   Complex x(-1.0, 3.0);
   Complex y(4.0, 5.0);
   Complex z(0.0, 0.0);

   z.Add(x, y);
   y.Conj();
   x.real = 23.0;   // access violation

   return 0;
}



Thomas P. Kelliher
Thu Mar 14 10:54:03 EST 1996
Tom Kelliher