0% found this document useful (0 votes)
176 views

C++ Primer: CSE225: Data Structures and Algorithms

The document discusses the basics of creating classes in C++ using Object Oriented Programming principles. It shows how to define a class for a dynamic array that can be used to store and retrieve values. The class is then generalized using templates to allow storing different data types in the dynamic array.

Uploaded by

Onurup Rahman
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
176 views

C++ Primer: CSE225: Data Structures and Algorithms

The document discusses the basics of creating classes in C++ using Object Oriented Programming principles. It shows how to define a class for a dynamic array that can be used to store and retrieve values. The class is then generalized using templates to allow storing different data types in the dynamic array.

Uploaded by

Onurup Rahman
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Lecture 03

C++ Primer
CSE225: Data Structures and Algorithms
Lets Get Started with “Classes”
• We already know how to allocate memory dynamically
• Lets do it the Object Oriented Way 
The Hackneyed Hello World
#include <iostream>
using namespace std;

int main()
{
cout << "Hello World" << endl;
return 0;
}
Basics
• For every data structure, we will create 3 files in our
Codeblocks project
• The declaration file (with .h extension)
• The definition file (with .cpp extension)
• The driver file (with .cpp extension)
Basics
#ifndef DYNARR_H_INCLUDED
#define DYNARR_H_INCLUDED

class dynArr
{
private:
int *data;
int size;

public:
dynArr();
dynArr(int);
~dynArr();
void allocate(int);
void setValue(int, int);
int getValue(int);
};

#endif //DYNARR_H_INCLUDED
dynarr.h (header file)
Basics
#include "dynarr.h" void dynArr::allocate(int s)
#include <iostream> {
using namespace std; data = new int[s];
size = s;
dynArr::dynArr() }
{
data = NULL; int dynArr::getValue(int index)
size = 0; {
} return data[index];
}
dynArr::dynArr(int s)
{ void dynArr::setValue(int index, int
data = new int[s]; value)
size = s; {
} data[index] = value;
}
dynArr::~dynArr()
{
delete [] data;
}
dynarr.cpp (definition file)
Basics
#include "dynarr.h"
#include <iostream>
using namespace std;

int main()
{
dynArr d(10);
int i;

for(i=0;i<10;i++)
d.setValue(i,i*2);
for(i=0;i<10;i++)
cout << d.getValue(i) << endl;
return 0;
}

main.cpp (driver file)


Template Class
• Now we have a neat class that gives us a 1D dynamic array
• But it only works for integer type
• What if we are to make it versatile, so that it works for any type,
e.g. float, double and char
• Should we have separate classes for each type?
• Write the same code for each type with just minor changes?
• Instead, we can use template classes
Template Class
#ifndef DYNARR_H_INCLUDED
#define DYNARR_H_INCLUDED

template <class T>


class dynArr
{
private:
T *data;
int size;

public:
dynArr();
dynArr(int);
~dynArr();
void allocate(int);
void setValue(int, T);
T getValue(int);
};

#endif // DYNARR_H_INCLUDED
dynarr.h (declaration file)
Template Class
#include "dynarr.h" template <class T>
#include <iostream> void dynArr<T>::allocate(int s)
using namespace std; {
data = new T[s];
template <class T> size = s;
dynArr<T>::dynArr() }
{
data = NULL;
template <class T>
size = 0;
T dynArr<T>::getValue(int index)
}
{
template <class T> return data[index];
dynArr<T>::dynArr(int s) }
{
data = new T[s]; template <class T>
size = s; void dynArr<T>::setValue(int index, T
} value)
{
template <class T>
dynArr<T>::~dynArr()
data[index] = value;
{ }
delete [] data;
} dynarr.cpp (definition file)
Template Class
#include "dynarr.h"
#include "dynarr.cpp"
#include <iostream>
using namespace std;

int main()
{
dynArr<int> di(10);
dynArr<double> dd(10);
int i;

for(i=0;i<10;i++)
{
di.setValue(i, i*2);
dd.setValue(i, i*.5);
}
for(i=0;i<10;i++)
cout << di.get(i) << " " << dd.getValue(i) <<
endl;

return 0; main.cpp (driver file)

You might also like