consolidate all repos to one for archive
This commit is contained in:
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;
|
||||
}
|
Reference in New Issue
Block a user