consolidate all repos to one for archive
This commit is contained in:
20
semester_2/programiranje_2/primeri/Example01/Point.cpp
Normal file
20
semester_2/programiranje_2/primeri/Example01/Point.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <iostream>
|
||||
#include "Point.h"
|
||||
|
||||
int Point::getX() {
|
||||
return x;
|
||||
}
|
||||
int Point::getY() {
|
||||
return y;
|
||||
}
|
||||
//void Point::setX(int xx) {
|
||||
void Point::setX(int x) {
|
||||
//x=xx;
|
||||
this->x=x;
|
||||
}
|
||||
void Point::setY(int y) {
|
||||
this->y=y;
|
||||
}
|
||||
void Point::print() {
|
||||
std::cout << "(" << x << "," << y << ")" << std::endl;
|
||||
}
|
17
semester_2/programiranje_2/primeri/Example01/Point.h
Normal file
17
semester_2/programiranje_2/primeri/Example01/Point.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef EXAMPLE01_POINT_H
|
||||
#define EXAMPLE01_POINT_H
|
||||
|
||||
class Point {
|
||||
private:
|
||||
int x, y;
|
||||
public:
|
||||
int getX();
|
||||
int getY();
|
||||
void setX(int x);
|
||||
void setY(int y);
|
||||
void print();
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //EXAMPLE01_POINT_H
|
20
semester_2/programiranje_2/primeri/Example01/main.cpp
Normal file
20
semester_2/programiranje_2/primeri/Example01/main.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <iostream>
|
||||
#include "Point.h"
|
||||
|
||||
int main() {
|
||||
Point a;
|
||||
Point b;
|
||||
//Point b(a);
|
||||
/**/
|
||||
a.setX(3);
|
||||
a.setY(2);
|
||||
b.setX(1);
|
||||
b.setY(1);
|
||||
|
||||
std::cout << a.getX() << std::endl;
|
||||
std::cout << a.getY() << std::endl;
|
||||
//b.x=10;
|
||||
a.print();
|
||||
b.print();
|
||||
return 0;
|
||||
}
|
15
semester_2/programiranje_2/primeri/Example01a/Stack01a.cpp
Normal file
15
semester_2/programiranje_2/primeri/Example01a/Stack01a.cpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#include "Stack01a.h"
|
||||
|
||||
void init(Stack& s) {
|
||||
s.top = EMPTY; }
|
||||
|
||||
void push(Stack& s, int n) {
|
||||
s.arr[++s.top] = n; }
|
||||
|
||||
int pop(Stack& s) {
|
||||
return s.arr[s.top--];}
|
||||
|
||||
int isEmpty(Stack& s) {
|
||||
return s.top == EMPTY;}
|
||||
|
||||
|
17
semester_2/programiranje_2/primeri/Example01a/Stack01a.h
Normal file
17
semester_2/programiranje_2/primeri/Example01a/Stack01a.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef EXAMPLE01A_STACK01A_H
|
||||
#define EXAMPLE01A_STACK01A_H
|
||||
|
||||
#define SIZE 50
|
||||
#define EMPTY (-1)
|
||||
|
||||
struct Stack {
|
||||
int arr[SIZE];
|
||||
int top;
|
||||
};
|
||||
|
||||
void init(Stack& s);
|
||||
void push(Stack& s, int n);
|
||||
int pop(Stack& s);
|
||||
int isEmpty(Stack& s);
|
||||
|
||||
#endif //EXAMPLE01A_STACK01A_H
|
30
semester_2/programiranje_2/primeri/Example01a/main.cpp
Normal file
30
semester_2/programiranje_2/primeri/Example01a/main.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#include <iostream>
|
||||
#include "Stack01a.h"
|
||||
|
||||
int main() {
|
||||
Stack my_stack1;
|
||||
init(my_stack1);
|
||||
|
||||
Stack my_stack2;
|
||||
init(my_stack2);
|
||||
|
||||
push(my_stack1, 7);
|
||||
push(my_stack1,1);
|
||||
push(my_stack2, 3);
|
||||
|
||||
//my_stack1.top=0;
|
||||
//my_stack1.arr[1]=4;
|
||||
std::cout << pop(my_stack1) << " ";
|
||||
std::cout << pop(my_stack1) << " ";
|
||||
std::cout << std::endl;
|
||||
|
||||
if (isEmpty(my_stack1))
|
||||
std::cout << "Stack1 is empty" << std::endl;
|
||||
else
|
||||
std::cout << "Stack1 isn't empty" << std::endl;
|
||||
if (isEmpty(my_stack2))
|
||||
std::cout << "Stack2 is empty" << std::endl;
|
||||
else
|
||||
std::cout << "Stack2 isn't empty" << std::endl;
|
||||
return 0;
|
||||
}
|
14
semester_2/programiranje_2/primeri/Example01b/Stack01b.cpp
Normal file
14
semester_2/programiranje_2/primeri/Example01b/Stack01b.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
|
||||
#include "Stack01b.h"
|
||||
|
||||
void push(int n) {
|
||||
arr[++top] = n;
|
||||
}
|
||||
|
||||
int pop() {
|
||||
return arr[top--];
|
||||
}
|
||||
|
||||
int isEmpty() {
|
||||
return top == EMPTY;
|
||||
}
|
15
semester_2/programiranje_2/primeri/Example01b/Stack01b.h
Normal file
15
semester_2/programiranje_2/primeri/Example01b/Stack01b.h
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
#ifndef EXAMPLE01B_STACK01B_H
|
||||
#define EXAMPLE01B_STACK01B_H
|
||||
|
||||
#define SIZE 50
|
||||
#define EMPTY (-1)
|
||||
|
||||
static int arr[SIZE];
|
||||
static int top = EMPTY;
|
||||
|
||||
void push(int n);
|
||||
int pop();
|
||||
int isEmpty();
|
||||
|
||||
#endif //EXAMPLE01B_STACK01B_H
|
16
semester_2/programiranje_2/primeri/Example01b/main.cpp
Normal file
16
semester_2/programiranje_2/primeri/Example01b/main.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#include <iostream>
|
||||
#include "Stack01b.h"
|
||||
|
||||
int main() {
|
||||
push(7);
|
||||
push(1);
|
||||
std::cout << "Pop from stack: ";
|
||||
std::cout << pop() << " ";
|
||||
std::cout << pop() << " ";
|
||||
std::cout << std::endl;
|
||||
if (isEmpty())
|
||||
std::cout << "Stack is empty" << std::endl;
|
||||
else
|
||||
std::cout << "Stack isn't empty" << std::endl;
|
||||
return 0;
|
||||
}
|
19
semester_2/programiranje_2/primeri/Example01c/Stack01c.cpp
Normal file
19
semester_2/programiranje_2/primeri/Example01c/Stack01c.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include "Stack01c.h"
|
||||
#define EMPTY (-1)
|
||||
|
||||
Stack::Stack() {
|
||||
top=EMPTY;
|
||||
}
|
||||
|
||||
void Stack::push(int n) {
|
||||
arr[++top] = n;
|
||||
}
|
||||
|
||||
int Stack::pop() {
|
||||
return arr[top--];
|
||||
}
|
||||
|
||||
int Stack::isEmpty() {
|
||||
return top == EMPTY;
|
||||
}
|
||||
|
19
semester_2/programiranje_2/primeri/Example01c/Stack01c.h
Normal file
19
semester_2/programiranje_2/primeri/Example01c/Stack01c.h
Normal file
@@ -0,0 +1,19 @@
|
||||
|
||||
#ifndef EXAMPLE01C_STACK01C_H
|
||||
#define EXAMPLE01C_STACK01C_H
|
||||
|
||||
#define SIZE 50
|
||||
|
||||
class Stack {
|
||||
private:
|
||||
int arr[SIZE];
|
||||
int top;
|
||||
public:
|
||||
Stack();
|
||||
void push(int n);
|
||||
int pop();
|
||||
int isEmpty();
|
||||
};
|
||||
|
||||
|
||||
#endif //EXAMPLE01C_STACK01C_H
|
23
semester_2/programiranje_2/primeri/Example01c/main.cpp
Normal file
23
semester_2/programiranje_2/primeri/Example01c/main.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <iostream>
|
||||
#include "Stack01c.h"
|
||||
|
||||
int main() {
|
||||
Stack my_stack1, my_stack2;
|
||||
my_stack1.push(7);
|
||||
my_stack1.push(1);
|
||||
my_stack2.push(2);
|
||||
//my_stack1.top=0;
|
||||
//my_stack1.arr[1]=4;
|
||||
std::cout << my_stack1.pop() << " ";
|
||||
std::cout << my_stack1.pop() << " ";
|
||||
std::cout << std::endl;
|
||||
if (my_stack1.isEmpty())
|
||||
std::cout << "Stack1 is empty" << std::endl;
|
||||
else
|
||||
std::cout << "Stack1 isn't empty" << std::endl;
|
||||
if (my_stack2.isEmpty())
|
||||
std::cout << "Stack2 is empty" << std::endl;
|
||||
else
|
||||
std::cout << "Stack2 isn't empty" << std::endl;
|
||||
return 0;
|
||||
}
|
40
semester_2/programiranje_2/primeri/Example02/Point.cpp
Normal file
40
semester_2/programiranje_2/primeri/Example02/Point.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include "Point.h"
|
||||
|
||||
Point::Point() : x(0), y(0) {
|
||||
}
|
||||
|
||||
Point::Point(const Point& t) : x(t.x), y(t.y) {
|
||||
}
|
||||
|
||||
Point::Point(int xy) : x(xy), y(xy) {
|
||||
}
|
||||
|
||||
Point::Point(int xx, int yy) {
|
||||
//Point::Point(int x, int y) {
|
||||
//Point::Point(int x, int y) : x(x), y(y) {
|
||||
x=xx;
|
||||
y=yy;
|
||||
//this->x=x;
|
||||
//this->y=y;
|
||||
}
|
||||
|
||||
Point::~Point() {
|
||||
}
|
||||
|
||||
int Point::getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
int Point::getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
void Point::print() {
|
||||
std::cout << "(" << x << ", " << y << ") " << std::endl;
|
||||
}
|
||||
|
||||
double Point::distance(Point t) {
|
||||
return std::sqrt((double)(x - t.x)*(x - t.x)+(y - t.y)*(y - t.y));
|
||||
}
|
20
semester_2/programiranje_2/primeri/Example02/Point.h
Normal file
20
semester_2/programiranje_2/primeri/Example02/Point.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#ifndef EXAMPLE02_POINT_H
|
||||
#define EXAMPLE02_POINT_H
|
||||
|
||||
class Point {
|
||||
private:
|
||||
int x, y;
|
||||
public:
|
||||
Point(); // default constructor is needed when other than copy constructor is defined
|
||||
Point(const Point& t); // copy constructor
|
||||
Point(int xy); // conversion constructor
|
||||
Point(int x, int y); // other constructor
|
||||
~Point(); // destructor
|
||||
// methods
|
||||
int getX();
|
||||
int getY();
|
||||
void print();
|
||||
double distance(Point t);
|
||||
};
|
||||
|
||||
#endif //EXAMPLE02_POINT_H
|
39
semester_2/programiranje_2/primeri/Example02/main.cpp
Normal file
39
semester_2/programiranje_2/primeri/Example02/main.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <iostream>
|
||||
#include "Point.h"
|
||||
|
||||
int main() {
|
||||
Point a;
|
||||
Point b(5);
|
||||
Point c(4,3);
|
||||
Point d(c);
|
||||
a.print();
|
||||
b.print();
|
||||
c.print();
|
||||
d.print();
|
||||
std::cout << a.distance(c) << std::endl;
|
||||
//std::cout << a.distance(5) << std::endl;
|
||||
/*
|
||||
Point arr[3]; // default constructor is called
|
||||
for (int i=0; i < 3; i++)
|
||||
arr[i].print();
|
||||
*/
|
||||
/*
|
||||
std::cout << "-----Dynamic allocation-----" << std::endl;
|
||||
Point* e = new Point();
|
||||
Point* f = new Point(5);
|
||||
Point* g = new Point(2,3);
|
||||
Point* h = new Point(*g);
|
||||
//e.print();
|
||||
//*e.print();
|
||||
//(*e).print();
|
||||
e->print();
|
||||
f->print();
|
||||
g->print();
|
||||
h->print();
|
||||
delete e;
|
||||
delete f;
|
||||
delete g;
|
||||
delete h;
|
||||
*/
|
||||
return 0;
|
||||
}
|
28
semester_2/programiranje_2/primeri/Example03/Complex.cpp
Normal file
28
semester_2/programiranje_2/primeri/Example03/Complex.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
#include "Complex.h"
|
||||
|
||||
Complex::Complex() : real(0), imag(0) {
|
||||
}
|
||||
|
||||
Complex::Complex(double r, double i): real(r), imag(i) {
|
||||
}
|
||||
|
||||
//Complex::Complex(const Complex& c) : real(c.real), imag(c.imag) {
|
||||
//}
|
||||
|
||||
//Complex::~Complex() {
|
||||
//}
|
||||
|
||||
void Complex::print() {
|
||||
std::cout << "(" << real << ", " << imag << "i)" << std::endl;
|
||||
}
|
||||
|
||||
//Complex Complex::plus(Complex c) {
|
||||
Complex Complex::plus(Complex& c) {
|
||||
Complex temp(real+c.real, imag+c.imag);
|
||||
//c.imag=0;
|
||||
return temp;
|
||||
}
|
||||
|
||||
|
||||
|
20
semester_2/programiranje_2/primeri/Example03/Complex.h
Normal file
20
semester_2/programiranje_2/primeri/Example03/Complex.h
Normal file
@@ -0,0 +1,20 @@
|
||||
|
||||
#ifndef EXAMPLE03_COMPLEX_H
|
||||
#define EXAMPLE03_COMPLEX_H
|
||||
|
||||
class Complex {
|
||||
private:
|
||||
double real, imag;
|
||||
public:
|
||||
Complex(); // default constructor
|
||||
// conversion constructor with default arguments
|
||||
Complex(double r, double i = 0);
|
||||
//Complex(const Complex& c); // copy constructor
|
||||
//~Complex(); // destructor
|
||||
|
||||
void print();
|
||||
//Complex plus(Complex c);
|
||||
Complex plus(Complex& c);
|
||||
};
|
||||
|
||||
#endif //EXAMPLE03_COMPLEX_H
|
30
semester_2/programiranje_2/primeri/Example03/main.cpp
Normal file
30
semester_2/programiranje_2/primeri/Example03/main.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#include <iostream>
|
||||
#include "Complex.h"
|
||||
|
||||
int main() {
|
||||
Complex c1(1,1), c2(1), i(0,1);
|
||||
//Complex c3(c1);
|
||||
c1.print();
|
||||
c2.print();
|
||||
i.print();
|
||||
//c3.print();
|
||||
std::cout << "----------" << std::endl;
|
||||
//c1.plus(5).print(); // doesn't work with Complex& as argument
|
||||
c1.plus(i).print();
|
||||
std::cout << "----------" << std::endl;
|
||||
c1.print();
|
||||
i.print();
|
||||
/*
|
||||
std::cout << "Dynamic allocation" << std::endl;
|
||||
Complex* p_c = new Complex(4,2);
|
||||
p_c->print();
|
||||
c1.plus(*p_c).print();
|
||||
Complex c3(c1.plus(*p_c));
|
||||
c3.print();
|
||||
Complex* p_c1 = new Complex(c1.plus(i));
|
||||
p_c1->print();
|
||||
delete p_c;
|
||||
delete p_c1;
|
||||
*/
|
||||
return 0;
|
||||
}
|
24
semester_2/programiranje_2/primeri/Example04/Complex.cpp
Normal file
24
semester_2/programiranje_2/primeri/Example04/Complex.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <iostream>
|
||||
#include "Complex.h"
|
||||
|
||||
Complex::Complex() : real(0), imag(0) {
|
||||
}
|
||||
|
||||
Complex::Complex(double r, double i): real(r), imag(i) {
|
||||
}
|
||||
|
||||
void Complex::print() const {
|
||||
std::cout << "(" << real << ", " << imag << "i)" << std::endl;
|
||||
}
|
||||
|
||||
Complex Complex::plus(Complex& c) const {
|
||||
//Complex Complex::plus(const Complex& c) const {
|
||||
Complex temp(real+c.real, imag+c.imag);
|
||||
//c.real=0;
|
||||
return temp;
|
||||
}
|
||||
|
||||
//void Complex::add(double d) const {
|
||||
void Complex::add(double d) {
|
||||
real+=d;
|
||||
}
|
20
semester_2/programiranje_2/primeri/Example04/Complex.h
Normal file
20
semester_2/programiranje_2/primeri/Example04/Complex.h
Normal file
@@ -0,0 +1,20 @@
|
||||
|
||||
#ifndef EXAMPLE04_COMPLEX_H
|
||||
#define EXAMPLE04_COMPLEX_H
|
||||
|
||||
class Complex {
|
||||
private:
|
||||
double real, imag;
|
||||
public:
|
||||
Complex();
|
||||
Complex(double r, double i = 0);
|
||||
|
||||
void print() const; // constant method
|
||||
// argument is a constant object and method is constant
|
||||
//Complex plus(const Complex& c) const;
|
||||
Complex plus(Complex& c) const;
|
||||
void add(double d); // non-constant method
|
||||
//void add(double d) const;
|
||||
};
|
||||
|
||||
#endif //EXAMPLE04_COMPLEX_H
|
18
semester_2/programiranje_2/primeri/Example04/main.cpp
Normal file
18
semester_2/programiranje_2/primeri/Example04/main.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include <iostream>
|
||||
#include "Complex.h"
|
||||
|
||||
int main() {
|
||||
Complex c1(1,1);
|
||||
const Complex i(0,1);
|
||||
c1.add(10);
|
||||
//i.add(1);
|
||||
i.print();
|
||||
c1.print();
|
||||
/*
|
||||
std::cout << "---------------" << std::endl;
|
||||
i.plus(c1).print();
|
||||
//c1.plus(i).print();
|
||||
c1.print();
|
||||
*/
|
||||
return 0;
|
||||
}
|
40
semester_2/programiranje_2/primeri/Example05/Complex.cpp
Normal file
40
semester_2/programiranje_2/primeri/Example05/Complex.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <iostream>
|
||||
#include "Complex.h"
|
||||
|
||||
int Complex::counter=0; // class variables can't be initialized in class definition
|
||||
//const int Complex::counter1=0; // constant class variable
|
||||
|
||||
/*
|
||||
int Complex::getCounter() { // class method (modifier static must be omitted)
|
||||
return counter;
|
||||
}
|
||||
*/
|
||||
|
||||
Complex::Complex(): real(0), imag(0) {
|
||||
counter++;
|
||||
}
|
||||
|
||||
Complex::Complex(double r, double i): real(r), imag(i) {
|
||||
counter++;
|
||||
}
|
||||
|
||||
Complex::Complex(const Complex& c): real(c.real), imag(c.imag) {
|
||||
counter++;
|
||||
}
|
||||
|
||||
Complex::~Complex() {
|
||||
counter--;
|
||||
//std::cout << "Destructor" << std::endl;
|
||||
}
|
||||
|
||||
void Complex::print() const {
|
||||
std::cout << "(" << real << ", " << imag << "i)" << std::endl;
|
||||
//std::cout << "(" << real << ", " << imag << "i)" << counter << std::endl; // methods can access class variables
|
||||
//std::cout << "(" << real << ", " << imag << "i)" << counter++ << std::endl; // class variables can be updated
|
||||
//std::cout << "Object address: " << this << "=(" << this->real << ", " << this->imag << "i)" << this->counter << std::endl;
|
||||
}
|
||||
|
||||
Complex Complex::plus(const Complex& c) const {
|
||||
Complex temp(real+c.real, imag+c.imag);
|
||||
return temp;
|
||||
}
|
35
semester_2/programiranje_2/primeri/Example05/Complex.h
Normal file
35
semester_2/programiranje_2/primeri/Example05/Complex.h
Normal file
@@ -0,0 +1,35 @@
|
||||
|
||||
#ifndef EXAMPLE05_COMPLEX_H
|
||||
#define EXAMPLE05_COMPLEX_H
|
||||
|
||||
class Complex {
|
||||
private:
|
||||
double real, imag;
|
||||
static int counter; // class variable
|
||||
//static const int counter1; // constant class variable
|
||||
public:
|
||||
Complex(); // default constructor
|
||||
Complex(double r, double i = 0); // conversion constructor
|
||||
Complex(const Complex& c); // copy constructor
|
||||
~Complex(); // destructor
|
||||
void print() const;
|
||||
Complex plus(const Complex& c) const;
|
||||
|
||||
//static int getCounter();
|
||||
|
||||
static int getCounter() { // class method
|
||||
return counter;
|
||||
//return real; // ERROR
|
||||
//return counter++; // OK
|
||||
//return this->counter; // ERROR
|
||||
}
|
||||
/*
|
||||
//static int getCounter1() const { // const is not allowed for class methods
|
||||
static int getCounter1() { // class method
|
||||
return counter1;
|
||||
//return counter1++;
|
||||
}
|
||||
*/
|
||||
};
|
||||
|
||||
#endif //EXAMPLE05_COMPLEX_H
|
31
semester_2/programiranje_2/primeri/Example05/main.cpp
Normal file
31
semester_2/programiranje_2/primeri/Example05/main.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <iostream>
|
||||
#include "Complex.h"
|
||||
|
||||
int main() {
|
||||
std::cout << "Current number of objects: " << Complex::getCounter() << std::endl;
|
||||
//std::cout << "Counter1: " << Complex::getCounter1() << std::endl;
|
||||
Complex c1(1,1);
|
||||
const Complex i(0,1); // constant object
|
||||
/*
|
||||
{
|
||||
Complex c2;
|
||||
std::cout << "Current number of objects: " << Complex::getCounter() << std::endl;
|
||||
}
|
||||
std::cout << "Current number of objects: " << Complex::getCounter() << std::endl;
|
||||
*/
|
||||
c1.print();
|
||||
i.print();
|
||||
std::cout << "Current number of objects: " << c1.getCounter() << std::endl;
|
||||
std::cout << "Current number of objects: " << i.getCounter() << std::endl;
|
||||
/*
|
||||
Complex* p_c1 = new Complex(c1.plus(i));
|
||||
Complex* p_c2 = new Complex();
|
||||
p_c1->print();
|
||||
p_c2->print();
|
||||
std::cout << "Current number of objects: " << p_c1->getCounter() << std::endl;
|
||||
delete p_c1;
|
||||
delete p_c2;
|
||||
std::cout << "Current number of objects: " << c1.getCounter() << std::endl;
|
||||
*/
|
||||
return 0;
|
||||
}
|
15
semester_2/programiranje_2/primeri/Example06/CPoint.cpp
Normal file
15
semester_2/programiranje_2/primeri/Example06/CPoint.cpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#include <iostream>
|
||||
#include "CPoint.h"
|
||||
|
||||
CPoint::CPoint() : p(), color(0) {
|
||||
}
|
||||
|
||||
CPoint::CPoint(int x, int y, int c) : p(x, y), color(c) {
|
||||
}
|
||||
void CPoint::print() const {
|
||||
p.print();
|
||||
std::cout << "color=" << color << " " << std::endl;
|
||||
//std::cout << "(" << p.x << ", " << p.y << ", color= " << color << ")" << std::endl;
|
||||
//std::cout << "(" << p.getX() << ", " << p.getY() << ", color= " << color << ")" << std::endl;
|
||||
}
|
||||
|
17
semester_2/programiranje_2/primeri/Example06/CPoint.h
Normal file
17
semester_2/programiranje_2/primeri/Example06/CPoint.h
Normal file
@@ -0,0 +1,17 @@
|
||||
|
||||
#ifndef EXAMPLE06_CPOINT_H
|
||||
#define EXAMPLE06_CPOINT_H
|
||||
|
||||
#include "Point.h"
|
||||
|
||||
class CPoint {
|
||||
private:
|
||||
Point p; // composition
|
||||
int color;
|
||||
public:
|
||||
CPoint();
|
||||
CPoint(int x, int y, int c);
|
||||
void print() const;
|
||||
};
|
||||
|
||||
#endif //EXAMPLE06_CPOINT_H
|
28
semester_2/programiranje_2/primeri/Example06/Point.cpp
Normal file
28
semester_2/programiranje_2/primeri/Example06/Point.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include "Point.h"
|
||||
|
||||
Point::Point() : x(0), y(0) {
|
||||
}
|
||||
|
||||
Point::Point (int x, int y) : x(x), y(y) {
|
||||
}
|
||||
|
||||
int Point::getX() const {
|
||||
return x;
|
||||
}
|
||||
|
||||
int Point::getY() const {
|
||||
return y;
|
||||
}
|
||||
|
||||
void Point::print() const {
|
||||
std::cout << "(" << x << ", " << y << ") " << std::endl;
|
||||
}
|
||||
|
||||
double Point::distance(const Point& p) const {
|
||||
return std::sqrt((double)(x - p.x)*(x - p.x)+(y - p.y)*(y - p.y));
|
||||
}
|
||||
|
||||
|
18
semester_2/programiranje_2/primeri/Example06/Point.h
Normal file
18
semester_2/programiranje_2/primeri/Example06/Point.h
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
#ifndef EXAMPLE06_POINT_H
|
||||
#define EXAMPLE06_POINT_H
|
||||
|
||||
class Point {
|
||||
private:
|
||||
int x, y;
|
||||
public:
|
||||
Point(); // default constructor
|
||||
Point (int x, int y); // constructor
|
||||
|
||||
int getX() const;
|
||||
int getY() const;
|
||||
void print() const;
|
||||
double distance(const Point& p) const;
|
||||
};
|
||||
|
||||
#endif //EXAMPLE06_POINT_H
|
19
semester_2/programiranje_2/primeri/Example06/main.cpp
Normal file
19
semester_2/programiranje_2/primeri/Example06/main.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <iostream>
|
||||
#include "CPoint.h"
|
||||
|
||||
int main() {
|
||||
Point p1(3,4);
|
||||
Point p2;
|
||||
CPoint c1;
|
||||
CPoint c2(1,2,3);
|
||||
CPoint c3(c2);
|
||||
|
||||
p1.print();
|
||||
c1.print();
|
||||
c2.print();
|
||||
c3.print();
|
||||
std::cout << p1.distance(p2) << std::endl;
|
||||
//std::cout << p1.distance(c1) << std::endl;
|
||||
//std::cout << c1.distance(p1) << std::endl;
|
||||
return 0;
|
||||
}
|
19
semester_2/programiranje_2/primeri/Example07/CPoint.cpp
Normal file
19
semester_2/programiranje_2/primeri/Example07/CPoint.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <iostream>
|
||||
#include "CPoint.h"
|
||||
|
||||
CPoint::CPoint() : Point(), color(0) {
|
||||
}
|
||||
|
||||
CPoint::CPoint(int x, int y, int c) : Point(x, y), color(c) {
|
||||
}
|
||||
|
||||
void CPoint::print() const {
|
||||
//print(); // recursive call
|
||||
//this->print(); // recursive call
|
||||
Point::print(); // method call from the superclass Point
|
||||
std::cout << "color=" << color << " " << std::endl;
|
||||
//std::cout << "(" << x << " " << y << ") color=" << color << std::endl; // we can't acceess to private members of the superclass
|
||||
//std::cout << "(" << getX() << ", " << getY() << ") color=" << color << std::endl;
|
||||
}
|
||||
|
||||
|
16
semester_2/programiranje_2/primeri/Example07/CPoint.h
Normal file
16
semester_2/programiranje_2/primeri/Example07/CPoint.h
Normal file
@@ -0,0 +1,16 @@
|
||||
|
||||
#ifndef EXAMPLE07_CPOINT_H
|
||||
#define EXAMPLE07_CPOINT_H
|
||||
#include "Point.h"
|
||||
|
||||
class CPoint : public Point {
|
||||
private:
|
||||
int color;
|
||||
public:
|
||||
CPoint();
|
||||
CPoint(int x, int y, int c);
|
||||
|
||||
void print() const;
|
||||
};
|
||||
|
||||
#endif //EXAMPLE07_CPOINT_H
|
26
semester_2/programiranje_2/primeri/Example07/Point.cpp
Normal file
26
semester_2/programiranje_2/primeri/Example07/Point.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include "Point.h"
|
||||
|
||||
Point::Point() : x(0), y(0) {
|
||||
}
|
||||
|
||||
Point::Point (int x, int y) : x(x), y(y) {
|
||||
}
|
||||
|
||||
int Point::getX() const {
|
||||
return x;
|
||||
}
|
||||
|
||||
int Point::getY() const {
|
||||
return y;
|
||||
}
|
||||
|
||||
void Point::print() const {
|
||||
std::cout << "(" << x << ", " << y << ") " << std::endl;
|
||||
}
|
||||
|
||||
double Point::distance(const Point& p) const {
|
||||
return std::sqrt((double)(x - p.x)*(x - p.x)+(y - p.y)*(y - p.y));
|
||||
}
|
18
semester_2/programiranje_2/primeri/Example07/Point.h
Normal file
18
semester_2/programiranje_2/primeri/Example07/Point.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef EXAMPLE07_POINT_H
|
||||
#define EXAMPLE07_POINT_H
|
||||
|
||||
class Point {
|
||||
private:
|
||||
//protected:
|
||||
int x, y;
|
||||
public:
|
||||
Point();
|
||||
Point (int x1, int y1);
|
||||
|
||||
int getX() const;
|
||||
int getY() const;
|
||||
void print() const;
|
||||
double distance(const Point& p) const;
|
||||
};
|
||||
|
||||
#endif //EXAMPLE07_POINT_H
|
23
semester_2/programiranje_2/primeri/Example07/main.cpp
Normal file
23
semester_2/programiranje_2/primeri/Example07/main.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <iostream>
|
||||
#include "CPoint.h"
|
||||
|
||||
int main() {
|
||||
Point p1(3,4);
|
||||
Point p2;
|
||||
CPoint c1;
|
||||
CPoint c2(1,2,3);
|
||||
CPoint c3(c2);
|
||||
|
||||
p1.print();
|
||||
c1.print();
|
||||
c2.print();
|
||||
c3.print();
|
||||
/*
|
||||
std::cout << "-------------" << std::endl;
|
||||
std::cout << p1.distance(p2) << std::endl;
|
||||
std::cout << p1.distance(c2) << std::endl;
|
||||
//std::cout << c1.distance(p1) << std::endl;
|
||||
//std::cout << c2.distance(c1) << std::endl;
|
||||
*/
|
||||
return 0;
|
||||
}
|
19
semester_2/programiranje_2/primeri/Example08/CPoint.cpp
Normal file
19
semester_2/programiranje_2/primeri/Example08/CPoint.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <iostream>
|
||||
#include "CPoint.h"
|
||||
|
||||
CPoint::CPoint() : Point(), color(0) {
|
||||
}
|
||||
|
||||
CPoint::CPoint(int x, int y, int c) : Point(x, y), color(c) {
|
||||
}
|
||||
|
||||
CPoint::~CPoint() {
|
||||
//std::cout << "Destructor CPoint" << std::endl;
|
||||
}
|
||||
|
||||
void CPoint::print() const {
|
||||
std::cout << "(" << x << ", " << y << ") color= " << color << std::endl;
|
||||
|
||||
}
|
||||
|
||||
|
16
semester_2/programiranje_2/primeri/Example08/CPoint.h
Normal file
16
semester_2/programiranje_2/primeri/Example08/CPoint.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#ifndef EXAMPLE08_CPOINT_H
|
||||
#define EXAMPLE08_CPOINT_H
|
||||
|
||||
#include "Point.h"
|
||||
|
||||
class CPoint : public Point {
|
||||
protected:
|
||||
int color;
|
||||
public:
|
||||
CPoint();
|
||||
CPoint(int x, int y, int c);
|
||||
~CPoint();
|
||||
void print() const;
|
||||
};
|
||||
|
||||
#endif //EXAMPLE08_CPOINT_H
|
30
semester_2/programiranje_2/primeri/Example08/Point.cpp
Normal file
30
semester_2/programiranje_2/primeri/Example08/Point.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include "Point.h"
|
||||
|
||||
Point::Point() : x(0), y(0) {
|
||||
}
|
||||
|
||||
Point::Point (int x, int y) : x(x), y(y) {
|
||||
}
|
||||
|
||||
Point::~Point() {
|
||||
//std::cout << "destructor Point" << std::endl;
|
||||
}
|
||||
|
||||
int Point::getX() const {
|
||||
return x;
|
||||
}
|
||||
|
||||
int Point::getY() const {
|
||||
return y;
|
||||
}
|
||||
|
||||
void Point::print() const {
|
||||
std::cout << "(" << x << ", " << y << ") " << std::endl;
|
||||
}
|
||||
|
||||
double Point::distance(const Point& p) const {
|
||||
return std::sqrt((double)(x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
|
||||
}
|
||||
|
21
semester_2/programiranje_2/primeri/Example08/Point.h
Normal file
21
semester_2/programiranje_2/primeri/Example08/Point.h
Normal file
@@ -0,0 +1,21 @@
|
||||
|
||||
#ifndef EXAMPLE08_POINT_H
|
||||
#define EXAMPLE08_POINT_H
|
||||
|
||||
class Point {
|
||||
protected:
|
||||
int x, y;
|
||||
public:
|
||||
Point();
|
||||
Point(int x, int y);
|
||||
//virtual ~Point(); // virtual destructor
|
||||
~Point(); // destructor
|
||||
|
||||
int getX() const;
|
||||
int getY() const;
|
||||
//virtual void print() const;
|
||||
void print() const;
|
||||
double distance(const Point& p) const;
|
||||
};
|
||||
|
||||
#endif //EXAMPLE08_POINT_H
|
30
semester_2/programiranje_2/primeri/Example08/main.cpp
Normal file
30
semester_2/programiranje_2/primeri/Example08/main.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#include <iostream>
|
||||
#include "CPoint.h"
|
||||
|
||||
int main() {
|
||||
Point p1(3,4);
|
||||
CPoint c1;
|
||||
|
||||
//p1=c1; // OK
|
||||
//c1=p1; // ERROR
|
||||
p1.print();
|
||||
c1.print();
|
||||
|
||||
Point* p_p1 = &p1;
|
||||
Point* p_c1 = &c1;
|
||||
//p_p1->print();
|
||||
//p_c1->print();
|
||||
/*
|
||||
Point* arrPoints[2];
|
||||
arrPoints[0]=&p1;
|
||||
arrPoints[1]=&c1;
|
||||
std::cout << "---------------" << std::endl;
|
||||
for (int i=0; i < 2; i++) {
|
||||
arrPoints[i]->print();
|
||||
}
|
||||
*/
|
||||
delete p_p1;
|
||||
delete p_c1;
|
||||
|
||||
return 0;
|
||||
}
|
11
semester_2/programiranje_2/primeri/Example09/Animal.h
Normal file
11
semester_2/programiranje_2/primeri/Example09/Animal.h
Normal file
@@ -0,0 +1,11 @@
|
||||
|
||||
#ifndef EXAMPLE09_ANIMAL_H
|
||||
#define EXAMPLE09_ANIMAL_H
|
||||
|
||||
class Animal { // abstract class
|
||||
public:
|
||||
virtual ~Animal() {}
|
||||
virtual void voice() const = 0; // abstract method
|
||||
};
|
||||
|
||||
#endif //EXAMPLE09_ANIMAL_H
|
13
semester_2/programiranje_2/primeri/Example09/Cat.h
Normal file
13
semester_2/programiranje_2/primeri/Example09/Cat.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef EXAMPLE09_CAT_H
|
||||
#define EXAMPLE09_CAT_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class Cat : public Animal {
|
||||
public:
|
||||
void voice() const {
|
||||
std::cout << "meow" << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //EXAMPLE09_CAT_H
|
14
semester_2/programiranje_2/primeri/Example09/Cow.h
Normal file
14
semester_2/programiranje_2/primeri/Example09/Cow.h
Normal file
@@ -0,0 +1,14 @@
|
||||
|
||||
#ifndef EXAMPLE09_COW_H
|
||||
#define EXAMPLE09_COW_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class Cow : public Animal {
|
||||
public:
|
||||
void voice() const {
|
||||
std::cout << "moo" << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //EXAMPLE09_COW_H
|
14
semester_2/programiranje_2/primeri/Example09/Dog.h
Normal file
14
semester_2/programiranje_2/primeri/Example09/Dog.h
Normal file
@@ -0,0 +1,14 @@
|
||||
|
||||
#ifndef EXAMPLE09_DOG_H
|
||||
#define EXAMPLE09_DOG_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class Dog : public Animal {
|
||||
public:
|
||||
void voice() const {
|
||||
std::cout << "bark" << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //EXAMPLE09_DOG_H
|
22
semester_2/programiranje_2/primeri/Example09/main.cpp
Normal file
22
semester_2/programiranje_2/primeri/Example09/main.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <iostream>
|
||||
#include "Animal.h"
|
||||
#include "Dog.h"
|
||||
#include "Cat.h"
|
||||
#include "Cow.h"
|
||||
|
||||
int main() {
|
||||
Animal* zoo[4];
|
||||
|
||||
//zoo[0] = new Animal;
|
||||
zoo[0] = new Dog;
|
||||
zoo[1] = new Cat;
|
||||
zoo[2] = new Dog;
|
||||
zoo[3] = new Cow;
|
||||
for (int i=0; i < 4; i++)
|
||||
zoo[i]->voice();
|
||||
|
||||
for (int i=0; i<4; i++)
|
||||
delete zoo[i];
|
||||
return 0;
|
||||
}
|
||||
|
27
semester_2/programiranje_2/primeri/Example10/Company.h
Normal file
27
semester_2/programiranje_2/primeri/Example10/Company.h
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
#ifndef EXAMPLE10_COMPANY_H
|
||||
#define EXAMPLE10_COMPANY_H
|
||||
|
||||
#include <iostream>
|
||||
#include "Employee.h"
|
||||
|
||||
class Company {
|
||||
private:
|
||||
std::string name;
|
||||
Employee* ptrEmployee; // aggregation
|
||||
public:
|
||||
Company(std::string cname, Employee* ename) : name(cname), ptrEmployee(ename) {
|
||||
std::cout << "Company::constructor" << std::endl;
|
||||
}
|
||||
virtual ~Company() {
|
||||
std::cout << "Company::destructor" << std::endl;
|
||||
}
|
||||
virtual const std::string& toString() const {
|
||||
return name;
|
||||
};
|
||||
virtual void employed() const {
|
||||
std::cout << ptrEmployee->toString();
|
||||
}
|
||||
};
|
||||
|
||||
#endif //EXAMPLE10_COMPANY_H
|
22
semester_2/programiranje_2/primeri/Example10/Employee.h
Normal file
22
semester_2/programiranje_2/primeri/Example10/Employee.h
Normal file
@@ -0,0 +1,22 @@
|
||||
|
||||
#ifndef EXAMPLE10_EMPLOYEE_H
|
||||
#define EXAMPLE10_EMPLOYEE_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class Employee {
|
||||
private:
|
||||
std::string name;
|
||||
public:
|
||||
Employee(std::string name) : name(name) {
|
||||
std::cout << "Employee::constructor" << std::endl;
|
||||
}
|
||||
virtual ~Employee() {
|
||||
std::cout << "Employee::destructor" << std::endl;
|
||||
}
|
||||
virtual const std::string& toString() const {
|
||||
return name;
|
||||
};
|
||||
};
|
||||
|
||||
#endif //EXAMPLE10_EMPLOYEE_H
|
25
semester_2/programiranje_2/primeri/Example10/main.cpp
Normal file
25
semester_2/programiranje_2/primeri/Example10/main.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <iostream>
|
||||
#include "Employee.h"
|
||||
#include "Company.h"
|
||||
|
||||
int main() {
|
||||
std::cout << "Aggregation example" << std::endl;
|
||||
{
|
||||
std::cout << "----------- nested block 1 begin -----------" << std::endl;
|
||||
Employee* ptrEmp = new Employee("John");
|
||||
{
|
||||
std::cout << "----------- nested block 2 begin -----------" << std::endl;
|
||||
Company c("SmartCo", ptrEmp);
|
||||
std::cout << "Employee ";
|
||||
c.employed();
|
||||
std::cout << " works for company " << c.toString() << std::endl;
|
||||
std::cout << "----------- nested block 2 end -----------" << std::endl;
|
||||
}
|
||||
std::cout << "Company doesn't exist anymore" << std::endl;
|
||||
std::cout << "But, employee " << ptrEmp->toString();
|
||||
std::cout << " still exists!" << std::endl;
|
||||
std::cout << "----------- nested block 1 end -----------" << std::endl;
|
||||
delete ptrEmp;
|
||||
}
|
||||
return 0;
|
||||
}
|
27
semester_2/programiranje_2/primeri/Example11/House.h
Normal file
27
semester_2/programiranje_2/primeri/Example11/House.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef EXAMPLE11_HOUSE_H
|
||||
#define EXAMPLE11_HOUSE_H
|
||||
|
||||
#include <iostream>
|
||||
#include "Room.h"
|
||||
|
||||
class House {
|
||||
private:
|
||||
std::string name;
|
||||
Room kitchen, livingRoom, bedroom; //composition
|
||||
public:
|
||||
House(std::string name) : name(name), kitchen("Kitchen1"), livingRoom("living room 1"),
|
||||
bedroom("TV bedroom") {
|
||||
std::cout << "House::constructor" << std::endl;
|
||||
}
|
||||
virtual ~House() {
|
||||
std::cout << "House::destructor" << std::endl;
|
||||
}
|
||||
virtual void print() const {
|
||||
std::cout << "House name: " << name << std::endl;
|
||||
kitchen.print();
|
||||
livingRoom.print();
|
||||
bedroom.print();
|
||||
}
|
||||
};
|
||||
|
||||
#endif //EXAMPLE11_HOUSE_H
|
21
semester_2/programiranje_2/primeri/Example11/Room.h
Normal file
21
semester_2/programiranje_2/primeri/Example11/Room.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#ifndef EXAMPLE11_ROOM_H
|
||||
#define EXAMPLE11_ROOM_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class Room {
|
||||
private:
|
||||
std::string name;
|
||||
public:
|
||||
Room(std::string name) : name(name) {
|
||||
std::cout << "Room::constructor" << std::endl;
|
||||
}
|
||||
virtual ~Room() {
|
||||
std::cout << "Room::destructor" << std::endl;
|
||||
}
|
||||
virtual void print() const {
|
||||
std::cout << "Room " << name << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //EXAMPLE11_ROOM_H
|
15
semester_2/programiranje_2/primeri/Example11/main.cpp
Normal file
15
semester_2/programiranje_2/primeri/Example11/main.cpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#include <iostream>
|
||||
#include "House.h"
|
||||
|
||||
int main() {
|
||||
std::cout << "Composition example" << std::endl;
|
||||
{
|
||||
std::cout << "----------- nested block begin -----------" << std::endl;
|
||||
House h1("My home");
|
||||
h1.print();
|
||||
std::cout << "----------- nested block end -----------" << std::endl;
|
||||
}
|
||||
std::cout<<"House and rooms don't exist anymore!" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
60
semester_2/programiranje_2/primeri/Example12/Date.cpp
Normal file
60
semester_2/programiranje_2/primeri/Example12/Date.cpp
Normal file
@@ -0,0 +1,60 @@
|
||||
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
#include "Date.h"
|
||||
|
||||
Date::Date() : day(1), month(1), year(1971) {}
|
||||
|
||||
//Date::Date(const Date& dat) : day(dat.day), month(dat.month), year(dat.year) {}
|
||||
|
||||
Date::Date(int d, int m, int y) : day(d), month(m), year(y) {
|
||||
}
|
||||
|
||||
Date::~Date() {}
|
||||
|
||||
int Date::getDay() const {
|
||||
return day;
|
||||
}
|
||||
|
||||
void Date::setDay(int day) {
|
||||
if (day < 1 || day > 31) {
|
||||
std::cout << "Wrong day: " << day << std::endl;
|
||||
this->day = 1;
|
||||
}
|
||||
this->day = day;
|
||||
}
|
||||
|
||||
int Date::getMonth() const {
|
||||
return month;
|
||||
}
|
||||
|
||||
void Date::setMonth(int month) {
|
||||
if (month < 1 || month > 12) {
|
||||
std::cout << "Wrong month: " << month << std::endl;
|
||||
this->month = 1;
|
||||
}
|
||||
this->month = month;
|
||||
}
|
||||
|
||||
int Date::getYear() const {
|
||||
return year;
|
||||
}
|
||||
|
||||
void Date::setYear(int year) {
|
||||
this->year = year;
|
||||
}
|
||||
|
||||
const std::string Date::toString() const {
|
||||
// A stringstream associates a string object with a stream allowing you to read
|
||||
// from the string as if it were a stream (like cin).
|
||||
std::stringstream ss;
|
||||
ss << day << "." << month << "." << year;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
bool Date::isEqual(const Date& second) const {
|
||||
if (day==second.day && month == second.month && year == second.year)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
32
semester_2/programiranje_2/primeri/Example12/Date.h
Normal file
32
semester_2/programiranje_2/primeri/Example12/Date.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef EXAMPLE12_DATE_H
|
||||
#define EXAMPLE12_DATE_H
|
||||
|
||||
#include <string>
|
||||
|
||||
class Date {
|
||||
private:
|
||||
int day;
|
||||
int month;
|
||||
int year;
|
||||
public:
|
||||
Date();
|
||||
// Date(const Date& date);
|
||||
Date(int d, int m, int l);
|
||||
virtual ~Date();
|
||||
|
||||
int getDay() const;
|
||||
void setDay(int day);
|
||||
int getMonth() const;
|
||||
void setMonth(int month);
|
||||
int getYear() const;
|
||||
void setYear(int year);
|
||||
|
||||
// A std::string is a class which defines objects that be represented as stream of characters.
|
||||
// Size of the character array has to allocated statically, more memory cannot be allocated
|
||||
// at run time if required. Unused allocated memory is wasted in case of character array.
|
||||
// In case of strings, memory is allocated dynamically. More memory can be allocated at run time
|
||||
// on demand. As no memory is preallocated, no memory is wasted.
|
||||
const std::string toString() const;
|
||||
bool isEqual(const Date& date) const;
|
||||
};
|
||||
#endif //EXAMPLE12_DATE_H
|
138
semester_2/programiranje_2/primeri/Example12/FlightTicket.cpp
Normal file
138
semester_2/programiranje_2/primeri/Example12/FlightTicket.cpp
Normal file
@@ -0,0 +1,138 @@
|
||||
#include <sstream>
|
||||
#include "FlightTicket.h"
|
||||
|
||||
int FlightTicket::numberOfFirstClass = 0;
|
||||
|
||||
FlightTicket::FlightTicket() :
|
||||
price(0.0), flightFrom("From?"), flightTo("To?"), addBaggage(false),
|
||||
ticketType(FlightTicketType::Economy) {
|
||||
ptrFlightDate = new Date(1, 1, 1971);
|
||||
}
|
||||
|
||||
FlightTicket::FlightTicket(const FlightTicket& t) :
|
||||
price(t.price), flightFrom(t.flightFrom), flightTo(t.flightTo),
|
||||
addBaggage(t.addBaggage), ticketType(t.ticketType), ptrFlightDate(t.ptrFlightDate) {
|
||||
if (ticketType == FlightTicketType::FirstClass)
|
||||
numberOfFirstClass++;
|
||||
}
|
||||
|
||||
FlightTicket::FlightTicket(double price, std::string ff, std::string ft,
|
||||
bool bag, FlightTicketType type, Date* date1) :
|
||||
price(price), flightFrom(ff), flightTo(ft), addBaggage(bag),
|
||||
ticketType(type), ptrFlightDate(date1) {
|
||||
if (ticketType == FlightTicketType::FirstClass)
|
||||
numberOfFirstClass++;
|
||||
}
|
||||
|
||||
FlightTicket::~FlightTicket() {
|
||||
}
|
||||
|
||||
double FlightTicket::getTotalPrice() const {
|
||||
double sum = price;
|
||||
|
||||
if (FlightTicketType::Business == ticketType)
|
||||
sum *= 1.20;
|
||||
else if (FlightTicketType::FirstClass == ticketType)
|
||||
sum *= 1.50;
|
||||
if (addBaggage == true)
|
||||
sum += 50;
|
||||
return sum;
|
||||
}
|
||||
|
||||
std::string FlightTicket::toString() const {
|
||||
std::stringstream ss;
|
||||
|
||||
ss << "Flight ticket details: " << "\n";
|
||||
ss << "\tPrice: " << price << "\n";
|
||||
ss << "\tAdditional baggage: " << (addBaggage ? "Yes" : "No") << "\n";
|
||||
ss << "\tFrom: " << flightFrom << "\n";
|
||||
ss << "\tTo: " << flightTo << "\n";
|
||||
ss << "\tTicket type: " << getTicketTypeString()
|
||||
<< "\n";
|
||||
ss << "\tDeparture: " << this->ptrFlightDate->toString() << "\n";
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
double FlightTicket::getPrice() const {
|
||||
return price;
|
||||
}
|
||||
|
||||
void FlightTicket::setPrice(double price) {
|
||||
this->price = price;
|
||||
}
|
||||
bool FlightTicket::isAddBaggage() const {
|
||||
return addBaggage;
|
||||
}
|
||||
|
||||
void FlightTicket::setAddBaggage(bool addBaggage) {
|
||||
this->addBaggage = addBaggage;
|
||||
}
|
||||
|
||||
Date* FlightTicket::getFlightDate() const {
|
||||
return ptrFlightDate;
|
||||
}
|
||||
|
||||
void FlightTicket::setFlightDate(Date* flightDate) {
|
||||
this->ptrFlightDate = flightDate;
|
||||
}
|
||||
|
||||
const std::string& FlightTicket::getFlightFrom() const {
|
||||
return flightFrom;
|
||||
}
|
||||
|
||||
void FlightTicket::setFlightFrom(const std::string& flightFrom) {
|
||||
this->flightFrom = flightFrom;
|
||||
}
|
||||
|
||||
const std::string& FlightTicket::getFlightTo() const {
|
||||
return flightTo;
|
||||
}
|
||||
|
||||
void FlightTicket::setFlightTo(const std::string& flightTo) {
|
||||
this->flightTo = flightTo;
|
||||
}
|
||||
|
||||
FlightTicketType FlightTicket::getTicketType() const {
|
||||
return ticketType;
|
||||
}
|
||||
|
||||
void FlightTicket::setTicketType(FlightTicketType ticketType) {
|
||||
if (ticketType < FlightTicketType::Economy
|
||||
|| ticketType > FlightTicketType::FirstClass) {
|
||||
std::cout << "Wrong flight ticket type ";
|
||||
this->ticketType = FlightTicketType::Economy;
|
||||
} else
|
||||
this->ticketType = ticketType;
|
||||
if (ticketType == FlightTicketType::FirstClass)
|
||||
numberOfFirstClass++;
|
||||
}
|
||||
|
||||
std::string FlightTicket::getTicketTypeString() const {
|
||||
switch (ticketType) {
|
||||
case FlightTicketType::Economy:
|
||||
return "Economy";
|
||||
break;
|
||||
case FlightTicketType::Business:
|
||||
return "Business";
|
||||
break;
|
||||
case FlightTicketType::FirstClass:
|
||||
return "First Class";
|
||||
break;
|
||||
default:
|
||||
return "?";
|
||||
}
|
||||
}
|
||||
|
||||
int FlightTicket::GetNumberOfFirstClass() {
|
||||
return numberOfFirstClass;
|
||||
}
|
||||
|
||||
bool FlightTicket::isEqual(const FlightTicket& drugi) const {
|
||||
if (this->price == drugi.price && this->addBaggage == drugi.addBaggage
|
||||
&& this->getTicketType() == drugi.getTicketType()
|
||||
&& this->flightFrom == drugi.flightFrom
|
||||
&& this->flightTo == drugi.flightTo
|
||||
&& this->ptrFlightDate->isEqual(*drugi.ptrFlightDate))
|
||||
return true;
|
||||
return false;
|
||||
}
|
58
semester_2/programiranje_2/primeri/Example12/FlightTicket.h
Normal file
58
semester_2/programiranje_2/primeri/Example12/FlightTicket.h
Normal file
@@ -0,0 +1,58 @@
|
||||
|
||||
#ifndef EXAMPLE12_FLIGHTTICKET_H
|
||||
#define EXAMPLE12_FLIGHTTICKET_H
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include "Date.h"
|
||||
|
||||
//C++11 has introduced enum classes (also called scoped enumerations),
|
||||
//that makes enumerations both strongly typed and strongly scoped.
|
||||
//Class enum doesn't allow implicit conversion to int, and also doesn't compare enumerators
|
||||
//from different enumerations.
|
||||
enum class FlightTicketType {
|
||||
Economy = 0,
|
||||
Business = 1,
|
||||
FirstClass = 2
|
||||
};
|
||||
|
||||
class FlightTicket {
|
||||
protected:
|
||||
double price;
|
||||
std::string flightFrom;
|
||||
std::string flightTo;
|
||||
bool addBaggage;
|
||||
FlightTicketType ticketType;
|
||||
Date* ptrFlightDate;
|
||||
static int numberOfFirstClass;
|
||||
public:
|
||||
FlightTicket();
|
||||
FlightTicket(const FlightTicket& t);
|
||||
FlightTicket(double price, std::string ff,std::string ft, bool bag, FlightTicketType type, Date* p_date);
|
||||
virtual ~FlightTicket();
|
||||
|
||||
//get/set methods
|
||||
double getPrice() const;
|
||||
void setPrice(double price);
|
||||
const std::string& getFlightFrom() const;
|
||||
void setFlightFrom(const std::string& flightFrom);
|
||||
const std::string& getFlightTo() const;
|
||||
void setFlightTo(const std::string& flightTo);
|
||||
bool isAddBaggage() const;
|
||||
void setAddBaggage(bool addBaggage);
|
||||
FlightTicketType getTicketType() const;
|
||||
void setTicketType(FlightTicketType ticketType);
|
||||
std::string getTicketTypeString() const;
|
||||
Date* getFlightDate() const;
|
||||
void setFlightDate(Date* flightDate);
|
||||
|
||||
//methods
|
||||
virtual double getTotalPrice() const;
|
||||
virtual std::string toString() const;
|
||||
virtual bool isEqual(const FlightTicket& drugi) const;
|
||||
|
||||
//static method
|
||||
static int GetNumberOfFirstClass();
|
||||
};
|
||||
|
||||
#endif //EXAMPLE12_FLIGHTTICKET_H
|
@@ -0,0 +1,41 @@
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include "ReturnFlightTicket.h"
|
||||
|
||||
ReturnFlightTicket::ReturnFlightTicket() : FlightTicket() {
|
||||
this->ptrReturnDate = new Date(1, 1, 1971);
|
||||
}
|
||||
|
||||
ReturnFlightTicket::ReturnFlightTicket(const ReturnFlightTicket& rft) :
|
||||
FlightTicket(rft), ptrReturnDate(rft.ptrReturnDate) {
|
||||
}
|
||||
|
||||
ReturnFlightTicket::ReturnFlightTicket(double price, std::string ff,
|
||||
std::string ft, bool bag, FlightTicketType type, Date* p_d1,
|
||||
Date* p_d2) :
|
||||
FlightTicket(price, ff, ft, bag, type, p_d1), ptrReturnDate(p_d2) {
|
||||
}
|
||||
|
||||
ReturnFlightTicket::~ReturnFlightTicket() {
|
||||
}
|
||||
|
||||
std::string ReturnFlightTicket::toString() const {
|
||||
std::stringstream ss;
|
||||
ss << FlightTicket::toString();
|
||||
ss << "\tReturn On: " << this->ptrReturnDate->toString() << "\n";
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
bool ReturnFlightTicket::isEqual(const ReturnFlightTicket& second) const {
|
||||
if (FlightTicket::isEqual(second)
|
||||
&& this->ptrReturnDate->isEqual(*second.ptrReturnDate))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
double ReturnFlightTicket::getTotalPrice() const {
|
||||
return FlightTicket::getTotalPrice() * 2;
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,25 @@
|
||||
|
||||
#ifndef EXAMPLE12_RETURNFLIGHTTICKET_H
|
||||
#define EXAMPLE12_RETURNFLIGHTTICKET_H
|
||||
|
||||
#include "FlightTicket.h"
|
||||
#include "Date.h"
|
||||
|
||||
class ReturnFlightTicket: public FlightTicket {
|
||||
private:
|
||||
Date* ptrReturnDate;
|
||||
public:
|
||||
ReturnFlightTicket();
|
||||
ReturnFlightTicket(const ReturnFlightTicket& rft);
|
||||
ReturnFlightTicket(double price, std::string ff, std::string ft, bool bag, FlightTicketType type, Date* p_d1, Date* p_d2);
|
||||
~ReturnFlightTicket();
|
||||
|
||||
//methods
|
||||
std::string toString() const;
|
||||
double getTotalPrice() const;
|
||||
bool isEqual(const ReturnFlightTicket& second) const;
|
||||
};
|
||||
|
||||
|
||||
|
||||
#endif //EXAMPLE12_RETURNFLIGHTTICKET_H
|
28
semester_2/programiranje_2/primeri/Example12/main.cpp
Normal file
28
semester_2/programiranje_2/primeri/Example12/main.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
#include "FlightTicket.h"
|
||||
#include "ReturnFlightTicket.h"
|
||||
|
||||
int main() {
|
||||
Date* ptrDate1 = new Date(17, 3, 2020);
|
||||
Date* ptrDate2 = new Date(18, 3, 2020);
|
||||
Date* ptrDate3 = new Date(19, 3, 2020);
|
||||
Date* ptrDate4 = new Date(29, 3, 2020);
|
||||
Date* ptrDate5 = new Date(31, 3, 2020);
|
||||
FlightTicket* ptrFlights[3];
|
||||
ptrFlights[0] = new FlightTicket(400, "Moskva", "Ljubljana", false, FlightTicketType::Economy, ptrDate1);
|
||||
ptrFlights[1] = new ReturnFlightTicket(1000, "Hong Kong", "Washington", true,
|
||||
FlightTicketType::FirstClass, ptrDate2, ptrDate4);
|
||||
ptrFlights[2] = new ReturnFlightTicket(800, "Vienna", "Chicago",
|
||||
false, FlightTicketType::Economy, ptrDate3, ptrDate5);
|
||||
for (int i=0; i<3; i++)
|
||||
std::cout << ptrFlights[i]->toString() << std::endl;
|
||||
delete ptrDate1;
|
||||
delete ptrDate2;
|
||||
delete ptrDate3;
|
||||
delete ptrDate4;
|
||||
delete ptrDate5;
|
||||
for (int i=0; i<3; i++)
|
||||
delete ptrFlights[i];
|
||||
return 0;
|
||||
}
|
||||
|
64
semester_2/programiranje_2/primeri/Example13/main.cpp
Normal file
64
semester_2/programiranje_2/primeri/Example13/main.cpp
Normal file
@@ -0,0 +1,64 @@
|
||||
#include <iostream>
|
||||
/*
|
||||
struct Node {
|
||||
int el;
|
||||
Node* ptrNext;
|
||||
};
|
||||
|
||||
//void insertAtBeginning(Node* start, int n) {
|
||||
void insertAtBeginning(Node*& start, int n) {
|
||||
Node* ptrTemp = new Node;
|
||||
ptrTemp->el = n;
|
||||
ptrTemp->ptrNext = start;
|
||||
start=ptrTemp;
|
||||
}
|
||||
|
||||
//void print(Node*& start) {
|
||||
void print(Node* start) {
|
||||
while (start) {
|
||||
std::cout << start-> el << " ";
|
||||
start=start->ptrNext;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
*/
|
||||
/*
|
||||
void print(Node*& start) {
|
||||
//void print(Node* start) {
|
||||
Node* temp = start;
|
||||
while (temp) {
|
||||
std::cout << temp-> el << " ";
|
||||
temp=temp->ptrNext;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
*/
|
||||
|
||||
int main() {
|
||||
int a = 10;
|
||||
int b = 20;
|
||||
int* p_a = &a;
|
||||
int** p_p_a = &p_a;
|
||||
std::cout << "a " << &a << " " << a << std::endl;
|
||||
std::cout << "b " << &b << " " << b << std::endl;
|
||||
std::cout << "p_a " << &p_a << " " << p_a << " " << *p_a << std::endl;
|
||||
std::cout << "p_p_a " << &p_p_a << " " << p_p_a << " " << *p_p_a << " " << **p_p_a << std::endl;
|
||||
std::cout << "------------------------" << std::endl;
|
||||
/*
|
||||
//The nullptr keyword represents a null pointer value.
|
||||
Node* ptrStart = nullptr;
|
||||
insertAtBeginning(ptrStart, 3);
|
||||
insertAtBeginning(ptrStart, 2);
|
||||
insertAtBeginning(ptrStart, 1);
|
||||
print(ptrStart);
|
||||
print(ptrStart);
|
||||
|
||||
while (ptrStart) {
|
||||
Node* ptrTemp = ptrStart->ptrNext;
|
||||
delete ptrStart;
|
||||
ptrStart=ptrTemp;
|
||||
}
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
|
23
semester_2/programiranje_2/primeri/Example14/Circle.h
Normal file
23
semester_2/programiranje_2/primeri/Example14/Circle.h
Normal file
@@ -0,0 +1,23 @@
|
||||
|
||||
#ifndef EXAMPLE14_CIRCLE_H
|
||||
#define EXAMPLE14_CIRCLE_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class Circle : public Shape {
|
||||
private:
|
||||
int r;
|
||||
public:
|
||||
Circle() : Shape(), r(0) {
|
||||
}
|
||||
Circle(int x, int y, int r) : Shape(x, y) , r(r) {
|
||||
}
|
||||
double area() const {
|
||||
return 3.14*r*r;
|
||||
}
|
||||
void print() const {
|
||||
std::cout << "Circle(" << x << ", " << y << ", " << r << ")" << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //EXAMPLE14_CIRCLE_H
|
33
semester_2/programiranje_2/primeri/Example14/Composite.cpp
Normal file
33
semester_2/programiranje_2/primeri/Example14/Composite.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
|
||||
#include "Shape.h"
|
||||
#include "Composite.h"
|
||||
|
||||
Composite::Composite(Shape* s) : Shape(), ptrShape(s), ptrNext(nullptr) {
|
||||
}
|
||||
|
||||
void Composite::add(Shape* s) {
|
||||
if (!ptrShape) ptrShape = s;
|
||||
else {
|
||||
if (!ptrNext) ptrNext = new Composite(s);
|
||||
else ptrNext->add(s);
|
||||
}
|
||||
}
|
||||
|
||||
double Composite::area() const {
|
||||
if (!ptrNext) return ptrShape->area();
|
||||
else return ptrShape->area() + ptrNext->area();
|
||||
}
|
||||
|
||||
void Composite::print() const {
|
||||
if (ptrShape) ptrShape->print();
|
||||
if (ptrNext) ptrNext->print();
|
||||
}
|
||||
void Composite::relMove(int x1, int y1) {
|
||||
if (ptrShape) ptrShape->relMove(x1, y1);
|
||||
if (ptrNext) ptrNext->relMove(x1, y1);
|
||||
}
|
||||
|
||||
void Composite::deleteRec() {
|
||||
if (ptrShape) delete ptrShape;
|
||||
if (ptrNext) ptrNext->deleteRec();
|
||||
}
|
17
semester_2/programiranje_2/primeri/Example14/Composite.h
Normal file
17
semester_2/programiranje_2/primeri/Example14/Composite.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#ifndef EXAMPLE14_COMPOSITE_H
|
||||
#define EXAMPLE14_COMPOSITE_H
|
||||
|
||||
class Composite : public Shape {
|
||||
private:
|
||||
Shape* ptrShape;
|
||||
Composite* ptrNext;
|
||||
public:
|
||||
Composite(Shape* s = nullptr);
|
||||
void add(Shape* s);
|
||||
double area() const;
|
||||
void print() const;
|
||||
void relMove(int x1, int y1);
|
||||
void deleteRec();
|
||||
};
|
||||
|
||||
#endif //EXAMPLE14_COMPOSITE_H
|
21
semester_2/programiranje_2/primeri/Example14/Rectangle.h
Normal file
21
semester_2/programiranje_2/primeri/Example14/Rectangle.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#ifndef EXAMPLE14_RECTANGLE_H
|
||||
#define EXAMPLE14_RECTANGLE_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class Rectangle : public Shape {
|
||||
private:
|
||||
int w, h;
|
||||
public:
|
||||
Rectangle() : Shape(), w(0), h(0) {
|
||||
}
|
||||
Rectangle(int x, int y, int w, int h) : Shape(x, y), w(w), h(h) {
|
||||
}
|
||||
double area() const {
|
||||
return w*h;
|
||||
}
|
||||
void print() const {
|
||||
std::cout << "Rectangle(" << x << ", " << y << ", " <<
|
||||
w << ", " << h << ")" << std::endl;}
|
||||
};
|
||||
#endif //EXAMPLE14_RECTANGLE_H
|
26
semester_2/programiranje_2/primeri/Example14/Shape.h
Normal file
26
semester_2/programiranje_2/primeri/Example14/Shape.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef EXAMPLE14_SHAPE_H
|
||||
#define EXAMPLE14_SHAPE_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class Shape { // abstract class
|
||||
protected:
|
||||
int x,y;
|
||||
public:
|
||||
Shape() : x(0), y(0) {}
|
||||
Shape(int x, int y) : x(x), y(y) {}
|
||||
virtual double area() const = 0; //abstract constant method
|
||||
virtual void print() const = 0; //abstract constant method
|
||||
virtual void relMove(int dx, int dy) {
|
||||
x+=dx;
|
||||
y+=dy;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif //EXAMPLE14_SHAPE_H
|
36
semester_2/programiranje_2/primeri/Example14/main.cpp
Normal file
36
semester_2/programiranje_2/primeri/Example14/main.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <iostream>
|
||||
#include "Shape.h"
|
||||
#include "Circle.h"
|
||||
#include "Rectangle.h"
|
||||
#include "Composite.h"
|
||||
|
||||
int main() {
|
||||
Rectangle r1(0,0,10,10);
|
||||
Circle c1(0, 0, 10);
|
||||
r1.print();
|
||||
c1.print();
|
||||
std::cout << "area of a circle = " << c1.area() << std::endl;
|
||||
std::cout << "area of a rectangle = " << r1.area() << std::endl;
|
||||
/*
|
||||
std::cout << "---------------" << std::endl;
|
||||
Composite c;
|
||||
c.add(new Rectangle(1,1,1,10));
|
||||
c.add(new Circle(1,1,1));
|
||||
c.print();
|
||||
std::cout << "area of a composite = " << c.area() << std::endl;
|
||||
|
||||
std::cout << "---------------" << std::endl;
|
||||
Composite cc;
|
||||
cc.add(new Circle(2,2,2));
|
||||
cc.add(&c);
|
||||
cc.print();
|
||||
std::cout << "area of a composite = " << cc.area() << std::endl; //3.14*2*2 + 1*10 + 3.14*1*1
|
||||
|
||||
std::cout << "---------------" << std::endl;
|
||||
cc.relMove(10,10);
|
||||
cc.print();
|
||||
*/
|
||||
//c.deleteRec();
|
||||
//cc.deleteRec();
|
||||
return 0;
|
||||
}
|
29
semester_2/programiranje_2/primeri/Example15/main.cpp
Normal file
29
semester_2/programiranje_2/primeri/Example15/main.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <iostream>
|
||||
|
||||
template < typename T >
|
||||
T max( T a, T b ) {
|
||||
return a>b ? a : b;
|
||||
}
|
||||
|
||||
float max(float a, float b) {
|
||||
return a*b;
|
||||
}
|
||||
|
||||
int main() {
|
||||
double x, y, res;
|
||||
x=1;
|
||||
y=1;
|
||||
res = max(x-1,y+2.5);
|
||||
std::cout << res << std::endl;
|
||||
|
||||
int i = 1;
|
||||
int k = max(i, 3);
|
||||
std::cout << k << std::endl;
|
||||
|
||||
float a = 1.1, b=2.2;
|
||||
float c = max(a,b);
|
||||
std::cout << c << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
61
semester_2/programiranje_2/primeri/Example16/main.cpp
Normal file
61
semester_2/programiranje_2/primeri/Example16/main.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
#include <iostream>
|
||||
|
||||
template < typename T >
|
||||
int spaceOf ( T x ) {
|
||||
int bytes = sizeof(x);
|
||||
return bytes/4 + (bytes%4>0); // size of memory cell is 4 bytes
|
||||
}
|
||||
/*
|
||||
template < typename T >
|
||||
int spaceOf () {
|
||||
int bytes = sizeof(T);
|
||||
return bytes/4 + (bytes%4>0);
|
||||
}
|
||||
*/
|
||||
/*
|
||||
template < typename T1, typename T2 >
|
||||
void f ( T1 v1, T2 v2 ) {
|
||||
std::cout << v1 << " " << v2 << std::endl;
|
||||
}
|
||||
*/
|
||||
/*
|
||||
template < int N, typename T >
|
||||
T power ( T v ) {
|
||||
T res = v;
|
||||
for ( int i=1; i<N; i++ )
|
||||
res *= v;
|
||||
return res;
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
int main() {
|
||||
int x1=199;
|
||||
double x2=2.99;
|
||||
|
||||
// implicit template instantiation
|
||||
std::cout << spaceOf(x1) << std::endl;
|
||||
std::cout << spaceOf(x2) << std::endl;
|
||||
std::cout << "-------------" << std::endl;
|
||||
/*
|
||||
// explicit template instantiation
|
||||
std::cout << spaceOf<int>() << std::endl;
|
||||
std::cout << spaceOf<double>() << std::endl;
|
||||
std::cout << "-------------" << std::endl;
|
||||
std::cout << spaceOf<int>(x1) << std::endl;
|
||||
std::cout << spaceOf<double>(x2) << std::endl;
|
||||
*/
|
||||
/*
|
||||
f(2.2,1); // implicit
|
||||
f<double,int>(2.2,1); // explicit complete
|
||||
f<double>(2.2,1); // explicit incomplete
|
||||
f<int,int>(2.2,1); // explicit complete
|
||||
*/
|
||||
/*
|
||||
std::cout << power<3>(2) << std::endl;
|
||||
std::cout << power<5, int>(1.2) << std::endl;
|
||||
std::cout << power<5>(1.2) << std::endl;
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
|
34
semester_2/programiranje_2/primeri/Example17/main.cpp
Normal file
34
semester_2/programiranje_2/primeri/Example17/main.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
// primary template
|
||||
template < typename T >
|
||||
bool less ( T v1, T v2 ) {
|
||||
return v1 < v2;
|
||||
}
|
||||
/*
|
||||
// explicit template specialization
|
||||
template<>
|
||||
bool less<const char*> (const char* v1, const char* v2) {
|
||||
//bool less (const char* v1, const char* v2) {
|
||||
return strcmp(v1,v2)<0;
|
||||
}
|
||||
*/
|
||||
/*
|
||||
// partial specialization
|
||||
template< typename T>
|
||||
bool less<T*> (T* v1, T* v2){
|
||||
return *v1 < *v2;
|
||||
}
|
||||
*/
|
||||
int main() {
|
||||
int i1=1;
|
||||
int i2=2;
|
||||
|
||||
bool l1 = less(i1,i2);
|
||||
bool l2 = less(1.2,3.4);
|
||||
bool l3 = less("abcd","abcx");
|
||||
bool l4 = less(&i1, &i2);
|
||||
std::cout << l1 << " " << l2 << " " << l3 << " " << l4 << std::endl;
|
||||
return 0;
|
||||
}
|
28
semester_2/programiranje_2/primeri/Example18/Stack.h
Normal file
28
semester_2/programiranje_2/primeri/Example18/Stack.h
Normal file
@@ -0,0 +1,28 @@
|
||||
|
||||
#ifndef EXMAPLE18_STACK_H
|
||||
#define EXMAPLE18_STACK_H
|
||||
|
||||
typedef void* T;
|
||||
|
||||
class Stack {
|
||||
private:
|
||||
int top;
|
||||
T* impl; // an array of pointers to void
|
||||
public:
|
||||
Stack(int n=5) : top(-1), impl(new T[n]) {
|
||||
}
|
||||
~Stack() {
|
||||
delete[] impl;
|
||||
}
|
||||
bool empty() const {
|
||||
return top == -1;
|
||||
}
|
||||
void push(T el) {
|
||||
impl[++top] = el;
|
||||
}
|
||||
T pop() {
|
||||
return impl[top--];
|
||||
}
|
||||
};
|
||||
|
||||
#endif //EXMAPLE18_STACK_H
|
20
semester_2/programiranje_2/primeri/Example18/main.cpp
Normal file
20
semester_2/programiranje_2/primeri/Example18/main.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include <iostream>
|
||||
#include "Stack.h"
|
||||
|
||||
int main() {
|
||||
Stack myStack(10);
|
||||
char plus = '+';
|
||||
char c = 'c';
|
||||
//int a=65;
|
||||
//double b=1.23;
|
||||
|
||||
myStack.push(&plus);
|
||||
myStack.push(&plus);
|
||||
myStack.push(&c);
|
||||
//myStack.push(&a);
|
||||
//myStack.push(&b);
|
||||
while (!myStack.empty()) {
|
||||
std::cout << *(char*)myStack.pop() << " ";
|
||||
}
|
||||
return 0;
|
||||
}
|
26
semester_2/programiranje_2/primeri/Example19/Stack.h
Normal file
26
semester_2/programiranje_2/primeri/Example19/Stack.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#ifndef EXAMPLE19_STACK_H
|
||||
#define EXAMPLE19_STACK_H
|
||||
|
||||
template <typename T>
|
||||
class Stack {
|
||||
private:
|
||||
int top;
|
||||
T* impl;
|
||||
public:
|
||||
Stack(int n=5) : top(-1), impl(new T[n]) {
|
||||
}
|
||||
~Stack() {
|
||||
delete[] impl;
|
||||
}
|
||||
bool empty() const {
|
||||
return top == -1;
|
||||
}
|
||||
void push(T el) {
|
||||
impl[++top] = el;
|
||||
}
|
||||
T pop() {
|
||||
return impl[top--];
|
||||
}
|
||||
};
|
||||
|
||||
#endif //EXAMPLE19_STACK_H
|
24
semester_2/programiranje_2/primeri/Example19/main.cpp
Normal file
24
semester_2/programiranje_2/primeri/Example19/main.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <iostream>
|
||||
#include "Stack.h"
|
||||
|
||||
int main() {
|
||||
Stack<char> myStack1(10); // always explicit instantiation
|
||||
myStack1.push('+');
|
||||
myStack1.push('+');
|
||||
myStack1.push('c');
|
||||
while (!myStack1.empty()) {
|
||||
std::cout << myStack1.pop() << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
Stack<int> myStack2;
|
||||
myStack2.push(1);
|
||||
myStack2.push(2);
|
||||
myStack2.push(3);
|
||||
while (!myStack2.empty()) {
|
||||
std::cout << myStack2.pop() << " ";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
32
semester_2/programiranje_2/primeri/Example20/C.h
Normal file
32
semester_2/programiranje_2/primeri/Example20/C.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef EXAMPLE20_C_H
|
||||
#define EXAMPLE20_C_H
|
||||
|
||||
#include <cstring>
|
||||
|
||||
// primary template
|
||||
template < typename T >
|
||||
class C {
|
||||
public:
|
||||
bool less (const T& v1, const T& v2) {
|
||||
return v1<v2;
|
||||
}
|
||||
};
|
||||
|
||||
// explicit template specialization
|
||||
template<>
|
||||
class C<const char*> {
|
||||
public:
|
||||
bool less (const char* v1, const char* v2) {
|
||||
return strcmp(v1,v2)<0;
|
||||
}
|
||||
};
|
||||
|
||||
// partial specialization
|
||||
template< typename T >
|
||||
class C<T*> {
|
||||
public:
|
||||
bool less (T* v1, T* v2) {
|
||||
return *v1 < *v2; }
|
||||
};
|
||||
|
||||
#endif //EXAMPLE20_C_H
|
19
semester_2/programiranje_2/primeri/Example20/main.cpp
Normal file
19
semester_2/programiranje_2/primeri/Example20/main.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <iostream>
|
||||
#include "C.h"
|
||||
|
||||
int main() {
|
||||
int i1 = 1;
|
||||
int i2 = 2;
|
||||
C<int> o1;
|
||||
C<double> o2;
|
||||
C<const char*> o3;
|
||||
C<int*> o4;
|
||||
std::cout << o1.less(i1, i2) << " ";
|
||||
std::cout << o2.less(1.2, 3.4) << " ";
|
||||
std::cout << o3.less("abcd", "abcx") << " ";
|
||||
std::cout << o4.less(&i1, &i2) << std::endl;
|
||||
//o2=o1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
16
semester_2/programiranje_2/primeri/Example21/main.cpp
Normal file
16
semester_2/programiranje_2/primeri/Example21/main.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#include <iostream>
|
||||
|
||||
template<int N>
|
||||
struct Fact {
|
||||
enum {RET = N * Fact<N-1>::RET};
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Fact<0>{
|
||||
enum {RET = 1};
|
||||
};
|
||||
|
||||
int main() {
|
||||
std::cout << Fact<5>::RET << std::endl;
|
||||
return 0;
|
||||
}
|
69
semester_2/programiranje_2/primeri/Example22/Complex.cpp
Normal file
69
semester_2/programiranje_2/primeri/Example22/Complex.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#include <iostream>
|
||||
#include "Complex.h"
|
||||
|
||||
Complex::Complex() : real(0), imag(0) {
|
||||
}
|
||||
|
||||
Complex::Complex(double r, double i): real(r), imag(i) {
|
||||
}
|
||||
|
||||
Complex::~Complex() {
|
||||
}
|
||||
|
||||
void Complex::print() const {
|
||||
std::cout << "(" << real << ", " << imag << "i)" << std::endl;
|
||||
}
|
||||
|
||||
Complex Complex::plus(const Complex& snd) const {
|
||||
Complex temp(real+snd.real, imag+snd.imag);
|
||||
return temp;
|
||||
}
|
||||
|
||||
Complex Complex::operator+(const Complex& snd) const {
|
||||
Complex temp(real+snd.real, imag+snd.imag);
|
||||
return temp;
|
||||
}
|
||||
|
||||
/*
|
||||
bool Complex::operator==(const Complex& snd) const {
|
||||
if (this == &snd)
|
||||
return true;
|
||||
if ((this->real == snd.real) && (this->imag == snd.imag))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
/*
|
||||
void Complex::operator=(const Complex& right) {
|
||||
this->real = right.real;
|
||||
this->imag = right.imag;
|
||||
}
|
||||
*/
|
||||
/*
|
||||
Complex& Complex::operator=(const Complex& right) {
|
||||
this->real = right.real;
|
||||
this->imag = right.imag;
|
||||
return *this;
|
||||
}
|
||||
*/
|
||||
/*
|
||||
//prefix operator++
|
||||
Complex& Complex::operator++() {
|
||||
this->real++;
|
||||
return *this;
|
||||
}
|
||||
//postfix operator++
|
||||
Complex& Complex::operator++(int dummy) {
|
||||
this->real += 10;
|
||||
return *this;
|
||||
}
|
||||
*/
|
||||
/*
|
||||
// global function - a friend of class Complex
|
||||
Complex operator+(double d, const Complex& snd) {
|
||||
Complex temp(d+snd.real, 0+snd.imag);
|
||||
return temp;
|
||||
}
|
||||
*/
|
||||
|
||||
|
23
semester_2/programiranje_2/primeri/Example22/Complex.h
Normal file
23
semester_2/programiranje_2/primeri/Example22/Complex.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef EXAMPLE22_COMPLEX_H
|
||||
#define EXAMPLE22_COMPLEX_H
|
||||
|
||||
class Complex {
|
||||
//friend Complex operator+(double d, const Complex& snd);
|
||||
private:
|
||||
double real, imag;
|
||||
public:
|
||||
Complex();
|
||||
Complex(double r, double i = 0);
|
||||
~Complex();
|
||||
void print() const;
|
||||
Complex plus(const Complex& snd) const;
|
||||
Complex operator+(const Complex& snd) const;
|
||||
// more operators
|
||||
//bool operator==(const Complex& snd) const;
|
||||
//void operator=(const Complex& right);
|
||||
//Complex& operator=(const Complex& right);
|
||||
//Complex& operator++(); // prefix operator++
|
||||
//Complex& operator++(int dummy); //postfix operator++
|
||||
};
|
||||
|
||||
#endif //EXAMPLE22_COMPLEX_H
|
63
semester_2/programiranje_2/primeri/Example22/main.cpp
Normal file
63
semester_2/programiranje_2/primeri/Example22/main.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#include <iostream>
|
||||
#include "Complex.h"
|
||||
|
||||
int main() {
|
||||
const Complex i(0,1);
|
||||
Complex c1(1,1);
|
||||
Complex c2, c3, c4;
|
||||
|
||||
i.plus(c1).print();
|
||||
i.operator+(c1).print();
|
||||
(i+c1).print();
|
||||
/*
|
||||
Complex* ptrc = new Complex(1,0);
|
||||
ptrc->plus(c1).print();
|
||||
ptrc->operator+(c1).print();
|
||||
//(ptrc+c1).print();
|
||||
(*ptrc+c1).print();
|
||||
delete ptrc;
|
||||
*/
|
||||
/*
|
||||
std::cout << "-----------------------" << std::endl;
|
||||
c2=c1+i; //c2.operator=(c1.operator+(i));
|
||||
c2.print();
|
||||
*/
|
||||
/*
|
||||
std::cout << "------------------" << std::endl;
|
||||
c2 = c1 + 1; //c2.operator=(c1.operator+(1));
|
||||
c2.print();
|
||||
//c3 = 2 + 1; //c3.operator=(3);
|
||||
//c3.print();
|
||||
//c4 = 1 + c1; // 1.operator+(c1) // operator+(1, c1)
|
||||
//c4.print();
|
||||
*/
|
||||
/*
|
||||
std::cout << "------------------" << std::endl;
|
||||
c2.print();
|
||||
c4 = c1 + 1;
|
||||
c4.print();
|
||||
if (c2 == c4)
|
||||
std::cout << "Complex numbers are equal" << std::endl;
|
||||
else
|
||||
std::cout << "Complex numbers are not equal" << std::endl;
|
||||
*/
|
||||
/*
|
||||
std::cout << "------------------" << std::endl;
|
||||
Complex c5, c6;
|
||||
c1.print();
|
||||
c4 = c1 + 1;
|
||||
c5 = c4;
|
||||
c4.print();
|
||||
c5.print();
|
||||
//c6=c5=c4+c1;
|
||||
//c5.print();
|
||||
//c6.print();
|
||||
*/
|
||||
/*
|
||||
std::cout << "------------------" << std::endl;
|
||||
c1.print();
|
||||
(++c1).print(); //c1.operator++()
|
||||
(c1++).print(); //c1.operator++(int)
|
||||
*/
|
||||
return 0;
|
||||
}
|
30
semester_2/programiranje_2/primeri/Example23/Stack.h
Normal file
30
semester_2/programiranje_2/primeri/Example23/Stack.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef EXAMPLE23_STACK_H
|
||||
#define EXAMPLE23_STACK_H
|
||||
|
||||
template <typename T>
|
||||
class Stack {
|
||||
public:
|
||||
int top;
|
||||
T arr[10];
|
||||
|
||||
Stack() : top(-1) {
|
||||
}
|
||||
void push(T value) {
|
||||
arr[++top] = value;
|
||||
}
|
||||
T pop() {
|
||||
return arr[top--];
|
||||
}
|
||||
/*
|
||||
template <typename T1> // member template
|
||||
Stack<T>& operator= (Stack<T1>& stack) {
|
||||
for (int i = 0; i <= stack.top; i++) {
|
||||
arr[i] = stack.arr[i];
|
||||
}
|
||||
top=stack.top;
|
||||
return *this;
|
||||
}
|
||||
*/
|
||||
};
|
||||
|
||||
#endif //EXAMPLE23_STACK_H
|
15
semester_2/programiranje_2/primeri/Example23/main.cpp
Normal file
15
semester_2/programiranje_2/primeri/Example23/main.cpp
Normal file
@@ -0,0 +1,15 @@
|
||||
#include <iostream>
|
||||
#include "Stack.h"
|
||||
|
||||
int main() {
|
||||
Stack<int> s1,s2;
|
||||
Stack<float> s3;
|
||||
s2.push(2);
|
||||
s2.push(3);
|
||||
s2.push(4);
|
||||
s1 = s2; // OK
|
||||
std::cout << s1.pop() << " " << s1.pop() << std::endl;
|
||||
//s3 = s2; // ERROR without member templates
|
||||
//std::cout << s3.pop() << " " << s3.pop() << std::endl;
|
||||
return 0;
|
||||
}
|
24
semester_2/programiranje_2/primeri/Example24/Assistant.h
Normal file
24
semester_2/programiranje_2/primeri/Example24/Assistant.h
Normal file
@@ -0,0 +1,24 @@
|
||||
|
||||
#ifndef EXAMPLE24_ASSISTANT_H
|
||||
#define EXAMPLE24_ASSISTANT_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class Assistant {
|
||||
protected:
|
||||
std::string name;
|
||||
std::string faculty;
|
||||
std::string subject;
|
||||
public:
|
||||
Assistant(std::string n, std::string f, std::string s) : name(n), faculty(f), subject(s) {
|
||||
}
|
||||
virtual ~Assistant() {
|
||||
}
|
||||
virtual void print() const {
|
||||
std::cout << "Assistant: " << name << " " << faculty << " " << subject << std::endl;
|
||||
}
|
||||
virtual std::string department() const {
|
||||
return faculty;
|
||||
}
|
||||
};
|
||||
#endif //EXAMPLE24_ASSISTANT_H
|
25
semester_2/programiranje_2/primeri/Example24/Student.h
Normal file
25
semester_2/programiranje_2/primeri/Example24/Student.h
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
#ifndef EXAMPLE24_STUDENT_H
|
||||
#define EXAMPLE24_STUDENT_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class Student {
|
||||
protected:
|
||||
std::string name;
|
||||
std::string faculty;
|
||||
double avgGrade;
|
||||
public:
|
||||
Student(std::string n, std::string f, double g) : name(n), faculty(f), avgGrade(g) {
|
||||
};
|
||||
virtual ~Student() {
|
||||
}
|
||||
virtual void print() const {
|
||||
std::cout << "Student: " << name << " " << faculty << " " << avgGrade << std::endl;
|
||||
}
|
||||
virtual std::string department() const {
|
||||
return faculty;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //EXAMPLE24_STUDENT_H
|
@@ -0,0 +1,19 @@
|
||||
|
||||
#ifndef EXAMPLE24_STUDENTASSISTANT_H
|
||||
#define EXAMPLE24_STUDENTASSISTANT_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class StudentAssistant : public Assistant, public Student {
|
||||
public:
|
||||
StudentAssistant(std::string n, std::string f1, double g, std::string f2, std::string s) :
|
||||
Assistant(n, f2, s), Student(n, f1, g) {
|
||||
}
|
||||
virtual ~StudentAssistant() {
|
||||
}
|
||||
void print() const {
|
||||
std::cout << "Student-Assistant: " << Student::name << " " << Student::faculty << " "
|
||||
<< avgGrade << " " << Assistant::faculty << " " << subject << std::endl;
|
||||
}
|
||||
};
|
||||
#endif //EXAMPLE24_STUDENTASSISTANT_H
|
34
semester_2/programiranje_2/primeri/Example24/main.cpp
Normal file
34
semester_2/programiranje_2/primeri/Example24/main.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
#include <iostream>
|
||||
#include "Student.h"
|
||||
#include "Assistant.h"
|
||||
#include "StudentAssistant.h"
|
||||
|
||||
int main() {
|
||||
Student s1("Mark", "FERI", 8.3);
|
||||
Assistant a1("Mary", "FKKT", "Introduction to chemistry");
|
||||
StudentAssistant sa1("John", "FNM", 10, "FERI", "Discrete math");
|
||||
|
||||
s1.print();
|
||||
a1.print();
|
||||
sa1.print();
|
||||
/*
|
||||
std::cout << "--------------" << std::endl;
|
||||
Student* university[2];
|
||||
university[0]=&s1;
|
||||
university[1]=&sa1;
|
||||
//university[1]=&a1; // ERROR: class Assistant is not with any relation with class Student
|
||||
for (int i=0; i < 2; i++) {
|
||||
university[i]->print();
|
||||
std::cout << "Department: " << university[i]->department() << std::endl;
|
||||
}
|
||||
*/
|
||||
/*
|
||||
std::cout << "--------------" << std::endl;
|
||||
sa1.print();
|
||||
//std::cout << "Department: " << sa1.department() << std::endl; // error
|
||||
std::cout << "Department Assistant: " << sa1.Assistant::department() << std::endl;
|
||||
std::cout << "Department Student: " << sa1.Student::department() << std::endl;
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
|
24
semester_2/programiranje_2/primeri/Example25/Assistant.h
Normal file
24
semester_2/programiranje_2/primeri/Example25/Assistant.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#ifndef EXAMPLE25_ASSISTANT_H
|
||||
#define EXAMPLE25_ASSISTANT_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class Assistant : public Person {
|
||||
//class Assistant : public virtual Person {
|
||||
protected:
|
||||
std::string faculty;
|
||||
std::string subject;
|
||||
public:
|
||||
Assistant(std::string n, std::string f, std::string s) : Person(n), faculty(f), subject(s) {
|
||||
}
|
||||
virtual ~Assistant() {
|
||||
}
|
||||
virtual void print() const {
|
||||
std::cout << "Assistant: " << name << " " << faculty << " " << subject << std::endl;
|
||||
}
|
||||
virtual std::string department() const {
|
||||
return faculty;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //EXAMPLE25_ASSISTANT_H
|
18
semester_2/programiranje_2/primeri/Example25/Person.h
Normal file
18
semester_2/programiranje_2/primeri/Example25/Person.h
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
#ifndef EXAMPLE25_PERSON_H
|
||||
#define EXAMPLE25_PERSON_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class Person {
|
||||
protected:
|
||||
std::string name;
|
||||
public:
|
||||
Person(std::string n) : name(n) {};
|
||||
virtual ~Person() {}
|
||||
virtual void print() const {
|
||||
std::cout << "Person: " << name << " " << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //EXAMPLE25_PERSON_H
|
25
semester_2/programiranje_2/primeri/Example25/Student.h
Normal file
25
semester_2/programiranje_2/primeri/Example25/Student.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef EXAMPLE25_STUDENT_H
|
||||
#define EXAMPLE25_STUDENT_H
|
||||
|
||||
#include <iostream>
|
||||
#include "Person.h"
|
||||
|
||||
class Student : public Person {
|
||||
//class Student : virtual public Person {
|
||||
protected:
|
||||
std::string faculty;
|
||||
double avgGrade;
|
||||
public:
|
||||
Student(std::string n, std::string f, double g) : Person(n), faculty(f), avgGrade(g) {
|
||||
};
|
||||
virtual ~Student() {
|
||||
}
|
||||
virtual void print() const {
|
||||
std::cout << "Student: " << name << " " << faculty << " " << avgGrade << std::endl;
|
||||
}
|
||||
virtual std::string department() const {
|
||||
return faculty;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //EXAMPLE25_STUDENT_H
|
@@ -0,0 +1,32 @@
|
||||
#ifndef EXAMPLE25_STUDENTASSISTANT_H
|
||||
#define EXAMPLE25_STUDENTASSISTANT_H
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class StudentAssistant : public Assistant, public Student {
|
||||
public:
|
||||
|
||||
StudentAssistant(std::string n, std::string f1, double g, std::string f2, std::string s) :
|
||||
Student(n, f1, g), Assistant(n, f2, s) {}
|
||||
//Student("S-"+n, f1, g), Assistant("A-"+n, f2, s) {}
|
||||
// When virtual derivation calls to constructors Person are ignored in classes Student and Assistant
|
||||
// calling constructor Person needs to be explicit here
|
||||
//StudentAssistant(std::string n, std::string f1, double g, std::string f2, std::string s) :
|
||||
//Person(n), Student(n, f1, g), Assistant(n, f2, s) {}
|
||||
//Person(n), Student("S-"+n, f1, g), Assistant("A-"+n, f2, s) {}
|
||||
|
||||
virtual ~StudentAssistant() {
|
||||
}
|
||||
void print() const {
|
||||
// without virtual derivation it does not work
|
||||
//std::cout << "Student-Assistant: " << Person::name << " " << Student::faculty << " "
|
||||
// << avgGrade << " " << Assistant::faculty << " " << subject << std::endl;
|
||||
std::cout << "Student-Assistant: " << Student::name << " " << Student::faculty << " "
|
||||
<< avgGrade << " " << Assistant::name << " " << Assistant::faculty << " " << subject << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif //EXAMPLE25_STUDENTASSISTANT_H
|
||||
|
||||
|
31
semester_2/programiranje_2/primeri/Example25/main.cpp
Normal file
31
semester_2/programiranje_2/primeri/Example25/main.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <iostream>
|
||||
#include "Person.h"
|
||||
#include "Student.h"
|
||||
#include "Assistant.h"
|
||||
#include "StudentAssistant.h"
|
||||
|
||||
int main() {
|
||||
Person o1("Anna");
|
||||
Student s1("Mark", "FERI", 8.3);
|
||||
Assistant a1("Mary", "FKKT", "Introduction to chemistry");
|
||||
StudentAssistant sa1("John", "FNM", 10, "FERI", "Discrete math");
|
||||
|
||||
o1.print();
|
||||
s1.print();
|
||||
a1.print();
|
||||
sa1.print();
|
||||
/*
|
||||
std::cout << "--------------" << std::endl;
|
||||
Person* society[4];
|
||||
society[0]=&o1;
|
||||
society[1]=&s1;
|
||||
society[2]=&a1;
|
||||
//society[3]=&a1;
|
||||
society[3]=&sa1; //without virtual derivation class StudentAssistant can not be converted to class Person
|
||||
for (int i=0; i < 4; i++) {
|
||||
society[i]->print();
|
||||
}
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
|
82
semester_2/programiranje_2/primeri/Example26/main.cpp
Normal file
82
semester_2/programiranje_2/primeri/Example26/main.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
#include <iostream>
|
||||
|
||||
bool condEq5 ( int x ) {
|
||||
return x==5;
|
||||
}
|
||||
|
||||
bool condRange100 ( int x ) {
|
||||
return (x>=0) && (x<=100);
|
||||
}
|
||||
|
||||
int* find2 ( int* pool, int n, bool (*c)(int) ) {
|
||||
int* p = pool;
|
||||
for ( int i = 0; i<n; i++ ) {
|
||||
if ( c(*p) ) return p;
|
||||
p++;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
/*
|
||||
// functional type
|
||||
class GreaterThan5 {
|
||||
public:
|
||||
bool operator()(int x) { return x>5; }
|
||||
};
|
||||
|
||||
int* find2 ( int* pool, int n, GreaterThan5 c ) {
|
||||
int* p = pool;
|
||||
for ( int i = 0; i<n; i++ ) {
|
||||
if ( c(*p) ) return p; // c.operator()(*p)
|
||||
p++;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
*/
|
||||
/*
|
||||
template < typename T, typename Comparator >
|
||||
T* find2 ( T* pool, int n, Comparator c ) {
|
||||
T* p = pool;
|
||||
for ( int i = 0; i<n; i++ ) {
|
||||
if ( c(*p) ) return p;
|
||||
p++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// different comparators
|
||||
template < typename T, T N >
|
||||
class Greater {
|
||||
public:
|
||||
bool operator()(T x) { return x>N; }
|
||||
};
|
||||
*/
|
||||
|
||||
int main() {
|
||||
int arr[] = {1, 3, 5, 7, 9};
|
||||
int* p = find2(arr,5, condEq5);
|
||||
//int* p = find2(arr,5, [](int x) -> bool {return x==5;}); // lambda expression
|
||||
std::cout << p << " " << *p << " : an array element is at index: " << p - arr << std::endl;
|
||||
p = find2(arr,5,condRange100);
|
||||
std::cout << p << " " << *p << " : an array element is at index: " << p - arr << std::endl;
|
||||
/*
|
||||
bool (*pf)(int) = condEq5; // pointer to function
|
||||
std::cout << condEq5(2) << std::endl; // explicit function call
|
||||
std::cout << pf(2) << std::endl; // implicit function call
|
||||
pf = condRange100;
|
||||
p = find2(arr,5,pf);
|
||||
std::cout << p << " " << *p << " : an array element is at index: " << p - arr << std::endl;
|
||||
*/
|
||||
/*
|
||||
std::cout << "-------------------" << std::endl;
|
||||
GreaterThan5 c; // c is functional object
|
||||
p = find2(arr,5, c);
|
||||
std::cout << p << " " << *p << " : an array element is at index: " << p - arr << std::endl;
|
||||
|
||||
std::cout << "-------------------" << std::endl;
|
||||
Greater<int,7> a;
|
||||
= find2(arr,5, a);
|
||||
//p = find2(arr,5, Greater<int,7>()); //calling defualt constructor
|
||||
std::cout << p << " " << *p << " : an array element is at index: " << p-arr << std::endl;
|
||||
*/
|
||||
return 0;
|
||||
}
|
51
semester_2/programiranje_2/primeri/Example27/main.cpp
Normal file
51
semester_2/programiranje_2/primeri/Example27/main.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#include <iostream>
|
||||
|
||||
class DivideByZero {
|
||||
private:
|
||||
std::string message;
|
||||
public:
|
||||
DivideByZero() : message("ERROR: Division by zero") {
|
||||
}
|
||||
void print() {
|
||||
std::cout << message << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
//float divide(int numerator, int denominator) throw (DivideByZero) { //only DivideByZero exception is allowed
|
||||
//float divide(int numerator, int denominator) throw () { //no exceptions allowed
|
||||
float divide(int numerator, int denominator) { // all exceptions allowed
|
||||
if (denominator==0) throw DivideByZero();
|
||||
return (float)numerator/denominator;
|
||||
}
|
||||
|
||||
void first() {
|
||||
int num1=1, num2=0;
|
||||
try {
|
||||
float res=divide(num1, num2);
|
||||
std::cout << "Result = " << res << std::endl;
|
||||
} catch (DivideByZero e) {
|
||||
e.print();
|
||||
}
|
||||
}
|
||||
/**/
|
||||
/*
|
||||
void second() {
|
||||
int num1=1, num2=0;
|
||||
float res=divide(num1, num2);
|
||||
std::cout << "Result = " << res << std::endl;
|
||||
}
|
||||
|
||||
void first() {
|
||||
try {
|
||||
second();
|
||||
} catch (DivideByZero e) {
|
||||
e.print();
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
int main() {
|
||||
first();
|
||||
//second();
|
||||
return 0;
|
||||
}
|
33
semester_2/programiranje_2/primeri/Example28/List.h
Normal file
33
semester_2/programiranje_2/primeri/Example28/List.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef EXAMPLE28_LIST_H
|
||||
#define EXAMPLE28_LIST_H
|
||||
#include "Node.h"
|
||||
|
||||
class List {
|
||||
private:
|
||||
Node* ptrStart;
|
||||
public:
|
||||
List() : ptrStart(nullptr) {
|
||||
}
|
||||
~List() {
|
||||
}
|
||||
void insertAtBeginning(int el) {
|
||||
Node* temp= new Node();
|
||||
temp->set(el, ptrStart);
|
||||
ptrStart=temp;
|
||||
}
|
||||
int deleteAtBeginning() {
|
||||
Node* temp = ptrStart;
|
||||
ptrStart=ptrStart->getNext();
|
||||
return temp->getData();
|
||||
}
|
||||
void print() {
|
||||
Node* temp = ptrStart;
|
||||
while (temp != nullptr) {
|
||||
std::cout << temp->getData() << " ";
|
||||
temp=temp->getNext();
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //EXAMPLE28_LIST_H
|
25
semester_2/programiranje_2/primeri/Example28/Node.h
Normal file
25
semester_2/programiranje_2/primeri/Example28/Node.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef EXAMPLE28_NODE_H
|
||||
#define EXAMPLE28_NODE_H
|
||||
|
||||
class Node {
|
||||
private:
|
||||
int data;
|
||||
Node* ptrNext;
|
||||
public:
|
||||
Node() : data(0), ptrNext(nullptr) {
|
||||
}
|
||||
~Node() {
|
||||
}
|
||||
int getData() {
|
||||
return data;
|
||||
}
|
||||
Node* getNext() {
|
||||
return ptrNext;
|
||||
}
|
||||
void set(int d, Node* n) {
|
||||
data=d;
|
||||
ptrNext=n;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //EXAMPLE28_NODE_H
|
18
semester_2/programiranje_2/primeri/Example28/Stack.h
Normal file
18
semester_2/programiranje_2/primeri/Example28/Stack.h
Normal file
@@ -0,0 +1,18 @@
|
||||
|
||||
#ifndef EXAMPLE28_STACK_H
|
||||
#define EXAMPLE28_STACK_H
|
||||
#include "List.h"
|
||||
|
||||
class Stack : private List {
|
||||
public:
|
||||
Stack() : List() {
|
||||
}
|
||||
void push(int x) {
|
||||
insertAtBeginning(x);
|
||||
}
|
||||
int pop() {
|
||||
return deleteAtBeginning();
|
||||
}
|
||||
};
|
||||
|
||||
#endif //EXAMPLE28_STACK_H
|
25
semester_2/programiranje_2/primeri/Example28/main.cpp
Normal file
25
semester_2/programiranje_2/primeri/Example28/main.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <iostream>
|
||||
#include "List.h"
|
||||
#include "Stack.h"
|
||||
|
||||
int main() {
|
||||
List l;
|
||||
l.insertAtBeginning(5);
|
||||
l.insertAtBeginning(4);
|
||||
l.print();
|
||||
l.insertAtBeginning(3);
|
||||
l.print();
|
||||
l.deleteAtBeginning();
|
||||
l.print();
|
||||
std::cout << "-------------------" << std::endl;
|
||||
/*
|
||||
Stack myStack;
|
||||
myStack.push(5);
|
||||
myStack.push(4);
|
||||
std::cout << myStack.pop() << std::endl;
|
||||
std::cout << myStack.pop() << std::endl;
|
||||
|
||||
//List* ptrList = new Stack(); //error
|
||||
*/
|
||||
return 0;
|
||||
}
|
46
semester_2/programiranje_2/primeri/Example29/main.cpp
Normal file
46
semester_2/programiranje_2/primeri/Example29/main.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#include <iostream>
|
||||
|
||||
class A {
|
||||
protected:
|
||||
int a;
|
||||
public:
|
||||
A() : a(1) {};
|
||||
int get() { return a; }
|
||||
int methodA() { return 100;}
|
||||
};
|
||||
|
||||
class B : protected A {
|
||||
//class B : private A {
|
||||
protected:
|
||||
int b;
|
||||
public:
|
||||
B() : A(), b(2) {};
|
||||
int get() { return b+a; }
|
||||
int methodB() { return methodA() + 200;}
|
||||
};
|
||||
|
||||
class C : private B {
|
||||
protected:
|
||||
int c;
|
||||
public:
|
||||
C() : B(), c(3) {};
|
||||
int get() { return c+b+a; }
|
||||
//int get() { return c+b; }
|
||||
int methodC() { return methodA() + 300;}
|
||||
//int methodC() { return methodB() + 300;}
|
||||
};
|
||||
|
||||
int main() {
|
||||
A a;
|
||||
B b;
|
||||
C c;
|
||||
std::cout << a.get() << std::endl;
|
||||
std::cout << b.get() << std::endl;
|
||||
std::cout << c.get() << std::endl;
|
||||
std::cout << "------------" << std::endl;
|
||||
std::cout << a.methodA() << std::endl;
|
||||
std::cout << b.methodB() << std::endl;
|
||||
std::cout << c.methodC() << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
23
semester_2/programiranje_2/primeri/Example30/main.cpp
Normal file
23
semester_2/programiranje_2/primeri/Example30/main.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <iostream>
|
||||
#include <stdarg.h>
|
||||
|
||||
// function with variable number of arguments (...)
|
||||
int sum (int stev, ...) {
|
||||
va_list args; // variable arguments list
|
||||
int arg;
|
||||
int k, vsota=0;
|
||||
va_start(args, stev); //set the last parameter before variable arguments list
|
||||
for (k=0; k<stev; k++) {
|
||||
arg = va_arg(args, int); // take one argument from variable arguments list
|
||||
vsota += arg;
|
||||
}
|
||||
va_end(args); //End using variable argument list
|
||||
return vsota;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::cout << sum(5, 10, 20, 30, 40, 50) << std::endl;
|
||||
std::cout << sum(3, 11, 12, 13) << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
45
semester_2/programiranje_2/primeri/Example31/main.cpp
Normal file
45
semester_2/programiranje_2/primeri/Example31/main.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <iostream>
|
||||
|
||||
void f(int) {
|
||||
std::cout << "Calling function f(int)" << std::endl;
|
||||
}
|
||||
|
||||
void f(double) {
|
||||
std::cout << "Calling function f(double)" << std::endl;
|
||||
}
|
||||
|
||||
void f(char*) {
|
||||
std::cout << "Calling function f(char*)" << std::endl;
|
||||
}
|
||||
/*
|
||||
template <typename T>
|
||||
void f(T) {
|
||||
std::cout << "Calling function that is instantiated from template" << std::endl;
|
||||
}
|
||||
*/
|
||||
|
||||
void f(int, double) {
|
||||
std::cout << "Calling function f(int, double)" << std::endl;
|
||||
}
|
||||
|
||||
void f(double, double) {
|
||||
std::cout << "Calling function f(double, double)" << std::endl;
|
||||
}
|
||||
/*
|
||||
void f(double, int) {
|
||||
std::cout << "Calling function f(double, int)" << std::endl;
|
||||
}
|
||||
*/
|
||||
|
||||
int main() {
|
||||
f('a'); // f(int) – numeric promotion
|
||||
f(0); // f(int) – exact match
|
||||
//long a=5L;
|
||||
//f(a); //ambiguous, long to int, and long to double are standard conversions
|
||||
//f(double(a)); // ambiguity is resolved by explicit conversion
|
||||
|
||||
// 1. parameter {f1} 2.parameter {f1, f2}: {f1} intersection {f1, f2} = {f1}
|
||||
f('a', 1); // f(int, double)
|
||||
return 0;
|
||||
}
|
||||
|
25
semester_2/programiranje_2/primeri/Example32/main.cpp
Normal file
25
semester_2/programiranje_2/primeri/Example32/main.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main() {
|
||||
char ch;
|
||||
std::ifstream in;
|
||||
std::ofstream out;
|
||||
in.open("a.txt");
|
||||
out.open("b.txt");
|
||||
if (!in.good()) {
|
||||
std::cout << "File doesn't exist" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
while (!in.eof()) {
|
||||
ch = in.get();
|
||||
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
|
||||
ch = '*';
|
||||
out.put(ch);
|
||||
}
|
||||
in.close();
|
||||
out.close();
|
||||
return 0;
|
||||
}
|
||||
|
28
semester_2/programiranje_2/primeri/Example33/main.cpp
Normal file
28
semester_2/programiranje_2/primeri/Example33/main.cpp
Normal file
@@ -0,0 +1,28 @@
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <iomanip>
|
||||
|
||||
int main() {
|
||||
int n=10;
|
||||
std::cout << std::hex << n << std::endl;
|
||||
std::cout << std::dec << n << std::endl;
|
||||
std::cout.operator<<(std::dec).operator<<(n).operator<<(std::endl);
|
||||
|
||||
std::ofstream output("square.txt", std::ios::out);
|
||||
output<<" N | Square"<<std::endl;
|
||||
output<<"------------"<<std::endl;
|
||||
for (int i=1; i<=n; i++) {
|
||||
output << i;
|
||||
output << i*i;
|
||||
/*
|
||||
output << std::setfill(' ');
|
||||
output << std::setw(2) << i;
|
||||
output << std::setw(5) << i*i;
|
||||
*/
|
||||
output << std::endl;
|
||||
}
|
||||
output.close();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
30
semester_2/programiranje_2/primeri/Example34/CPoint.h
Normal file
30
semester_2/programiranje_2/primeri/Example34/CPoint.h
Normal file
@@ -0,0 +1,30 @@
|
||||
#ifndef EXAMPLE34_CPOINT_H
|
||||
#define EXAMPLE34_CPOINT_H
|
||||
|
||||
#include <iostream>
|
||||
#include "Point.h"
|
||||
|
||||
class CPoint : public Point {
|
||||
private:
|
||||
int color;
|
||||
public:
|
||||
CPoint() : Point(), color(0) {
|
||||
}
|
||||
CPoint(int x1, int y1, int b) : Point(x1, y1), color(b) {
|
||||
}
|
||||
~CPoint() {
|
||||
}
|
||||
bool equal(Point& p) {
|
||||
std::cout << "Method CPoint::equal(Point)" << std::endl;
|
||||
return true;
|
||||
}
|
||||
bool equal(CPoint& cp) {
|
||||
std::cout << "Method CPoint::equal(CPoint)" << std::endl;
|
||||
if ( (this->x==cp.x) && (this->y == cp.y) && (this->color == cp.color))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //EXAMPLE34_CPOINT_H
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user