Advanced class features
Advanced class features

Inline functions

Designed to speed up programs (like macros)

The complier replaces the function call with the function code (no function call!)

Advantage: speed

Disadvantage: code bload

ex. 10 function calls → 10 * function's size

Advanced class features

How to make a function inline

Use the inline keywork either in function declaration or in function definition

Both member and standalone functions can be inline

Common practice:

Place the implementation of the inline function into the header file

Only small functions are eligible as inline

The Compiler may completely ignore your request

Advanced class features

inline function examples

inline double square(double a){
	return a * a;
}
class Value{
	int val;
public:
	line int getValue() const{
		return val;
	}
	inline void setVal(int val){
		this->val  = val;
	}
}
			
Advanced class features

Stack vs Heap

Members of the class

Heap - Dynamic allocation

void draw(){
	Point *p = new Point();
	p->move(3, 3);
	//....
	delete p;
}
			

Stack - Automatic allocation

void draw(){
	Point p;
	p.move(6, 6);
	//....
}
			
Classes and Objects

Array of object

class Point{
	int x, y;
public:
	Point(int x = 0, int y = 0);
}
			

What is the difference between these two arrays?

Point* a = new Point[4];
Point b[4];
			
Classes and Objects

Array of pointers

Point** a = new Point*[4];
for(int i = 0; i < 4; i++){
	a[i] = new Point(0, 0);
}
for(int i = 0; i < 4; i++){
	cout << *t[i] << endl;
}
			
Classes and Objects

Static members

static methods

static data

Functions belonging to a class scope which don't access object's data can be static

Static method can't be const methods (they do not access object's state)

They not called on specific objects => they have ho this pointer

Classes and Objects

Static members

//Complex.h
class Complex{
public:
	Complex(int re = 0, int im = 0);
	static int getNumComplex();
private:
	static int num_complex;
	double re, im;
}
			
//Complex.cpp
//Initializing static class member
int Complex::num_complex = 0;
int Complext::getNumComplex(){
	return num_complex;
}
Complex::Complex(int re, int im){
	this->re = re;
	this->im = im;
	++num_complex;
}
				
Advanced class features

Static method invocation

Complex z1(1, 2), z2(3, 3), z3;
//Elegant
cout << "Number of complexes: " << Complex::getNumComplex() << endl;
//non-Elegant
cout << "Number of complexes: " << z1.getNumComplex();
			
Advanced class features

Classes vs. Structs

default access specifier

class: private

struct: public

class: data + method, can be used polimorphically

struct: mostly data + convenience methods

Advanced class features

Classes vs. Structs

class List{
	struct Node{
		Node* next = NULL;
		int val;
	};
	Node* head = NULL;
public:
	List();
	~List();
	void insert(int a);
	void printAll() const;
};
			
Advanced class features

Passing function arguments

By value

The function works on a copy of the variable

By reference

The function works on the original variable, may modify it

By constant reference

The function works on the original variable, may not modify (verified by the complier)

Advanced class features

Passing function arguments

Passing primitive values

void f1(int x){
	x++;
}
void f2(int& x){
	x++;
}
void f3(const int& x){
	x++;// Error
}
void f4(int *x){
	*x++;
}
				
int main(){
	int y = 5;
	f1(y);
	f2(y);
	f3(y);
	f4(y);
	return 0;
}
				
Advanced class features

Passing function arguments

Passing objects

void f1(Point p);//Copy constructor will be used on the argument
void f2(Point& p);
void f3(const Point& p);//only const methods of the class can be invoked on this argument
void f4(Point *p);
int main(){
	Point p1(3, 3);
	f1(p1);
	f2(p1);
	f3(p1);
	f4(&p1);
	return 0;
}
			
Advanced class features

friend functions, friend classes, friend member functions

friends are allowed to access private member of a class

Use it rarely

operator overloading

Advanced class features

friend vs. static functions

class Test{
	int a;
	static int s;
public:
	Test(int a):a(a){}
	void print() const;
	static void print(const Test& obj);
	friend void print(const Test& obj);
};
			
Advanced class features

friend vs. static functions

int Test::s = 0;
void Test::print()const{
	cout << "Member" << a << endl;
}
void Test::print(const Test &obj){
	cout << "Static" << obj.a << endl;
}
void print(const Test obj){
	cout << "Static" << obj.a << endl;
}
int main(){
	Test test(10);
	test.print();
	Test::print();
	print(test);
	return 0;
}
			
Advanced class features

friend class vs. friend member function

class List{
	Element *head;
public:
	bool find(int key);
	...
};
				
class Element{
	int key;
	Element* next;
	friend class List;
	...	
};
class Element{
	int key;
	Element* next;
	friend class List:find(int key);
	...
};
				
Advanced class features

Returning a reference to a const object

//version 1
Fraction max(const Fraction &f1, const Fraction &f2){
	if(f1.getNumerator() * f2.getDenominator() - f1.getDenominator() * f2.getNumerator()){
		return f1;//copy constructor invocation
	}
	return f2;
}
//version 2
const Fraction& max(const Fraction &f1, const Fraction& f2){
	if(f1.getNumerator() * f2.getDenominator() - f1.getDenominator() * f2.getNumerator()){
		return f1;//More efficient
	}
	return f2;
}
//The reference should be to non-local object
			
Advanced class features

Constructor delegation (C++ 11)

	//C++ 03
class A{
	void init(){
		cout << "This is initial" << endl;
	}
	vodi doSomething(){
		cout << "Do something" << endl;
	}
public:
	A(){
		init();
	}
	A(int a){
		init();
		doSomething();
	}
}
			
Advanced class features

Constructor delegation (C++ 11)

	//C++ 11
class A{
	void doSomething(){
		cout << "Do something" << endl;
	}
public:
	A(){
		cout << "Initial" << endl;
	}
	A(int a):A(){
		doSomething();
	}
}