Data Abstraction


class listType
{
public:
    bool isEmptyList() const;
    bool isFullList() const;
    int search(int searchItem) const;
    void insert(int newElement);
    void remove(int removeElement);
    void destroyList();
    void printList() const;
    listType(); // constructor

private:
    int list[1000];
    int length;
}


struct versus a class

information hiding

the header file (listType.h)

class listType
{
public:
    bool isEmptyList() const;
    // returns true if the list is empty (length=0)
    // returns false if the list is not empty

    bool isFullList() const;
    // returns true if the list is filled up (length=1000)
    // returns false if the list is not filled up yet

    int search(int searchItem) const;
    // returns the index of the list element
    // that is equal to the parameter value.

    void insert(int newElement);
    // inserts a new element to the end of the list.

    void remove(int removeElement);
    // removes the element with the index
    // passed as the parameter value.

    void printList() const;
    // prints the elements of the list

    listType();
    // constructor.

private:
    int list[1000];
    // variable to store the list elements
    int length; 
    // variable to store the size of the list at a given instance.
};


the implementation file (listTypeImp.cpp)


#include<iostream>
#include "listType.h"
// angular brackets are for system provided header files
// double quotes for user defined header files.

using namespace std;

bool listType::isEmptyList() const
{
    if (length>0)
        return true;
    else return false;
}

bool listType::isFullList() const
{
    if (length==1000)
        return true;
    else return false;

}

void listType::insert(int newElement)
{
    length++;
    list[length-1] = newElement;
}

listType::listType()
{
    length = 0;
}


void listType::printList() const
{
    for(int i=0;i<length;i++)
    {
        cout<<i<<"th element of the list is"<<list[i]<<endl;
    }
}

void listType::remove(int removeElement)
{
    for(int i=removeElement;i<length;i++)
    {
        list[i] = list[i+1];
    }
    length--;
}

int listType::search(int searchItem) const
{
    for(int i=0;i<length;i++)
    {
        if (list[i]==searchItem)
            return i;
    }
    return -1;
}


the executable file (test.cpp)


#include "listType.h"

void main()
{
    listType newlist = listType();
    listType oldlist;

    newlist.insert(6);
    newlist.insert(5);
    newlist.insert(9);
    newlist.printList();
    newlist.remove(0);
    newlist.printList();
    newlist.insert(37);
    newlist.insert(42);
}


static members of a class

  • Consider the below class definition.
     
  • class Student
    {
     public:
         
         void print() const;
         
    static int getStudCount();
        
    Student();

     private:
         static int
    studCount;
         float gpa;
         string name;
     };

    Student::Student()
    {
        strcpy(name, " New Student");
        gpa = 0.0;
        studCount++;
    }
     

    Student::~Student()
    {
        studCount--;
    }
     

    int Student::getStudCount()
    {
        return studCount;
    }

    Student s1;
    cout << "\nThe number of objects is " ;
         << s1.getStudCount();