Last active
December 10, 2015 23:48
-
-
Save pstachula-dev/4512183 to your computer and use it in GitHub Desktop.
Programowanie Obiektowe w2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
struktura A *a, *a1, **a2, mA; | |
*/ | |
#include <cstdlib> | |
#include <iostream> | |
using namespace std; | |
class A{ | |
int id; | |
public: | |
void get(); | |
void set(int tid); | |
}; | |
// Metody | |
void A::set(int tid) | |
{ | |
id = tid; | |
} | |
void A::get() | |
{ | |
printf("%d, ", id); | |
} | |
// Tworzenie | |
void create(A **&n, int size) | |
{ | |
n = new A *[size]; | |
for(int i = 0; i < size; i++){ | |
n[i] = new A; | |
} | |
} | |
void create(A *&n, int size) | |
{ | |
n = new A [size]; | |
} | |
void create(A *&n) | |
{ | |
n = new A; | |
} | |
// Usuwanie | |
void destroy(A **&n, int size) | |
{ | |
for(int i = 0; i < size; i++){ | |
delete n[i]; | |
} | |
delete [] n; | |
} | |
void destroy(A *&n) | |
{ | |
delete [] n; | |
} | |
// Dodawanie | |
void add(A **&n, int size) | |
{ | |
for(int i = 0; i < size; i++){ | |
n[i]->set(i); | |
} | |
} | |
void add(A *&n, int size) | |
{ | |
for(int i = 0; i < size; i++){ | |
n[i].set(i); | |
} | |
} | |
void add(A *&n) | |
{ | |
n->set(1); | |
} | |
void add(A &n) | |
{ | |
n.set(99); | |
} | |
// Pokaz | |
void print(A **&n, int size) | |
{ | |
for(int i = 0; i < size; i++){ | |
n[i]->get(); | |
} | |
cout << "\n\n"; | |
} | |
void print(A *&n, int size) | |
{ | |
for(int i = 0; i < size; i++){ | |
n[i].get(); | |
} | |
cout << "\n\n"; | |
} | |
void print(A *&n) | |
{ | |
n->get(); | |
cout << "\n\n"; | |
} | |
void print(A &n) | |
{ | |
n.get(); | |
cout << "\n\n"; | |
} | |
int main() { | |
A *a, *a1, **a2, mA; | |
int x = 3; | |
add(mA); | |
print(mA); | |
create(a); | |
add(a); | |
print(a); | |
destroy(a); | |
create(a1, x); | |
add(a1, x); | |
print(a1, x); | |
destroy(a1); | |
create(a2, x); | |
add(a2, x); | |
print(a2, x); | |
destroy(a2, x); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment