Website Summary Files Commits Branches @commit @diff @stats
path: root / Classes
diff options
context:
space:
mode:
Commit112a0d344da9e66d20ee9ab5d488e59c5d52e6ad (patch)
Author
 Angeld55 <angel_dimitriev@abv.bg>
2020-04-09 18:09:42 +0300
Committer
 Angeld55 <angel_dimitriev@abv.bg>
2020-04-09 18:09:42 +0300
Filese3bb9cf17c24a3b53c746d9142906d550d549465 /Classes
Object-oriented_programming_FMI-112a0d344da9e66d20ee9ab5d488e59c5d52e6ad.tar
Object-oriented_programming_FMI-112a0d344da9e66d20ee9ab5d488e59c5d52e6ad.tar.gz
Object-oriented_programming_FMI-112a0d344da9e66d20ee9ab5d488e59c5d52e6ad.zip
Parentec26fab32d144b436d08f9da6049bd053156d783 (diff)
added new examples
4 files changed, 53 insertions, 113 deletions
-rw-r--r--Classes/String/String.cpp (renamed from Classes/String/String.hpp)62
-rw-r--r--Classes/String/String.h45
-rw-r--r--Classes/Triangle/Triangle.hpp36
-rw-r--r--Classes/Triangle/point.hpp23
diff --git a/Classes/String/String.hpp b/Classes/String/String.cpp
index b2c333f..f4f567b 100644
--- a/Classes/String/String.hpp
+++ b/Classes/String/String.cpp
@@ -1,48 +1,4 @@
-#ifndef STRING_HDR
-#define STRING_HDR
-
-#include<iostream>
-#include<fstream>
-
-const int MAX_SIZE = 1024;
-
-class String {
-
-private:
- char* str;
- int len;
-
- void CopyFrom(const String& other);
- void Free();
-
-public:
- String();
- String(char ch);
- String(const char* str);
- String(const String& other);
- String& operator=(const String& other);
- ~String();
-
- int getLenght() const;
- String SubString(int start);
- String SubString(int start, int end);
- int IndexOf(const String& other, int index); //return the index of the first encounter of other in the object
- int IndexOf(const char* str, int index); //reusing IndexOf(const String& other, int index)
- int CountOcurences(String str); // How many times a string is found as substring
- int GetOcurence(String str,int i); //Get if the i-th substring (count start from 1)
-
- const char& operator[](int index) const;
- char& operator[](int index);
- String& operator+=(const String& other); //return the concatenation of the object with other
- friend bool operator==(const String& lhs, const String& rhs); //return true if two strings are equivalent
- friend bool operator!=(const String& lhs, const String& rhs); //return true if two strings are not equivalent
- friend std::ostream& operator<<(std::ostream& os, const String& obj);
- friend std::istream& operator>>(std::istream& is, String& obj);
-
- String* Split(String str);
-};
-const String operator+(const String& lhs, const String& rhs);
-
+#include "String.h"
void String::CopyFrom(const String& other) {
@@ -107,10 +63,10 @@ String String::SubString(int start) {
char* newStr = new char[len - start + 2];
int j = 0;
- for (int i = start; i < len; i++)
+ for (int i = start; i < len; i++)
newStr[j++] = str[i];
newStr[j] = '\0';
-
+
String newString(newStr);
delete[] newStr;
@@ -136,7 +92,7 @@ String String::SubString(int start, int end) {
int String::IndexOf(const String& other, int index) {
- if(index < 0)
+ if (index < 0)
throw std::logic_error("Invalid index.\n");
int iter = 0;
@@ -159,10 +115,10 @@ int String::IndexOf(const char* str, int index) {
}
int String::CountOcurences(String str)
-{
+{
int count = 0;
- int i = IndexOf(str,0);
- while (i!=-1)
+ int i = IndexOf(str, 0);
+ while (i != -1)
{
count++;
i = IndexOf(str, i + 1);
@@ -186,7 +142,7 @@ inline int String::GetOcurence(String str, int num)
const char& String::operator[](int index) const {
- if(index >= len)
+ if (index >= len)
throw std::out_of_range("Out of range!");
return str[index];
}
@@ -236,5 +192,3 @@ const String operator+(const String& lhs, const String& rhs) {
return lhs;
return String(lhs) += rhs;
}
-
-#endif // !STRING_HDR
diff --git a/Classes/String/String.h b/Classes/String/String.h
new file mode 100644
index 0000000..37bbc6d
--- /dev/null
+++ b/Classes/String/String.h
@@ -0,0 +1,45 @@
+#ifndef STRING_HDR
+#define STRING_HDR
+
+#include<iostream>
+#include<fstream>
+
+const int MAX_SIZE = 1024;
+
+class String {
+
+private:
+ char* str;
+ int len;
+
+ void CopyFrom(const String& other);
+ void Free();
+
+public:
+ String();
+ String(char ch);
+ String(const char* str);
+ String(const String& other);
+ String& operator=(const String& other);
+ ~String();
+
+ int getLenght() const;
+ String SubString(int start);
+ String SubString(int start, int end);
+ int IndexOf(const String& other, int index); //return the index of the first encounter of other in the object
+ int IndexOf(const char* str, int index); //reusing IndexOf(const String& other, int index)
+ int CountOcurences(String str); // How many times a string is found as substring
+ int GetOcurence(String str, int i); //Get if the i-th substring (count start from 1)
+
+ const char& operator[](int index) const;
+ char& operator[](int index);
+ String& operator+=(const String& other); //return the concatenation of the object with other
+ friend bool operator==(const String& lhs, const String& rhs); //return true if two strings are equivalent
+ friend bool operator!=(const String& lhs, const String& rhs); //return true if two strings are not equivalent
+ friend std::ostream& operator<<(std::ostream& os, const String& obj);
+ friend std::istream& operator>>(std::istream& is, String& obj);
+
+ String* Split(String str);
+};
+const String operator+(const String& lhs, const String& rhs);
+#endif // !STRING_HDR \ No newline at end of file
diff --git a/Classes/Triangle/Triangle.hpp b/Classes/Triangle/Triangle.hpp
deleted file mode 100644
index ab0a8ff..0000000
--- a/Classes/Triangle/Triangle.hpp
+++ /dev/null
@@ -1,36 +0,0 @@
-#include "point.hpp"
-
-class Triangle
-{
-public:
- Point p1;
- Point p2;
- Point p3;
- //Кой конструктор е по-добре да ползваме и защо?
- Triangle(Point p1, Point p2, Point p3);
- Triangle(int x1,int y1,int x2,int y2,int x3,int y3);
-
- double getPer();
-
-
-};
-Triangle::Triangle(Point p1, Point p2, Point p3)
-{
- this->p1 = p1;
- this->p2 = p2;
- this->p3 = p3;
-}
-Triangle::Triangle(int x1,int y1,int x2,int y2,int x3,int y3)
-{
- p1.x = x1;
- p1.y = y1;
- p2.x = x2;
- p2.y = y2;
- p3.x = x3;
- p3.y = y3;
-}
-// Използваме вече дефинираните функции, без да се интересуваме от тяхната реализация
-double Triangle::getPer()
-{
- return p1.DistTo(p2) + p2.DistTo(p3) + p3.DistTo(p1);
-}
diff --git a/Classes/Triangle/point.hpp b/Classes/Triangle/point.hpp
deleted file mode 100644
index a8a2c19..0000000
--- a/Classes/Triangle/point.hpp
+++ /dev/null
@@ -1,23 +0,0 @@
-#include <iostream>
-
-struct Point
-{
- Point()
- {
- x = 0;
- y = 0;
- }
- Point(int x, int y)
- {
- this->x = x;
- this->y = y;
- }
- double DistTo(const Point& other)
- {
- int dx = x - other.x;
- int dy = y - other.y;
- return sqrt(dx*dx + dy*dy);
- }
- int x;
- int y;
-}; \ No newline at end of file