Skip to content

Instantly share code, notes, and snippets.

@pstachula-dev
Last active December 10, 2015 23:48
Show Gist options
  • Select an option

  • Save pstachula-dev/4511877 to your computer and use it in GitHub Desktop.

Select an option

Save pstachula-dev/4511877 to your computer and use it in GitHub Desktop.
Programowanie Obiektowe w1
#include<iostream>
using namespace std;
struct A{
int id;
};
// Alkowanie pamieci
void create(A **&el, int size)
{
el = (A **)malloc(size * sizeof(A));
for(int i = 0; i< size; i++){
el[i] = (A *)malloc(sizeof(A));
}
}
void create(A *&el, int size = 1)
{
el = (A *)malloc(size * sizeof(A));
}
// Ustawianie danych poczatkowych
void set(A **&el, int size)
{
for(int i = 0; i < size; i++){
el[i]->id = i;
}
}
void set(A *&el, int size = 1)
{
for(int i = 0; i < size; i++){
el[i].id = i;
}
}
void set(A &el)
{
el.id = 88;
}
// Wyswietlanie zawartosci
void get(A **el, int size)
{
for(int i = 0; i < size; i++){
printf("%d, ", el[i]->id);
}
}
void get(A *el, int size = 1)
{
for(int i = 0; i < size; i++){
printf("%d, ", el[i].id);
}
}
void get(A &el)
{
printf("%d, ", el.id);
}
// Zwalnianie pamieci
void destroy(A **&el, int size)
{
for(int i = 0; i < size; i++){
free(el[i]);
}
free (el);
}
void destroy(A *&el)
{
free (el);
}
// Main
int main()
{
A w, *w1, *w2, **mW;
int x = 3;
// Dla obiektu klasy
set(w);
get(w);
// Dla wskaznika
create(w1);
set(w1);
get(w1);
destroy(w1);
// Dla tablicy obiektow
create(w2, x);
set(w2, x);
get(w2, x);
destroy(w2);
// Dla tablicy wskaznikow
create(mW, x);
set(mW, x);
get(mW, x);
destroy(mW, x);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment