Advertisement
Josif_tepe

Untitled

Nov 1st, 2021
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. ofstream fout;
  5. class Person {
  6. private:
  7.     char name[55];
  8.     char number[55];
  9.     char address[105];
  10. public:
  11.     Person() {}
  12.     Person(char *_name, char *_number, char *_address) {
  13.         strcpy(name, _name);
  14.         strcpy(number, _number);
  15.         strcpy(address, _address);
  16.     }
  17.     Person(const Person &obj) {
  18.         strcpy(name, obj.name);
  19.         strcpy(number, obj.number);
  20.         strcpy(address, obj.address);
  21.     }
  22.     ~Person() {}
  23.     const char* get_name() {
  24.         return name;
  25.     }
  26.     const char* get_number() {
  27.         return number;
  28.     }
  29.     const char* get_address() {
  30.         return address;
  31.     }
  32.     void set_name(char *s) {
  33.         strcpy(name, s);
  34.     }
  35.     void set_number(char *s) {
  36.         strcpy(number, s);
  37.     }
  38.     void set_address(char *s) {
  39.         strcpy(address, s);
  40.     }
  41.     friend ostream& operator << (ostream &stream, Person &p);
  42.    
  43. };
  44. ostream& operator << (ostream &stream, Person &p) {
  45.     stream << p.name << " " << p.address << " " << p.number << endl;
  46.     return stream;
  47. }
  48. class InheritedPerson : public Person {
  49. private:
  50.     char *adr[105];
  51. public:
  52.     InheritedPerson() {}
  53.     InheritedPerson(char *_name, char *_number, char *_address) : Person(_name, _number, _address) {
  54.         adr[0] = _address;
  55.     }
  56.     ~InheritedPerson() {
  57.        
  58.     }
  59.    
  60. };
  61. void print_same_address(Person p[], int n, char *adr) {
  62.     for(int i = 0; i < n; i++) {
  63.         if(strcmp(adr, p[i].get_address()) == 0) {
  64.             fout << p[i];
  65.         }
  66.     }
  67. }
  68. void print_same_number(Person p[], int n, char *num)  {
  69.     for(int i = 0; i < n; i++) {
  70.         if(strcmp(num, p[i].get_number()) == 0) {
  71.             fout << p[i];
  72.         }
  73.     }
  74. }
  75. int main(){
  76.     fout.open("file.txt", ios::out);
  77.     Person p1("john", "123", "houston");
  78.     Person p2("jack", "234", "houston");
  79.     Person p3("jerry", "123", "texas");
  80.     Person p[] = {p1, p2, p3};
  81.     print_same_number(p, 3, "123");
  82.     print_same_address(p, 3, "houston");
  83.    
  84.     return 0;
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement