Basic C++ for DATA STRUCTURES

Anna Kim
5 min readMar 7, 2019

--

As a first post, I want to make a point of why and how I am going to use my IT blog. Mainly, all contents are based on University class where I study Computer Engineering for the second major. As a college of Liberal Arts student, starting Engineering needs courage as well as lots of studying to catch up. This blog is specifically for people who want to gain basic knowledge of computer engineering step by step and also a good tool for me to study hard.

(let's get started!)

starting my IT study blog _ photo in HKU library

before data structure…

Reviewing C++

C++ is one of Object Oriented programming languages and OOPs have characteristics of “Encapsulation, Inheritance, Polymorphism, Abstraction”. Encapsulation is about capturing data and keeping it safely and securely from outside interfaces. This can be accomplished by allowing variables changed only by functions inside the class. Binding data and method(operation) into one single unit(a class) and using Access Modifier(ex. public, private, protected) are two important aspects of encapsulation. Understanding STL already built in C++ and using it well is important. (course link: https://code.plus/course/2)

Object Pointer

Reference : <명품 c++프로그래밍> from class
int a = 10;
int *pa; // pointer variable *pa
pa = &a; // address value &a

Reminding concept or pointer, the value of a and *a is both 10. Object pointer can also be used as an example above and *p is used to point at a donut object. To change private member variable of object donut, it has to be either (p->getArea()) or ((*p).getArea) or (donut.getArea()) and not (p->radius).

Object array

Likewise, with object array you can access private attribute by either (circleArray[0].getArea()) or (p->getArea()).

reviewing quiz (object pointer, object array)

Static Allocation vs Dynamic Allocation

image from computer science geeks

The difference between static and dynamic allocation is when the allocation is done, where they are allocated, the keyword used. While static memory allocates in advance, dynamic memory allocates during program execution. Static memory allocation is done on stack whereas dynamic memory allocation is on heap. When we don’t know the exact number to allocate, that’s when dynamic allocation is needed.

int a; int a[100]; //variables are allocated instantly when compile is done and saved in stackint *pInt = new int; //variables are allocated during the program executionCircle *pCircle = new Circle();delete pInt;
delete pCircle;

In C++ keyword “new, delete” is used for dynamic allocation and always use pointer to allocate dynamically. Usage is same basically. Don’t forget to delete after dynamically allocating memory.

Constructor, dynamic allocation, delete
Circle *q = new Circle(30);
delete q;
//dynamic array of objects
Circle *p = new Circle[30];
delete [] p;
dynamic array of class

this pointer

this in C++ is 1. Currently running object and 2. pointer. In the example above, circle c1, c2, c3 changes from 1, 2, 3 to 4, 5, 6.

Reference variable

int n=2;
int &refn = n; // reference variable refn is another name for n
Circle circle;
Circle &refc = circle;

Reference variable refn and variable n share one memory space which is both referring to 2. So basically when reference variable is declared, no memory space is created but just new name calling the same value.

#include <iostream>
using namespace std;
class Circle {
int radius;
public:
Circle() { radius = 1; }
Circle(int radius) { this->radius = radius; }
void setRadius(int radius) { this-> radius = radius; }
double getArea() { return 3.14*radius*radius; }
};
int main() {
Circle circle;
Circle &refc = circle;
refc.setRadius(10); //not refc->setRadius(10);
cout<<ref.getArea()<<" "<<circle.getArea();

refc.setRadius(10) is right way to change radius value of refc just like circle.setRadius(10).

/* difference in usage*/
circle *c;
c=&circle;
c->setRadius(10); //pointer variable
int n=3;
int &refn=n;
refn.setRadius(10); //reference varibale

Call by reference

#include <iostream>
using namespace std;
void swap(int &a, int &b) {
int tmp;

tmp = a;
a = b;
b = tmp;
}
int main() {
int m=2, n=9;
swap(m,n);
cout<<m<<' '<<n;
}

This example shows how reference variable is very convenient when calling parameters and using the exact address of that data to change. If we want to swap a and b when using pointer,

void swap(int *a, int *b) {
int tmp;

tmp=*a;
*a=*b;
*b=tmp;
}

but with reference variable, a and b can be used as it is. It’s because reference variable is a type of pointer format and referring to the address of value that we want to change.

wrap up quiz for reference variable

Overloading

OOP characteristics are “Encapsulation, Inheritance, Polymorphism” and representative example of polymorphism is 1. Overloading 2.Overriding 3.Default parameter.

what is overloading?

  • same function name
  • different parameter number and type
int add(int a, int b) { .. }
double add(double a, double b) { .. }
int add(int a, int b, int c) { ...}

In C++ functions with same name are treated differently if they have different parameter number, type and this is applied to array as well as object.

concept of overload

So you can use the same function name ‘add’ over and over instead of naming something like ‘intAdd’, ‘doubleAdd’, ‘intAddWithThreeParameter’… and so on.

Duplicate Constructor Function

Purpose of duplicating constructor function is to set initial value when creating an object.

class string {
...
public:
string(); //create string object with empty string
string(string& str); //create string object with copying str
string(char* s);
...
};
string str;
string copyAddress(address);
string address("Seoul");

Default parameter

If the parameter does not pass the value, the parameter is declared to have the default value. So ‘parameter = default value’ is the format of default parameter

void star(int a=5); // default value of a is 5star(); //value of a is 5
star(10); //value of a is 10

How to use default parameter

  • General parameter comes in front and Default parameter is put behind.
  • General parameter cannot be omitted.
void calc(int a, int b=5, int c, int d=0) //compile error
void sum(int a=0, int b, int c) //compile error
void calc(int a, int b=5, int c=0, int d=0) //correct!

To wrap up, with function overloading and default parameter, we can simplify the function!

--

--