Advertisement
stefan1919

OOP_C++

Apr 25th, 2016
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. //
  2. //  main.cpp
  3. //  ClassesOOp
  4. //
  5. //  Created by Rossitza on 4/25/16.
  6. //  Copyright © 2016 Stefan. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <string>
  11.  
  12. using namespace std;
  13.  
  14. class Creature // base class
  15. {
  16.     string type;
  17. public:
  18.     Creature()
  19.     {
  20.         type = "";
  21.     }
  22.     Creature(string type)
  23.     {
  24.         this->type = type;
  25.     }
  26.     void display_type()
  27.     {
  28.         cout << type << endl;
  29.     }
  30. };
  31.  
  32. class Human : public Creature
  33. {
  34.     int age;
  35.     string gender;
  36. public:
  37.     Human():Creature()
  38.     {
  39.         age = 0;
  40.         gender = "feminine";
  41.     }
  42.     Human(int age, string gender, string type):Creature(type) {
  43.         this->age = age;
  44.         this->gender = gender;
  45.     }
  46.     void display_age() {
  47.         cout<< age <<endl;
  48.     }
  49.     void display_gender() {
  50.         cout<< gender <<endl;
  51.     }
  52.     ~Human() { cout << "destructor is called" << endl;}
  53. };
  54.  
  55. class Man: public Human
  56. {
  57.     bool is_reliable;
  58. public:
  59.     Man(bool is_reliable, int age, string gender, string type):Human(age, gender, type) {
  60.         this->is_reliable = is_reliable;
  61.     }
  62.    
  63.     void display_is_reliable(){
  64.         cout << is_reliable << endl;
  65.     }
  66. };
  67.  
  68. class Child : public Human
  69. {
  70.     string name_of_school;
  71. public:
  72.    
  73.     Child():Human(){
  74.     name_of_school = "";
  75.     }
  76.    
  77.     Child(string name_of_school, int age, string gender, string type):Human(age, gender, type) {
  78.         this->name_of_school = name_of_school;
  79.     }
  80. };
  81.  
  82. int main(int argc, const char * argv[]) {
  83. //    int n = 5;
  84. //    Human h1;
  85. //    while (n > 1) {
  86. //        Human human2;
  87. //        if (n%2 == 0) {
  88. //            Man man(false, 22, "m", "mammal");
  89. //        }
  90. //        n--;
  91. //    }
  92.  
  93.     Human* human1 = new Human(20, "feminine", "mammal");
  94.     human1->display_age();
  95.     delete human1;
  96.     return 0;
  97. }
  98.  
  99. // Answers:
  100. // The destructor will be called 6 times
  101. // The default construcitor for class human will be called 4 times
  102. //The non-default constructor will be invoked 2 times, when the Man is instantiated
  103. // The non-default constructor for class creature will be invoked 2 times
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement