Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // ClassesOOp
- //
- // Created by Rossitza on 4/25/16.
- // Copyright © 2016 Stefan. All rights reserved.
- //
- #include <iostream>
- #include <string>
- using namespace std;
- class Creature // base class
- {
- string type;
- public:
- Creature()
- {
- type = "";
- }
- Creature(string type)
- {
- this->type = type;
- }
- void display_type()
- {
- cout << type << endl;
- }
- };
- class Human : public Creature
- {
- int age;
- string gender;
- public:
- Human():Creature()
- {
- age = 0;
- gender = "feminine";
- }
- Human(int age, string gender, string type):Creature(type) {
- this->age = age;
- this->gender = gender;
- }
- void display_age() {
- cout<< age <<endl;
- }
- void display_gender() {
- cout<< gender <<endl;
- }
- ~Human() { cout << "destructor is called" << endl;}
- };
- class Man: public Human
- {
- bool is_reliable;
- public:
- Man(bool is_reliable, int age, string gender, string type):Human(age, gender, type) {
- this->is_reliable = is_reliable;
- }
- void display_is_reliable(){
- cout << is_reliable << endl;
- }
- };
- class Child : public Human
- {
- string name_of_school;
- public:
- Child():Human(){
- name_of_school = "";
- }
- Child(string name_of_school, int age, string gender, string type):Human(age, gender, type) {
- this->name_of_school = name_of_school;
- }
- };
- int main(int argc, const char * argv[]) {
- // int n = 5;
- // Human h1;
- // while (n > 1) {
- // Human human2;
- // if (n%2 == 0) {
- // Man man(false, 22, "m", "mammal");
- // }
- // n--;
- // }
- Human* human1 = new Human(20, "feminine", "mammal");
- human1->display_age();
- delete human1;
- return 0;
- }
- // Answers:
- // The destructor will be called 6 times
- // The default construcitor for class human will be called 4 times
- //The non-default constructor will be invoked 2 times, when the Man is instantiated
- // The non-default constructor for class creature will be invoked 2 times
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement