Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- class Pole{
- int* pole;
- size_t velikost;
- public:
- Pole(size_t pocet){
- if (pocet < 1){
- throw exception("Rozmer pole musi byt vetsi nez 1!");
- }
- velikost = pocet;
- pole = new int[velikost];
- for (size_t i = 0; i < velikost; i++)
- {
- pole[i] = 0;
- }
- }
- Pole(string str){
- if (str.length() == 0){
- throw exception("Retezec je prazdny!");
- }
- velikost= str.length();
- pole = new int[velikost];
- for (size_t i = 0; i < velikost; i++)
- {
- pole[i] = 0;
- }
- for (size_t i = 0; i < velikost; i++) {
- if (str.at(i) < '0' || str[i] > '9'){
- throw exception("Cislo neni v rozsahu 1-9");
- }
- pole[i] =str.at(i)-'0';
- }
- }
- const int& operator[](size_t index) const{
- if (index >=velikost){
- throw exception("Index mimo pole hombre!");
- }
- return pole[index];
- }
- int& operator[](size_t index){
- if (index >= velikost){
- throw exception("Index mimo pole hombre!");
- }
- return pole[index];
- }
- ~Pole(){
- delete[] pole;
- }
- };
- int main(){
- Pole p1(3);
- Pole p2("5555");
- printf("Pole: %d %d %d \n", p1[0], p1[1], p1[2]);
- printf("Pole: %d %d %d %d \n", p2[0], p2[1], p2[2], p2[3]);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement