C++ Classes

Linked Lists Operator Overloading

Introduction

- ADT and C++
- Consider integer type
- Class gives mechanism for user to define a type
Syntax
Declaration
Format
class <ADT> {
  <class body>
}
for <ADT> ::= <id>
<class body> includes data members, function members, and access levels for data members and function members
Data members
Data structure(s) used to implement the ADT
Format same as for other declarations except explicit initialization not allowed
By convention, list in ascending order of memory space required
Function members
Specify operations on ADT
Declared in class body using function prototype and defined in another file; or may be defined in same file
Have access to all data members
Access level
Mechanism for data hiding
Three levels: public, private, protected
Public: accessible from anywhere within program
Private: accessible only by member functions and friends of the class
Protected: accessible by member functions and by functions of a derived class ( to be discussed later)
Any data member or function member not specifically public or protected is private
Function definition
The body of the function
May be defined along with declaration of function in body of class; automatically "inline" (i.e. is inserted at point of invocation at compile time rather at run time)
May be defined in file other than file which contains declaration of class
- Syntax: scope operator (::) required as prefix to function name
- #include of file containing declaration necessary
Class object
Memory allocated for object of class type when object declared
Declaration <classtype> <classobject>;
for <classtype> ::= <ADT>
<classobject> ::= <id>
Examples:
stack S;
stack A,B;
Manipulating class object
- Private class members cannot be accessed
- Access outside class by scope operator (.)
<classobject>.<classmember>
for
<classmember> ::= <non-priv function member>| <non-priv data member>
Examples:
S.push(c);
if (!S.empty())
cout<< "not empty\n";

 

 

//Sample Program for Complex Numbers

#include <iostream.h>

class complex {
  int real, imag;
  public:
    complex ();
    void read ();
    void write ();
    int iszero ();
    void add (complex C1, complex C2);
};

complex::complex () {
  real = imag = 0;
}

void complex::read () {
  cin >> real >> imag;
}

void complex::write () {
  cout << real <<" + "<< imag << 'i';
}

int complex::iszero () {
  return (real ==0 && imag == 0)
}

void complex::add (complex C1, complex C2) {
  real = C1.real + c2.real;
  imag = C1.imag + c2.imag;
}

main () {
  complex C,D,S;
  if (C.iszero)
    cout << "value is zero\n";
  else
    cout << "value is not zero\n";
  C.write ();
  cout << '\n';
  C.read ();
  if (C.iszero)
    cout << "value is zero\n";
  else
    cout << "value is not zero\n";
  C.write ();
  cout << '\n';
  D.read ();
  S.add(C,D);
  S.write ();
  cout << '\n';
}

//Sample Program for Stack

//stack.h
#ifndef stack_h
#define stack_h

#include <iostream.h>

const Max = 8;

class Stack {
  int Top;
  char SData[Max];

public:
  Stack ();
  Stack (char Ch);
  int Push (char Ch);
  int Pop (char &Ch);
  int Empty ();
  void CopyStack (Stack &S);
  void PrintStack ();
};
#endif

//stack.cc
#include "stack.h"

Stack::Stack () {
  Top = 0;
}

Stack::Stack (char Ch) {
  SData[0] = Ch;
  Top = 1;
}

int Stack::Push (char Ch) {
  if (Top<Max) {
    SData[Top++] = Ch;
    return 1;
  }
  else
    return 0;
}

int Stack::Pop (char &Ch) {
  if (Top>0) {
    Ch = SData[--Top];
    return 1;
  }
  else
    return 0;
}

int Stack::Empty () {
  return (Top==0);
}

void Stack::CopyStack (Stack &S) {
  S.Top = Top;
  for (int i=0; i<Top; i++)
    S.SData[i] = SData[i];
}

void Stack::PrintStack () {
  for (int i=Top-1;i>=0;i--)
    cout << SData[i];
  cout << '\n';
}

Member functions

Categories of member functions
Manager functions: initialization, assignment, memory management
Implementor functions: operations associated with class abstraction
Utility functions: provide support for other class member functions
Access functions: provide access to data;
Manager functions
Constructor
-Provides automatic initialization of data members
-Has name of class
-Invoked implicitly when class object is defined or allocated using new
-Has no return type or explicitly returned value
-May be overloaded to provide alternative ways of initializing members of a class
Destructor
-Invoked when object goes out of scope or delete is applied to class pointer
-Has name of class preceded by ~
-Has no arguments
-May be explicitly invoked, but this is unusual

//Trace this program to find the output

#include <iostream.h>

class T {
  int a;
  public:
    T();
    ~T();
    void f();
    void g();
    void h();
};

T::T() { cout << "in T()\n"; a = 3; }
T::~T() { cout << "in ~T()\n"; }
void T::f() { cout << "in T::f()\n"; }
void T::g() { cout << "in T::g()\n"; }
void T::h() { cout << "in T::h()\n"; }

class S {
  int b;
  public:
    S();
    ~S();
    void f();
    void g();
};

S::S() { cout << "in S()\n"; b = 2; }
S::~S() { cout << "in ~S()\n"; }
void S::f() { cout << "in S::f()\n"; }
void S::g() { cout << "in S::g()\n"; }

void p(S x) {
  cout << "enter p\n";
  T y;
  y.f();
  x.f();
  x.g();
  cout << "exit p\n";
}

main() {
  S s;
  T t;
  s.f();
  t.g();
  p(s);
  cout << "exit main\n";
}

Output:
in S()
in T()
in S::f()
in T::g()
enter p
in T()
in T::f()
in S::f()
in S::g()
exit p
in ~T()
in ~S()
exit main
in ~T()
in ~S()

Back Home Up Next