Skip to content

Commit

Permalink
Kody przykładowych klas w c++
Browse files Browse the repository at this point in the history
  • Loading branch information
xinulsw committed Mar 30, 2017
1 parent c9394bd commit 2d214e4
Show file tree
Hide file tree
Showing 21 changed files with 886 additions and 0 deletions.
28 changes: 28 additions & 0 deletions cpp/klasy/czas/czas.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <iostream>
#include "czas.h"

using namespace std;

//Czas::Czas() { godz = min = sek = 0 }

Czas::Czas(int g, int m, int s) {
ustawCzas(g,m,s);
}

void Czas::ustawCzas( int g, int m, int s) {
godz = (g >= 0 && g < 24) ? g : 0;
min = (m >= 0 && m < 59) ? m : 0;
sek = (s >= 0 && s < 59) ? s : 0;
}

void Czas::drukujWojskowy() {
cout << (godz < 10 ? "0" : "") << godz << ":"
<< (min < 10 ? "0" : "") << min;
}

void Czas::drukujStandard() {
cout << ((godz == 0 || godz == 12) ? 12 : godz%12 )
<< ":" << (min < 10 ? "0" : "") << min
<< ":" << (sek < 10 ? "0" : "") << sek
<< (godz < 12 ? " AM" : " PM");
}
18 changes: 18 additions & 0 deletions cpp/klasy/czas/czas.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef CZAS_H
#define CZAS_H

class Czas {
public:
Czas(int = 0, int = 0, int = 0); //konstruktor
void ustawCzas( int, int, int ); //ustawia godz., min, i sek.
void drukujWojskowy(); //wyświetla czas w formacie wojskowym
void drukujStandard(); //wyświetla czas w formacie standardowym

private:
int godz; //0-23
int min; //0-59
int sek; //0-59
/* add your private declarations */
};

#endif /* CZAS_H */
48 changes: 48 additions & 0 deletions cpp/klasy/czas/czas1_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Przykład użycia klasy Czas
#include <iostream>
#include <cstdio>

using namespace std;

class Czas {
public:
Czas(); //konstruktor
void ustawCzas( int, int, int); //ustawia godz., min, i sek.
void drukujWojskowy(); //wyświetla czas w formacie wojskowym
void drukujStandard(); //wyświetla czas w formacie standardowym
private:
int godz; //0-23
int min; //0-59
int sek; //0-59
};

//Konstruktor inicjuje składowe wartością 0
Czas::Czas () { godz = min = sek = 0; }

//Ustawia wartości składowych
void Czas::ustawCzas( int g, int m, int s) {
godz = (g >= 0 && g < 24) ? g : 0;
min = (m >= 0 && m < 59) ? m : 0;
sek = (s >= 0 && s < 59) ? s : 0;
}

void Czas::drukujWojskowy() {
cout << (godz < 10 ? "0" : "") << godz << ":"
<< (min < 10 ? "0" : "") << min;
}

void Czas::drukujStandard() {
cout << ((godz == 0 || godz == 12) ? 12 : godz%12 )
<< ":" << (min < 10 ? "0" : "") << min
<< ":" << (sek < 10 ? "0" : "") << sek
<< (godz < 12 ? " AM" : " PM");
}

int main(int argc, char *argv[]) {
Czas t;
cout << "Czas początkowy w f. wojskowym: " << endl;
t.drukujWojskowy();
cout << "\nCzas początkowy w f. standardowym: " << endl;
t.drukujStandard();
return 0;
}
19 changes: 19 additions & 0 deletions cpp/klasy/czas/klasy1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <iostream>
#include <cstdio>
#include "czas.h"

using namespace std;

int main(int argc, char *argv[]) {
Czas t1;
// Czas t2(23,59);
cout << "Czas początkowy w f. wojskowym: " << endl;
t1.drukujWojskowy();
// cout << " ";
// t2.drukujWojskowy();
cout << "\nCzas początkowy w f. standardowym: " << endl;
t1.drukujStandard();
// cout << " ";
// t2.drukujStandard();
return 0;
}
14 changes: 14 additions & 0 deletions cpp/klasy/czas/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
CC=g++
CFLAGS=-c -Wall
LDFLAGS=
SOURCES=klasy1.cpp czas.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=klasy1

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
$(CC) $(CFLAGS) $< -o $@
69 changes: 69 additions & 0 deletions cpp/klasy/figury.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <iostream>
#include <cstdio>
#include <fstream>
#include <cstring>

using namespace std;

class Figura {
public:
Figura(char figura); //konstruktor
void getNazwa(); //wyświetl nazwę figury
char getTyp(); //zwraca typ figury
void getPole(); //wyświetl pole figury
void getObwod(); //wyświetl obwod figury
private:
char typ;
char nazwa[15];
float pole;
float obwod;
void setTyp(char figura);
};

Figura::Figura(char figura) {
setTyp(figura);
cout << "Utworzono: ";
getNazwa();
cout << endl;
}

void Figura::setTyp(char figura) {
switch (figura) {
case 'k': strcpy(nazwa,"kwadrat"); typ = figura; break;
case 'p': strcpy(nazwa,"prostokąt"); typ = figura; break;
case 't': strcpy(nazwa,"trójkąt"); typ = figura; break;
case 'o': strcpy(nazwa,"koło"); typ = figura; break;
default:
cout << "Nieznany typ figury!";
strcpy(nazwa,"kwadrat"); typ = 'k';
}
}

char Figura::getTyp() {
return typ;
}

void Figura::getNazwa() {
cout << nazwa;
}

void Figura::getPole() {
cout << pole;
}

void Figura::getObwod() {
cout << obwod;
}

int main(int argc, char *argv[]) {
char odp;
cout << "Wybierz figurę: " << endl;
cout << " k - kwadrat\n";
cout << " p - prostokąt\n";
cout << " t - trójkąt\n";
cout << " o - koło\n";
cout << " x - koniec\n";
cin >> odp;
Figura figura(odp);
return 0;
}
99 changes: 99 additions & 0 deletions cpp/klasy/pracownicy/pracownicy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

class Data {
public:
Data(int=1, int=1, int=1990); //konstruktor
void drukuj() const; //data w formacie d/m/r
~Data();
private:
int dzien;
int miesiac;
int rok;
int sprawdzDzien(int); //fun. narzędziowa
};

Data::Data(int d, int m, int r) {
if (m>0 && m<=12) miesiac = m;
else {
miesiac = 1;
cout << "Niepoprawny miesiąc: " << m << ". Przyjęto wartość 1.\n";
}
rok = r;//powinno być sprawdzone
dzien = sprawdzDzien(d);
cout << "Konstruktor obiektu klasy Data dla daty: ";
drukuj();
cout << endl;
}

void Data::drukuj() const {
cout << dzien << "/" << miesiac << "/" << rok;
}

Data::~Data() {
cout << "Destruktor obiektu klasy Data dla daty: ";
drukuj();
cout << endl;
}

int Data::sprawdzDzien(int testDzien) {
static const int dniMiesiecy[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
if (testDzien > 0 && testDzien <= dniMiesiecy[miesiac])
return testDzien;
if (miesiac == 2 && //sprawdzanie roku przestępnego
testDzien == 29 && (rok % 400 == 00 || (rok % 4 == 0 && rok % 100 != 0 ))) //rok 2000?
return testDzien;
cout << "Niepoprawny dzien: " << testDzien << ". Przyjęto wartość 1.\n";
return 1;
}

class Pracownik {
public:
Pracownik(char const *, char const *, int, int, int, int, int, int); //konstruktor
void drukuj() const; //stała funkcja składowa
~Pracownik(); //destruktor
private:
char imie[25];
char nazwisko[25];
const Data dataUrodzenia; //stały obiekt składowy
const Data dataZatrudnienia; //stały obiekt składowy
};

Pracownik::Pracownik(char const *pImie, char const *pNazwisko, int uDzien, int uMiesiac, int uRok, int zDzien, int zMiesiac, int zRok)
: dataUrodzenia(uDzien,uMiesiac,uRok), dataZatrudnienia(zDzien,zMiesiac,zRok) //inicjatory składowych
{
int dlugosc = strlen(pImie);
dlugosc = (dlugosc < 25 ? dlugosc : 24);
strncpy(imie,pImie,dlugosc);
imie[dlugosc]='\0';

dlugosc = strlen(pNazwisko);
dlugosc = (dlugosc < 25 ? dlugosc : 24);
strncpy(nazwisko,pNazwisko,dlugosc);
nazwisko[dlugosc]='\0';

cout << "Konstruktor obiektu klasy Pracownik: " << imie << ' ' << nazwisko << endl;
}

void Pracownik::drukuj() const {
cout << nazwisko << ", " << imie << "\nZatrudniony: ";
dataZatrudnienia.drukuj();
cout << " Data urodzenia: ";
dataUrodzenia.drukuj();
cout << endl << endl;
}

Pracownik::~Pracownik() {
cout << "Destruktor klasy Pracownik: " << nazwisko << " " << imie << endl;
}

int main(int argc, char *argv[]) {
Pracownik p("Jan","Kowalski",24,7,1970,3,12,1997);
cout << "\n";
p.drukuj();

return 0;
}

0 comments on commit 2d214e4

Please sign in to comment.