DSTC Unit-I
DSTC Unit-I
Abstract Data Types and the C++ Class: An Introduction to C++ Class, Data Abstraction and
Encapsulation in C++, Declaring Class Objects and Invoking Member Functions, Special Class
Operations (Constructor, Destructor and Operator Overloading), Miscellaneous Topics (Struct,
Union, Static), ADTs and C++ Classes, The Array as an Abstract Data Type, The Polynomial
Abstract Data type: Introduction, Polynomial Representation, Polynomial Addition. Spares
Matrices: Introduction, Sparse Matrix Representation, Transposing a Matrix, Matrix
Multiplication, Representation of Arrays
DATA STRUCTURE:-
A data structure is the way of organizing and storing the data into the computer, for efficient
accessing or retrieval of data.
Primitive Data Structures: The data structures that are not defined by any other data structures,
The basic data types are int, real, char, Boolean.
Non-Primitive Data Structures: The data structures that are defined by any other data structures.
Again it is categorized into 2 types.
1. Linear Data Structures: A data structure is said to be linear if its elements form a sequence or a
linear list.
Examples: Array, Linked List, Stacks, Queues
2. Non-Linear Data: A data structure is said to be Non-linear if its elements form a not sequence or
non linear list.
Examples: Trees, Graphs
1
Operations on Linear and Non Linear Data Structures
Traversal : Visit every part of the data structure
Search : Traversal through the data structure for a given element
Insertion : Adding new elements to the data structure
Deletion: Removing an element from the data structure.
Sorting : Rearranging the elements in some type of order(e.g Increasing or Decreasing)
Merging : Combining two similar data structures into one
Class is a blueprint of data members and member functions. Class is a user defined data type
like structures and unions in C.
2) Data members: the data that makes up the class (e.g. name. roll_no and age).
3) Member functions: the set of operations that may be applied to the objects of a class (e.g.
getData(), display() ).
4) Levels of program access: these control the level of access to data members and member
functions from program code that is outside the class. There are three levels of access to class
members: public(Access outside of the class), protected(Immediate derived class can access) and
private(can’t access outside of the class).
2
Declaring Class Objects and Invoking Member Functions:-
The class objects are declared and created in the same way that variables are declared and
created.
The data members and member functions of class can be accessed using the dot ( . ) or
arrow () operator with the object.
The public data members are accessed, the private data members are not allowed to be
accessed directly by the object, it can access by public member functions.
While implementing the member functions outside of the class we use special reference
called scope resolution operator (::).
3
Data abstraction and Encapsulation:-
The binding the data and functions into a single unit called class is known as encapsulation.
The data is not accessible to the outside world, and only those functions which are enclosed
in the class can access it.
Abstraction is seperating the logical properties from their implementation. It shows only the
essential features of the application and hiding the details.
In C++, classes can provide methods to the outside world to access & use the data
variables, keeping the variables hidden from direct access. This can be done using access
specifiers(private).
Constructor, Destructor:- These are special member functions of the class.
Operator Overloading:
Operator overloading is a compile-time polymorphism in which the operator is overloaded
to provide the special meaning to the user-defined data type.
To overload an operator must write a function in public access specifies.
Syntax: returnType operator symbol (arguments)
{ ... .. ... }
This pointer:
'this' pointer is a constant pointer that holds the memory address of the current object. If
‘this’ pointer is written in member function of a class, it can access data members of that
class for given object.
Friend Function:
It is non-member function of a class but can access all members of a class (Both public and
private data)
While defining friend function out of the class, no need to use scope resolution (: :) operator
and class name.
Syntax:
Declaration with in class: friend returnType function_name(parameters);
Calling through main: function_name(parameters);
Definition outside of the class: returntype function_name(parameters) { - - - }
5
Example: Implement a program on operator overloading, this pointer and friend function using C++
#include <iostream.h>
class Test void dosth(Test t)
{ {
private: t.count=t.count+100;
int count; cout<<"total count: "<<t.count;
int a,b; }
public:
void get() {count=5;} int main()
void put() {
{ cout<<"Count: "<<count<<endl; } Test t;
void operator ++() t.get();
{ count = count+1; } t.put();
++t;
Test input(int a,int b) t.put();
{
this->a=a+b;
this->b=a-b; Test t1,t2;
return *this; t2=t1.input(10,5);
} t1.output();
void output() t2.output();
{ cout<<"a,b values are:";
cout<<a<<" "<<b<<endl;} dosth(t);
return 0;
friend void dosth(Test);
}; }
MISCELLANEOUS TOPICS:
Structure:
Both the structure and classes are almost equivalent.
Structure is collection of different data items. The default access specifier is public.
Class is blue print of data members and member functions. The default access specifier is private.
Union:
Union reserves the storage for one of its largest data member at run time.
Unions are used in Linked List and Inheritance.
Static:
Static data member is similar to global variable throughout the class, because it is not similar data
member in the class.
Each object doesn’t have its own copy; we have only one copy, it shares by all objects of the class.
Examples:
struct student union student
{ {
int rollno, age, marks; int rollno, age, marks;
char name[20]; char name[20];
}S1; // it allocates 26 bytes of memory }S2; // it allocates only 20 bytes of memory
class func void main()
{ public: {
func() { func f1; //11,6
int a=10; func f2; //11,7
static int b=5; func f3; //11,8
a++; b++; func f4; // 11,9
cout<<a<<b; } }; }
6
ADT (Abstract Data Type):-
Abstract Data type (ADT) is a type (or class) for objects whose behavior is defined by a set of
value and a set of operations.
The definition of ADT only mentions what operations are to be performed but not how these
operations will be implemented.
The process of providing only the essentials and hiding the details is known as abstraction
8
Column-Major Order:
In this method the elements are stored column wise, i.e. m elements of first column are
stored in first m locations, m elements of second column are stored in next m locations and
so on
Address of A [ i ][ j ] = BaseAddress + Size of datatype * [ i + (m *j)]
Row-Major Order:
In this method the elements are stored row wise, i.e. n elements of first row are stored in
first n locations, n elements of second row are stored in next n locations and so on
Address of A [ i ][ j ] = BaseAddress + Size of datatype * [ ( i * n) + j ]
Example:
9
Polynomial Abstract Data Type:
e
Polynomial is a result of terms, where each has a form aX , where “X” is a variable, “a” is the coefficient,
10
Code for representations:
class Polynomial class Term
{ friend Polynomial;
{
private:
private: int exp;
float coef;
int degree;
};
float coef[degree+1]; class Polynomial
{ private:
………
Term *termArray;
}; ………
};
Node of a Polynomial:
In each node the exponent field will store the corresponding exponent and the coefficient field will store
the corresponding coefficient. Link field points to the next item in the polynomial.
11
Ex: Addition of two polynomials using arrays
Sparse Matrix:
A matrix is a two-dimensional data object made of m rows and n columns, therefore having total m
x n values. If majority of the elements of the matrix have 0 value, then it is called a sparse matrix.
Why to use Sparse Matrix?
Storage: There are lesser non-zero elements than zeros, lesser memory can be used to store only
those non zero elements.
Computing time: Computing time can be saved for traversing only non-zero elements..
Representing a sparse matrix by a 2D array leads to wastage of lots of memory as zeroes in the
matrix are of no use. So, instead of storing zeroes, we store only non-zero elements. This means
storing non-zero elements with triples- (Row, Column, value).
Representation: Sparse Matrix Representations can be done in following ways
12
Method 2: Using Linked Lists
In linked list, each node has five fields. These fields are defined as:
Row: Index of row, where non-zero element is located
Column: Index of column, where non-zero element is
located
Value: Value of the non zero element located at index
Down: Address of next non-zero in the same Column
Right: Address of next non-zero in the same row
13
ADT SparseMatrix
{
Instances:
A set of triples, <row, col, value>
//where row and col are integers and form a unique combination.
Operations:
void create(n);//creates a SparseMatrix that can hold n non-zero elements information.
SparseMatrix transpose(SparseMatrix A); //It return the matrix produced by
interchanging the row and column value of every triple.
SparseMatrix add(SparseMatrix A, SparseMatrix B); //if dimensions of A and B are the
same return the matrix else return error.
SparseMatrix multiply(SparseMatrix A, SparseMatrix B); // if number of columns in A
equals number of rows in B return the matrix C produced by multiplying A by B according
to the formula: C[i][j]= Ʃ(A[i][k]*B[k][j]) where C[i][j] is the (i,j) th element else return
error.
}
dij a b ik kj
k 0
In general, you multiply a values at row 'i' in matrix A with a column in the matrix B and
store the sum of the row operation as a result in the resultant matrix.
However, since this problem involves sparse matrices, we can ignore the multiplication with
the column in matrix B if the value in matrix A is 0(zero). This small optimization helps us in
avoiding K operations where K is the number of rows in the matrix B or the number of columns in
the matrix A.
iA.rows; jB.cols; kA.cols/B.rows
sum += A[i][k]*B[k][j];
C[i][j] = sum;
16