From 59b1a0fe991e320d3a0c30092b5c74a03a0d4391 Mon Sep 17 00:00:00 2001 From: next1code Date: Sun, 7 Apr 2024 01:58:57 +0330 Subject: [PATCH 1/7] everything ok until operator += --- 1/1.cpp | 102 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 82 insertions(+), 20 deletions(-) diff --git a/1/1.cpp b/1/1.cpp index b83dbef..c556c92 100644 --- a/1/1.cpp +++ b/1/1.cpp @@ -6,20 +6,25 @@ using namespace std; class Person { public: - + Person(const string& _name , int _age ) : name(_name) , age(_age) { //me + } Person(const string& name) { this->name = name; - + } + Person() { //me + name = " "; + age = 0; } string& getName() { return name; } - + string getName()const { return name; } //me + friend class Group; private: string name; int age; }; -ostream& operator<<(ostream& out, const Person& p) { +ostream& operator<<(ostream& out, const Person& p) { //check cout << p.getName(); return out; } @@ -31,9 +36,17 @@ class Group { Group(int max_lenght) { this->cap = max_lenght; - this->members = new Person[max_lenght]; + this->members = new Person[max_lenght]; //check this->size = 0; } + Group(Group& obj) { + this->cap = obj.cap; + this->size = obj.size; + this->members = new Person[this->cap]; + for (int i = 0; i < this->size; ++i) { + this->members[i] = obj.members[i]; + } + } void add(Person p) { if (this->size == this->cap) { return; @@ -46,57 +59,106 @@ class Group { for (i = 0; i < size; i++) { if(members[i].name == name) break; - } for (int index = i; index < size - 1; index++) { members[index] = members[index + 1]; } size--; } + Group& operator=(const Group& obj) { //me + this->size = obj.size; + this->cap = obj.cap; + this->members = new Person[this->cap]; + for (int i = 0; i < this->size; ++i) { + this->members[i] = obj.members[i]; + } + return *this; + } + Group operator+(const Group &obj) { //me + Group sum(this->cap + obj.cap); + sum.size = this->size + obj.size; + sum.cap = this->cap + obj.cap; + for (int i = 0; i < this->size ; ++i) { + sum.members[i] = this->members[i]; + } + for (int i = 0; i < obj.size; ++i) { + sum.members[this->size + i] = obj.members[i]; + } + return sum; + } + /*Group& operator+=(const Group& obj) { //me + Group temp(this->cap); + temp.size = this->size; + for (int i = 0; i < this->size; ++i) { + temp.members[i] = this->members[i]; + } + delete[] this->members; + this->size = this->size + obj.size; + this->cap = this->cap + obj.cap; + this->members = new Person[this->cap]; + for (int i = 0; i < temp.size; ++i) { + this->members[i] = temp.members[i]; + } + /*for (int i = 0; i < obj.size; ++i) { + this->members[temp.size + i] = obj.members[i]; + }*/ + return *this; + }*/ + friend bool isGroupFull(Group ); //me + friend ostream& operator<<(ostream& ,const Group& ); //me + ~Group() { delete[] members; } - + private: int size; int cap; Person* members; }; +ostream& operator<<(ostream& out ,const Group& obj) { //me + for (int i = 0; i < obj.size ; ++i) { + out << obj.members[i] << "\t"; + } + cout << endl; + return out; +} -bool isGroupFull(Group g) { - return (g.size == g.cap); +bool isGroupFull(Group g) { //check + return (g.size == g.cap); } int main() { - /* + Person p1("somename"); - Person p1("somename2"); + Person p2("somename2"); cout << p1< Date: Sun, 7 Apr 2024 22:03:25 +0330 Subject: [PATCH 2/7] all fixed befor find function --- 1/1.cpp | 48 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/1/1.cpp b/1/1.cpp index c556c92..196e8a5 100644 --- a/1/1.cpp +++ b/1/1.cpp @@ -2,6 +2,7 @@ #include #include #include +#include using namespace std; class Person { @@ -17,6 +18,23 @@ class Person { } string& getName() { return name; } string getName()const { return name; } //me + bool operator < (const Person& other) const{ //me + return this->name < other.name; + } + bool operator ==(const Person& other) const { //me + return this->name == other.name && this->age == other.age; + } + Person* find(Person* start, Person* end,const Person &goal){ //me + cout << "here"; + for (Person* i = start; i < end; ++i) { + if (*i == goal) { + cout << "Hey"; + return i; + } + } + cout << "can not find\n"; + return end ; + } friend class Group; private: string name; @@ -86,24 +104,25 @@ class Group { } return sum; } - /*Group& operator+=(const Group& obj) { //me + Group& operator+=(const Group& obj) { //me Group temp(this->cap); temp.size = this->size; + int pirmary_size = obj.size; for (int i = 0; i < this->size; ++i) { temp.members[i] = this->members[i]; } delete[] this->members; - this->size = this->size + obj.size; + this->size = temp.size + obj.size; this->cap = this->cap + obj.cap; this->members = new Person[this->cap]; - for (int i = 0; i < temp.size; ++i) { + for (int i = 0; i < temp.size ; ++i) { this->members[i] = temp.members[i]; } - /*for (int i = 0; i < obj.size; ++i) { + for (int i = 0; i < pirmary_size; ++i) { this->members[temp.size + i] = obj.members[i]; - }*/ + } return *this; - }*/ + } friend bool isGroupFull(Group ); //me friend ostream& operator<<(ostream& ,const Group& ); //me @@ -150,30 +169,31 @@ int main() Group g3(10); g2.deletePerson("Erfan"); cout << g1< Date: Tue, 9 Apr 2024 16:53:16 +0330 Subject: [PATCH 3/7] everything is ok --- 1/1.cpp | 52 ++++++++++++++++++++++++++++++---------------------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/1/1.cpp b/1/1.cpp index 196e8a5..79975ef 100644 --- a/1/1.cpp +++ b/1/1.cpp @@ -18,23 +18,14 @@ class Person { } string& getName() { return name; } string getName()const { return name; } //me + int getAge()const { return age; } //me bool operator < (const Person& other) const{ //me return this->name < other.name; } bool operator ==(const Person& other) const { //me return this->name == other.name && this->age == other.age; } - Person* find(Person* start, Person* end,const Person &goal){ //me - cout << "here"; - for (Person* i = start; i < end; ++i) { - if (*i == goal) { - cout << "Hey"; - return i; - } - } - cout << "can not find\n"; - return end ; - } + friend class Group; private: string name; @@ -42,10 +33,31 @@ class Person { }; + + +ostream& operator<< (ostream& out, const Person* p) { + if (p == NULL) { + cout << "can not find\n"; + return out; + } + cout << p->getName(); + return out; +} ostream& operator<<(ostream& out, const Person& p) { //check cout << p.getName(); return out; } +Person* find(Person* start, Person* end, const Person& goal) { //me + cout << "here"; + for (Person* i = start; i <= end; ++i) { + if (i->getName() == goal.getName() && i->getAge() == goal.getAge()) { + cout << "Hey"; + return i; + } + } + cout << "can not find\n"; + return end; +} @@ -185,23 +197,19 @@ int main() persons[i].getName()=to_string(rand()%100); } - sort(persons,persons+10);//what operator should be overloaded for sort? :) + sort(persons, persons + 10);//what operator should be overloaded for sort? :) for (int i = 0;i<10;i++) { cout< Date: Tue, 9 Apr 2024 17:22:26 +0330 Subject: [PATCH 4/7] add new comment --- 1/1.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/1/1.cpp b/1/1.cpp index 79975ef..4ebb4ff 100644 --- a/1/1.cpp +++ b/1/1.cpp @@ -35,19 +35,19 @@ class Person { -ostream& operator<< (ostream& out, const Person* p) { +/*ostream& operator<< (ostream& out, const Person* p) { if (p == NULL) { cout << "can not find\n"; return out; } cout << p->getName(); return out; -} +}*/ ostream& operator<<(ostream& out, const Person& p) { //check cout << p.getName(); return out; } -Person* find(Person* start, Person* end, const Person& goal) { //me +/*Person* find(Person* start, Person* end, const Person& goal) { //me cout << "here"; for (Person* i = start; i <= end; ++i) { if (i->getName() == goal.getName() && i->getAge() == goal.getAge()) { @@ -57,7 +57,7 @@ Person* find(Person* start, Person* end, const Person& goal) { //me } cout << "can not find\n"; return end; -} +}*/ @@ -210,6 +210,5 @@ int main() else cout << *find(persons, persons + 10, Erfan); - } From ea93b1e3a0a4be846f265c89b60126240f7b21f8 Mon Sep 17 00:00:00 2001 From: next1code Date: Wed, 10 Apr 2024 12:05:45 +0330 Subject: [PATCH 5/7] add some function ang pragma once to solve the issue --- 2/Score.cpp | 27 +++++++++++++++++++++++++++ 2/Score.h | 9 +++++++-- 2/SomeRandomDotHFile.h | 6 +++++- 2/main.cpp | 7 ++++--- 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/2/Score.cpp b/2/Score.cpp index c107032..0fd94da 100644 --- a/2/Score.cpp +++ b/2/Score.cpp @@ -1,4 +1,9 @@ #include "Score.h" +int Score:: ID_generator = 0; +void Score::setID_generator(int input) +{ + ID_generator = input; +} Score::Score(int score,char* detail, Label label) :ID(Score::ID_generator) { this->score = score; @@ -6,7 +11,29 @@ Score::Score(int score,char* detail, Label label) :ID(Score::ID_generator) { this->detail = detail; ID_generator++; +} +Score::Score(int _score, const char* _detail , Label _label) : ID(Score :: ID_generator) { + this->score = _score; + this->label = _label; + this->detail = _detail; + + ID_generator++; +} + +Score::Score() : ID(Score::ID_generator) { + this->score =0; + this->detail = ""; + ID_generator++; +} +Score& Score::operator=(const Score& obj) +{ + this->score = obj.score; + this->detail = obj.detail; + this->label = obj.label; + return *this; } + + diff --git a/2/Score.h b/2/Score.h index 0dd79cb..d2bb557 100644 --- a/2/Score.h +++ b/2/Score.h @@ -1,10 +1,12 @@ +#pragma once #include #include using namespace std; class Score { public: - friend void print(const Score& score); - + friend void print( const Score& score); //check + friend int main(); + static void setID_generator(int ); typedef enum { GREAT, GOOD, @@ -13,6 +15,9 @@ class Score { }Label; Score(int score, char* detail, Label label); + Score(int, const char* , Label); //me + Score();//me + Score& operator=(const Score& obj); private: int score; Label label; diff --git a/2/SomeRandomDotHFile.h b/2/SomeRandomDotHFile.h index 445514a..834fc0d 100644 --- a/2/SomeRandomDotHFile.h +++ b/2/SomeRandomDotHFile.h @@ -1 +1,5 @@ -#include "Score.h" \ No newline at end of file +#pragma once +#include "Score.h" +void print(const Score& obj) { + cout << "score : " << obj.score << " Label : " << obj.label << " detail : " << obj.detail << " ID : " << obj.ID << endl; +} \ No newline at end of file diff --git a/2/main.cpp b/2/main.cpp index 64d213e..d0c8d89 100644 --- a/2/main.cpp +++ b/2/main.cpp @@ -16,16 +16,17 @@ int main() srand(time(NULL)); - Score s1(1, "do better next time", Score::BAD); - Score scores[10];//we have default constructor so we should be fine right?:) + Score s1(1, "do better next time", Score::BAD); //check + Score scores[10];//we have default constructor so we should be fine right?:) //check FOR(10) { - SETSCORE(i) + SETSCORE(i); //check //why = betwean two scores raises an error?:) } FOR(10) { print(scores[i]); } + Score::setID_generator(Score::ID_generator+100);//just a setter for ID_generator } From b9e81187d1c6bf2db7f40b8a7ad41c1571c2e85d Mon Sep 17 00:00:00 2001 From: next1code Date: Fri, 12 Apr 2024 11:42:17 +0330 Subject: [PATCH 6/7] final changes --- 2/Score.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/2/Score.cpp b/2/Score.cpp index 0fd94da..c8d88c6 100644 --- a/2/Score.cpp +++ b/2/Score.cpp @@ -12,7 +12,7 @@ Score::Score(int score,char* detail, Label label) :ID(Score::ID_generator) { ID_generator++; } -Score::Score(int _score, const char* _detail , Label _label) : ID(Score :: ID_generator) { +Score::Score(int _score, const char* _detail, Label _label) : ID(Score::ID_generator) { this->score = _score; this->label = _label; this->detail = _detail; From b9fbbf12034510169bacd930deefb6ca4da0b581 Mon Sep 17 00:00:00 2001 From: next1code Date: Fri, 12 Apr 2024 11:59:30 +0330 Subject: [PATCH 7/7] change default constructor --- 2/Score.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/2/Score.cpp b/2/Score.cpp index c8d88c6..3f71fd7 100644 --- a/2/Score.cpp +++ b/2/Score.cpp @@ -23,6 +23,7 @@ Score::Score(int _score, const char* _detail, Label _label) : ID(Score::ID_gener Score::Score() : ID(Score::ID_generator) { this->score =0; this->detail = ""; + this->label = GREAT; ID_generator++; }